Reverse Geocoding
2014-11-28
####REVERSEGEocdoing####
#####It is for this script /scripts/apiscript.py and jsontopandas.py#####
1. As per the notes on Geonames_Pandas_Shapefile.md
, the postal code latitude and longitude do not have high resolution to differentiate between to nearby postal code points. For this need a high-resolution latitude-longitude data, Google Map API gives solution. As per its documentation, giving address, the latitude and longitude json files can be obtained with its API key, based on this and getting the API access key from here. For more info, info2
1. To get the latitude and longitude of location, used following script based on this and the script is below
import json
import urllib2
url = "https://maps.googleapis.com/maps/api/geocode/json?address=SRIRAMAKRISHNAENGINEERINGCOLLEGE+Vattamalaipalayam+NGGOColonyPost+Coimbatore+641022&key=APIkey"
import pandas as pd
db=pd.read_csv('Coimbatore_postal.csv')
names=db['placeName_1'].tolist()
db["placeName_1"] = df["placeName"].map(str.strip)
data = json.load(urllib2.urlopen(url))
a=[]
for name in names:
url = "https://maps.googleapis.com/maps/api/geocode/json?address="+name+"&key=APIkey"
data = json.load(urllib2.urlopen(url))
a.append(data)
ouputfilename='loc_latA1.json'
with open(outputfilename, 'wb') as outfile:
json.dump(a, outfile)
- In which the data frame also can be added as a list but having problem with names having space in it such name having
RS Puram
which through the error of ‘url wrong’ so it has to converted intoRS+PURAM
- For that to replace the space with +, use below code as per this
db[‘placeName1’] = db[‘placeName’].str.replace(’ ‘, ‘+’) db[‘placeName2’] = db[‘placeName_1’].str.replace(‘.’, ‘+’)
- With this change, the above script returned all the json output into the file specified and contains around 550 element removing empty spaces.
- This json has to be converted into csv or pandas data frame to be used with location merging and creating shape file etc.
- Major steps made was on with reading json as data frames, removing any empty spaces such ZERO_STATUS in API call, then converted into array list of dictionary and for a loop this list to get the required field of information from json file of API call.
- The created JSON by earlier step is not a standard json. Instead, it is a list of dictionaries, JSON is pure dictionaries without list, this seems to making problem in incorrect query.