如果地理点在多边形内,则提取多边形名称?

问题描述 投票:0回答:2

如果地理点在多边形?内,则提取多边形名称。我有两个数据集,一个是多边形名称和多边形,另一个是位置名称以及纬度和经度。

数据1(Geopandas数据框)

COMMUNITY NAME   POLYGON
New York         MULTIPOLYGON (((55.1993358199345 25.20971347951325, 
                 55.19385836251354 25.20134197109752.... 25.20971347951325)))
Chennai          MULTIPOLYGON (((65.1993358199345 22.20871347951325, 
                 55.19325836251354 15.20132197109752 .... 15.20971347951325)))        

数据2(数据框)

STOP NAME            LONGITUDE       LANGITUDE
Chennai main stop    55.307228       25.248844
Cabra stop           55.278824       25.205862
USA stop NY          55.069368       24.973946

如果数据2(stop_name)位于数据1(多边形)的内部,则需要提取多边形的名称。即。如果任何“纽约”中都存在USA Stop NY,则需要在data2的新列中添加名称。

示例代码:

from shapely.geometry import Point, Polygon
# Create Point objects
p1 = Point(55.230830, 25.128038)
p2 = Point(24.976567, 60.1612500)
# Create a Polygon
coords = [(55.199335819934504,25.209713479513255),(55.193858362513538,25.20134197109752),(55.187450889885667,25.195407028080979 )]
poly = Polygon(coords)
p1.within(poly)

更新1

数据1(KML转换为Json,Json转换为Dataframe)

 import geopandas as gpd
data_poly = gpd.read_file(path + "Data_community_file.geojson")
python python-3.x gis geopandas shapely
2个回答
0
投票

我发现了一篇有趣的文章,描述了如何从多边形中提取几何点。http://archived.mhermans.net/geojson-shapely-geocoding.html

 import json
    from shapely.geometry import shape, Point
    # depending on your version, use: from shapely.geometry import shape, Point

    # load GeoJSON file containing sectors
    with open('sectors.json') as f:
        js = json.load(f)

    # construct point based on lon/lat returned by geocoder
    point = Point(-122.7924463, 45.4519896)

    # check each polygon to see if it contains the point
for feature in js['features']:
    polygon = shape(feature['geometry'])
    if polygon.contains(point):
        print(feature)

它能够从geojson中提取匹配的多边形。问题 :如果我们使用数据框中的点,则其抛出错误

AttributeError: 'Series' object has no attribute '_geom'

0
投票

这里是上述问题的答案。

Install these two packages to avoid the "Error"

    #!pip install rtree
    #conda install -c conda-forge libspatialindex 

Polygon Data (GeoDataFrame)
data_poly = gpd.read_file("data.geojson")
# Readonly the required columns 
# Drop NAN

Location Data (GeoDataFrame)
bus = pd.read_Csv(busstop.csv)

#convert dataframe to geodatframe
gdf = geopandas.GeoDataFrame(
    bus, geometry=geopandas.points_from_xy(bus.stop_location_longitiude, bus.stop_location_latitiude))

#Output
joined_gdf = gpd.sjoin(gdf, data_poly, op='within')
© www.soinside.com 2019 - 2024. All rights reserved.