python.observation.util_carto

Created on Tue Apr 25 09:08:54 2023

@author: philippe@loco-labs.io

 1# -*- coding: utf-8 -*-
 2"""
 3Created on Tue Apr 25 09:08:54 2023
 4
 5@author: philippe@loco-labs.io
 6"""
 7
 8import folium
 9
10
11class Cart():
12    '''folium map with markers'''
13
14    def __init__(self, location=None, zoom_start=5):
15        self.map = folium.Map(location=location, zoom_start=zoom_start)
16
17    def add_markers(self, location, **kwargs):
18        '''add markers with popup in a layer group'''
19        opt = {'popup': None, 'max_width': 150, 'icon': 'bomb', 'color': 'blue',
20               'group': None} | kwargs
21        grp = self.map
22        color = opt['color']
23        if opt['group']:
24            grp = folium.map.FeatureGroup(opt['group'])
25            grp.add_to(self.map)
26        if opt['popup']:
27            for loc, pop in zip(location, opt['popup']):
28                icon = folium.Icon(color=color, icon=opt['icon'], prefix='fa')
29                txt = folium.Popup(Cart.html(pop), max_width=opt['max_width'])
30                folium.Marker(location=loc, popup=txt, icon=icon).add_to(grp)
31        else:
32            for loc in location:
33                icon = folium.Icon(color=color, icon=opt['icon'], prefix='fa')
34                folium.Marker(location=loc, icon=icon).add_to(grp)
35
36    def show(self, mapname=None):
37        '''return the map and save it if mapname is present'''
38        folium.LayerControl().add_to(self.map)
39        if mapname:
40            self.map.save(mapname)
41        return self.map
42
43    @staticmethod
44    def html(dic):
45        '''return an html string from a dict'''
46        pop = ''
47        for key, val in dic.items():
48            pop += '<div>' + '<b>' + \
49                str(key) + '</b>' + ' : ' + str(val) + '</div>'
50        return pop
class Cart:
12class Cart():
13    '''folium map with markers'''
14
15    def __init__(self, location=None, zoom_start=5):
16        self.map = folium.Map(location=location, zoom_start=zoom_start)
17
18    def add_markers(self, location, **kwargs):
19        '''add markers with popup in a layer group'''
20        opt = {'popup': None, 'max_width': 150, 'icon': 'bomb', 'color': 'blue',
21               'group': None} | kwargs
22        grp = self.map
23        color = opt['color']
24        if opt['group']:
25            grp = folium.map.FeatureGroup(opt['group'])
26            grp.add_to(self.map)
27        if opt['popup']:
28            for loc, pop in zip(location, opt['popup']):
29                icon = folium.Icon(color=color, icon=opt['icon'], prefix='fa')
30                txt = folium.Popup(Cart.html(pop), max_width=opt['max_width'])
31                folium.Marker(location=loc, popup=txt, icon=icon).add_to(grp)
32        else:
33            for loc in location:
34                icon = folium.Icon(color=color, icon=opt['icon'], prefix='fa')
35                folium.Marker(location=loc, icon=icon).add_to(grp)
36
37    def show(self, mapname=None):
38        '''return the map and save it if mapname is present'''
39        folium.LayerControl().add_to(self.map)
40        if mapname:
41            self.map.save(mapname)
42        return self.map
43
44    @staticmethod
45    def html(dic):
46        '''return an html string from a dict'''
47        pop = ''
48        for key, val in dic.items():
49            pop += '<div>' + '<b>' + \
50                str(key) + '</b>' + ' : ' + str(val) + '</div>'
51        return pop

folium map with markers

Cart(location=None, zoom_start=5)
15    def __init__(self, location=None, zoom_start=5):
16        self.map = folium.Map(location=location, zoom_start=zoom_start)
map
def add_markers(self, location, **kwargs):
18    def add_markers(self, location, **kwargs):
19        '''add markers with popup in a layer group'''
20        opt = {'popup': None, 'max_width': 150, 'icon': 'bomb', 'color': 'blue',
21               'group': None} | kwargs
22        grp = self.map
23        color = opt['color']
24        if opt['group']:
25            grp = folium.map.FeatureGroup(opt['group'])
26            grp.add_to(self.map)
27        if opt['popup']:
28            for loc, pop in zip(location, opt['popup']):
29                icon = folium.Icon(color=color, icon=opt['icon'], prefix='fa')
30                txt = folium.Popup(Cart.html(pop), max_width=opt['max_width'])
31                folium.Marker(location=loc, popup=txt, icon=icon).add_to(grp)
32        else:
33            for loc in location:
34                icon = folium.Icon(color=color, icon=opt['icon'], prefix='fa')
35                folium.Marker(location=loc, icon=icon).add_to(grp)

add markers with popup in a layer group

def show(self, mapname=None):
37    def show(self, mapname=None):
38        '''return the map and save it if mapname is present'''
39        folium.LayerControl().add_to(self.map)
40        if mapname:
41            self.map.save(mapname)
42        return self.map

return the map and save it if mapname is present

@staticmethod
def html(dic):
44    @staticmethod
45    def html(dic):
46        '''return an html string from a dict'''
47        pop = ''
48        for key, val in dic.items():
49            pop += '<div>' + '<b>' + \
50                str(key) + '</b>' + ' : ' + str(val) + '</div>'
51        return pop

return an html string from a dict