I want to convert the following javascript to python:

var f = final.sample({region: county_j,
    scale: scale,
    projection: 'EPSG:4326',
    numPixels: numPixels});

The problem is how to translate the sample function, my python code is:

f = final.sample({region: county_j,
    scale: scale,
    projection: 'EPSG:4326',
    numPixels: numPixels})

But it faces an error:

ee.ee_exception.EException: Invalid GeoJSON geometry.

Could you tell me how to write the sample function in python?


1 Answer 1


I honestly don't know how your shapefile looks like or what columns it has, is it a point, line or polygon but I believe you're reading the shapefile using Geopandas and then trying to use the sample function? If that's the case here's how the Python syntax should look like:

scale = 1000  # define a scale in meters
f = final.sample(county_j, scale)

You can find more information on Python API from this docs.

  • I find an example at Line 25-26 in github.com/google/earthengine-community/blob/… The python api can be written as javascript api and only add '**' before the {}.
    – wxystudio
    Commented Apr 24, 2023 at 18:49
  • 1
    Yes the '**' used at the front is used to unpack the values in a dictionary in Python.
    – Rezwan
    Commented Apr 25, 2023 at 15:17

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.