Advertisement
Guest User

Untitled

a guest
Mar 5th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. import psycopg2
  2. from geopy.geocoders import Nominatim
  3. import os
  4. import googlemaps
  5. import requests
  6. import json
  7. from pandas.io.json import json_normalize
  8. import pandas as pd
  9. from sqlalchemy import create_engine
  10. import sql
  11. import difflib
  12.  
  13.  
  14. # REVERSE GEOCODING: From lat/long to address
  15.  
  16.  
  17. def get_google_results(latlng, api_key=None, return_full_response=False, sp=None):
  18.     """
  19.    Get geocode results from Google Maps Geocoding API.
  20.    Note, that in the case of multiple google geocode reuslts,
  21.    this function returns details of the FIRST result.
  22.    """
  23.     # Set up your Geocoding url
  24.     geocode_url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng={0}&key={1}'
  25.     request = geocode_url.format(latlng, api_key)
  26.     #if api_key is not None:
  27.         #geocode_url = geocode_url + "&key={}".format(api_key)
  28.     result = {}
  29.     # Ping google for the reuslts:
  30.     #results = requests.get(geocode_url)
  31.     data = json.loads(requests.get(request).text)
  32.     if len(data['results']) > 0:
  33.         result = data['results'][0]
  34.     answer = result
  35.     output = {
  36.         "address_components": answer.get('address_components'),
  37.         "formatted_address": answer.get('formatted_address'),
  38.         "latitude": answer.get('geometry').get('location').get('lat'),
  39.         "longitude": answer.get('geometry').get('location').get('lng'),
  40.         "accuracy": answer.get('geometry').get('location_type'),
  41.         "place_id": answer.get("place_id"),
  42.         "type": ",".join(answer.get('types'))
  43.     }
  44.  
  45.     output['input_string'] = latlng
  46.     output['status'] = result.get('status')
  47.     output['license'] = sp
  48.  
  49.     return output
  50.  
  51.  
  52. if __name__ == "__main__":
  53.     conn = psycopg2.connect("dbname=mehmed user=postgres password = '' host=localhost")
  54.     cur = conn.cursor()
  55.     return_full_response = False
  56.     api_key = os.environ["GOOGLE_API_KEY"] = "KEY"
  57.     #gm = googlemaps.Client(key=api_key)
  58.  
  59.     cur.execute("SELECT distinct on (location) location, license from businesses where location is not null limit 595")
  60.     latlng = cur.fetchall()
  61.     t = 0
  62.     x = []
  63.     s = []
  64.     s1 = ""
  65.     sp = ''
  66.     br = 0
  67.     for l in latlng:
  68.         for a in l:
  69.             if br < 1:
  70.                 s.append(a)
  71.             else:
  72.                 sp = a
  73.             br = br + 1
  74.         br = 0
  75.         s1 = str(s)
  76.         s1 = s1[3:-3]
  77.         x.append(get_google_results(s1, api_key, False, sp))
  78.         sp = ''
  79.         s = []
  80.         if t == 1:
  81.             break
  82.         t = t + 1
  83.  
  84.     df = pd.DataFrame.from_dict(json_normalize(x), orient='columns')
  85.  
  86.     #engine = create_engine('postgresql://localhost:5432')
  87.     #df.to_sql('forward', engine)
  88.     #sql.write_frame(df, 'forward', conn, flavor='postgresql')
  89.     df.to_csv('reverse_geocoding_APIx13.csv', sep = ',')
  90.     print(df.columns)
  91.  
  92.  
  93.     """
  94.    print(df["accuracy"])
  95.    print(df["formatted_address"])
  96.    print(df["google_place_id"])
  97.    print(df["input_string"])
  98.    print(df["latitude"])
  99.    print(df["longitude"])
  100.    print(df["number_of_results"])
  101.    print(df["postcode"])
  102.    print(df["status"])
  103.    print(df["type"])
  104.    """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement