0

I am using geopandas to import a shapefile and then check if a point is within that shape. I am using the shapefile found in http://opendata.atlantaregional.com/datasets/cities-georgia?geometry=-87.766%2C33.036%2C-81.448%2C34.633 to get the shape of Atlanta. However, whenever I "ask" if a point is within that shape (the point is surely within the shape), the function returns False. I have tried creating a Polygon and then asking if a point is within that polygon and it works as expected.

I don't know what else to do to troubleshot or debug this issue.

I am doing all of this using a jupyter notebook. Here is the MWE: https://github.com/vcaballero-salle/mwe-shapefile

The code looks like (full code with imports can be found at the github repo, in the README.md):

plt.clf()
shape = gp.read_file(r'./Cities_Georgia/Cities_Georgia.shp')

atlanta_shape = shape[shape["Name"] == "Atlanta"]

atlanta_shape.plot()

plt.show()
plt.ion()


# In[10]:


def within_shape(df, shapes):
    in_shape = []
    for sh in shapes.geometry:
        within = df.within(sh)
        in_shape.append(within)
    return in_shape


print(within_shape(Point(33.751079, -84.443274), atlanta_shape)) # This returns [False]
  • Is't your point rather close to the South Pole? – user30184 Jan 20 '18 at 11:56
  • @user30184 the point is in the middle of Atlanta – vicaba Jan 20 '18 at 13:24
  • WKT takes coordinates in longitude-latitude order. Flip your coordinates and try again. – user30184 Jan 20 '18 at 14:30
  • @user30184 it is working now! Thank you! So, .shp is in WKT format? – vicaba Jan 20 '18 at 14:34
  • Your reference geometry that you give as "POINT(...)" is expressed as Well Known Text. – user30184 Jan 20 '18 at 16:24
1

You inverted the "Latitude" and "Longitude" when you create the point...

print(within_shape(Point(-84.443274, 33.751079), atlanta_shape)) #This returns [True]

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.