2

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"]

  • You are passing a GeoDataFrame. What hapens if you try the following line: fc = ee.FeatureCollection(ee.Geometry(shapefile.to_json())) – Marcelo Villa Sep 3 at 13:49
  • Still, the error persists. "Invalid GeoJSON geometry." – Aslı Büşra Ç. Sep 3 at 14:48
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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