1

I am using Google Earth Engine Python API to download images given a specific polygon coordinates. This is the code I am using so far:

import urllib



lonmin = 39.40666318517972
lanmin = 25.9924500079897
lonmax = 39.41158346062836
lanmax = 25.9968725232256


polygon = [[[lonmin,lanmin],[lonmin,lanmax],[lonmax,lanmax],[lonmax,lanmin],[lonmin,lanmin]]]

project = 'projects/earthengine-public'
asset_id = 'COPERNICUS/S2_SR'
name = '{}/assets/{}'.format(project, asset_id)

url = 'https://earthengine.googleapis.com/v1alpha/{}:listImages?{}'.format(
  name, urllib.parse.urlencode({
    'startTime': '2021-09-01T00:00:00.000Z',
    'endTime': '2021-10-01T00:00:00.000Z',
    'region': '{"type":"Polygon", "coordinates":' + str(polygon) + '}',
    'filter': 'CLOUDY_PIXEL_PERCENTAGE < 10',
}))
response = session.get(url)
content = response.content
print(json.loads(content))
idFinal = ''
for asset in json.loads(content)['images']:
    id = asset['id']
    cloud_cover = asset['properties']['CLOUDY_PIXEL_PERCENTAGE']
    if cloud_cover <= 0.1:
      idFinal = id
    print('%s : %s' % (id, cloud_cover))
asset_id = idFinal

import numpy
import io
name = '{}/assets/{}'.format(project, asset_id)
url = 'https://earthengine.googleapis.com/v1alpha/{}'.format(name)

response = session.get(url)
content = response.content

asset = json.loads(content)
print('Band Names: %s' % ','.join(band['id'] for band in asset['bands']))
print('First Band: %s' % json.dumps(asset['bands'][0], indent=2, sort_keys=True))


dic = {'type':'Polygon','coordinates':polygon}
print(text)
url = 'https://earthengine.googleapis.com/v1alpha/{}:getPixels'.format(name)
body = json.dumps({
    'fileFormat': 'PNG',
    'bandIds': ['B4', 'B3', 'B2'],
    'region': asset['geometry'],
    'grid': {
        'affineTransform': {
            'scaleX': 10,
            'scaleY': -10,
            'translateX': 499980,
            'translateY': 4200000,
        },
        'dimensions': {'width': 512, 'height': 512},
    },
    'visualizationOptions': {
        'ranges': [{'min': 0, 'max': 3000}],
    },
})

image_response = session.post(url, body)
image_content = image_response.content
from IPython.display import Image
Image(image_content)

First, the generated image does not have the same polygon coordinates I specified, it changed them. Second, I tried to pass my polygon coordinates again in the body above to zoom in after the image was generated, but the quality of the generated image was very, very bad.

How can I zoom in the generated image to my object of interest?

Above is my polygon coordinates and here is what it changed them after I requested the image:

{
  'type': 'Polygon',
  'coordinates': [
    [
      [
        40.068957316948854,
        26.215994185768736
      ],
      [
        40.068920713524705,
        26.216043295503244
      ],
      [
        40.068896076592026,
        26.216046032380927
      ],
      [
        39.801629988024615,
        26.217780594646012
      ],
      [
        39.53435706551925,
        26.219021030857405
      ],
      [
        39.26707957895235,
        26.219767303486467
      ],
      [
        38.999799798752655,
        26.22001938995396
      ],
      [
        38.99975356593368,
        26.21998284030853
      ],
      [
        38.999702638177034,
        26.21995173879716
      ],
      [
        38.9996994551909,
        26.21993691936522
      ],
      [
        38.999701922005165,
        25.228593761877335
      ],
      [
        38.99974234591577,
        25.228552348588565
      ],
      [
        38.99977672213461,
        25.22850659421654
      ],
      [
        38.99979311449172,
        25.228503755080244
      ],
      [
        39.20059077048368,
        25.228365391280175
      ],
      [
        39.401387521499025,
        25.227955697228307
      ],
      [
        39.602182471530334,
        25.2272746797847
      ],
      [
        39.80297472469079,
        25.22632235035219
      ],
      [
        39.80635817334824,
        25.226665737874107
      ],
      [
        39.80638904465522,
        25.226696521712388
      ],
      [
        39.80643272006535,
        25.2267093977483
      ],
      [
        39.80644261772588,
        25.22673003630085
      ],
      [
        39.81009238823766,
        25.238090120501266
      ],
      [
        39.87313702930068,
        25.472324123015845
      ],
      [
        39.93642769882005,
        25.706531086086887
      ],
      [
        39.999995134252245,
        25.940683449121202
      ],
      [
        40.06381580309454,
        26.17480795918538
      ],
      [
        40.06899493711149,
        26.215945937347012
      ],
      [
        40.068957316948854,
        26.215994185768736
      ]
    ]
  ]
}

1 Answer 1

0

The geometry of the image extents will be different to that of the polygon unless you clip to the polygon. When you provided a polygon to the region argument, that is used to filter images that overlap the region. It looks like the image returned does overlap your polygon.

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.