I have an application that allows users to submit a shapefile or GeoJSON file. I would like to use Python to validate the files and geometries within them to make sure I process valid geoms. Im having trouble understanding how I go from a Fiona Collection to geometries that I can validate with the geojson package – In the case of a geojson file upload. I assume that I'll have to iterate over each geometry. And possibly involve Shapely?
Below is my code where I am validating the files themselves via Fiona. But I think to avoid further issues in processing down the line I should validate the individual geometries as well, since they will be used as AOIs to clip out other features.
I'm not clear on what steps are needed for the geojson package to accept the geoms
def fiona_validate(filename):
""" Validates GeoJson or Shapefile Inputs
:param filename: Submitted file to be validated
"""
try:
src = fiona.open(filename)
profile = src.profile
if profile['driver'] in ['ESRI Shapefile', 'GeoJSON']:
print("Valid File Type")
except Exception as e:
print(f"Invalid File format. {e}")
# checkin if the correct geomtery type has been submitted
try:
if profile['schema']['geometry'] in ['Polygon', 'MultiPolygon']:
print("Valid geometry type")
except:
print(f"File not in the valid geometry types (Polygon, MultiPolygon)")
return src
Best Answer
Not sure you need to but you can access the features by just iterating over the open dataset
src
like so: