[GIS] Accessing a shapefile with GoogleEarthEngine-API : Invalid GeoJSON geometry

geopandasgoogle-earth-apigoogle-earth-enginepolygonpython

I'm trying to download Sentinel-2 data of my area of interests on Google Earth Engine API for Python.

I'm trying to access my shape file (contains 2 different polygons) on the API. I added the shape file using geopandas as gpd.

shapefile = gpd.read_file("polygon.shp")
fc = ee.FeatureCollection(ee.Geometry(shapefile))

The Error:

Traceback (most recent call last):
  File "D:/Hacettepe/GFZ/PyScript/googleEarthEngine_S2.py", line 32, in <module>
    fc = ee.FeatureCollection(ee.Geometry(shapefile))
  File "C:\Users\polyt\AppData\Local\Programs\Python\Python37\lib\site-packages\ee\computedobject.py", line 32, in __call__
    return type.__call__(cls, *args, **kwargs)
  File "C:\Users\polyt\AppData\Local\Programs\Python\Python37\lib\site-packages\ee\geometry.py", line 83, in __init__
    raise ee_exception.EEException('Invalid GeoJSON geometry.')
ee.ee_exception.EEException: Invalid GeoJSON geometry.

I tried to find different ways to access polygons (not creating a new one), but I couldn't find any source. So I would be happy if you could help me.

[Also I'm getting "HTTP Error 403: Forbidden"]

Best Answer

As @Marcelo Villa said, you are trying to pass a GeoDataFrame into an ee.Geometry constructor which fails because it is expecting geojson like information. You will need to extract the information needed from the GeoDataFrame to create the feature object. Here is an example:

shapefile = gpd.read_file("polygon.shp")

features = []
for i in range(shapefile.shape[0]):
    geom = shapefile.iloc[i:i+1,:] 
    jsonDict = eval(geom.to_json()) 
    geojsonDict = jsonDict['features'][0] 
    features.append(ee.Feature(geojsonDict)) 

fc = ee.FeatureCollection(features)

empty = ee.Image().byte() 
outline = empty.paint(
    featureCollection=fc,
    color=1,
    width=3
).clip(fc.geometry())
print(outline.visualize(palette='FF0000').getThumbURL())

Disclaimer: I tested this out on a shapefile I had on hand (which was only one feature) and the code may need some changes to work with different number of features and/or shape formats (point vs line vs polygon).

Edit: Updated code example with correct use of variable names.

Related Question