gee-community / geemap

A Python package for interactive geospatial analysis and visualization with Google Earth Engine.

Home Page:https://geemap.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

View extent in cartoee module switched up

djcotto opened this issue · comments

Description

Hej all,

I tried to make a publication map via the cartoee module. In the end, the names of districts were constantly not in the centroid eventhough I just calculated them correctly and double checked correctness. I think I now located a slight switch-up of numbers in the cartoee module. If a region is specified as part of the add_layer() function the view_extent is set wrongly. Code is in line 232.

Instead of

view_extent = (region[2], region[0], region[1], region[3])

It should be

view_extent = (region[0], region[2], region[1], region[3])

Unsure if you need any further info. First bug report :)

Thank you for reporting. Would you like to submit a pull request to fix the issue?

I tested it. Changing it will resulting in incorrect extent. Can you share sample code that can reproduce the issue?

image

Basically what happens on my side is that the outline of the districts is mapped correctly. But as soon as I define a region /bounding box for my figure manually the coordinates for that figure on the x-axis are switched around. See the picture below where the intervalls on the x-axis define 1.2°W on the left and 1.8°W on the right, while for Kumasi it is the other way around.
This gets more clear when trying to place something on the figure according to the coordinates. Below you can see that the district names calculated to be in the centroid are not in the centroid of each district because of the interchanged view_extent that gets defined for the figure when a region is specified.

Happy to submit a pull request when agreed on the issue.

view_extent_Kumasi

import ee
import matplotlib.pyplot as plt
from geemap import cartoee

FAO_adm_lvl2 = ee.FeatureCollection("FAO/GAUL/2015/level2") 
filtered = FAO_adm_lvl2.filter(ee.Filter.And(ee.Filter.inList("ADM2_CODE", [190581, 190568, 190575, 190567, 16578, 16576, 190719, 190720]), 
                                        ee.Filter.eq("ADM0_NAME", "Ghana")))

style = {"color": "#000000", "width": 1, "fillColor": "#00000000"}

adm2_outline = ee.Image(filtered.style(**style))

region = [-1.95, 6.4, -1.2, 7.2]

specified_regions_with_centroids = filtered.map(lambda feature: feature.set('centroid', feature.geometry().centroid()))
specified_regions_list = specified_regions_with_centroids.toList(specified_regions_with_centroids.size())

# Iterate over the list to extract geometries and attributes
region_centroids = []
region_names = []
for i in range(specified_regions_list.size().getInfo()):
    feature = ee.Feature(specified_regions_list.get(i))
    region_centroids.append(dict(feature.get('centroid').getInfo())["coordinates"])
    region_names.append(feature.get('ADM2_NAME').getInfo())

fig = plt.figure(figsize=(15, 10))

ax = cartoee.get_map(adm2_outline, region=region) 
cartoee.add_gridlines(ax, interval=0.2, xtick_rotation=0, linestyle=":")

for name, centroid in zip(region_names, region_centroids):
    plt.annotate(text=name, xy=centroid, fontsize=8, color='red', ha='center')

plt.savefig("view_extent_Kumasi")

The region should be in the format of [E,S,W,N].

region = [-1.2, 6.4, -1.95, 7.2]

image