Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import csv
- import time
- import math
- import itertools
- import urllib
- import json
- from googlemaps import places
- API_KEY = ["AIzaSyDT67KyLaX7PLiTji5iSLK2zCiw9CszLag"]
- SAVE_PATH = '/Users/kawsernasim/Desktop/maps-test'
- COMPANY_SEARCH = 'Los Angeles'
- RADIUS_KM = 8
- DEFAULT_LIMIT = 60
- class coordinates_box(object):
- """
- Initialise a coordinates_box class which will hold the produced coordinates and
- output a html map of the search area
- """
- def __init__(self):
- self.coordset = []
- def createcoordinates(self,
- southwest_lat,
- southwest_lng,
- northeast_lat,
- northeast_lng):
- """
- Based on the input radius this tesselates a 2D space with circles in
- a hexagonal structure
- """
- earth_radius_km = 6371
- lat_start = math.radians(southwest_lat)
- lon_start = math.radians(southwest_lng)
- lat = lat_start
- lon = lon_start
- lat_level = 1
- while True:
- if (math.degrees(lat) <= northeast_lat) & (math.degrees(lon) <= northeast_lng):
- self.coordset.append([math.degrees(lat), math.degrees(lon)])
- parallel_radius = earth_radius_km * math.cos(lat)
- if math.degrees(lat) > northeast_lat:
- break
- elif math.degrees(lon) > northeast_lng:
- lat_level += 1
- lat += (RADIUS_KM / earth_radius_km) + (RADIUS_KM / earth_radius_km) * math.sin(math.radians(30))
- if lat_level % 2 != 0:
- lon = lon_start
- else:
- lon = lon_start + (RADIUS_KM / parallel_radius) * math.cos(math.radians(30))
- else:
- lon += 2 * (RADIUS_KM / parallel_radius) * math.cos(math.radians(30))
- print('Coordinates-set contains %d coordinates' % len(self.coordset))
- #Save coordinates:
- f = open('circles_' + COMPANY_SEARCH + '_python_mined.csv', 'w', newline='')
- w = csv.writer(f)
- for coord in self.coordset:
- w.writerow(coord)
- f.close()
- class counter(object):
- def __init__(self):
- self.numRegion = 0
- self.numRegionStores = 0
- numStore = 0
- def increment_region(self):
- self.numRegion += 1
- def increment_region_stores(self):
- self.numRegionStores += 1
- def increment_stores(amount):
- numStore +=amount
- class GooglePlaces(object):
- API_KEY = "AIzaSyDT67KyLaX7PLiTji5iSLK2zCiw9CszLag"
- def __init__(self, radius, types):
- self.radius = radius
- self.types = types
- def get_stores_by_query(self, location, radius, types):
- stores = []
- endpoint_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
- params = {
- 'location' : location,
- 'radius' : radius,
- 'types' : types,
- 'key' : API_KEY
- }
- res = requests.get(endpoint_url, params)
- results = json.loads(res.content)
- stores.extend(results['results'])
- time.sleep(2)
- while "next_page_token" in results:
- params['pagetoken'] = results['next_page_token'],
- res = requests.get(endpoint_url, params = params)
- results = json.loads(res.content)
- stores.extend(results['results'])
- time.sleep(2)
- print("LENFTH OF STORES IS")
- print(len(stores))
- numStores = counter()
- numStores.increment_stores(len(stores))
- print(numStores)
- return stores
- def main():
- coord = coordinates_box()
- coord.createcoordinates(32.668888, -117.244962, 33.090996, -116.909879)
- mapRun = GooglePlaces(4*1000, 'jewelry_store')
- with open('circles_Los Angeles_python_mined.csv') as csvfile:
- readCSV = csv.reader(csvfile, delimiter=',')
- for row in readCSV:
- coord_passed = row[0] + "," + row[1]
- mapRun.get_stores_by_query(coord_passed, 4000, 'jewelry_store')
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement