|
| 1 | +from operator import add |
| 2 | + |
| 3 | +from bs4 import BeautifulSoup |
| 4 | +from IPython.core.display import display, HTML |
| 5 | +from jsbeautifier import beautify |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | + |
| 9 | +def info_window(values): |
| 10 | + data = ''.join([f'<dt>{k}</dt><dd>{v}</dd>' for k, v in values.items()]) |
| 11 | + return f'<dl>{data}</dl>' |
| 12 | + |
| 13 | + |
| 14 | +class LatLng(str): |
| 15 | + ''' A LatLng is a point in geographical coordinates: latitude and |
| 16 | + longitude. ''' |
| 17 | + def __new__(self, lat, lng): |
| 18 | + self.lat = lat |
| 19 | + self.lng = lng |
| 20 | + return str.__new__(self, f'{{lat: {lat}, lng: {lng}}}') |
| 21 | + |
| 22 | + |
| 23 | +class Marker(str): |
| 24 | + ''' A marker identifies a location on a map. |
| 25 | + Parameters: |
| 26 | + * location (tuple or list, default None) – Latitude and Longitude of Map (Northing, Easting). |
| 27 | + ''' |
| 28 | + def __new__(self, location=None, icon=None, title='', gmap='map', info_window=None): |
| 29 | + |
| 30 | + self.attributes = {} |
| 31 | + self.attributes['position'] = LatLng(*position) |
| 32 | + self.attributes['map'] = gmap |
| 33 | + |
| 34 | + if title != '': |
| 35 | + self.attributes['title'] = f'"{title}"' |
| 36 | + |
| 37 | + if icon is not None: |
| 38 | + self.attributes['icon'] = f'"{icon}"' |
| 39 | + |
| 40 | + self.template = self.fill_template(self.attributes) |
| 41 | + self.template = self.add_info_window(info_window, self.template) |
| 42 | + |
| 43 | + return str.__new__(self, self.template) |
| 44 | + |
| 45 | + def fill_template(attr): |
| 46 | + template = 'var marker = new google.maps.Marker({{{}}}); @infowindow' |
| 47 | + args = ',\n '.join([f'{k}: {v}' for k, v in attr.items()]) |
| 48 | + return template.format(args) |
| 49 | + |
| 50 | + def add_info_window(info_window, template): |
| 51 | + if info_window is not None: |
| 52 | + func = f'attachInfoWindow(marker, "{info_window}");' |
| 53 | + return template.replace('@infowindow', func) |
| 54 | + return template.replace('@infowindow', '') |
| 55 | + |
| 56 | + def __repr__(self): |
| 57 | + return beautify(self.template) |
| 58 | + |
| 59 | + def __str__(self): |
| 60 | + return beautify(self.template) |
| 61 | + |
| 62 | + |
| 63 | +class Map(): |
| 64 | + def __init__(self, api_key, markers, map_type='roadmap', height='250px', width='100%', zoom=4, icon=None, labels=None, |
| 65 | + title='', info_windows=None, show_zoomcontrol=True, show_pegman=True): |
| 66 | + self.template = open('template.html').read() |
| 67 | + self.api_key = api_key |
| 68 | + self.map_type = map_type |
| 69 | + self.height = height |
| 70 | + self.width = width |
| 71 | + self.zoom = zoom |
| 72 | + self.markers = markers |
| 73 | + self.add_markers(self.markers, icon=icon, labels=labels, info_windows=info_windows) |
| 74 | + self.title = title |
| 75 | + self.show_pegman = show_pegman |
| 76 | + self.show_zoomcontrol = show_zoomcontrol |
| 77 | + |
| 78 | + def add_markers(self, markers, icon=None, labels=None, info_windows=None): |
| 79 | + if isinstance(markers, pd.core.frame.DataFrame): |
| 80 | + for n, pos in enumerate(markers.itertuples(index=False)): |
| 81 | + label = '' |
| 82 | + info = None |
| 83 | + if labels is not None: |
| 84 | + label = labels[n] |
| 85 | + |
| 86 | + if info_windows is not None: |
| 87 | + info = info_windows[n] |
| 88 | + |
| 89 | + marker = Marker(pos, icon=icon, title=label, info_window=info) |
| 90 | + |
| 91 | + self.template = self.template.replace('@marker', f'{marker}\n@marker') |
| 92 | + |
| 93 | + @property |
| 94 | + def center(self): |
| 95 | + if isinstance(self.markers, pd.core.frame.DataFrame): |
| 96 | + return LatLng(*self.markers.mean()) |
| 97 | + elif isinstance(self.markers[0], (float, int)): |
| 98 | + center = self.markers |
| 99 | + else: |
| 100 | + if len(self.markers) > 1: |
| 101 | + center = [sum(i) / len(self.markers) |
| 102 | + for i in [i for i in zip(*self.markers)]] |
| 103 | + else: |
| 104 | + center = self.markers[0] |
| 105 | + return LatLng(*center) |
| 106 | + |
| 107 | + @property |
| 108 | + def html(self): |
| 109 | + html = self.template.replace('@marker', '') |
| 110 | + html = html.replace('@api_key', self.api_key) |
| 111 | + html = html.replace('@map_height', self.height) |
| 112 | + html = html.replace('@map_width', self.width) |
| 113 | + html = html.replace('@map_center', self.center) |
| 114 | + html = html.replace('@map_zoom', str(self.zoom)) |
| 115 | + html = html.replace('@map_type', f'"{self.map_type}"') |
| 116 | + html = html.replace('@title', str(self.title)) |
| 117 | + html = html.replace('@map_showpegman', str(self.show_pegman).lower()) |
| 118 | + assert isinstance(self.show_zoomcontrol, bool) |
| 119 | + html = html.replace('@map_showzoomcontrol', str(self.show_zoomcontrol).lower()) |
| 120 | + return html |
| 121 | + |
| 122 | + def show(self): |
| 123 | + return display(HTML(self.html)) |
| 124 | + |
| 125 | + def __repr__(self): |
| 126 | + return BeautifulSoup(self.html, 'html.parser').prettify() |
0 commit comments