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
        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
        if profile['schema']['geometry'] in ['Polygon', 'MultiPolygon']:
            print("Valid geometry type")
        print(f"File not in the valid geometry types (Polygon, MultiPolygon)")

    return src

1 Answer 1


Not sure you need to but you can access the features by just iterating over the open dataset src like so:

import fiona
import geojson

src = fiona.open(filename)
for feature in src:
    if not geojson.Feature(feature).is_valid or not src.validate_record_geometry(feature):
        raise RuntimeError("Feature is not valid")
  • thank you @user2856 , that worked! I still havent wrapped my head around shapely for some reason. maybe Im just not using it enough. But its nice to know its not necessary in this instance. I also wasnt aware of, validate_record_geometry() so thank you for pointing that out to me as well.
    – glw
    Oct 11, 2023 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.