def save_polygons(poly, output_folder, fname, meta=None): driver = ogr.GetDriverByName('Esri Shapefile') ds = driver.CreateDataSource(output_folder + '{}.shp'.format(fname)) srs = osr.SpatialReference() srs.ImportFromEPSG(4326) layer = ds.CreateLayer('', srs, ogr.wkbMultiPolygon) # Add one attribute layer.CreateField(ogr.FieldDefn('id', ogr.OFTInteger)) defn = layer.GetLayerDefn() ## If there are multiple geometries, put the "for" loop here # Create a new feature (attribute and geometry) feat = ogr.Feature(defn) feat.SetField('id', 123) # Make a geometry, from Shapely object geom = ogr.CreateGeometryFromWkb(poly.wkb) feat.SetGeometry(geom) layer.CreateFeature(feat) feat = geom = None # destroy these # Save and close everything ds = layer = feat = geom = None
def write_shapefile(self, cells, out_shp, data=False): driver = ogr.GetDriverByName('Esri Shapefile') ds = driver.CreateDataSource(out_shp) layer = ds.CreateLayer('', None, ogr.wkbPolygon) layer.CreateField(ogr.FieldDefn('id', ogr.OFTString)) if data: layer.CreateField(ogr.FieldDefn('data', ogr.OFSTJSON)) defn = layer.GetLayerDefn() for cell in cells: feat = ogr.Feature(defn) feat.SetField('id', cell['id']) geom = ogr.CreateGeometryFromWkt(cell['cell_poly']) feat.SetGeometry(geom) if data: if cell['id'] in cell['data']: feat.SetField('data', json.dumps(cell['data'][cell['id']])) else: feat.SetField('data', json.dumps(cell['data'])) layer.CreateFeature(feat) feat = geom = None # destroy these ds = layer = feat = geom = None
def addBuffer(file,size, new_file): fgc_shp = ogr.Open(file) layer = fgc_shp.GetLayer() driver=ogr.GetDriverByName('ESRI Shapefile') ds=driver.CreateDataSource(new_file) out_lyr=ds.CreateLayer('temp', layer.GetSpatialRef(), ogr.wkbPolygon) out_lyr = createFieldsFrom(layer, out_lyr) for feature in layer: geom = feature.GetGeometryRef() if geom != None: new_feat = ogr.Feature(out_lyr.GetLayerDefn()) new_feat.SetFrom(feature) new_feat.SetGeometry(geom.Buffer(size)) out_lyr.CreateFeature(new_feat) # else: #out_lyr.CreateFeature(feature) new_feat = None geom = None out_lyr= None ds=None driver = None layer = None fgc_shp = None
def extent_polygon(file,out): fgc_shp = ogr.Open(file) lyr = fgc_shp.GetLayer() extent = lyr.GetExtent() ring = ogr.Geometry(ogr.wkbLinearRing) ring.AddPoint(extent[0], extent[2]) ring.AddPoint(extent[1], extent[2]) ring.AddPoint(extent[1], extent[3]) ring.AddPoint(extent[0], extent[3]) ring.AddPoint(extent[0], extent[2]) poly = ogr.Geometry(ogr.wkbPolygon) poly.AddGeometry(ring) driver=ogr.GetDriverByName('ESRI Shapefile') ds=driver.CreateDataSource(out) extent_lyr = ds.CreateLayer('temp', lyr.GetSpatialRef(), ogr.wkbMultiPolygon ) new_feat = ogr.Feature(extent_lyr.GetLayerDefn()) new_feat.SetGeometry(poly) extent_lyr.CreateFeature(new_feat) extent_lyr = None ds = None driver = None new_feat = None lyr = None fgc_shp = None
def merge_files(f1, f2, out): print("Merging files...") try: fgc_shp = ogr.Open(f1) lyr1 = fgc_shp.GetLayer() fgc_shp2 = ogr.Open(f2) lyr2 = fgc_shp2.GetLayer() driver=ogr.GetDriverByName('ESRI Shapefile') ds=driver.CreateDataSource(out) merge_lyr = ds.CreateLayer('temp', lyr2.GetSpatialRef(), ogr.wkbMultiPolygon ) # diff_lyr = createFieldsFrom(lyr2, diff_lyr) print(" First file uninon..") union1 = ogr.Geometry(ogr.wkbMultiPolygon) for feat1 in lyr1: geom1 = feat1.GetGeometryRef() if geom1 != None: union1 = union1.Union(geom1) geom1 = None print(" Second file uninon..") union2 = ogr.Geometry(ogr.wkbMultiPolygon) for feat2 in lyr2: geom2 = feat2.GetGeometryRef() if geom2 != None: union2 = union2.Union(geom2) geom2 = None union1 = union1.Buffer(0) union2 = union2.Buffer(0) print(" Final uninon..") merge = union1.Union(union2) new_feat = ogr.Feature(merge_lyr.GetLayerDefn()) new_feat.SetGeometry(merge) merge_lyr.CreateFeature(new_feat) except: print("exception thrown!") traceback.print_exc() new_feat= None union1 = None union2 = None diff_lyr = None fgc_shp = None fgc_shp2 = None ds = None lyr1 = None lyr2 = None
def test_concav(file,out): points_by_id = get_points_from_geomety(file) fgc_shp = ogr.Open(file) lyr = fgc_shp.GetLayer() driver=ogr.GetDriverByName('ESRI Shapefile') ds1=driver.CreateDataSource(out) concave_lyr=ds1.CreateLayer('temp', lyr.GetSpatialRef(), ogr.wkbPolygon) concave_lyr = createFieldsFrom(lyr,concave_lyr) print("starting concave") ps = np.array([ ( points_by_id.iloc[xi,0] , np.array(points_by_id.iloc[xi,1]) ) for xi in range(0,points_by_id.shape[0]) ], dtype=object) #Processar em várias threads pool = multiprocessing.Pool(processes=multiprocessing.cpu_count()) res = pool.map( temp,ps ) print("End of concave calculations.") print("Saving to file") res = np.asarray(res,dtype=object) for i in range(0, res.shape[0]): try: fgc_id = res[i][0] hull = res[i][1] new_ring = ogr.Geometry(ogr.wkbLinearRing) hull = np.asarray(hull) for i in range(0, hull.shape[0]): new_ring.AddPoint(hull[i,0], hull[i,1]) poly = ogr.Geometry(ogr.wkbPolygon) poly.AddGeometry(new_ring) new_feat = ogr.Feature(concave_lyr.GetLayerDefn()) new_feat.SetField("ID_SEQ",fgc_id) new_feat.SetGeometry(poly) concave_lyr.CreateFeature(new_feat) new_feat = None except: print("exception thrown!") traceback.print_exc() break
def remove_biggest_polygon(f1, area, out): print("Removing biggest polygon") fgc_shp = ogr.Open(f1) lyr = fgc_shp.GetLayer() driver=ogr.GetDriverByName('ESRI Shapefile') ds=driver.CreateDataSource(out) holes_lyr = ds.CreateLayer('temp', lyr.GetSpatialRef(), ogr.wkbMultiPolygon ) max_area = 0.0 max_index = 0 for feature in lyr: geom = feature.GetGeometryRef() print(geom.GetGeometryCount()) for j in range(0,geom.GetGeometryCount() ): ring = geom.GetGeometryRef(j) area = ring.GetArea() print(area) if area> max_area: max_index = j max_area = area lyr =None fgc_shp = None # print("max= ", max_index) fgc_shp2 = ogr.Open(f1) lyr2 = fgc_shp2.GetLayer() for feature in lyr2: geom = feature.GetGeometryRef() for j in range(0,geom.GetGeometryCount()): ring = geom.GetGeometryRef(j) if j != max_index: new_feat = ogr.Feature(holes_lyr.GetLayerDefn()) new_feat.SetGeometry(ring.Buffer(0)) holes_lyr.CreateFeature(new_feat) new_feat = None # print(ring.GetArea()) holes_lyr = None ds=None lyr =None fgc_shp = None
def get_datasource_from_bbox(bbox: BBOX, output_dir: str) -> None: driver = ogr.GetDriverByName("GPKG") gpkg_path = os.path.join(output_dir, BBOX_GPKG_NAME) datasource = driver.Open(gpkg_path) if not datasource: datasource = driver.CreateDataSource(gpkg_path) layer = datasource.GetLayerByName(BBOX_LAYER_NAME) srs = osr.SpatialReference() srs.SetFromUserInput(bbox.crs_code) if not layer: layer = datasource.CreateLayer(BBOX_LAYER_NAME, srs, ogr.wkbPolygon) if layer.GetFeatureCount() == 0: geometry = ogr.CreateGeometryFromWkt(bbox.get_wkt()) feature_defn = layer.GetLayerDefn() feature = ogr.Feature(feature_defn) feature.SetGeometry(geometry) layer.CreateFeature(feature) feature = None layer, datasource = None, None return gpkg_path
def record_run(result_dir: str, bbox: BBOX) -> None: gpkg_path = _get_gpkg_path(result_dir) gpkg_datasource = GPKG_DRIVER.Open(gpkg_path, 1) if not gpkg_datasource: gpkg_datasource = GPKG_DRIVER.CreateDataSource(gpkg_path) cumulative_layer = gpkg_datasource.GetLayerByName(LAYER_NAME) if not cumulative_layer: srs = osr.SpatialReference() srs.SetFromUserInput("CRS:84") cumulative_layer = gpkg_datasource.CreateLayer(LAYER_NAME, srs, ogr.wkbPolygon) geometry = ogr.CreateGeometryFromWkt(bbox.get_wkt()) feature_defn = cumulative_layer.GetLayerDefn() feature = ogr.Feature(feature_defn) feature.SetGeometryDirectly(geometry) cumulative_layer.CreateFeature(feature) kml_path = os.path.join(result_dir, "coverage.kml") if os.path.exists(kml_path): os.remove(kml_path) kml_driver = ogr.GetDriverByName("KML") kml_datasource = kml_driver.CreateDataSource(kml_path) kml_datasource.CopyLayer(cumulative_layer, "areas") geojson_path = os.path.join(result_dir, "coverage.geojson") if os.path.exists(geojson_path): os.remove(geojson_path) geojson_driver = ogr.GetDriverByName("GeoJSON") geojson_datasource = geojson_driver.CreateDataSource(geojson_path) geojson_datasource.CopyLayer(cumulative_layer, "areas") cumulative_layer, gpkg_datasource, kml_datasource, geojson_datasource = ( None, None, None, None, )
# create the layer layer = data_source.CreateLayer(csvname + "_Aggregations", srs, ogr.wkbPoint) # Add the fields we're interested in field_name = ogr.FieldDefn("Name", ogr.OFTString) field_name.SetWidth(24) layer.CreateField(field_name) layer.CreateField(ogr.FieldDefn("Latitude", ogr.OFTReal)) layer.CreateField(ogr.FieldDefn("Longitude", ogr.OFTReal)) with open(csvfile, 'r') as csvf: reader = csv.DictReader(csvf) # Process the text file and add the attributes and features to the shapefile for row in reader: # create the feature feature = ogr.Feature(layer.GetLayerDefn()) # Set the attributes using the values from the delimited text file feature.SetField("Name", row['geohash']) feature.SetField("Latitude", row['c_y']) feature.SetField("Longitude", row['c_x']) # create the WKT for the feature using Python string formatting wkt = "POINT(%f %f)" % (float(row['Longitude']), float( row['Latitude'])) # Create the point from the Well Known Txt point = ogr.CreateGeometryFromWkt(wkt) # Set the feature geometry using the point feature.SetGeometry(point) # Create the feature in the layer (shapefile)
def _tiger_to_tract(self, infile): """ Converts collection of Census Tiger files into a geopandas.GeoDataFrame of census tracts Modified from original at https://svn.osgeo.org/gdal/tags/1.4.3/gdal/pymod/samples/tigerpoly.py """ class Module(object): def __init__(mod): mod.lines = {} mod.poly_line_links = {} outfile = 'tracts.shp' # Open the datasource to operate on. ds = ogr.Open(infile, update=0) poly_layer = ds.GetLayerByName('Polygon') # Create output file for the composed polygons. nad83 = osr.SpatialReference() nad83.SetFromUserInput('NAD83') shp_driver = ogr.GetDriverByName('ESRI Shapefile') shp_driver.DeleteDataSource(outfile) shp_ds = shp_driver.CreateDataSource(outfile) shp_layer = shp_ds.CreateLayer( 'out', geom_type=ogr.wkbPolygon, srs=nad83) src_defn = poly_layer.GetLayerDefn() poly_field_count = src_defn.GetFieldCount() for fld_index in range(poly_field_count): src_fd = src_defn.GetFieldDefn(fld_index) fd = ogr.FieldDefn(src_fd.GetName(), src_fd.GetType()) fd.SetWidth(src_fd.GetWidth()) fd.SetPrecision(src_fd.GetPrecision()) shp_layer.CreateField(fd) # Read all features in the line layer, holding just the geometry in a hash # for fast lookup by TLID. line_layer = ds.GetLayerByName('CompleteChain') line_count = 0 modules_hash = {} feat = line_layer.GetNextFeature() geom_id_field = feat.GetFieldIndex('TLID') tile_ref_field = feat.GetFieldIndex('MODULE') while feat is not None: geom_id = feat.GetField(geom_id_field) tile_ref = feat.GetField(tile_ref_field) try: module = modules_hash[tile_ref] except: module = Module() modules_hash[tile_ref] = module module.lines[geom_id] = feat.GetGeometryRef().Clone() line_count = line_count + 1 feat.Destroy() feat = line_layer.GetNextFeature() # Read all polygon/chain links and build a hash keyed by POLY_ID listing # the chains (by TLID) attached to it. link_layer = ds.GetLayerByName('PolyChainLink') feat = link_layer.GetNextFeature() geom_id_field = feat.GetFieldIndex('TLID') tile_ref_field = feat.GetFieldIndex('MODULE') lpoly_field = feat.GetFieldIndex('POLYIDL') rpoly_field = feat.GetFieldIndex('POLYIDR') link_count = 0 while feat is not None: module = modules_hash[feat.GetField(tile_ref_field)] tlid = feat.GetField(geom_id_field) lpoly_id = feat.GetField(lpoly_field) rpoly_id = feat.GetField(rpoly_field) if lpoly_id == rpoly_id: feat.Destroy() feat = link_layer.GetNextFeature() continue try: module.poly_line_links[lpoly_id].append(tlid) except: module.poly_line_links[lpoly_id] = [tlid] try: module.poly_line_links[rpoly_id].append(tlid) except: module.poly_line_links[rpoly_id] = [tlid] link_count = link_count + 1 feat.Destroy() feat = link_layer.GetNextFeature() # Process all polygon features. feat = poly_layer.GetNextFeature() tile_ref_field = feat.GetFieldIndex('MODULE') polyid_field = feat.GetFieldIndex('POLYID') degenerate_count = 0 while feat is not None: module = modules_hash[feat.GetField(tile_ref_field)] polyid = feat.GetField(polyid_field) tlid_list = module.poly_line_links[polyid] link_coll = ogr.Geometry(type=ogr.wkbGeometryCollection) for tlid in tlid_list: geom = module.lines[tlid] link_coll.AddGeometry(geom) try: poly = ogr.BuildPolygonFromEdges(link_coll) if poly.GetGeometryRef(0).GetPointCount() < 4: degenerate_count = degenerate_count + 1 poly.Destroy() feat.Destroy() feat = poly_layer.GetNextFeature() continue feat2 = ogr.Feature(feature_def=shp_layer.GetLayerDefn()) for fld_index in range(poly_field_count): feat2.SetField(fld_index, feat.GetField(fld_index)) feat2.SetGeometryDirectly(poly) shp_layer.CreateFeature(feat2) feat2.Destroy() except: warn('BuildPolygonFromEdges failed.') feat.Destroy() feat = poly_layer.GetNextFeature() if degenerate_count: warn('Discarded %d degenerate polygons.' % degenerate_count) # Cleanup shp_ds.Destroy() shp_ds = None ds.Destroy() ds = None # build a fully-qualified fips code and dissolve on it to create tract geographies gdf = gpd.read_file(outfile) if "CTBNA90" in gdf.columns: gdf = gdf.rename(columns={"CTBNA90": 'TRACT', "BLK90": "BLOCK"}) gdf['STATE'] = gdf['STATE'].astype(str).str.rjust(2, "0") gdf['COUNTY'] = gdf['COUNTY'].astype(str).str.rjust(3, "0") gdf['TRACT'] = gdf['TRACT'].astype(str).str.rjust(6, "0") gdf['BLOCK'] = gdf['BLOCK'].astype(str).str.rjust(4, "0") gdf['fips'] = gdf.STATE + gdf.COUNTY + gdf.TRACT if self.geom == 'block': gdf['fips'] += gdf.BLOCK gdf = gdf.dropna(subset=['fips']) gdf.geometry = gdf.buffer(0) gdf = gdf.dissolve(by='fips') gdf.reset_index(inplace=True) shp_driver.DeleteDataSource(outfile) return gdf
def difference(f1, f2, out): try: print("Difference between:") print(" ",f1) print(" ",f2) fgc_shp = ogr.Open(f1) lyr1 = fgc_shp.GetLayer() fgc_shp2 = ogr.Open(f2) lyr2 = fgc_shp2.GetLayer() driver=ogr.GetDriverByName('ESRI Shapefile') ds=driver.CreateDataSource(out) diff_lyr = ds.CreateLayer('temp', lyr2.GetSpatialRef(), ogr.wkbMultiPolygon ) # diff_lyr = createFieldsFrom(lyr2, diff_lyr) union1 = ogr.Geometry(ogr.wkbMultiPolygon) for feat1 in lyr1: geom1 = feat1.GetGeometryRef() if geom1 != None: union1 = union1.Union(geom1) geom1 = None union2 = ogr.Geometry(ogr.wkbMultiPolygon) for feat2 in lyr2: geom2 = feat2.GetGeometryRef() if geom2 != None: union2 = union2.Union(geom2) geom2 = None union1 = union1.Buffer(0) union2 = union2.Buffer(0) print(union1.GetGeometryCount()) diff = union1.Difference(union2) new_feat = ogr.Feature(diff_lyr.GetLayerDefn()) new_feat.SetGeometry(diff) diff_lyr.CreateFeature(new_feat) # union1 = ogr.Geometry(ogr.wkbMultiPolygon) # for feat1 in lyr1: # geom1 = feat1.GetGeometryRef() # fgc_shp2 = ogr.Open(f2) # lyr2 = fgc_shp2.GetLayer() # for feat2 in lyr2: # geom2 = feat2.GetGeometryRef() # if geom1 != None and geom2 != None: ## if geom2.Intersects(geom1) or geom2.Within(geom1) or geom2.Overlaps(geom1) or geom2.Crosses(geom1): # diff = geom1.SymmetricDifference(geom2) # if diff != None: # union1.AddGeometry(diff) ## new_feat = ogr.Feature(diff_lyr.GetLayerDefn()) ## new_feat.SetGeometry(diff) ## diff_lyr.CreateFeature(new_feat) # # geom2 = None # new_feat = None # diff=None # # geom1 = None # new_feat = ogr.Feature(diff_lyr.GetLayerDefn()) # new_feat.SetGeometry(union1) # diff_lyr.CreateFeature(new_feat) except: print("exception thrown!") traceback.print_exc() new_feat= None union1 = None union2 = None diff_lyr = None fgc_shp = None fgc_shp2 = None ds = None lyr1 = None lyr2 = None
utils.create_feature_field([layer, layer_r, layer_c, layer_gjc], "name", ogr.OFTString) utils.create_feature_field([layer, layer_r, layer_c, layer_gjc], "key", ogr.OFTString) utils.create_feature_field([layer, layer_r, layer_c, layer_gjc], "area", ogr.OFTReal) utils.create_feature_field([layer, layer_r, layer_c], "totalArea", ogr.OFTReal) utils.create_feature_field([layer, layer_r, layer_c], "totalCount", ogr.OFTInteger) total_area = 0.0 for i, p in enumerate(mult_p): # new feature for object feature = ogr.Feature(layer.GetLayerDefn()) # new feature for rect object centroid feature_c = ogr.Feature(layer_c.GetLayerDefn()) # new feature for mapbox object centroid feature_gjc = ogr.Feature(layer_gjc.GetLayerDefn()) # create geometry from polygon geom_poly = ogr.CreateGeometryFromWkb(p.wkb) # transform geometry from source_srs to target_srs geom_poly.Transform(transform) # set the feature geometry and attributes feature.SetGeometry(geom_poly) feature_c.SetGeometry(geom_poly.Centroid()) feature_gjc.SetGeometry(ogr.CreateGeometryFromWkb(p.wkb).Centroid()) utils.set_feature_field([feature, feature_c, feature_gjc], "name",