简体   繁体   English

多边形 pandas 内的点

[英]Points within polygon pandas

Given a dataframe I did some Operations to get geometry through the geopandas :给定一个dataframe我做了一些操作来通过geopandas获取geometry

turin_point = 

id_easy ordinal latitude    longitude       epoch   day_of_week geometry
0   qw   1138.0  22.6433       2.6602    22:00:22   Friday      POINT (8.66020 44.64330)
1   qw   1139.0  22.6291       2.6595    22:01:22   Friday      POINT (8.65950 44.62910)
2   qw   1140.0  22.6146       2.6564    22:02:22   Friday      POINT (8.65640 44.61460)
3   qw   1141.0  22.6013       2.6657    22:03:22   Friday      POINT (8.66570 44.60130)
4   qw   1142.0  22.5866       2.6648    22:04:22   Friday      POINT (8.66480 44.58660)

Another data frame which is shapefile:另一个数据框是 shapefile:

border = 
zone    geometry
  12    POLYGON ((2.80190 22.96504, 2.80205 22.96483,2.80190 22.96504, 2.80205 22.96483))

Want to check points of turin_point within border shapefile.想要检查border shapefile 中的turin_point点。

My try:我的尝试:

import numpy as np import pandas as pd导入 numpy 作为 np 导入 pandas 作为 pd

import geopandas as gpd
from shapely.geometry import Point, Polygon


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

AttributeError: 'Polygon' object has no attribute 'x' AttributeError:“多边形”object 没有属性“x”

Try this, with your border df, create a geodf and a new column on turin_point试试这个,用你的边界 df,在 turin_point 创建一个 geodf 和一个新列

import geopandas as gpd

border_gdf = gpd.GeoDataFrame(border, geometry='geometry')


turin_point['inside'] = turin_point['geometry'].apply(border_gdf.contains)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM