2

I'm using python library GeoPandas which bundles Shapely, Fiona, PyProj and others. I'm importing a countries shapefile to project lat/lon coordinates from a Google Maps API to find the country that contains that location.

Problem is the country returned is not correct. The code below looks up a location in Cincinnati, OH, and the lookup returns Antarctica. Both my point data and country data appear to be using decimal latitude and longitude. What am I doing wrong?

Here the code:

import geopandas as gpd
from shapely.geometry import Point

# Cincinnati, OH using Google Maps
pt = Point(39.0972618663624, -84.5065838098526) 

# countries shapefile from 
# http://thematicmapping.org/downloads/world_borders.php
data = gpd.read_file('TM_WORLD_BORDERS_SIMPL-0.3.shp')

# loop over countries
for index, row in data.iterrows():
    poly = row['geometry']
    if poly.contains(pt):
        print row  # prints Antarctica
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Joel L
  • 41
  • 2

1 Answers1

2

Ok, I feel pretty silly. Apparently I need to pass the coordinates into the Point in reverse order. Longitude, then Latitude. The follow appears to work correctly:

# Cincinnati, OH using Google Maps
pt = Point(-84.5065838098526, 39.0972618663624) 

# countries shapefile from 
# http://thematicmapping.org/downloads/world_borders.php
data = gpd.read_file('TM_WORLD_BORDERS_SIMPL-0.3.shp')

# loop over countries
for index, row in data.iterrows():
    poly = row['geometry']
    if poly.contains(pt):
        print row  # prints Antarctica
Joel L
  • 41
  • 2