How to get Address from Latitude & Longitude in Django GeoIP? - python

I cannot see anything in their API to do this:
https://docs.djangoproject.com/en/dev/ref/contrib/gis/geoip/#geoip-api
Or should I just use Google API for Reverse Geocoding?

Solution - call this URL and parse it's JSON.
http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false

Use geopy, it can handle multiple geocoders including googlev3.
from geopy.geocoders import GoogleV3
geolocator = GoogleV3()
location = geolocator.reverse("52.509669, 13.376294")
print(location.address)
>>> Potsdamer Platz, Mitte, Berlin, 10117, Deutschland, European Union
install with pip:
pip install geopy
infos found on: https://github.com/geopy/geopy

You can use maps API. I've included a snippet which I use to calculate marathon start points converted into a PointField using Postgis with Django. This should set you on your way.
import requests
def geocode(data):
url_list = []
for item in data:
address = ('%s+%s' % (item.city, item.country)).replace(' ', '+')
url = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false' % address
url_list.append([item.pk, url])
json_results = []
for url in url_list:
r = requests.get(url[1])
json_results.append([url[0], r.json])
result_list = []
for result in json_results:
if result[1]['status'] == 'OK':
lat = float(result[1]['results'][0]['geometry']['location']['lat'])
lng = float(result[1]['results'][0]['geometry']['location']['lng'])
marathon = Marathon.objects.get(pk=result[0])
marathon.point = GEOSGeometry('POINT(%s %s)' % (lng, lat))
marathon.save()
return result_list

#rawsix answer seems smart for a django user.
Note however that the location returned by geolocator.reverse(query) is a list and not a Location object; so attempting to retrieve attribute address from it would result in an error.
Usually, the first item in that list has the closest address information. so u can simply do:
location = location[0]
address = location.address
Additionally, instead of passing a string latitude and longitude to the reverse method, you can use a tuple and it must be the latitude before the longitude. You can do:
from geopy.geocoders import GoogleV3()
geocoder = GoogleV3()
location_list = geocoder.reverse((latitude, longitude))
location = location_list[0]
address = location.address

Related

Finding accurate location using opencage geocode

I am able to get the coordinates, but they are not accurate. I have removed the API keys. Is there a problem with the code, or I need to change my API? I use Opencage geocoder.
import phonenumbers
import folium
from target import number
from phonenumbers import geocoder
key = ''
theNumber = phonenumbers.parse(number)
yourLocation = geocoder.description_for_number(theNumber, "en")
print(yourLocation)
#Other details
from phonenumbers import carrier
service_provider = phonenumbers.parse(number)
print(carrier.name_for_number(service_provider, "en"))
from opencage.geocoder import OpenCageGeocode
geocoder = OpenCageGeocode(key)
query =str(yourLocation)
results = geocoder.geocode(query)
#print(results)
lat = results[0]['geometry']['lat']
long = results[0]['geometry']['lng']
print(lat, long)
myMap = folium.Map(location =[lat, long], zoom_start= 0)
folium.Marker([lat , long], popup= yourLocation).add_to(myMap)
#html save
myMap.save("myLoc.html")

sorting the tags and print id with coordinates

I need to sort out the data openstreetmap file with map. I need to find only ways which are closed (first nd coordinate is equal to last), and what's more only ways for tag with key=='building'. Then I need to print out its id and on the next string the vocabluary with list of tuples, containing the coordinates of nodes, which are included in this way. It can look like this:
28889642
[(55.5652795, 37.5695507), (55.5651145, 37.5702288), (55.5648475, 37.5700314), (55.5650147, 37.5693509), (55.5652795, 37.5695507)]
28911067
[(55.5683532, 37.5644676), (55.5682987, 37.5644271), (55.5679549, 37.5641683), (55.5679974, 37.5639919), (55.5683976, 37.5642929), (55.5686577, 37.5632112), (55.5687302, 37.5632692), (55.5687094, 37.5633574), (55.5687319, 37.5633741), (55.5686567, 37.5636906), (55.5686342, 37.5636738), (55.5685984, 37.5638247), (55.5686198, 37.5638406), (55.5684996, 37.5643462), (55.5684605, 37.5643171), (55.5684327, 37.5644347), (55.5683718, 37.5643896), (55.5683532, 37.5644676)]
My code allows me to find appropriate ways and print them, but it does not see the vocabulary with coordinates appropriate for this way.
The code:
from urllib.request import urlopen, urlretrieve
from bs4 import BeautifulSoup
from urllib.request import urlopen, urlretrieve
response = urlopen(' https://stepik.org/media/attachments/lesson/266078/mapcity.osm')
xml = response.read().decode('utf8')
soup = BeautifulSoup(xml, 'lxml')
dict = {}
for node in soup.find_all('node'):
lat = node['lat']
lon = node['lon']
id = node['id']
dict[id] = (lat, lon)
for way in soup.find_all('way'):
if way.find_all('nd')[0]==way.find_all('nd')[-1]:
for tag in way('tag'):
if tag['k'] == 'building':
print(way['id'])
elif way['id'] in dict[id]:
print(dict[id])
I'm not familiar with BS. I'll give you another example. The code logic is interlinked. I hope it can help you.
from simplified_scrapy import SimplifiedDoc, utils, req
res = req.get('https://stepik.org/media/attachments/lesson/266078/mapcity.osm')
xml = res.read().decode('utf-8')
doc = SimplifiedDoc(xml)
dic = {}
for node in doc.nodes:
lat = node['lat']
lon = node['lon']
id = node['id']
dic[id] = (lat, lon)
for way in doc.selects('way'):
nds = way.selects('nd>ref()') # Find all nd
building = way.select('tag#k=building') # Judge whether there is a tag with k = building
if nds[0]==nds[-1] and building:
print(way['id'])
print([dic[nd] for nd in nds if nd in dic])
Result:
28889642
[('55.5652795', '37.5695507'), ('55.5651145', '37.5702288'), ('55.5648475', '37.5700314'), ('55.5650147', '37.5693509'), ('55.5652795', '37.5695507')]
28911067
...

