Geopandas – How to Fix Spatial Join Not Working Issue?

geopandaspython

I have two GeoDataframes – one has polygons, the other points.

import geopandas as gpd
from shapely.geometry import Point
area = gpd.GeoDataFrame.from_file('areas.shp')
print area.crs

{u'lon_0': -91.8666666667, u'datum': u'NAD83', u'y_0': 3000000, u'no_defs': True, u'proj': u'lcc', u'x_0': 6200000, u'units': u'm', u'lat_2': 77, u'lat_1': 49, u'lat_0': 63.390675}

Addresses were assigned geolocations through an API, so I assigned a CRS 4326, as that's the one most commonly used it seems.

location['geometry'] = location.Geolocation.apply(lambda x: Point(eval(x)))
gdf = gpd.GeoDataFrame(location, geometry='geometry', crs={'init': 'epsg:4326'})

Then I changed the area's CRS:

area = area.to_crs({'init': 'epsg:4326'})

And tried to merge the two dataframes

merged = gpd.sjoin(area, gdf, op='intersects')

But the result it gives me is Empty GeoDataFrame. I am not sure why, because the points definitely do fall inside polygons. What am I doing wrong?

Here are my files

https://www.dropbox.com/sh/9lioawgsor5y4wc/AABkaaFpjAjisQEVaLrFwSnsa?dl=0

I suspect that its because of the points that were somehow not properly processed into the right coordinate system maybe.

Best Answer

Looking at your data, the issue is in the property points that you've geocoded:

|                     Property Address |               Geolocation |                      geometry |
|--------------------------------------|---------------------------|-------------------------------|
| 1586 LORNE ST E,  Kamloops BC Canada | (50.673675,  -120.298973) | POINT (50.673675 -120.298973) |

When you've geocoded the addresses you've got latitude and longitude, which is fine, but you've created the points in that order. Latitude and Longitude are Y, X measures respectively. You'll have to swap the coordinates when you create the points in your Geolocation (and doing so works fine in terms of the spatial join).