TransWikia.com

Geocoding address involving non-ASCII characters in PyQGIS

Geographic Information Systems Asked on November 30, 2020

I have installed the "GeoCoding" Plugin in QGIS and it worked well even though I have used non-ASCII characters to geocode (i.e. Hacettepe Üniversitesi). However, when I tried to do the same in PyQGIS with the following code I got the error:

"UnicodeEncodeError: ‘ascii’ codec can’t encode character"

The problem is that the returned address contains non-ASCII characters. Any ideas to solve this?

from geopy.geocoders import Nominatim

#Next, we'll create our geocoder:
geocoder = Nominatim()

#geocode an address:
location = geocoder.geocode("Hacettepe Üniversitesi")

print location

Another relevant question: I rely on Joel Lawhead’s book (‘QGIS Python Programming Cookbook’) and the import statement relevant to this example in the book is:

from GeoCoding.geopy.geocoders import Nominatim

However, this statement gave the following error

ImportError: No module named geopy.geocoders

Is there a source we could follow to make these import statements without a try-and-guess?

One Answer

Here is one way to do it:

  • I used Ubuntu with Jupyter notebook, but it should work with any Jupyter environment with Python 3

  • pip install geopy

    after install, make that line as comment to avoid reinstall

  • from geopy.geocoders import Nominatim

  • geocoder = Nominatim(user_agent="http")

    The line above sets user agent to avoid the error you were getting as without agent, it violates this https://operations.osmfoundation.org/policies/nominatim/

  • location = geocoder.geocode("Hacettepe Üniversitesi")

  • print(location)

Result is

Hacettepe Üniversitesi Beytepe Kampüsü, Hacettepe-Beytepe Kampüs Yolu, Bilkent, Üniversiteler Mahallesi, Çankaya, Ankara, İç Anadolu Bölgesi, 06800, Türkiye

Added link to colab where you can see it in action 
https://colab.research.google.com/drive/1GX29YuMwsehPGL-WcQkHtyh-0w97rpoN?usp=sharing

enter image description here

Answered by ivan on November 30, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP