Skip to content

Instantly share code, notes, and snippets.

@ghtmtt
Last active Sep 14, 2021
Embed
What would you like to do?
Geocoding in QGIS Field Calculator
# the function needs geopy
# for Windows users, to install geopy, open the OSGEO4W Shell and type:
# py3_env
# pip3 install geopy
from qgis.core import *
from qgis.gui import *
from geopy.geocoders import Nominatim
locator = Nominatim(user_agent="myGeocoder")
@qgsfunction(args='auto', group='Custom')
def geocode(field, feature, parent):
"""
Geocoding
field: string field of the original layer made like 'piazza navona, rome, italy'
returns a simple string of lat and long, like '43.56565 17.45454'
"""
location = locator.geocode(field)
lat = location.latitude
long = location.longitude
tot = f'{lat} {long}'
return tot
@pigreco

This comment has been minimized.

Copy link

@pigreco pigreco commented Mar 4, 2020

OSGeo4W shell

py3_env
pip3 install geopy
@ghtmtt

This comment has been minimized.

Copy link
Owner Author

@ghtmtt ghtmtt commented Mar 5, 2020

@pigreco

thanks, gist updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment