Procházet zdrojové kódy

Delete old gen module on server

Kirk Trombley před 4 roky
rodič
revize
cd46ff189b

+ 0 - 56
server/app/gen/__init__.py

@@ -1,56 +0,0 @@
-from typing import Dict, Tuple, List
-
-from .shared import PointSource
-from .map_crunch import MapCrunchPointSource
-from .random_street_view import RSVPointSource
-from .urban_centers import UrbanPointSource
-
-from ..schemas import GenMethodEnum, GameConfig, CacheInfo
-
-stock_target = 20
-
-"""
-Dictionary of PointSources
-Maps (generation_method, only_america) -> PointSource
-"""
-sources: Dict[Tuple[GenMethodEnum, bool], PointSource] = {
-    (GenMethodEnum.map_crunch, False): MapCrunchPointSource(stock_target=stock_target, max_retries=1000, only_america=False),
-    (GenMethodEnum.map_crunch, True): MapCrunchPointSource(stock_target=stock_target, max_retries=1000, only_america=True),
-    (GenMethodEnum.rsv, False): RSVPointSource(stock_target=stock_target, only_america=False),
-    (GenMethodEnum.rsv, True): RSVPointSource(stock_target=stock_target, only_america=True),
-    (GenMethodEnum.urban, False): UrbanPointSource(
-        stock_target=stock_target,
-        max_retries=100,
-        retries_per_point=30,
-        max_dist_km=25,
-        usa_chance=0.1
-    ),
-    (GenMethodEnum.urban, True): UrbanPointSource(
-        stock_target=stock_target,
-        max_retries=100,
-        retries_per_point=30,
-        max_dist_km=25,
-        usa_chance=1.0
-    )
-}
-
-
-def generate_points(config: GameConfig) -> List[Tuple[float, float]]:
-    """
-    Generate points according to the GameConfig.
-    """
-    return sources[(config.generation_method, config.only_america)].get_points(config.rounds)
-
-
-def restock_source(config: GameConfig):
-    """
-    Restock the PointSource associated with the GameConfig
-    """
-    sources[(config.generation_method, config.only_america)].restock()
-
-
-def get_cache_info() -> List[CacheInfo]:
-    """
-    Get CacheInfo for each of the PointSources
-    """
-    return [CacheInfo(generation_method=gm, only_america=oa, size=len(src.stock)) for ((gm, oa), src) in sources.items()]

+ 0 - 46
server/app/gen/map_crunch.py

