2

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.

  • 1
    Have you tried plotting them to make sure they overlap? geopandas.org/mapping.html#maps-with-layers – BERA Jul 21 '18 at 7:19
  • If you can post some sample data perhaps? – om_henners Jul 21 '18 at 23:50
  • @BERA, I have, the points for some reason are not plotting right, that probably is my problem there. – user4718221 Jul 25 '18 at 3:29
  • 1
    @om_henners, I just added them to the original question – user4718221 Jul 25 '18 at 3:29
2

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).

  • Thank you for spotting that, I would've never guessed that is where the mistake was. But now I'm facing a problem with swapping the coordinates. Below is the script I wrote that is for some reason not functioning. for u in gdf['Geolocation']: u = u.replace('(', '') u = u.replace(')', '') a, b = u.split(',') correct = [b, a] correct = gdf['Geolocation_correct'] – user4718221 Aug 6 '18 at 6:00

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.