Skip to content

Conversation

@ErikSeras
Copy link
Contributor

Hi,

I would like to contribute with the improvement of the function cartoee.add_scale_bar(). Which allows adding a scale bar with an improved layout. The following image is an example of its use.

image

@giswqs
Copy link
Member

giswqs commented Feb 21, 2021

@ErikSeras Thanks a lot for your contribution!

@giswqs giswqs merged commit db1d7a7 into gee-community:master Feb 21, 2021
@ErikSeras ErikSeras deleted the update_add_scale_bar branch February 21, 2021 05:01
@giswqs
Copy link
Member

giswqs commented Feb 21, 2021

@ErikSeras Could you provide a working example for the add_scale_bar function? It does not seem to work.

https://gist.github.com/giswqs/96a346ae3cb880dcc08cdea3e923f380

@ErikSeras
Copy link
Contributor Author

Hi @giswqs. I tried the initial code and I think the last line of the function can throw that error. That line should be removed.
fig.tight_layout()

image

After that, this would be an example code.

import ee
import geemap
from geemap import cartoee
import matplotlib.pyplot as plt
geemap.ee_initialize()
# Get image
lon = -115.1585
lat = 36.1500
start_year = 1984
end_year = 2011

point = ee.Geometry.Point(lon, lat)
years = ee.List.sequence(start_year, end_year)

def get_best_image(year):   

    start_date = ee.Date.fromYMD(year, 1, 1)
    end_date = ee.Date.fromYMD(year, 12, 31)
    image = ee.ImageCollection("LANDSAT/LT05/C01/T1_SR") \
        .filterBounds(point) \
        .filterDate(start_date, end_date) \
        .sort("CLOUD_COVER") \
        .first()    
    return ee.Image(image)

collection = ee.ImageCollection(years.map(get_best_image))


vis_params = {
  "bands": ['B4', 'B3', 'B2'],
  "min": 0,
  "max": 5000
}

image = ee.Image(collection.first())
w = 0.4
h = 0.3

region = [lon-w, lat-h, lon+w, lat+h]

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

# use cartoee to get a map
ax = cartoee.get_map(image, region=region, vis_params=vis_params)

# add gridlines to the map at a specified interval
cartoee.add_gridlines(ax, interval=[0.2, 0.2], linestyle=":")

# add north arrow
north_arrow_dict = {
    "text": "N",
    "xy": (0.25, 0.4),
    "arrow_length": 0.15,
    "text_color": "white",
    "arrow_color": "white",
    "fontsize": 20,
    "width": 5,
    "headwidth": 15,
    "ha": "center",
    "va": "center"
}
cartoee.add_north_arrow(ax, **north_arrow_dict)

# add scale bar
scale_bar_dict = {
    'metric_distance': 4,
    'unit': "km",
    'at_x':(0.1, 0.4),
    'at_y':(0.08, 0.11),
    'max_stripes': 5,
    'ytick_label_margins': 0.25,
    'fontsize': 8,
    'font_weight': "bold",
    'rotation': 45,
    'zorder': 999,
    'paddings': {"xmin": 0.05, "xmax": 0.05, "ymin": 2.2, "ymax": 0.5},
}


cartoee.add_scale_bar(
    ax, 
    **scale_bar_dict
)

ax.set_title(label = 'Las Vegas, NV', fontsize=15)
plt.show()

image

giswqs added a commit that referenced this pull request Feb 21, 2021
@giswqs
Copy link
Member

giswqs commented Feb 21, 2021

@ErikSeras Problem solved. Many thanks for your help!

https://geemap.org/notebooks/61_cartoee_scalebar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants