0

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

Mamed
  • 1,102
  • 8
  • 23
  • You have two layer of points. Point within point does not make sense, it will never give you meaningful result. You want your border layer to be a polygon? – martinfleis Oct 08 '19 at 21:10
  • @martinfleis I want to check which points from `turin` are located in `border` and save them. – Mamed Oct 08 '19 at 21:12
  • But your border is a set of points, it has no area. If your set of points is supposed to be a Polygon, you have to generate it out of those points. – martinfleis Oct 08 '19 at 21:16
  • @martinfleis So I am asking this – Mamed Oct 08 '19 at 21:18
  • Not knowing the correct order of points, you may get incorrect polygon. But you can try `turin_final = Polygon([[p.x, p.y] for p in border_point.geometry])` instead your `turin_final` line. – martinfleis Oct 08 '19 at 21:29
  • @martinfleis I guess you have solved the issue. post it as the answer – Mamed Oct 08 '19 at 21:45

1 Answers1

2

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.

martinfleis
  • 7,124
  • 2
  • 22
  • 30