google-earth-engine – How to Convert Local SHP/CSV Files into Earth Engine (EE) Objects

geopandasgoogle-earth-enginejupyter notebook

I work on Jupyter lab notebook on my local PC.
I want to download images from Google Earth Engine based on a shape I have, which is a GeoPandas.

The GeoPandas dataframe I have is coming from a shapefile that I have on my local computer, that was manipulated on Jupyter lab (I have applied the envelope function on polygons I have).
When I try to convert it to EE object, it never works:

#plots is the dataframe that I have uploaded from a shapefile that is local on my computer
fc = geemap.geopandas_to_ee(plots)

It works only if I save the data on the GEE platform as asset.
Is there any way I can convert local files on my PC to EE object without manually upload them as assets to EE platform?

Best Answer

You can convert your dataframe to GeoJSON (https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.to_json.html). The GeoJSON can then be used to generate an ee.FeatureCollection, which you finally use to filter your ee.ImageCollection.

from geopandas import geopandas
from shapely.geometry import Point
import json
import ee

ee.Initialize()

d = {'col1': ['name1', 'name2'], 'geometry': [Point(10.0, 10.1), Point(10.1, 10.0)]}
gdf = geopandas.GeoDataFrame(d, crs="EPSG:4326")
geo_json = gdf.to_json()

featureCollection = ee.FeatureCollection(json.loads(geo_json))

imageCollection = ee.ImageCollection('COPERNICUS/S2') \
    .filterDate('2022-01-01', '2022-01-05') \
    .filterBounds(featureCollection.geometry())

imageCollection.size().getInfo()
Related Question