Skip to main content
5 of 6
Tiny adjustments
Taras
  • 34k
  • 4
  • 73
  • 148

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.

Taras
  • 34k
  • 4
  • 73
  • 148