1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#!/usr/bin/env python
"""
Example of using Google Maps queries and PyMap3D
https://developers.google.com/places/web-service/search
This requires a Google Cloud key, and costs a couple US cents per query.
TODO: Would like to instead query a larger region, would OSM be an option?
"""
from pathlib import Path
from argparse import ArgumentParser
import requests
import functools
import pandas
from pymap3d.vincenty import vdist
URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
@functools.lru_cache()
def get_place_coords(
place_type: str, latitude: float, longitude: float, search_radius_km: int, keyfn: Path
) -> pandas.DataFrame:
"""
Get places using Google Maps Places API
Requires you to have a Google Cloud account with API key.
"""
keyfn = Path(keyfn).expanduser()
key = keyfn.read_text()
stub = URL + "location={},{}".format(latitude, longitude)
stub += "&radius={}".format(search_radius_km * 1000)
stub += "&types={}".format(place_type)
stub += "&key={}".format(key)
r = requests.get(stub)
r.raise_for_status()
place_json = r.json()["results"]
places = pandas.DataFrame(
index=[p["name"] for p in place_json],
columns=["latitude", "longitude", "distance_km", "vicinity"],
)
places["latitude"] = [p["geometry"]["location"]["lat"] for p in place_json]
places["longitude"] = [p["geometry"]["location"]["lng"] for p in place_json]
places["vicinity"] = [p["vicinity"] for p in place_json]
return places
if __name__ == "__main__":
p = ArgumentParser()
p.add_argument(
"place_type",
help="Place type to search: https://developers.google.com/places/supported_types",
)
p.add_argument(
"searchloc", help="initial latituude, longitude to search from", nargs=2, type=float
)
p.add_argument("radius", help="search radius (kilometers)", type=int)
p.add_argument("refloc", help="reference location (lat, lon)", nargs=2, type=float)
p.add_argument("-k", "--keyfn", help="Google Places API key file", default="~/googlemaps.key")
a = p.parse_args()
place_coords = get_place_coords(a.place_type, *a.searchloc, a.radius, a.keyfn)
place_coords["distance_km"] = (
vdist(place_coords["latitude"], place_coords["longitude"], *a.refloc)[0] / 1e3
)
|