GeoPy-geokodowanie z Pythonem

Krzysztof Żuraw

Co to jest geokodowanie?

GeoPy

>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode('Świętego Antoniego 2/4, 
                                   Wrocław')
>>> location.raw
{'boundingbox': ['51.108693', '51.108793', 
                 '17.0266331', '17.0267331'],
 'class': 'office',
 'display_name': 'Unic Sp. z o.o., 2/4, 
                  Świętego Antoniego, 
                  Dzielnica Czterech Świątyń,
                  Osiedle Stare Miasto, 
                  Wrocław, dolnośląskie, 50-073, Polska',
 'importance': 0.311,
 'lat': '51.108743',
 'licence': 'Data © OpenStreetMap contributors, 
             ODbL 1.0. http://www.openstreetmap.org/copyright',
 'lon': '17.0266831',
 'osm_id': '3066838854',
 'osm_type': 'node',
 'place_id': '42611105',
 'type': 'it'}

reverse geocoding

>>> geolocator.reverse(("{lat},{lon}").format(
                        lat=location.latitude, 
                        lon=location.longitude))
Location(Unic Sp. z o.o., 2/4, Świętego Antoniego, 
Dzielnica Czterech Świątyń, Osiedle Stare Miasto, 
Wrocław, dolnośląskie, 50-073, Polska, 
(51.108743, 17.0266831, 0.0))

Metoda Vincentego

>>> vincenty(wroclaw, krakow).kilometers
>>> from geopy.distance import vincenty, great_circle
236.92353915307228

Great-circle

>>> great_circle(wroclaw, krakow).kilometers
236.40709536785647

Wykorzystanie

views.py

from urllib.error import URLError
from geopy.geocoders.osm import Nominatim, GeocoderQueryError

def geocode_address(address):
    geocoder = Nominatim()
    try:
        _, latlon = geocoder.geocode(address)
    except (URLError, GeocoderQueryError, ValueError):
        return None
    else:
        return latlon
def get_shops(longitude, latitude):
    current_point = geos.fromstr("POINT({lon} {lat})".format(
                    lon=longitude, lat=latitude))
    return Shop.gis.distance(current_point) \
                            .order_by('distance')[:3]

Pytania?

GeoPy

GeoPy-geokodowanie z Pythonem

By Krzysztof Żuraw

GeoPy-geokodowanie z Pythonem

  • 723
Loading comments...

More from Krzysztof Żuraw