Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def findBestFit(filelist,clip,all_coords,all_proj,all_pixsize):
logging.debug("got file list {}".format(filelist))
lon_min = clip[0]
lat_max = clip[1]
lon_max = clip[2]
lat_min = clip[3]
wkt1 = "POLYGON ((%s %s, %s %s, %s %s, %s %s, %s %s))" % (lat_min,lon_min,lat_max,lon_min,lat_max,lon_max,lat_min,lon_max,lat_min,lon_min)
poly0 = ogr.CreateGeometryFromWkt(wkt1)
total_area = poly0.GetArea()
logging.info("Bounding Box {}".format(poly0.ExportToWkt()))
logging.info("Total area is {}".format(total_area))
# Find location of best overlap with bounding box
max_frac = 0.0
for i in range(len(filelist)):
coords = all_coords[i]
lat_max1 = coords[1]
lat_min1 = coords[3]
lon_min1 = coords[0]
lon_max1 = coords[2]
wkt2 = "POLYGON ((%s %s, %s %s, %s %s, %s %s, %s %s))" % (lat_min1,lon_min1,lat_max1,lon_min1,lat_max1,lon_max1,lat_min1,lon_max1,lat_min1,lon_min1)
the plots within a las file."""
plot_shp = self.plot_shp
plot_geoms = self.get_geom(plot_shp)
print(plot_geoms[0])
self.all_cells = []
if plot_shp == None:
print("Consider adding a plot shapefile path.")
else:
print("Indexing plots with grid.")
for plot in plot_geoms:
plot_cells = []
plot = ogr.CreateGeometryFromWkt(plot)
grid = self.get_geom(self.grid_path)
for cell in grid:
cell = ogr.CreateGeometryFromWkt(cell)
if cell.Overlaps(plot) == True or cell.Within(plot) == True:
json_cell = cell.ExportToJson()
plot_cells.append(json_cell)
self.all_cells.append(plot_cells)
def extract_plot(self, square_df, plot_index):
"""converts dataframe points to something"""
plot = self.get_geom(self.plot_shp)[plot_index]
plot = ogr.CreateGeometryFromWkt(plot)
plot_df = square_df
point_array = np.column_stack([plot_df.index.values, plot_df.x.values, plot_df.y.values])
plot_points = []
for point in point_array:
thing = ogr.Geometry(ogr.wkbPoint)
thing.AddPoint(point[1], point[2])
if thing.Within(plot):
plot_points.append(point[0])
new_df = plot_df.loc[plot_points, :]
return new_df
# Convert a shapefile to WKT using ogr
# https://github.com/GeospatialPython/Learning/raw/master/polygon.zip
import ogr
shape = ogr.Open("polygon.shp")
layer = shape.GetLayer()
feature = layer.GetNextFeature()
geom = feature.GetGeometryRef()
wkt = geom.ExportToWkt()
# View the WKT
print(wkt)
# Ingest the WKT we made and check the envelope
poly = ogr.CreateGeometryFromWkt(wkt)
print(poly.GetEnvelope())
k=-1
for row in curs:
wkt = str(row[numfields])
# run only for features with geometry
# -----------------------------------
if wkt!=None:
geom_type=FeatureType(wkt)
# create a new feature
feature = ogr.Feature(featureDefn)
polyg = ogr.Geometry(geom_type)
polyg = ogr.CreateGeometryFromWkt(wkt)
# adds the value of the fields
for i in range(numfields):
valore=row[i]
feature.SetField(FieldList[i], valore)
feature.SetGeometry(polyg)
# add the feature to the output layer
layer_mem.CreateFeature(feature)
# destroy the geometry and feature and close the data source
polyg.Destroy()
feature.Destroy()
return layer_mem
# WKT requires other use of comma and spaces in coordinate list
wkt_image2 = wkt_image1.replace(' ', ';')
wkt_image3 = wkt_image2.replace(',', ' ')
wkt_image = wkt_image3.replace(';', ',')
# Define projections
datasetEPSG = pyproj.Proj('+init=EPSG:4326')
locationEPSG = pyproj.Proj('+init=EPSG:4326')
# Transform coordinates of location into file coordinates
upper_left_x, upper_left_y = pyproj.transform(locationEPSG, datasetEPSG, location[0], location[1])
lower_right_x, lower_right_y = pyproj.transform(locationEPSG, datasetEPSG, location[2], location[3])
wkt_location = 'POLYGON((' + str(upper_left_x) + ' ' + str(upper_left_y) + ',' + str(upper_left_x) + ' ' + str(lower_right_y) + ',' + str(lower_right_x) + ' ' + str(lower_right_y) + ',' + str(lower_right_x) + ' ' + str(upper_left_y) + ',' + str(upper_left_x) + ' ' + str(upper_left_y) + '))'
# Use ogr to check if polygon contained
poly_location = ogr.CreateGeometryFromWkt(wkt_location)
poly_image = ogr.CreateGeometryFromWkt(wkt_image)
contained = poly_location.Intersect(poly_image)
# print contained
shutil.rmtree(os.path.join(output_folder, fileshortname + '.SAFE'))
return contained
if progress.wasCanceled():
break
#Create OGR Feature
feat = ogr.Feature(lyr.GetLayerDefn())
for i in range(len(columns)):
colName = columns[i]
#Check if its the geometry column in the iteration
if colName == geom:
if r[i] is not None:
featGeom = ogr.CreateGeometryFromWkt(r[i])
else:
featGeom = ogr.CreateGeometryFromWkt("")
feat.SetGeometry(featGeom)
else:
field_value = r[i]
field_value = unicode(field_value).encode('utf-8')
feat.SetField(i,field_value)
if lyr.CreateFeature(feat) != 0:
progress.close()
raise Exception(
"Failed to create feature in %s"%(self._targetFile)
)
if featGeom is not None:
featGeom.Destroy()
def intersect_plots(self):
"""Exports a list of JSON coordinates of cell vertices near the plot location."""
# FIXME: Only works for first plot in list.
plot_shp = self.plot_shp
plot_geoms = self.get_geom(plot_shp)
print(plot_geoms[0])
self.all_cells = []
if plot_shp == None:
print("Consider adding a plot shapefile path.")
else:
print("Indexing plots with grid.")
for plot in plot_geoms:
plot_cells = []
plot = ogr.CreateGeometryFromWkt(plot)
grid = self.get_geom(self.grid_path)
for cell in grid:
cell = ogr.CreateGeometryFromWkt(cell)
if cell.Overlaps(plot) == True or cell.Within(plot) == True:
json_cell = cell.ExportToJson()
plot_cells.append(json_cell)
self.all_cells.append(plot_cells)