Skip to main content

Hi,

Is there a way to create a Geometry Filter using defining the AOI directly from a geojson file in Python (ideally using geopandas)? The only examples I am seeing in tutorials is about copy-pasting coordinates, but I hope there is a more automated way to do this.

Thank you!

Hi,

Have you gone through the getting started guides on the GeoPandas documentation site? The dataframes work the same as Pandas dataframes for the most part so from the linked introductory guide you should be able work out a method on how to filter your dataframe.


Thank you for your reply! I realized my post was not well stated, I meant create a geometry filter for the Planet Data API search from a geojson file. The examples I found are about copy pasting coordinates and kind of building the geojson by hand. I was hoping there is a less manual way to do this if you have several AOIs as geojson on your drive (or stored in a geopandas dataframe). Julien


Hi brunj7, to load geojson from a file and use it in Python (we recommend the Python SDK for this, but you can also use the output with your own API calls): 

# open file and read it as json
with open("./test.geojson") as f:
data = json.load(f)

The geojson in my test.geojson file simply contains a Polygon (i.e. not a Feature). If you load a Feature you will need to grab the `geometry` within.

Here’s a snippet that illustrates how to use the result with the SDK (see the SDK Getting Started guide first):

# open file and read it as json
with open("./test.geojson") as f:
data = json.load(f)

# for searching, you can pass the geojson data directly to the `geometry` field
rprint(item) async for item in client.search(r"PSScene"], geometry=data, limit=10)]

# if you prefer to create a GeometryFilter, use it with the `search_filter` field:
geom_filter = filters.geometry_filter(data)
rprint(item) async for item in client.search(r"PSScene"], search_filter=geom_filter, limit=10)]

To get geojson from a GeoPandas object, try using `geometry.__geo_interface__`. You should be able to pass that value to the search method or the geometry_filter function. Let me know if that works.

Lastly, if you want to store and re-use these geometries, check out Features API. You can also pass feature references directly to the above methods.


Reply