Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix js-py conversion bug #2130

Merged
merged 1 commit into from
Sep 18, 2024
Merged

Fix js-py conversion bug #2130

merged 1 commit into from
Sep 18, 2024

Conversation

giswqs
Copy link
Member

@giswqs giswqs commented Sep 18, 2024

This PR fixes the bug reported in #2129 by

  • Adding quotes around keys
  • Removing everything after the comma and before ');' for the Map.setOptions() method.

Input:

import geemap

js_code = '''
var baseChange =
    [{featureType: 'all', stylers: [{saturation: -100}, {lightness: 45}]}];
Map.setOptions('baseChange', {'baseChange': baseChange});
var image1975 = ee.Image('JRC/GHSL/P2023A/GHS_POP/1975');
var image1990 = ee.Image('JRC/GHSL/P2023A/GHS_POP/1990');
var image2020 = ee.Image('JRC/GHSL/P2023A/GHS_POP/2020');
var populationCountVis = {
  min: 0.0,
  max: 100.0,
  palette:
      ['000004', '320A5A', '781B6C', 'BB3654', 'EC6824', 'FBB41A', 'FCFFA4']
};
Map.setCenter(8, 48, 7);
image1975 = image1975.updateMask(image1975.gt(0));
image1990 = image1990.updateMask(image1990.gt(0));
image2020 = image2020.updateMask(image2020.gt(0));
Map.addLayer(image1975, populationCountVis, 'Population count, 1975');
Map.addLayer(image1990, populationCountVis, 'Population count, 1990');
Map.addLayer(image2020, populationCountVis, 'Population count, 2020');
'''

code_list = geemap.js_snippet_to_py(js_code,
                        add_new_cell=False,
                        import_ee=False,
                        import_geemap=False,
                        show_map=False)
bad_python_code = ''.join(code_list)
print(bad_python_code)

Output:

m = geemap.Map()

baseChange = \
[{"featureType": 'all', "stylers": [{"saturation": -100}, {"lightness": 45}]}]
m.setOptions('baseChange')
image1975 = ee.Image('JRC/GHSL/P2023A/GHS_POP/1975')
image1990 = ee.Image('JRC/GHSL/P2023A/GHS_POP/1990')
image2020 = ee.Image('JRC/GHSL/P2023A/GHS_POP/2020')
populationCountVis = {
    "min": 0.0,
    "max": 100.0,
    "palette":
    ['000004', '320A5A', '781B6C', 'BB3654', 'EC6824', 'FBB41A', 'FCFFA4']
}
m.setCenter(8, 48, 7)
image1975 = image1975.updateMask(image1975.gt(0))
image1990 = image1990.updateMask(image1990.gt(0))
image2020 = image2020.updateMask(image2020.gt(0))
m.addLayer(image1975, populationCountVis, 'Population count, 1975')
m.addLayer(image1990, populationCountVis, 'Population count, 1990')
m.addLayer(image2020, populationCountVis, 'Population count, 2020')
m

@giswqs giswqs added the big-fix Bug fixes label Sep 18, 2024
@giswqs giswqs linked an issue Sep 18, 2024 that may be closed by this pull request
Copy link

@github-actions github-actions bot temporarily deployed to pull request September 18, 2024 02:23 Inactive
@giswqs giswqs merged commit e62b260 into master Sep 18, 2024
14 checks passed
@giswqs giswqs deleted the conversion branch September 18, 2024 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
big-fix Bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

py-js conversion bug
1 participant