Ver código fonte

Changing urban generator logic to retry the same city

Kirk Trombley 5 anos atrás
pai
commit
1198c9b205
1 arquivos alterados com 18 adições e 16 exclusões
  1. 18 16
      server/lib.py

+ 18 - 16
server/lib.py

@@ -87,35 +87,37 @@ def random_street_view_generator(only_america=False):
             yield points.pop()
 
 
-def urban_coord(max_retries=100, max_dist_km=25):
+def urban_coord(max_retries=10, retries_per_point=10, max_dist_km=25):
     """
     Returns (latitude, longitude) of usable coord (where google has data) that is near
     a known urban center. Points will be at most max_dist_km kilometers away. This function will
-    check at most max_retries points, and if none have street view data, will return None.
-    Otherwise, it will exit as soon as suitable point is found.
+    generate at most retries_per_point points around an urban center, and will try at most
+    max_retries urban centers. If none of the generated points have street view data, 
+    this will return None. Otherwise, it will exit as soon as suitable point is found.
 
     This function calls the streetview metadata endpoint - there is no quota consumed.
     """
 
     for _ in range(max_retries):
-        (city_lat, city_lng) = random.choice(urban_centers)
-        # start in a city, turn a random direction, and go random distance
-        dist_km = random.random() * max_dist_km
-        angle_rad = random.random() * 2 * math.pi
         # logic adapted from https://stackoverflow.com/a/7835325
-        d_over_radius = dist_km / mean_earth_radius_km
-        sin_dor = math.sin(d_over_radius)
-        cos_dor = math.cos(d_over_radius)
+        # start in a city, turn a random direction, and go random distance
+        (city_lat, city_lng) = random.choice(urban_centers)
         city_lat_rad = math.radians(city_lat)
         sin_lat = math.sin(city_lat_rad)
         cos_lat = math.cos(city_lat_rad)
         city_lng_rad = math.radians(city_lng)
-        pt_lat_rad = math.asin(sin_lat * cos_dor + cos_lat * sin_dor * math.cos(angle_rad))
-        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))
-        pt_lat = math.degrees(pt_lat_rad)
-        pt_lng = math.degrees(pt_lng_rad)
-        if point_has_streetview(pt_lat, pt_lng):
-            return (pt_lat, pt_lng)
+        for _ in range(retries_per_point):
+            dist_km = random.random() * max_dist_km
+            angle_rad = random.random() * 2 * math.pi
+            d_over_radius = dist_km / mean_earth_radius_km
+            sin_dor = math.sin(d_over_radius)
+            cos_dor = math.cos(d_over_radius)
+            pt_lat_rad = math.asin(sin_lat * cos_dor + cos_lat * sin_dor * math.cos(angle_rad))
+            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))
+            pt_lat = math.degrees(pt_lat_rad)
+            pt_lng = math.degrees(pt_lng_rad)
+            if point_has_streetview(pt_lat, pt_lng):
+                return (pt_lat, pt_lng)
 
 
 mean_earth_radius_km = (6378 + 6357) / 2