Last active
September 14, 2021 04:40
-
-
Save ghtmtt/53d8592eabd2fddc8617b40146075e59 to your computer and use it in GitHub Desktop.
Geocoding in QGIS Field Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OSGeo4W shell