From d5aba8c5f30b1196b38701a95697c0c8387fd51a Mon Sep 17 00:00:00 2001 From: kenseii Date: Wed, 2 Oct 2019 12:17:14 +0900 Subject: [PATCH 01/22] removed the geocoding part --- README.rst | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/README.rst b/README.rst index b45e390..3a7f6ef 100644 --- a/README.rst +++ b/README.rst @@ -78,20 +78,6 @@ NOTE: Not all the write functions have been migrated from Google Maps to Leaflet # also, by default if a marker has title it is shown as a pop-up -Geocoding ---------- - -NOTE: NOT MIGRATE YET - -``llplot`` contains a simple wrapper around Google's geocoding service enabling -map initilization to the location of your choice. Rather than providing latitude, -longitude, and zoom level during initialization, grab your llplot instance with -a location: - -:: - - gmap = llplot.GoogleMapPlotter.from_geocode("San Francisco") - Plot types ---------- From 0c2976f65fa523f9698a0513c2a88c871a75ff19 Mon Sep 17 00:00:00 2001 From: kenseii Date: Wed, 2 Oct 2019 15:55:11 +0900 Subject: [PATCH 02/22] adding nominatim geocode --- llplot/llplot.py | 46 ++++++++++------------------------------------ 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index fc644f9..ced72ef 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -4,6 +4,7 @@ import math import os import requests +import urllib import warnings from collections import namedtuple @@ -59,11 +60,14 @@ def from_geocode(cls, location_string, zoom=13): @classmethod def geocode(self, location_string): - geocode = requests.get( - 'http://maps.googleapis.com/maps/api/geocode/json?address="%s"' % location_string) - geocode = json.loads(geocode.text) - latlng_dict = geocode['results'][0]['geometry']['location'] - return latlng_dict['lat'], latlng_dict['lng'] + q_string = urllib.parse.urlencode({'q': location_string, + 'format': 'json', + 'addressdetails': 1, + }) + + geocode = requests.post( + 'https://nominatim.openstreetmap.org/search?q="%s"' % q_string) + return geocode['lat'], geocode['lon'] def grid(self, slat, elat, latin, slng, elng, lngin): self.gridsetting = [slat, elat, latin, slng, elng, lngin] @@ -531,34 +535,4 @@ def write_fitbounds(self, f): self.bounding_box[2], self.bounding_box[3])) f.write('llMap.fitBounds(bounds);\n') -if __name__ == "__main__": - - mymap = LeafletPlotter(37.428, -122.145, 16) - # mymap = GoogleMapPlotter.from_geocode("Stanford University") - - mymap.grid(37.42, 37.43, 0.001, -122.15, -122.14, 0.001) - mymap.marker(37.427, -122.145, "yellow") - mymap.marker(37.428, -122.146, "cornflowerblue") - mymap.marker(37.429, -122.144, "k") - lat, lng = mymap.geocode("Stanford University") - mymap.marker(lat, lng, "red") - mymap.circle(37.429, -122.145, 100, "#FF0000", ew=2) - path = [(37.429, 37.428, 37.427, 37.427, 37.427), - (-122.145, -122.145, -122.145, -122.146, -122.146)] - path2 = [[i+.01 for i in path[0]], [i+.02 for i in path[1]]] - path3 = [(37.433302 , 37.431257 , 37.427644 , 37.430303), (-122.14488, -122.133121, -122.137799, -122.148743)] - path4 = [(37.423074, 37.422700, 37.422410, 37.422188, 37.422274, 37.422495, 37.422962, 37.423552, 37.424387, 37.425920, 37.425937), - (-122.150288, -122.149794, -122.148936, -122.148142, -122.146747, -122.14561, -122.144773, -122.143936, -122.142992, -122.147863, -122.145953)] - mymap.plot(path[0], path[1], "plum", edge_width=10) - mymap.plot(path2[0], path2[1], "red") - mymap.polygon(path3[0], path3[1], edge_color="cyan", edge_width=5, face_color="blue", face_alpha=0.1) - mymap.heatmap(path4[0], path4[1], threshold=10, radius=40) - mymap.heatmap(path3[0], path3[1], threshold=10, radius=40, dissipating=False, gradient=[(30,30,30,0), (30,30,30,1), (50, 50, 50, 1)]) - mymap.scatter(path4[0], path4[1], c='r', marker=True) - mymap.scatter(path4[0], path4[1], s=90, marker=False, alpha=0.9, symbol='x', c='red', edge_width=4) - # Get more points with: - # http://www.findlatitudeandlongitude.com/click-lat-lng-list/ - scatter_path = ([37.424435, 37.424417, 37.424417, 37.424554, 37.424775, 37.425099, 37.425235, 37.425082, 37.424656, 37.423957, 37.422952, 37.421759, 37.420447, 37.419135, 37.417822, 37.417209], - [-122.142048, -122.141275, -122.140503, -122.139688, -122.138872, -122.138078, -122.137241, -122.136405, -122.135568, -122.134731, -122.133894, -122.133057, -122.13222, -122.131383, -122.130557, -122.129999]) - mymap.scatter(scatter_path[0], scatter_path[1], c='r', marker=True) - mymap.draw('./mymap.html') + From 565bd507da1757abda26517b7b0715c6d6d0e8c7 Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 11:22:16 +0900 Subject: [PATCH 03/22] convert response to json before getting lat and long --- llplot/llplot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/llplot/llplot.py b/llplot/llplot.py index ced72ef..1832878 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -67,6 +67,7 @@ def geocode(self, location_string): geocode = requests.post( 'https://nominatim.openstreetmap.org/search?q="%s"' % q_string) + geocode = geocode.json() return geocode['lat'], geocode['lon'] def grid(self, slat, elat, latin, slng, elng, lngin): From bb9caa0848aee124ba3632243d46bbf023963b7d Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 12:10:49 +0900 Subject: [PATCH 04/22] convert response to json before getting lat and long --- llplot/llplot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/llplot/llplot.py b/llplot/llplot.py index 1832878..c38e14c 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -68,6 +68,7 @@ def geocode(self, location_string): geocode = requests.post( 'https://nominatim.openstreetmap.org/search?q="%s"' % q_string) geocode = geocode.json() + print(geocode) return geocode['lat'], geocode['lon'] def grid(self, slat, elat, latin, slng, elng, lngin): From 92084a8ec4c8bdf60e69ffd3b10811823645608c Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 12:27:06 +0900 Subject: [PATCH 05/22] convert response to json before getting lat and long --- llplot/llplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index c38e14c..610c240 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -69,7 +69,7 @@ def geocode(self, location_string): 'https://nominatim.openstreetmap.org/search?q="%s"' % q_string) geocode = geocode.json() print(geocode) - return geocode['lat'], geocode['lon'] + return geocode[0]['lat'], geocode[0]['lon'] def grid(self, slat, elat, latin, slng, elng, lngin): self.gridsetting = [slat, elat, latin, slng, elng, lngin] From e0d2ff2cf60e4c117cc5738da95e20d1f62f7e28 Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 12:28:10 +0900 Subject: [PATCH 06/22] trying to see the q_string content --- llplot/llplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index 610c240..f256a91 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -68,7 +68,7 @@ def geocode(self, location_string): geocode = requests.post( 'https://nominatim.openstreetmap.org/search?q="%s"' % q_string) geocode = geocode.json() - print(geocode) + print(q_string) return geocode[0]['lat'], geocode[0]['lon'] def grid(self, slat, elat, latin, slng, elng, lngin): From 3ced2cb478ed5e71dd0add2d7056fbd7af001777 Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 12:34:06 +0900 Subject: [PATCH 07/22] format the request url according to the api docs --- llplot/llplot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index f256a91..7fee63d 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -63,10 +63,11 @@ def geocode(self, location_string): q_string = urllib.parse.urlencode({'q': location_string, 'format': 'json', 'addressdetails': 1, + 'limit':1, }) geocode = requests.post( - 'https://nominatim.openstreetmap.org/search?q="%s"' % q_string) + 'https://nominatim.openstreetmap.org/?"%s"' % q_string) geocode = geocode.json() print(q_string) return geocode[0]['lat'], geocode[0]['lon'] From 0793b03eaf75dc021deca1c361a86267ef9fc8b5 Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 12:39:29 +0900 Subject: [PATCH 08/22] debug the json response --- llplot/llplot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index 7fee63d..cffbf2c 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -69,8 +69,8 @@ def geocode(self, location_string): geocode = requests.post( 'https://nominatim.openstreetmap.org/?"%s"' % q_string) geocode = geocode.json() - print(q_string) - return geocode[0]['lat'], geocode[0]['lon'] + print(geocode) + return geocode['lat'], geocode['lon'] def grid(self, slat, elat, latin, slng, elng, lngin): self.gridsetting = [slat, elat, latin, slng, elng, lngin] From 2375033120fdbfd740c3a26773ce3cf18e6d2c41 Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 12:44:23 +0900 Subject: [PATCH 09/22] return error on failure --- llplot/llplot.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index cffbf2c..fb84cae 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -63,14 +63,17 @@ def geocode(self, location_string): q_string = urllib.parse.urlencode({'q': location_string, 'format': 'json', 'addressdetails': 1, - 'limit':1, + 'limit': 1, }) geocode = requests.post( 'https://nominatim.openstreetmap.org/?"%s"' % q_string) geocode = geocode.json() - print(geocode) - return geocode['lat'], geocode['lon'] + + try: + return geocode['lat'], geocode['lon'] + except: + print(geocode) def grid(self, slat, elat, latin, slng, elng, lngin): self.gridsetting = [slat, elat, latin, slng, elng, lngin] From b90fc2f6477cb5458d5ce739971a8c4ca7d81f39 Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 12:45:48 +0900 Subject: [PATCH 10/22] trying to debug the query string --- llplot/llplot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index fb84cae..87d93ab 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -63,8 +63,9 @@ def geocode(self, location_string): q_string = urllib.parse.urlencode({'q': location_string, 'format': 'json', 'addressdetails': 1, - 'limit': 1, + 'limit': 1 }) + print(q_string) geocode = requests.post( 'https://nominatim.openstreetmap.org/?"%s"' % q_string) From 0a1e9c367b6d080337a7208d6a808cc0a88a7300 Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 12:48:35 +0900 Subject: [PATCH 11/22] removing trailing space --- llplot/llplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index 87d93ab..bf67a95 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -68,7 +68,7 @@ def geocode(self, location_string): print(q_string) geocode = requests.post( - 'https://nominatim.openstreetmap.org/?"%s"' % q_string) + 'https://nominatim.openstreetmap.org/?"%s"' % q_string.strip()) geocode = geocode.json() try: From afdd9a9d2ee4a9a9c3ea9634c50691c2f28296dc Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 12:52:30 +0900 Subject: [PATCH 12/22] remove limit1 --- llplot/llplot.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index bf67a95..02a7257 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -63,7 +63,6 @@ def geocode(self, location_string): q_string = urllib.parse.urlencode({'q': location_string, 'format': 'json', 'addressdetails': 1, - 'limit': 1 }) print(q_string) @@ -72,7 +71,7 @@ def geocode(self, location_string): geocode = geocode.json() try: - return geocode['lat'], geocode['lon'] + return geocode[0]['lat'], geocode[0]['lon'] except: print(geocode) From 06e61ac4a548309abee86011d7406ad564ab6005 Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 12:53:56 +0900 Subject: [PATCH 13/22] switched to get instead of post --- llplot/llplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index 02a7257..b19417e 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -66,7 +66,7 @@ def geocode(self, location_string): }) print(q_string) - geocode = requests.post( + geocode = requests.get( 'https://nominatim.openstreetmap.org/?"%s"' % q_string.strip()) geocode = geocode.json() From fada267c9becc12aeddd526f55671078b914daef Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 12:56:20 +0900 Subject: [PATCH 14/22] join the url before requesting it --- llplot/llplot.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index b19417e..6b19274 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -64,10 +64,9 @@ def geocode(self, location_string): 'format': 'json', 'addressdetails': 1, }) - print(q_string) + request_url = 'https://nominatim.openstreetmap.org/?%s' % q_string - geocode = requests.get( - 'https://nominatim.openstreetmap.org/?"%s"' % q_string.strip()) + geocode = requests.get(request_url) geocode = geocode.json() try: From 6dc5af170e96a8126702c3cd8fabc33c97e6aa67 Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 12:57:30 +0900 Subject: [PATCH 15/22] added default zoom level --- llplot/llplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index 6b19274..df78412 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -70,7 +70,7 @@ def geocode(self, location_string): geocode = geocode.json() try: - return geocode[0]['lat'], geocode[0]['lon'] + return geocode[0]['lat'], geocode[0]['lon'], 13 except: print(geocode) From c6abef3db1cd2ab42ea9ad4a6094ac7968b6ce16 Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 13:01:00 +0900 Subject: [PATCH 16/22] no need to passin the zoom level --- llplot/llplot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index df78412..090247b 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -54,8 +54,9 @@ def __init__(self, tile_url, center_lat, center_lng, zoom, apikey='', self.html_color_codes = html_color_codes @classmethod - def from_geocode(cls, location_string, zoom=13): + def from_geocode(cls, location_string): lat, lng = cls.geocode(location_string) + zoom = 13 return cls(lat, lng, zoom) @classmethod From ad6cf140e30715e6e9a14a2cbe3aa6baff776f1e Mon Sep 17 00:00:00 2001 From: kenseii Date: Thu, 3 Oct 2019 13:02:10 +0900 Subject: [PATCH 17/22] removed the return of the zoom level as it is set to default --- llplot/llplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index 090247b..18d7d28 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -71,7 +71,7 @@ def geocode(self, location_string): geocode = geocode.json() try: - return geocode[0]['lat'], geocode[0]['lon'], 13 + return geocode[0]['lat'], geocode[0]['lon'] except: print(geocode) From 21bf58d74b616d3cd8a6dbe3075110e335d6508d Mon Sep 17 00:00:00 2001 From: kenseii Date: Fri, 4 Oct 2019 11:00:11 +0900 Subject: [PATCH 18/22] readded the zoom argument to the class method --- llplot/llplot.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index 18d7d28..6b19274 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -54,9 +54,8 @@ def __init__(self, tile_url, center_lat, center_lng, zoom, apikey='', self.html_color_codes = html_color_codes @classmethod - def from_geocode(cls, location_string): + def from_geocode(cls, location_string, zoom=13): lat, lng = cls.geocode(location_string) - zoom = 13 return cls(lat, lng, zoom) @classmethod From 81233c6085cb2379fabe9d64a299d6d0236ca270 Mon Sep 17 00:00:00 2001 From: kenseii Date: Fri, 4 Oct 2019 11:04:09 +0900 Subject: [PATCH 19/22] added default zoom --- llplot/llplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index 6b19274..4f71579 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -35,7 +35,7 @@ def __init__(self, tile_url, center_lat, center_lng, zoom, apikey='', attribution=DEFAULT_ATTRIBUTION): self.tile_url = tile_url self.center = (float(center_lat), float(center_lng)) - self.zoom = int(zoom) + self.zoom = int(zoom) or 13 self.apikey = str(apikey) self.attribution = attribution self.grids = None From b41b584b02f8c7560d910cda560b3b9750781083 Mon Sep 17 00:00:00 2001 From: kenseii Date: Fri, 4 Oct 2019 11:45:01 +0900 Subject: [PATCH 20/22] removed the from_geocode method, instead the geocode method will do its work --- llplot/llplot.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/llplot/llplot.py b/llplot/llplot.py index 4f71579..3c19403 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -53,10 +53,6 @@ def __init__(self, tile_url, center_lat, center_lng, zoom, apikey='', self.color_dict = mpl_color_map self.html_color_codes = html_color_codes - @classmethod - def from_geocode(cls, location_string, zoom=13): - lat, lng = cls.geocode(location_string) - return cls(lat, lng, zoom) @classmethod def geocode(self, location_string): From 3db64d1955d09a27642cf5fce0c4dabea4ded436 Mon Sep 17 00:00:00 2001 From: kenseii Date: Fri, 4 Oct 2019 11:57:37 +0900 Subject: [PATCH 21/22] updated the readme --- README.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 3a7f6ef..2ee049e 100644 --- a/README.rst +++ b/README.rst @@ -12,7 +12,7 @@ make creating exploratory map views effortless. Here's a crash course: mapbox_token = "" # Place map - gmap = llplot.LeafletPlotter( + lmap = llplot.LeafletPlotter( "https://{s}.tiles.mapbox.com/v4/mapbox.{mapid}/{z}/{x}/{y}@2x.png?" "access_token=" + mapbox_token, 37.766956, -122.438481, 13 @@ -32,14 +32,14 @@ make creating exploratory map views effortless. Here's a crash course: (37.764028, -122.510347), (37.771269, -122.511015) ]) - gmap.plot(golden_gate_park_lats, golden_gate_park_lons, 'cornflowerblue', edge_width=10) + lmap.plot(golden_gate_park_lats, golden_gate_park_lons, 'cornflowerblue', edge_width=10) # Marker hidden_gem_lat, hidden_gem_lon = 37.770776, -122.461689 - gmap.marker(hidden_gem_lat, hidden_gem_lon, 'cornflowerblue') + lmap.marker(hidden_gem_lat, hidden_gem_lon, 'cornflowerblue') # Draw - gmap.draw("my_map.html") + lmap.draw("my_map.html") .. image:: https://i.imgur.com/12KXJS3.png From b8a64b5e217787dc7076898fc3fc074696edc5d8 Mon Sep 17 00:00:00 2001 From: kenseii Date: Fri, 4 Oct 2019 12:07:18 +0900 Subject: [PATCH 22/22] updated the readme to hold the geocoding feature --- README.rst | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.rst b/README.rst index 2ee049e..839224c 100644 --- a/README.rst +++ b/README.rst @@ -77,6 +77,25 @@ NOTE: Not all the write functions have been migrated from Google Maps to Leaflet # also, by default if a marker has title it is shown as a pop-up +Geocoding +--------- + +``llplot`` contains a simple wrapper around Nominatim geocoding service enabling +map initilization to the location of your choice. Rather than providing latitude, +longitude, and zoom level during initialization, grab your llplot instance with +a location: + +:: + + import llplot as lp + + mymap = lp.LeafletPlotter( + "https://{s}.tiles.mapbox.com/v4/mapbox.{mapid}/{z}/{x}/{y}@2x.png?" + "access_token=ACCESS_TOKEN", + 34.6901, 135.1956, 10 + ) + print(mymap.geocode("Stanford University")) + # Should return ('37.43131385', '-122.169365354983') Plot types ----------