Selecting values from a JSON file in Python

I am getting JIRA data using the following python code,
how do I store the response for more than one key (my example shows only one KEY but in general I get lot of data) and print only the values corresponding to total,key, customfield_12830, summary
import requests
import json
import logging
import datetime
import base64
import urllib
serverURL = 'https://jira-stability-tools.company.com/jira'
user = 'username'
password = 'password'
query = 'project = PROJECTNAME AND "Build Info" ~ BUILDNAME AND assignee=ASSIGNEENAME'
jql = '/rest/api/2/search?jql=%s' % urllib.quote(query)
response = requests.get(serverURL + jql,verify=False,auth=(user, password))
print response.json()
response.json() OUTPUT:-
http://pastebin.com/h8R4QMgB
From the the link you pasted to pastebin and from the json that I saw, its a you issues as list containing key, fields(which holds custom fields), self, id, expand.
You can simply iterate through this response and extract values for keys you want. You can go like.
data = response.json()
issues = data.get('issues', list())
x = list()
for issue in issues:
temp = {
'key': issue['key'],
'customfield': issue['fields']['customfield_12830'],
'total': issue['fields']['progress']['total']
}
x.append(temp)
print(x)
x is list of dictionaries containing the data for fields you mentioned. Let me know if I have been unclear somewhere or what I have given is not what you are looking for.
PS: It is always advisable to use dict.get('keyname', None) to get values as you can always put a default value if key is not found. For this solution I didn't do it as I just wanted to provide approach.
Update: In the comments you(OP) mentioned that it gives attributerror.Try this code
data = response.json()
issues = data.get('issues', list())
x = list()
for issue in issues:
temp = dict()
key = issue.get('key', None)
if key:
temp['key'] = key
fields = issue.get('fields', None)
if fields:
customfield = fields.get('customfield_12830', None)
temp['customfield'] = customfield
progress = fields.get('progress', None)
if progress:
total = progress.get('total', None)
temp['total'] = total
x.append(temp)
print(x)

Get the highest value of a specific field from an API reponse in Python

