import json import math import requests import haversine metadata_url = "https://maps.googleapis.com/maps/api/streetview/metadata" mapcrunch_url = "http://www.mapcrunch.com/_r/" def generate_coord(key): """ Takes in a Google API key. Returns (latitude, longitude) of usable coord (where google has data). Returns None if, after trying several random points, no usable coords could be found. This function calls the streetview metadata endpoint - there is no quota consumed """ points_res = requests.get(mapcrunch_url).text points_js = json.loads(points_res.strip("while(1); ")) for lat, lng in points_js["points"]: params = { "key": key, "location": f"{lat},{lng}", } js = requests.get(metadata_url, params=params).json() if js["status"] != "ZERO_RESULTS": return (lat, lng) mean_earth_radius_km = (6378 + 6357) / 2 # the farthest you can be from another point on Earth antipode_dist_km = math.pi * mean_earth_radius_km min_dist_km = 0.15 # if you're within 150m, you get a perfect score perfect_score = 5000 def score(target, guess): """ Takes in two (latitude, longitude) pairs and produces an int score. Score is in the (inclusive) range [0, 5000] Higher scores are closer. """ dist_km = haversine.haversine(target, guess) if dist_km <= min_dist_km: return perfect_score # TODO might want to try something logarithmic here eventually return perfect_score * (1 - (dist_km / antipode_dist_km))