我有一个经度和纬度客户列表,我想定义哪些客户在给定的多边形内。 但是我得到的结果与该多边形无关,而且是不正确的。
能请你帮忙吗?谢谢!
from shapely.geometry import Polygon
from shapely.geometry import Point
import pandas as pd
import geopandas as gpd
df=pd.read_csv("C:\\Users\\n.nguyen.2\\Documents\\order from May 1.csv")
geometry=[Point(xy) for xy in zip(df['customer_lat'],df['customer_lng'])]
crs={'init':'epsg:4326'}
gdf=gpd.GeoDataFrame(df,crs=crs,geometry=geometry)
gdf.head()
polygon= Polygon ([(103.85362669999994, 1.4090082), (103.8477709, 1.4051988), (103.84821190000002, 1.4029509), (103.84933950000004, 1.4012179), (103.85182859999998, 1.4001453), (103.85393150000004, 1.3986867), (103.85745050000001, 1.3962412), (103.85809410000002, 1.3925516), (103.85843750000004, 1.3901491), (103.8583946, 1.3870601), (103.8585663, 1.3838853), (103.8582659, 1.3812682), (103.85822299999997, 1.3792946), (103.85843750000004, 1.3777931), (103.85882370000002, 1.3748757), (103.86015410000005, 1.3719582), (103.8607978, 1.3700276), (103.86092659999998, 1.368097), (103.86036880000006, 1.3657372), (103.8593174, 1.3633562), (103.85852339999995, 1.3607605), (103.85745050000001, 1.3581005), (103.8571071, 1.355655), (103.85736459999998, 1.3520941), (103.85873790000007, 1.3483615), (103.86187100000006, 1.3456583), (103.86488409999993, 1.340689), (103.87096889999998, 1.3378933), (103.87519599999996, 1.3373354), (103.88178349999998, 1.3408963), (103.88508790000004, 1.3433418), (103.89186870000005, 1.3436426), (103.89742610000008, 1.342355), (103.91813279999997, 1.3805388), (103.91824964404806, 1.3813377489306), (103.91433759243228, 1.38607494841128), (103.91607279999994, 1.3895484), (103.91942029999996, 1.3940104), (103.92903330000001, 1.4009604), (103.9342689, 1.402076), (103.93289559999994, 1.4075675), (103.92534249999994, 1.4146035), (103.92517090000003, 1.4211246), (103.90972139999997, 1.4238704), (103.89942169999993, 1.4202666), (103.89744760000008, 1.4224117), (103.89315599999998, 1.425758), (103.88740540000003, 1.4285896), (103.88148309999995, 1.4328798), (103.87478829999998, 1.4331372), (103.85918850000007, 1.4249644), (103.85401679999995, 1.4114284), (103.85362669999994, 1.4090082)])
gdf['answer']=gdf['geometry'].within(polygon)
writer = pd.ExcelWriter("C:\\Users\\n.nguyen.2\\Documents\\order may define1.xlsx")
gdf.to_excel(writer, 'Sheet1', index=False)
writer.save()
结果都是假的。
原始数据:
结果:
答案 0 :(得分:1)
添加我的评论作为以后的参考。
您已按坐标顺序切换了经度和纬度。查看多边形和点的坐标。您生成的点的坐标为(纬度,经度),而多边形(纬度,经度)为。因此,这些点不在此多边形内。
geometry=[Point(xy) for xy in zip(df['customer_lng'],df['customer_lat'])]
相反,它将起作用。
为了使您的生活更轻松,geopandas具有用于从多边形points_from_xy()
(http://geopandas.org/gallery/create_geopandas_from_pandas.html?highlight=points_from_xy)创建点的辅助功能