1

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
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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