Skip to content

Instantly share code, notes, and snippets.

@ghtmtt
Last active September 14, 2021 04:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghtmtt/53d8592eabd2fddc8617b40146075e59 to your computer and use it in GitHub Desktop.
Save ghtmtt/53d8592eabd2fddc8617b40146075e59 to your computer and use it in GitHub Desktop.
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
Copy link

pigreco commented Mar 4, 2020

OSGeo4W shell

py3_env
pip3 install geopy

@ghtmtt
Copy link
Author

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