Mamed
Mamed

Reputation: 772

Get values only within the shape geopandas

I have a big dataset with geo data. I want to extact data only related to the part of the city. So I need to create a shape and check whether or not my point is there.

I tried to work with this answer:

POINTS

1)

turin.head() = 

       latitude     longitude   
    0   44.9125     7.7432  
    21  45.0764     7.5249  
    22  45.0764     7.5249  
    23  45.0755     7.5248 
    24  45.0718     7.5236 

2)

geometry = [Point(xy) for xy in zip(turin.longitude,turin.latitude)]
turin_point = gpd.GeoDataFrame(turin,crs=crs,geometry=geometry)
turin_point.head()


    latitude    longitude   geometry
0   44.9125     7.7432  POINT (7.74320 44.91250)
21  45.0764     7.5249  POINT (7.52490 45.07640)
22  45.0764     7.5249  POINT (7.52490 45.07640)
23  45.0755     7.5248  POINT (7.52480 45.07550)
24  45.0718     7.5236  POINT (7.52360 45.07180)

BORDERS

1)

border.head()

    longitude   latitude    
0   7.577835    45.041828   
1   7.579849    45.039877   
2   7.580106    45.039628   
3   7.580852    45.038576   
4   7.580866    45.038556   

2)

geometry2 = [Point(xy) for xy in zip(border.longitude,border.latitude)]
border_point = gpd.GeoDataFrame(border,crs=crs,geometry=geometry2)
border_point.head() = 

        longitude   latitude    geometry
    0   7.577835    45.041828   POINT (7.57783 45.04183)
    1   7.579849    45.039877   POINT (7.57985 45.03988)
    2   7.580106    45.039628   POINT (7.58011 45.03963)
    3   7.580852    45.038576   POINT (7.58085 45.03858)
    4   7.580866    45.038556   POINT (7.58087 45.03856)

Then according to reply:

turin_final= border_point.geometry.unary_union
within_turin = turin_point[turin_point.geometry.within(turin_final)]

IndexError: too many indices for array

Upvotes: 0

Views: 1167

Answers (1)

martinfleis
martinfleis

Reputation: 7804

If you want to find points within your border, the border itself needs to be a Polygon, not another set of points. If coords of border points are in the correct order, you can try replacing last two lines with these:

from shapely.geometry import Polygon

turin_final = Polygon([[p.x, p.y] for p in border_point.geometry])
within_turin = turin_point[turin_point.geometry.within(turin_final)]

Often that is not the case, but as I understood from your comments, it has resolved your issue.

Upvotes: 2

Related Questions