2

I am trying to geocode a point shape. But first I want to determine the coordinate pair of the point shapefile before geocoding. The error I get is: **ERROR** :ValueError: Must be a coordinate pair or Point

redshape = fiona.open("murt.shp")
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="my-ride")
location = geolocator.reverse(redshape)
print(location.address)

Any ideas?

1 Answer 1

5

The geolocator.reverse() method is expecting a Point obejct or a tuple with two coordinates. However, you are passing redshape, which is a fiona.collection.Collection object with all the features in your shapefile. You have to iterate through the collection, extract the coordinates and then call the geolocator. Here is an example:

import fiona
from geopy.geocoders import Nominatim

redshape = fiona.open("murt.shp")
geolocator = Nominatim(user_agent="my-ride")

for feature in redshape:
    coords = feature['geometry']['coordinates']  # here is the x, y pair of coordinates
    location = geolocator.reverse(coords)
    print(location.address)

As have you already pointed out, the order of the coordinates for the geolocator.reverse() method is y,x and not x,y as I initially thought. Keeping this in mind, you can unpack the x and y values individually and pass them in a specific order. Note that they are still passed inside a tuple.

x, y = feature['geometry']['coordinates']
location = geolocator.reverse((y, x))
5
  • hi, the location = geolocator.reverse(coords) always outputted no location because co-ordinates are on x,y instead of y,x. any idea how i can reverse that
    – rachel
    Commented May 10, 2020 at 8:22
  • @rachel I forgot you must pass the coordinates in that order to geolocator. Check the edit I just made. Commented May 10, 2020 at 16:01
  • ok, please i have a question to ask. i have to point shapefile representing towns within a state and i want find the distance between them. so my question is since points are in different shapefile how do i merge them so they would be in the same shapefile before computing the linestring or is it possible to compute the linestring without merging both point shapefiles
    – rachel
    Commented May 10, 2020 at 16:08
  • 1
    If you don't want to use something like ArcGIS or QGIS to merge the shapefiles beforehand, you can open them with Python (e.g. geopandas) and merge them in memory before calculating the linestring. Before going into more detail, I encourage you to ask this as a new question (if you don't find anything similar on this site, of course) as it is a completely different question from the original. Commented May 10, 2020 at 16:15
  • ok thanks i will do that
    – rachel
    Commented May 10, 2020 at 16:16

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.