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")
Related
I want to pull and print dt, temp, weather, humidity and wind speed given the latitude and longitude.
I thought that using data = r.json() would allow me to access the API Response like a dictionary and be able to access/print the elements I want using print(r[" "]), however, I am getting "KeyError: 'dt'"
def get_weather(lat,lon):
import json
import requests
r = requests.post('https://api.openweathermap.org/data/2.5/onecall?lat={}&lon={}&exclude=hourly,daily&appid=92d93ccc6ac5587d35d3ccc4479083a1'.format(lat,lon))
data = r.json()
dt = data["dt"]
temp = data["temp"]
weather = data["weather"]
humidity = data["humidity"]
wind_speed = data["wind_speed"]
print(r["dt"])
print(r["temp"])
print(r["weather"])
print(r["humidity"])
print(r["wind_speed"])
get_weather(33,44)
'dt' is a member of 'current', so you have to access it through 2 keys, you can see this if you print all items in the json like below:
def get_weather(lat,lon):
import json
import requests
r = requests.post('https://api.openweathermap.org/data/2.5/onecall?lat={}&lon={}&exclude=hourly,daily&appid=92d93ccc6ac5587d35d3ccc4479083a1'.format(lat,lon))
data = r.json()
for k,v in data.items():
print(k,v)
current = data['current']
print(current['dt'])
get_weather(33,44)
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
...
I'm trying to scrape all of the French map.
I've got one issue :
1 - I'm limited by the zoom of the map
import requests
url ='https://www.iadfrance.fr/agent-search-location?southwestlat=47.0270782&southwestlng=-2.1560669&northeastlat=47.4930807&northeastlng=-1.0093689'
jsonObj = requests.get(url).json()
emails = jsonObj['agents']
#print (emails)
for agent in emails:
email = agent['email']
print(email)
Thank you
You'll have to utilize the longitude, latitude parameters in the request to "zoom out"
You can either change them manually, or I'm a fan of osmnx. You can use that to get the boundaries of different areas, then set a radius in meters to create your boundary box:
import requests
import osmnx as ox
import os
os.environ["PROJ_LIB"] = "C:/Users/xxxxxxx/AppData/Local/Continuum/anaconda3/Library/share"; #fixr
# Get a boundary box of a city/place/address
city = ox.gdf_from_place('Paris, France')
# Distance to make boundary from center in meters
# Essentially allows you to zoom out
distance = 300000
# Get centroid of that city/place boundary box
point = ( city['geometry'].centroid.x.iloc[0], city['geometry'].centroid.y.iloc[0] )
# Get a new boundary box a certain distance in North, South, East, West directions for x meters
boundary = ox.bbox_from_point(point, distance=distance , project_utm=False, return_crs=False)
sw_lat = boundary[3]
sw_lng = boundary[0]*-1
ne_lat = boundary[2]
ne_lng = boundary[1]*-1
# website to scrape https://www.iadfrance.fr/trouver-un-conseiller
url ='https://www.iadfrance.fr/agent-search-location'
# Here is the coordinates from orginial post
#payload = {
#'southwestlat': '47.0270782',
#'southwestlng': '-2.1560669',
#'northeastlat': '47.4930807',
#'northeastlng': '-1.0093689'}
payload = {
'southwestlat': sw_lat,
'southwestlng': sw_lng,
'northeastlat': ne_lat,
'northeastlng': ne_lng}
jsonObj = requests.get(url, params=payload).json()
emails = jsonObj['agents']
#print (emails)
for agent in emails:
email = agent['email']
print(email)
I find the right way, I must think out the box.
I've set 2 geographic data manually in a very very large area. ( one in Atlantic and an other in Russia ).
It works !
import requests
url ='https://www.iadfrance.fr/agent-search-location?southwestlat=9.884462&southwestlng=-35.58398&northeastlat=68.714264&northeastlng=44.796407'
jsonObj = requests.get(url).json()
emails = jsonObj['agents']
#print (emails)
for agent in emails:
email = agent['email']
print(email)
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
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