1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import json
- import math
- import requests
- import haversine
- # Google API key, with access to Street View Static API
- google_api_key = "AIzaSyAqjCYR6Szph0X0H_iD6O1HenFhL9jySOo"
- metadata_url = "https://maps.googleapis.com/maps/api/streetview/metadata"
- mapcrunch_url = "http://www.mapcrunch.com/_r/"
- def generate_coord():
- """
- 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": google_api_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
- # if you're more than 1/4 of the Earth away, you get 0
- max_dist_km = antipode_dist_km / 2
- 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
- if dist_km >= max_dist_km:
- return 0
- # TODO probably still needs tweaking
- return int(perfect_score * (1 - (dist_km / max_dist_km) ** 2))
|