urban_centers.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import math
  2. import random
  3. import csv
  4. import logging
  5. from collections import defaultdict
  6. from .shared import point_has_streetview
  7. from ..scoring import mean_earth_radius_km
  8. logger = logging.getLogger(__name__)
  9. URBAN_CENTERS = defaultdict(list)
  10. _found_countries = set()
  11. _urban_center_count = 0
  12. with open("./data/urban-centers.csv") as infile:
  13. for code, name, lat, lng in csv.reader(infile, delimiter=",", quotechar='"'):
  14. URBAN_CENTERS[code].append((name, float(lat), float(lng)))
  15. _found_countries.add(code)
  16. _urban_center_count += 1
  17. logger.info(f"Read {_urban_center_count} urban centers from {len(_found_countries)} countries.")
  18. VALID_COUNTRIES = tuple(_found_countries)
  19. async def urban_coord(country_lock, city_retries=10, point_retries=10, max_dist_km=25):
  20. """
  21. Returns (latitude, longitude) of usable coord (where google has data) that is near
  22. a known urban center. Points will be at most max_dist_km kilometers away. This function
  23. will use country_lock to determine the country from which to pull a known urban center,
  24. generate at most point_retries points around that urban center, and try at most
  25. city_retries urban centers in that country. If none of the generated points have street
  26. view data, this will return None. Otherwise, it will exit as soon as suitable point is
  27. found.
  28. This function calls the streetview metadata endpoint - there is no quota consumed.
  29. """
  30. country_lock = country_lock.lower()
  31. cities = URBAN_CENTERS[country_lock]
  32. src = random.sample(cities, k=min(city_retries, len(cities)))
  33. logger.info(f"Trying {len(src)} centers in {country_lock}")
  34. for (name, city_lat, city_lng) in src:
  35. # logic adapted from https://stackoverflow.com/a/7835325
  36. # start in a city
  37. logger.info(f"Trying at most {point_retries} points around {name}")
  38. city_lat_rad = math.radians(city_lat)
  39. sin_lat = math.sin(city_lat_rad)
  40. cos_lat = math.cos(city_lat_rad)
  41. city_lng_rad = math.radians(city_lng)
  42. for _ in range(point_retries):
  43. # turn a random direction, and go random distance
  44. dist_km = random.random() * max_dist_km
  45. angle_rad = random.random() * 2 * math.pi
  46. d_over_radius = dist_km / mean_earth_radius_km
  47. sin_dor = math.sin(d_over_radius)
  48. cos_dor = math.cos(d_over_radius)
  49. pt_lat_rad = math.asin(sin_lat * cos_dor + cos_lat * sin_dor * math.cos(angle_rad))
  50. pt_lng_rad = city_lng_rad + math.atan2(math.sin(angle_rad) * sin_dor * cos_lat, cos_dor - sin_lat * math.sin(pt_lat_rad))
  51. pt_lat = math.degrees(pt_lat_rad)
  52. pt_lng = math.degrees(pt_lng_rad)
  53. if await point_has_streetview(pt_lat, pt_lng):
  54. logger.info("Point found!")
  55. return (country_lock, pt_lat, pt_lng)