Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.21 KB | None | 0 0
  1. import requests
  2. import csv
  3. import time
  4. import math
  5. import itertools
  6. import urllib
  7. import json
  8. from googlemaps import places
  9.  
  10. API_KEY = ["AIzaSyDT67KyLaX7PLiTji5iSLK2zCiw9CszLag"]
  11.  
  12. SAVE_PATH = '/Users/kawsernasim/Desktop/maps-test'
  13. COMPANY_SEARCH = 'Los Angeles'
  14. RADIUS_KM = 8
  15. DEFAULT_LIMIT = 60
  16.  
  17. class coordinates_box(object):
  18.     """
  19.    Initialise a coordinates_box class which will hold the produced coordinates and
  20.    output a html map of the search area
  21.    """
  22.     def __init__(self):
  23.         self.coordset = []
  24.  
  25.     def createcoordinates(self,
  26.                           southwest_lat,
  27.                           southwest_lng,
  28.                           northeast_lat,
  29.                           northeast_lng):
  30.         """
  31.        Based on the input radius this tesselates a 2D space with circles in
  32.        a hexagonal structure
  33.        """
  34.         earth_radius_km = 6371
  35.         lat_start = math.radians(southwest_lat)
  36.         lon_start = math.radians(southwest_lng)
  37.         lat = lat_start
  38.         lon = lon_start
  39.         lat_level = 1
  40.         while True:
  41.             if (math.degrees(lat) <= northeast_lat) & (math.degrees(lon) <= northeast_lng):
  42.                 self.coordset.append([math.degrees(lat), math.degrees(lon)])
  43.             parallel_radius = earth_radius_km * math.cos(lat)
  44.             if math.degrees(lat) > northeast_lat:
  45.                 break
  46.             elif math.degrees(lon) > northeast_lng:
  47.                 lat_level += 1
  48.                 lat += (RADIUS_KM / earth_radius_km) + (RADIUS_KM / earth_radius_km) * math.sin(math.radians(30))
  49.                 if lat_level % 2 != 0:
  50.                     lon = lon_start
  51.                 else:
  52.                     lon = lon_start + (RADIUS_KM / parallel_radius) * math.cos(math.radians(30))
  53.             else:
  54.                 lon += 2 * (RADIUS_KM / parallel_radius) * math.cos(math.radians(30))
  55.        
  56.         print('Coordinates-set contains %d coordinates' % len(self.coordset))
  57.         #Save coordinates:
  58.         f = open('circles_' + COMPANY_SEARCH + '_python_mined.csv', 'w', newline='')
  59.         w = csv.writer(f)
  60.         for coord in self.coordset:
  61.             w.writerow(coord)
  62.        
  63.         f.close()
  64.  
  65. class counter(object):
  66.     def __init__(self):
  67.         self.numRegion = 0
  68.         self.numRegionStores = 0
  69.         numStore = 0
  70.  
  71.     def increment_region(self):
  72.         self.numRegion += 1
  73.    
  74.     def increment_region_stores(self):
  75.         self.numRegionStores += 1
  76.    
  77.     def increment_stores(amount):
  78.         numStore +=amount
  79.  
  80. class GooglePlaces(object):
  81.     API_KEY = "AIzaSyDT67KyLaX7PLiTji5iSLK2zCiw9CszLag"
  82.     def __init__(self, radius, types):
  83.         self.radius = radius
  84.         self.types = types
  85.  
  86.     def get_stores_by_query(self, location, radius, types):
  87.         stores = []
  88.         endpoint_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
  89.         params = {
  90.             'location' : location,
  91.             'radius' : radius,
  92.             'types' : types,
  93.             'key' : API_KEY
  94.         }
  95.        
  96.         res = requests.get(endpoint_url, params)
  97.         results = json.loads(res.content)
  98.        
  99.         stores.extend(results['results'])
  100.         time.sleep(2)
  101.  
  102.         while "next_page_token" in results:
  103.             params['pagetoken'] = results['next_page_token'],
  104.             res = requests.get(endpoint_url, params = params)
  105.             results = json.loads(res.content)
  106.             stores.extend(results['results'])
  107.             time.sleep(2)
  108.         print("LENFTH OF STORES IS")
  109.         print(len(stores))
  110.         numStores = counter()
  111.         numStores.increment_stores(len(stores))
  112.         print(numStores)
  113.         return stores
  114.  
  115. def main():
  116.     coord = coordinates_box()
  117.     coord.createcoordinates(32.668888, -117.244962, 33.090996, -116.909879)
  118.     mapRun = GooglePlaces(4*1000, 'jewelry_store')
  119.  
  120.     with open('circles_Los Angeles_python_mined.csv') as csvfile:
  121.         readCSV = csv.reader(csvfile, delimiter=',')
  122.         for row in readCSV:
  123.             coord_passed = row[0] + "," + row[1]
  124.             mapRun.get_stores_by_query(coord_passed, 4000, 'jewelry_store')
  125.            
  126.            
  127.        
  128.  
  129. if __name__ == "__main__":
  130.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement