-1

I am a bit confused. I am trying to geocode coordinates of a point shapefile, but in long lat instead of lat long and add the address to a new shapefile. Any ideas?

import fiona
from geopy.geocoders import Nominatim
out=fiona.open('saint.shp')
redshape = fiona.open("six.shp")
geolocator = Nominatim(user_agent="my-ride")

with fiona.open('geo.shp', 'w', 'ESRI Shapefile', out.schema.copy(), crs=from_epsg(4326)) as output:
   for feature in redshape:
       coords = feature['geometry']['coordinates']  # here is the x, y pair of coordinates
       print(coords)
       location = geolocator.reverse(coords)
       output.write(feature)
5
  • 1
    Any shapefile with lat,lon coordinate order is corrupt, since the specification calls for X,Y order. You hardly need to geocode a shapefile, since the coordinates are already present. Are you trying to reverse geocode, turning coordinates into addresses?
    – Vince
    May 10, 2020 at 11:37
  • yes am trying to turn co-ordinates to address
    – stella
    May 10, 2020 at 12:05
  • 1
    You provided a code sample, but it doesn't attempt to do what you are asking. It doesn't even attempt to fix coordinate order. And you don't say what error was encountered or what problem exists.
    – Vince
    May 10, 2020 at 12:22
  • This was already answered here. May 12, 2020 at 3:25
  • Does this answer your question? Python function to get the co-ordinate pair of a point May 12, 2020 at 3:25

1 Answer 1

0

Simply reverse the order of the coordinates of the shapefile

import fiona
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="me", timeout=10)
redshape = fiona.open("points_geopy.shp")
# coordinates of the first feature (x,y = long,lat)
redshape[0]['geometry']['coordinates']
(-75.17009743393821, 39.9427660880249)
# iterate 
for feature in redshape:
    # reverse the coordinates x,y to y,x
    coords = feature['geometry']['coordinates'][::-1]
    location = geolocator.reverse(coords)
    print(coords, location.address)
(39.9427660880249, -75.17009743393821) 723, South Chadwick Street, Hawthorne, South Philadelphia, Philadelphia, Philadelphia County, Pennsylvania, 19146, United States of America
(39.941477314134396, -75.166112027818) Tindley Temple United Methodist Church, 750, South Broad Street, Hawthorne, South Philadelphia, Philadelphia, Philadelphia County, Pennsylvania, 19146, United States of America
(39.9430681055253, -75.1634826347411) South 13th Street, Hawthorne, South Philadelphia, Philadelphia, Philadelphia County, Pennsylvania, 19148, United States of America
(39.9445279530865, -75.16772651013079) 1538, Naudain Street, Rittenhouse Square, Philadelphia, Philadelphia County, Pennsylvania, 19146, United States of America
...

With GeoPandas (use Fiona)

import geopandas as god
df = gpd.read_file("points_geopy.shp")
# reverse the coordinates x,y to y,x
df["geom"] = df.geometry.y.map(str) + ',' + df.geometry.x.map(str)
df["location"] = df.apply(lambda row:geolocator.reverse(row['geom']), axis=1)
df.head
              geom                            geometry                         location
0   39.9427660880249,-75.17009743393821 POINT (-75.17010 39.94277)  (723, South Chadwick Street, Hawthorne, South ...
1   39.941477314134396,-75.166112027818 POINT (-75.16611 39.94148)  (Tindley Temple United Methodist Church, 750, ...
2   39.9430681055253,-75.1634826347411  POINT (-75.16348 39.94307)  (South 13th Street, Hawthorne, South Philadelp...
3   39.9445279530865,-75.16772651013079 POINT (-75.16773 39.94453)  (1538, Naudain Street, Rittenhouse Square, Phi...
4   39.9414861490123,-75.17186304554191 POINT (-75.17186 39.94149)  (Marian Anderson Community Center, Catharine S...

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