Python fiona.collection() Examples
The following are 15
code examples of fiona.collection().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
fiona
, or try the search function
.
Example #1
Source File: vector.py From OpenSarToolkit with MIT License | 6 votes |
def latlon_to_shp(lon, lat, shapefile): shapefile = str(shapefile) schema = {'geometry': 'Point', 'properties': {'id': 'str'}} wkt = loads('POINT ({} {})'.format(lon, lat)) with collection(shapefile, "w", crs=from_epsg(4326), driver="ESRI Shapefile", schema=schema) as output: output.write({'geometry': mapping(wkt), 'properties': {'id': '1'}})
Example #2
Source File: vector.py From OpenSarToolkit with MIT License | 6 votes |
def buffer_shape(infile, outfile, buffer=None): with collection(infile, "r") as in_shape: # schema = in_shape.schema.copy() schema = {'geometry': 'Polygon', 'properties': {'id': 'int'}} crs = in_shape.crs with collection( outfile, "w", "ESRI Shapefile", schema, crs=crs) as output: for i, point in enumerate(in_shape): output.write({ 'properties': { 'id': i }, 'geometry': mapping( shape(point['geometry']).buffer(buffer)) })
Example #3
Source File: check_shp.py From labuildings with BSD 3-Clause "New" or "Revised" License | 5 votes |
def check_file(filename): with collection(filename, 'r') as features: for feature in features: try: shape = asShape(feature['geometry']) if not shape.is_valid: geometry = json.dumps(feature, indent=2) print "Invalid geometry:\n" print geometry print '\n' except: print "Error parsing:\n" print json.dumps(feature, indent=2)
Example #4
Source File: chunk.py From labuildings with BSD 3-Clause "New" or "Revised" License | 5 votes |
def chunk(featureFileName, sectionFileName, pattern, key = None): # Load and index with collection(featureFileName, "r") as featureFile: featureIdx = index.Index() features = [] for feature in featureFile: try: shape = asShape(feature['geometry']) features.append(feature) featureIdx.add(len(features) - 1, shape.bounds) except ValueError: print "Error parsing feature" pprint(feature) # Break up by sections and export with collection(sectionFileName, "r") as sectionFile: i = 0 for section in sectionFile: fileName = pattern % i if key: fileName = pattern % section['properties'][key] properties = {} try: with collection(fileName, 'w', 'ESRI Shapefile', schema = featureFile.schema, crs = featureFile.crs) as output: sectionShape = asShape(section['geometry']) for j in featureIdx.intersection(sectionShape.bounds): if asShape(features[j]['geometry']).intersects(sectionShape): properties = features[j]['properties'] output.write(features[j]) print "Exported %s" % fileName i = i + 1 except ValueError: print "Error exporting " + fileName pprint(properties) pprint(featureFile.schema)
Example #5
Source File: pycrown.py From pycrown with GNU General Public License v3.0 | 5 votes |
def export_tree_locations(self, loc='top'): """ Convert tree top raster indices to georeferenced 3D point shapefile Parameters ---------- loc : str, optional tree seed position: `top` or `top_cor` """ outfile = self.outpath / f'tree_location_{loc}.shp' outfile.parent.mkdir(parents=True, exist_ok=True) if outfile.exists(): outfile.unlink() schema = { 'geometry': '3D Point', 'properties': {'DN': 'int', 'TH': 'float', 'COR': 'int'} } with fiona.collection( str(outfile), 'w', 'ESRI Shapefile', schema, crs=self.srs ) as output: for tidx in range(len(self.trees)): feat = {} tree = self.trees.iloc[tidx] feat['geometry'] = mapping( Point(tree[loc].x, tree[loc].y, tree[f'{loc}_elevation']) ) feat['properties'] = {'DN': tidx, 'TH': float(tree[f'{loc}_height']), 'COR': int(tree.tt_corrected)} output.write(feat)
Example #6
Source File: pycrown.py From pycrown with GNU General Public License v3.0 | 5 votes |
def export_tree_crowns(self, crowntype='crown_poly_smooth'): """ Convert tree crown raster to georeferenced polygon shapefile Parameters ---------- crowntype : str, optional choose whether the raster of smoothed version should be exported: `crown_poly_smooth` or `crown_poly_raster` """ outfile = self.outpath / f'tree_{crowntype}.shp' outfile.parent.mkdir(parents=True, exist_ok=True) if outfile.exists(): outfile.unlink() schema = { 'geometry': 'Polygon', 'properties': {'DN': 'int', 'TTH': 'float', 'TCH': 'float'} } with fiona.collection( str(outfile), 'w', 'ESRI Shapefile', schema, crs=self.srs ) as output: for tidx in range(len(self.trees)): feat = {} tree = self.trees.iloc[tidx] feat['geometry'] = mapping(tree[crowntype]) feat['properties'] = { 'DN': tidx, 'TTH': float(tree.top_height), 'TCH': float(tree.top_cor_height) } output.write(feat)
Example #7
Source File: nyc_tax_lot.py From gazetteer with MIT License | 4 votes |
def extract_shapefile(shapefile, uri_name, simplify_tolerance=None): for feature in collection(shapefile, "r"): geometry = feature["geometry"] properties = feature["properties"] #calculate centroid geom_obj = asShape(geometry) try: centroid = [geom_obj.centroid.x , geom_obj.centroid.y] except AttributeError: print "Error: ", feature continue borough = "" boros = {"1":"Manhattan", "2": "Bronx", "3":"Brooklyn", "4": "Queens", "5": "Staten Island"} if properties.get("BORO"): borough = boros[properties["BORO"]] + ", " block = "" if properties.get("BLOCK"): block = "Block " + str(properties["BLOCK"]) +", " name = borough + block + "Lot " + str(properties["LOT"]) bbl = properties.get("BBL") #stored with uris #feature code mapping feature_code = "ADM7" source = properties #keep all fields anyhow # unique URI which internally gets converted to the place id # Must be unique! uri = uri_name + bbl uri_bbl = "bbl:"+bbl timeframe = {} updated = datetime.datetime.utcnow().replace(second=0, microsecond=0).isoformat() place = { "name":name, "centroid":centroid, "feature_code": feature_code, "geometry":geometry, "is_primary": True, "source": source, "alternate": [], "updated": updated, "uris":[uri, uri_bbl], "relationships": [], "timeframe":timeframe, "admin":[] } #print place dump.write(uri, place)
Example #8
Source File: nyc_township_shapefile_2010.py From gazetteer with MIT License | 4 votes |
def extract_shapefile(shapefile, uri_name, simplify_tolerance=None): for feature in collection(shapefile, "r"): geometry = feature["geometry"] properties = feature["properties"] #calculate centroid geom_obj = asShape(geometry) try: centroid = [geom_obj.centroid.x , geom_obj.centroid.y] except AttributeError: print "Error: ", feature continue if properties["NAME"]: name = properties["NAME"] else: continue #feature code mapping feature_code = "ADM3" if properties["LSAD"] == "Resvn": feature_code = "RESV" area = properties["CENSUSAREA"] source = properties #keep all fields anyhow # unique URI which internally gets converted to the place id # Must be unique! uri = uri_name + "." + properties["GEO_ID"] + "."+ feature["id"] timeframe = {} timeframe = {"start": "2000-01-01","start_range":0, "end": "2010-01-01", "end_range":0} updated = "2012-01-31" place = { "name":name, "centroid":centroid, "feature_code": feature_code, "geometry":geometry, "is_primary": True, "source": source, "alternate": [], "updated": updated, "area": area, "uris":[uri], "relationships": [], "timeframe":timeframe, "admin":[] } #print place dump.write(uri, place)
Example #9
Source File: nyc_hist_states_shapefile.py From gazetteer with MIT License | 4 votes |
def extract_shapefile(shapefile, uri_name, simplify_tolerance=None): for feature in collection(shapefile, "r"): geometry = feature["geometry"] properties = feature["properties"] #calculate centroid geom_obj = asShape(geometry) if simplify_tolerance: geom_obj = geom_obj.simplify(simplify_tolerance) try: centroid = [geom_obj.centroid.x , geom_obj.centroid.y] except AttributeError: print "Error: ", feature continue geometry = mapping(geom_obj) if properties["FULL_NAME"]: name = properties["FULL_NAME"] #feature code mapping feature_code = "ADM1H" source = properties #keep all fields anyhow # unique URI which internally gets converted to the place id # Must be unique! uri = uri_name + "." + properties["ID"] + "."+ str(properties["VERSION"]) #1766/07/02 to 1766-01-01 timeframe = {"start": properties["START_DATE"].replace('/','-'), "start_range":0, "end": properties["END_DATE"].replace('/','-'), "end_range":0} #TODO admin? for counties? updated = "2011-10-01" area = properties["AREA_SQMI"] place = { "name":name, "centroid":centroid, "feature_code": feature_code, "geometry":geometry, "is_primary": True, "source": source, "updated": updated, "uris":[uri], "relationships": [], "timeframe":timeframe, "admin":[], "area": area } dump.write(uri, place)
Example #10
Source File: nyc_township_shapefile_2000.py From gazetteer with MIT License | 4 votes |
def extract_shapefile(shapefile, uri_name, simplify_tolerance=None): for feature in collection(shapefile, "r"): geometry = feature["geometry"] properties = feature["properties"] #calculate centroid geom_obj = asShape(geometry) try: centroid = [geom_obj.centroid.x , geom_obj.centroid.y] except AttributeError: print "Error: ", feature continue if properties["NAME"]: name = properties["NAME"] else: continue #feature code mapping feature_code = "ADM3" if properties["LSAD_TRANS"] == "Reservation": feature_code = "RESV" source = properties #keep all fields anyhow # unique URI which internally gets converted to the place id # Must be unique! uri = uri_name + "." + properties["COUSUBFP"] + "."+ feature["id"] timeframe = {} timeframe = {"start": "1990-01-01", "start_range":0, "end": "2000-01-01", "end_range":0} updated = "2012-01-31" place = { "name":name, "centroid":centroid, "feature_code": feature_code, "geometry":geometry, "is_primary": True, "source": source, "alternate": [], "updated": updated, "uris":[uri], "relationships": [], "timeframe":timeframe, "admin":[] } #print place dump.write(uri, place)
Example #11
Source File: nyc_tax_block.py From gazetteer with MIT License | 4 votes |
def extract_shapefile(shapefile, uri_name, simplify_tolerance=None): for feature in collection(shapefile, "r"): geometry = feature["geometry"] properties = feature["properties"] #calculate centroid geom_obj = asShape(geometry) try: centroid = [geom_obj.centroid.x , geom_obj.centroid.y] except AttributeError: print "Error: ", feature continue boros = {"1":"Manhattan", "2": "Bronx", "3":"Brooklyn", "4": "Queens", "5": "Staten Island"} name = "Block "+str(properties["BLOCK"]) if properties.get("BORO"): borough = boros[properties["BORO"]] name = borough + ", Block "+ str(properties["BLOCK"]) #feature code mapping feature_code = "ADM6" source = properties #keep all fields anyhow # unique URI which internally gets converted to the place id # Must be unique! bb = str(properties.get("BORO"))+str(properties["BLOCK"]) uri = uri_name + bb +"/"+feature["id"] uri_bb = "bb:"+bb timeframe = {} updated = datetime.datetime.utcnow().replace(second=0, microsecond=0).isoformat() place = { "name":name, "centroid":centroid, "feature_code": feature_code, "geometry":geometry, "is_primary": True, "source": source, "alternate": [], "updated": updated, "uris":[uri,uri_bb], "relationships": [], "timeframe":timeframe, "admin":[] } #print place dump.write(uri, place)
Example #12
Source File: nyc_hist_counties_shapefile.py From gazetteer with MIT License | 4 votes |
def extract_shapefile(shapefile, uri_name, simplify_tolerance=None): for feature in collection(shapefile, "r"): geometry = feature["geometry"] properties = feature["properties"] #calculate centroid geom_obj = asShape(geometry) if simplify_tolerance: geom_obj = geom_obj.simplify(simplify_tolerance) try: centroid = [geom_obj.centroid.x , geom_obj.centroid.y] except AttributeError: print "Error: ", feature continue geometry = mapping(geom_obj) if properties["FULL_NAME"]: name = properties["FULL_NAME"] #feature code mapping feature_code = "ADM2H" #default code (building) source = properties #keep all fields anyhow # unique URI which internally gets converted to the place id # Must be unique! uri = uri_name + "." + properties["ID"] + "."+ str(properties["VERSION"]) #1766/07/02 to 1766-01-01 timeframe = {"start": properties["START_DATE"].replace('/','-'), "start_range":0, "end": properties["END_DATE"].replace('/','-'), "end_range":0} #TODO admin? for counties? updated = "2010-02-12" area = properties["AREA_SQMI"] place = { "name":name, "centroid":centroid, "feature_code": feature_code, "geometry":geometry, "is_primary": True, "source": source, "updated": updated, "uris":[uri], "relationships": [], "timeframe":timeframe, "admin":[], "area": area } dump.write(uri, place)
Example #13
Source File: nyc_landmarks_polygons.py From gazetteer with MIT License | 4 votes |
def extract_shapefile(shapefile, uri_name, simplify_tolerance=None): for feature in collection(shapefile, "r"): geometry = feature["geometry"] properties = feature["properties"] #calculate centroid geom_obj = asShape(geometry) try: centroid = [geom_obj.centroid.x , geom_obj.centroid.y] except AttributeError: print "Error: ", feature continue name = properties.get("NAME") #feature code mapping feature_code = "HSTS" source = properties #keep all fields anyhow # unique URI which internally gets converted to the place id # Must be unique! uri = uri_name + properties["LP_Number"] timeframe = {} updated = datetime.datetime.utcnow().replace(second=0, microsecond=0).isoformat() place = { "name":name, "centroid":centroid, "feature_code": feature_code, "geometry":geometry, "is_primary": True, "source": source, "alternate": [], "updated": updated, "uris":[uri], "relationships": [], "timeframe":timeframe, "admin":[] } dump.write(uri, place)
Example #14
Source File: nyc_township_shapefile_1990.py From gazetteer with MIT License | 4 votes |
def extract_shapefile(shapefile, uri_name, simplify_tolerance=None): for feature in collection(shapefile, "r"): geometry = feature["geometry"] properties = feature["properties"] #calculate centroid geom_obj = asShape(geometry) try: centroid = [geom_obj.centroid.x , geom_obj.centroid.y] except AttributeError: print "Error: ", feature continue if properties["NAME"]: name = properties["NAME"] else: continue #feature code mapping feature_code = "ADM3" area = properties["AREATOT"] source = properties #keep all fields anyhow # unique URI which internally gets converted to the place id # Must be unique! uri = uri_name + "." + properties["GEOID"] + "."+ feature["id"] timeframe = {} timeframe = {"start": "1980-01-01", "start_range": 0, "end": "1990-01-01", "end_range":0} updated = "2012-01-31" place = { "name":name, "centroid":centroid, "feature_code": feature_code, "geometry":geometry, "is_primary": True, "source": source, "alternate": [], "updated": updated, "area": area, "uris":[uri], "relationships": [], "timeframe":timeframe, "admin":[] } #print place dump.write(uri, place)
Example #15
Source File: vector.py From OpenSarToolkit with MIT License | 4 votes |
def wkt_to_gdf(wkt): geometry = loads(wkt) # point wkt if geometry.geom_type == 'Point': data = {'id': ['1'], 'geometry': loads(wkt).buffer(0.05).envelope} gdf = gpd.GeoDataFrame(data) # polygon wkt elif geometry.geom_type == 'Polygon': data = {'id': ['1'], 'geometry': loads(wkt)} gdf = gpd.GeoDataFrame(data) # geometry collection of single multiploygon elif geometry.geom_type == 'GeometryCollection' and len(geometry) == 1 and 'MULTIPOLYGON' in str(geometry): data = {'id': ['1'], 'geometry': geometry} gdf = gpd.GeoDataFrame(data, crs = {'init': 'epsg:4326', 'no_defs': True}) ids, feats =[], [] for i, feat in enumerate(gdf.geometry.values[0]): ids.append(i) feats.append(feat) gdf = gpd.GeoDataFrame({'id': ids, 'geometry': feats}, geometry='geometry', crs = gdf.crs ) # geometry collection of single polygon elif geometry.geom_type == 'GeometryCollection' and len(geometry) == 1: data = {'id': ['1'], 'geometry': geometry} gdf = gpd.GeoDataFrame(data, crs = {'init': 'epsg:4326', 'no_defs': True}) # everything else (hopefully) else: i, ids, geoms = 1, [], [] for geom in geometry: ids.append(i) geoms.append(geom) i += 1 gdf = gpd.GeoDataFrame({'id': ids, 'geometry': geoms}, crs = {'init': 'epsg:4326', 'no_defs': True} ) return gdf