From 06d2afbe250949a9956239e522414c2df3e6fb34 Mon Sep 17 00:00:00 2001 From: heyarne Date: Mon, 22 Feb 2021 17:47:18 +0000 Subject: [PATCH] Add function to plot GeoDataframe / GeoSeries on folium map --- true-color-image/sentinel_helpers.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/true-color-image/sentinel_helpers.py b/true-color-image/sentinel_helpers.py index 8b93f86..1e034f0 100644 --- a/true-color-image/sentinel_helpers.py +++ b/true-color-image/sentinel_helpers.py @@ -149,7 +149,7 @@ def scihub_cloud_mask(product_path, **kwargs): except ValueError: # empty cloud mask mask = Polygon([]) - + if kwargs.get('rasterize'): # return raster version of the vector geometry we found above target_shape = kwargs.get('target_shape') @@ -197,4 +197,17 @@ class RasterReaderList(): def __exit__(self, _type, _value, _traceback): for f in self.open_files: - f.close() \ No newline at end of file + f.close() + + +def geodataframe_on_map(geodataframe): + ''' + Plot a GeoDataframe or GeoSeries on a Leaflet map; map automatically + centers + ''' + bbox = geodataframe.unary_union.bounds + minx, miny, maxx, maxy = bbox + m = folium.Map([0, 0], tiles='cartodbpositron') + folium.GeoJson(geodataframe.to_json()).add_to(m) + m.fit_bounds([[miny, minx], [maxy, maxx]]) + return m