4

I would like to add addresses to the attributes of a series of points using the Graphical Modeller.

I can use the GeoCoding Plugin to get the address by clicking manually but would like it to be automatic. Any ideas?

1 Answer 1

7

There is a possibility of using a "Field calculator" inside the Graphical Modeler.

Let's assume there is a point layer 'test' with some features in it, see the image below

input

Thereafter I created a simple model for reverse geocoding as following:

model

where a "Field calculator" possess the following parameters

field_calc

In the 'Formula' tab apply this expression:

reverse_geocoding(y(transform($geometry,@project_crs,'EPSG:4326')), x(transform($geometry,@project_crs,'EPSG:4326')))

A custom reverse_geocoding() function defined as

import geopy
from geopy.geocoders import Nominatim
from geopy.point import Point
from qgis.core import *
from qgis.gui import *

locator = Nominatim(user_agent="MyReverseGeocoder", timeout=10)

@qgsfunction(args='auto', group='Custom')
def reverse_geocoding(lat, lon, feature, parent):
    try:
        location = locator.reverse(Point(lat, lon))
        return location.raw['display_name']
    except:
        pass

Here the Nominatim geocoder (free to choose) from the GeoPy geocoding Python library was used, for more details, please check the documentation. Also, coordinates of point features should be transmitted as a pair, otherwise, you may get this error ValueError: Must be a coordinate pair or Point. Therefore from geopy.point import Point was used.

Mind that additionally installation of geopy library inside the QGIS is required, the how-to process described in Installing 3rd party python libraries for QGIS on Windows, Installing Python packages in QGIS 3 (for Windows), and Install QGIS through OSGeo4W (also applicable for Python packages).

And afterwards, I ran the model with run (F5 as shortcut) and got the output

result

P.S. As you can see I used 'display_name' from the output dictionary location.raw, however, other key-value pairs (e.g. location.raw['address']) can be extracted from the full geocoder’s response for this result.


Another solution that may inspire you with, described in @csk's answer. So, you may try to find and download the reference point data with addresses for your area, and then simply use the "Join attributes by nearest" geoalgorithm in the Graphical Modeler.


Some hints on how to perform reverse geocoding (for QGIS versions 3.18 and higher) using QGIS's "native" functions, can be found in the @ThomasG77's implementation. Discussion about native geocoding including the reverse geocoding described in Add interface API for geocoders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.