|
@@ -8,6 +8,22 @@ import haversine
|
|
|
google_api_key = "AIzaSyAqjCYR6Szph0X0H_iD6O1HenFhL9jySOo"
|
|
|
metadata_url = "https://maps.googleapis.com/maps/api/streetview/metadata"
|
|
|
mapcrunch_url = "http://www.mapcrunch.com/_r/"
|
|
|
+rsv_url = "https://randomstreetview.com/data"
|
|
|
+
|
|
|
+
|
|
|
+def point_has_streetview(lat, lng):
|
|
|
+ """
|
|
|
+ Returns True if the streetview metadata endpoint says a given point has
|
|
|
+ data available, and False otherwise.
|
|
|
+
|
|
|
+ This function calls the streetview metadata endpoint - there is no quota consumed.
|
|
|
+ """
|
|
|
+ params = {
|
|
|
+ "key": google_api_key,
|
|
|
+ "location": f"{lat},{lng}",
|
|
|
+ }
|
|
|
+ js = requests.get(metadata_url, params=params).json()
|
|
|
+ return js["status"] == "OK"
|
|
|
|
|
|
|
|
|
def generate_coord(max_retries=100, only_america=False):
|
|
@@ -16,7 +32,7 @@ def generate_coord(max_retries=100, only_america=False):
|
|
|
This function will attempt at most max_retries calls to map crunch to fetch
|
|
|
candidate points, and will exit as soon as a suitable candidate is found.
|
|
|
|
|
|
- This function calls the streetview metadata endpoint - there is no quota consumed
|
|
|
+ This function calls the streetview metadata endpoint - there is no quota consumed.
|
|
|
"""
|
|
|
mc_url = mapcrunch_url + ("?c=21" if only_america else "")
|
|
|
for _ in range(max_retries):
|
|
@@ -25,15 +41,42 @@ def generate_coord(max_retries=100, only_america=False):
|
|
|
if "c=" not in mc_url:
|
|
|
mc_url += f"?c={points_js['country']}" # lock to the first country randomed
|
|
|
for lat, lng in points_js["points"]:
|
|
|
- params = {
|
|
|
- "key": google_api_key,
|
|
|
- "location": f"{lat},{lng}",
|
|
|
- }
|
|
|
- js = requests.get(metadata_url, params=params).json()
|
|
|
- if js["status"] == "OK":
|
|
|
+ if point_has_streetview(lat, lng):
|
|
|
return (lat, lng)
|
|
|
|
|
|
|
|
|
+def call_random_street_view(only_america=False):
|
|
|
+ """
|
|
|
+ Returns an array of (some number of) tuples, each being (latitude, longitude).
|
|
|
+ All points will be valid streetview coordinates. There is no guarantee as to the
|
|
|
+ length of this array (it may be empty), but it will never be None.
|
|
|
+
|
|
|
+ This function calls the streetview metadata endpoint - there is no quota consumed.
|
|
|
+ """
|
|
|
+ rsv_js = requests.post(rsv_url, data={"country": "us" if only_america else "all"}).json()
|
|
|
+ if not rsv_js["success"]:
|
|
|
+ return []
|
|
|
+
|
|
|
+ return [
|
|
|
+ (point["lat"], point["lng"])
|
|
|
+ for point in rsv_js["locations"]
|
|
|
+ if point_has_streetview(point["lat"], point["lng"])
|
|
|
+ ]
|
|
|
+
|
|
|
+
|
|
|
+def random_street_view_generator(only_america=False):
|
|
|
+ """
|
|
|
+ Returns a generator which will lazily use call_random_street_view to generate new
|
|
|
+ street view points.
|
|
|
+ """
|
|
|
+ points = []
|
|
|
+ while True:
|
|
|
+ if len(points) == 0:
|
|
|
+ points = call_random_street_view(only_america=only_america)
|
|
|
+ else:
|
|
|
+ yield points.pop()
|
|
|
+
|
|
|
+
|
|
|
mean_earth_radius_km = (6378 + 6357) / 2
|
|
|
|
|
|
# if you're more than 1/4 of the Earth's circumfrence away, you get 0
|