diff --git a/README.rst b/README.rst index b45e390..839224c 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 @@ -77,20 +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 --------- -NOTE: NOT MIGRATE YET - -``llplot`` contains a simple wrapper around Google's geocoding service enabling +``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: :: - gmap = llplot.GoogleMapPlotter.from_geocode("San Francisco") + 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 ---------- diff --git a/llplot/llplot.py b/llplot/llplot.py index fc644f9..3c19403 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 @@ -34,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 @@ -52,18 +53,22 @@ 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): - 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, + }) + request_url = 'https://nominatim.openstreetmap.org/?%s' % q_string + + geocode = requests.get(request_url) + geocode = geocode.json() + + try: + return geocode[0]['lat'], geocode[0]['lon'] + except: + print(geocode) def grid(self, slat, elat, latin, slng, elng, lngin): self.gridsetting = [slat, elat, latin, slng, elng, lngin] @@ -531,34 +536,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') +