【问题标题】:Extract polygon name if the geo-point is inside polygon?如果地理点在多边形内,则提取多边形名称?
【发布时间】:2020-04-12 13:15:40
【问题描述】:

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

数据 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(多边形)里面,需要提取多边形的名称。 IE。如果 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")

【问题讨论】:

  • 您只是发布家庭作业或工作任务吗?你的问题到底出在哪里?你试过什么?
  • 好吧,让我们看看。你从哪里得到数据?文件,网站?你是如何访问它的?
  • 您是否有一些功能可以将其下载并放入文件或数据库中,或者您是否正在处理?
  • 你在使用 Pandas 吗?还是 JSON?
  • 或者 GeoPandas?

标签: python python-3.x gis geopandas shapely


【解决方案1】:

以上问题的答案如下。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 2010-10-28
      • 2013-10-03
      相关资源
      最近更新 更多