@@ -1,46 +0,0 @@
-import json
-
-import requests
-
-from .shared import point_has_streetview, PointSource
-
-mapcrunch_url = "http://www.mapcrunch.com/_r/"
-
-
-def generate_coord(max_retries=100, only_america=False):
-    """
-    Returns (latitude, longitude) of usable coord (where google has data).
-    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.
-    If no suitable candidate is found in this allotted number of retries, None is
-    returned.
-
-    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):
-        points_res = requests.get(mc_url).text
-        points_js = json.loads(points_res.strip("while(1); "))
-        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"]:
-            if point_has_streetview(lat, lng):
-                return (lat, lng)
-
-
-class MapCrunchPointSource(PointSource):
-    def __init__(self, stock_target=20, max_retries=100, only_america=False):
-        super().__init__(stock_target=stock_target)
-        self.max_retries = max_retries
-        self.only_america = only_america
-
-    def _restock_impl(self, n):
-        points = []
-        while len(points) < n:
-            pt = generate_coord(
-                max_retries=self.max_retries,
-                only_america=self.only_america
-            )
-            if pt is not None:
-                points.append(pt)
-        return points

+ 0 - 36
server/app/gen/random_street_view.py

@@ -1,36 +0,0 @@
-import requests
-
-from .shared import point_has_streetview, PointSource
-
-rsv_url = "https://randomstreetview.com/data"
-
-
-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"])
-    ]
-
-
-class RSVPointSource(PointSource):
-    def __init__(self, stock_target=20, only_america=False):
-        super().__init__(stock_target=stock_target)
-        self.only_america = only_america
-    
-    def _restock_impl(self, n):
-        points = []
-        while len(points) < n:
-            points.extend(call_random_street_view(only_america=self.only_america))
-        return points

+ 0 - 75
server/app/gen/shared.py

@@ -1,75 +0,0 @@
-import collections
-import logging
-
-import requests
-
-# 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"
-
-logger = logging.getLogger(__name__)
-
-
-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.
-    """
-    return requests.get(metadata_url, params={
-        "key": google_api_key,
-        "location": f"{lat},{lng}",
-    }).json()["status"] == "OK"
-
-
-class PointSource:
-    """
-    Base class to handle the logic of managing a cache of (lat, lng) points.
-    """
-    def __init__(self, stock_target):
-        self.stock = collections.deque()
-        self.stock_target = stock_target
-
-    def _restock_impl(self, n):
-        """
-        Returns a list of new points to add to the stock.
-        Implementations of this method should try to return at least n points for performance.
-        """
-        raise NotImplementedError("Subclasses must implement this")
-    
-    def restock(self, n=None):
-        """
-        Restock at least n points into this source.
-        If n is not provided, it will default to stock_target, as set during the
-        construction of this point source.
-        """
-        n = n if n is not None else self.stock_target - len(self.stock)
-        if n > 0:
-            logger.info(f"Restocking {type(self).__name__} with {n} points")
-            pts = self._restock_impl(n)
-            self.stock.extend(pts)
-            diff = n - len(pts)
-            if diff > 0:
-                # if implementations of _restock_impl are well behaved, this will
-                # never actually need to recurse to finish the job.
-                self.restock(n=diff)
-            logger.info(f"Finished restocking {type(self).__name__}")
-
-    def get_points(self, n=1):
-        """
-        Pull n points from the current stock.
-        It is recommended to call PointSource.restock after this, to ensure the
-        stock is not depleted. If possible, calling restock in another thread is
-        recommended, as it can be a long operation depending on implementation.
-        """
-        if len(self.stock) >= n:
-            pts = []
-            for _ in range(n):
-                pts.append(self.stock.popleft())
-            return pts
-        self.restock(n=n)
-        # this is safe as long as restock does actually add enough new points.
-        # unless this object is being rapidly drained by another thread,
-        # this will recur at most once.
-        return self.get_points(n=n)

+ 0 - 87
server/app/gen/urban_centers.py

@@ -1,87 +0,0 @@
-import math
-import random
-
-from .shared import point_has_streetview, PointSource
-from ..scoring import mean_earth_radius_km
-
-initialized = False
-urban_centers_usa = []
-urban_centers_non_usa = []
-
-
-def init():
-    """
-    Read in the urban centers data files. Should be called before trying to generate points.
-    """
-    global initialized
-    if initialized:
-        return
-    with open("./data/urban-centers-usa.csv") as infile:
-        for line in infile:
-            lat, lng = line.split(",")
-            urban_centers_usa.append((float(lat.strip()), float(lng.strip())))
-    with open("./data/urban-centers-non-usa.csv") as infile:
-        for line in infile:
-            lat, lng = line.split(",")
-            urban_centers_non_usa.append((float(lat.strip()), float(lng.strip())))
-    initialized = True
-
-
-def urban_coord(max_retries=10, retries_per_point=30, max_dist_km=25, usa_chance=0.1):
-    """
-    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
-    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.
-    """
-
-    src = urban_centers_usa if random.random() <= usa_chance else urban_centers_non_usa
-
-    for _ in range(max_retries):
-        # logic adapted from https://stackoverflow.com/a/7835325
-        # start in a city
-        (city_lat, city_lng) = random.choice(src)
-        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)
-        for _ in range(retries_per_point):
-            # turn a random direction, and go random distance
-            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)
-
-
-class UrbanPointSource(PointSource):
-    def __init__(self, stock_target=20, max_retries=10, retries_per_point=30, max_dist_km=25, usa_chance=0.1):
-        super().__init__(stock_target=stock_target)
-        self.max_retries = max_retries
-        self.retries_per_point = retries_per_point
-        self.max_dist_km = max_dist_km
-        self.usa_chance = usa_chance
-        if not initialized:
-            init()
-    
-    def _restock_impl(self, n):
-        points = []
-        while len(points) < n:
-            pt = urban_coord(
-                max_retries=self.max_retries,
-                retries_per_point=self.retries_per_point,
-                max_dist_km=self.max_dist_km,
-                usa_chance=self.usa_chance
-            )
-            if pt is not None:
-                points.append(pt)
-        return points