I make a GET to a API
I got this back
{"status":200,"message":"Success","data":[{"email_address":"admin#nyunets.com","password":"admin","account_id":1000,"account_type":"admin","name_prefix":null,"first_name":null,"middle_names":null,"last_name":"Admin","name_suffix":null,"non_person_name":false,"dba":"","display_name":"Admin","address1":"111 Park Ave","address2":"Floor 4","address3":"Suite 4011","city":"New York","state":"NY","postal_code":"10022","nation_code":"USA","phone1":"212-555-1212","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":2,"last_updated_utc_in_secs":1446127072},{"email_address":"mhn#nyu.com","password":"nyu123","account_id":1002,"account_type":"customer","name_prefix":"","first_name":"MHN","middle_names":"","last_name":"User","name_suffix":"","non_person_name":false,"dba":"","display_name":"MHNUser","address1":"3101 Knox St","address2":"","address3":"","city":"Dallas","state":"TX","postal_code":"75205","nation_code":"USA","phone1":"8623875097","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":2,"last_updated_utc_in_secs":1461166172},{"email_address":"mhn1#nyu.com","password":"nyu123","account_id":1004,"account_type":"customer","name_prefix":"","first_name":"MHN1","middle_names":"","last_name":"User","name_suffix":"","non_person_name":false,"dba":"","display_name":"MHN1User","address1":"1010 Rosedale Shopping Center","address2":"","address3":"","city":"Roseville","state":"MN","postal_code":"55113","nation_code":"USA","phone1":"8279856982","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":2,"last_updated_utc_in_secs":1461166417},{"email_address":"location#nyu.com","password":"nyu123","account_id":1005,"account_type":"customer","name_prefix":"","first_name":"BB","middle_names":"","last_name":"HH","name_suffix":"","non_person_name":false,"dba":"","display_name":"BBHH","address1":"9906 Beverly Dr","address2":"9906 Beverly Dr","address3":"","city":"Beverly Hills","state":"CA","postal_code":"90210","nation_code":"90210","phone1":"3105559906","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":1,"last_updated_utc_in_secs":1461167224},{"email_address":"mbn1#nyu.com","password":"nyu123","account_id":1003,"account_type":"customer","name_prefix":"","first_name":"MBN1","middle_names":"","last_name":"User","name_suffix":"","non_person_name":false,"dba":"","display_name":"MBN1User","address1":"3200 S Las Vegas Blvd","address2":"","address3":"","city":"Las Vegas","state":"NV","postal_code":"89109","nation_code":"USA","phone1":"9273597497","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":1,"last_updated_utc_in_secs":1461593233},{"email_address":"mbn#nyu.com","password":"nyu123","account_id":1001,"account_type":"customer","name_prefix":"","first_name":"MBN","middle_names":"","last_name":"User","name_suffix":"","non_person_name":false,"dba":"","display_name":"MBNUser","address1":"300 Concord Road","address2":"","address3":"","city":"Billerica","state":"MA","postal_code":"01821","nation_code":"USA","phone1":"8127085695","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":1,"last_updated_utc_in_secs":1461784499},{"email_address":"usermbn#nyu.com","password":"nyu123","account_id":1006,"account_type":"customer","name_prefix":"","first_name":"User","middle_names":"","last_name":"MBN","name_suffix":"","non_person_name":false,"dba":"","display_name":"UserMBN","address1":"75 Saint Alphonsus Street","address2":"","address3":"","city":"Boston","state":"MA","postal_code":"01821","nation_code":"USA","phone1":"8127085695","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":1,"last_updated_utc_in_secs":1462285561},{"email_address":"emile.barnaby#example.com","password":"nyu123","account_id":2000,"account_type":"customer","name_prefix":"","first_name":"emile","middle_names":"","last_name":"barnaby","name_suffix":"","non_person_name":false,"dba":"","display_name":"emilebarnaby","address1":"300 Concord Rd","address2":"","address3":"","city":"8239grandmaraisave","state":"manitoba","postal_code":"56798","nation_code":"USA","phone1":"414-140-1435","phone2":"414-140-1435","phone3":"414-140-1435","time_zone_offset_from_utc":-5,"customer_type":1,"last_updated_utc_in_secs":1462211572}]}
I have
import requests
import json
url = "http://api/users"
accounts = requests.get(url).json()
data = json.loads(accounts)
object_with_max_account_id = max(accounts['data'], key=lambda x: x['account_id'])
print(object_with_max_account_id['account_id'])
Goal
is to get the highest account id out of it.
Usually we like to see what OPs try themselves, this is pretty much straightforward.
import requests
url = "http://api/users"
accounts = requests.get(url).json()
object_with_max_account_id = max(accounts['data'], key=lambda x: x['account_id'])
print(object_with_max_account_id['account_id'])
>> 2000
Edit: Apparently, you first need to parse your input as JSON.
Check out simplejson.
import simplejson as json
data_obj = json.loads(data)
The s in loads means load from string.
Then, if you want to be looping through, how about something like:
maxID= -1
for account in data_obj:
if(account[account_id])>maxID:
maxID= account[account_id]
print "Max ID is %d" % maxID

getting distance between two location using geocoding

I want to find the distance between two location using google API. I want output to be look like - "The distance between location 1 and location 2 is 500 miles ( distance here is example purposes )", but how can i get the desired output as the current program is showing various output ( which i cant use to get he desired output ) . can you guys please show me the way or show me what is the exact procedure to do it?
import urllib
import json
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
while True:
address = raw_input('Enter location: ')
if len(address) < 1 : break
url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
try: js = json.loads(str(data))
except: js = None
if 'status' not in js or js['status'] != 'OK':
print '==== Failure To Retrieve ===='
print data
continue
print json.dumps(js, indent=4)
lat = js["results"][0]["geometry"]["location"]["lat"]
lng = js["results"][0]["geometry"]["location"]["lng"]
print 'lat',lat,'lng',lng
location = js['results'][0]['formatted_address']
print location
Google has a specific api for that, it's called Google Maps Distance Matrix API.
Distance & duration for multiple destinations and transport modes.
Retrieve duration and distance values based on the recommended route
between start and end points.
If you just need the distance between two points on the globe you may want to use the Haversine formula
If you know lat and lon, use the geopy package:
In [1]: from geopy.distance import great_circle
In [2]: newport_ri = (41.49008, -71.312796)
In [3]: cleveland_oh = (41.499498, -81.695391)
In [4]: great_circle(newport_ri, cleveland_oh).kilometers
Out[4]: 864.4567616296598

Categories