如果地质点位于多边形内部,是否提取多边形名称?

2024-06-11 18:48:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

数据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(停止名称)位于数据1(多边形)的内部,则需要提取多边形的名称。 即,如果美国站纽约出现在任何“纽约”中,则需要在数据2中的新列中添加名称

示例代码:

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")

Tags: 数据nameimport名称jsoncreatecoords多边形
2条回答

以上问题的答案如下

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')

我发现了一篇有趣的文章,介绍了如何从多边形中提取地质点。
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'

相关问题 更多 >