如果地理点在多边形内,则提取多边形名称?
Extract polygon name if the geo-point is inside polygon?
如果地理点在多边形内,则提取多边形名称?。我有两个数据集,一个带有多边形名称和多边形,另一个带有位置名称和经纬度。
数据 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 存在于任何 "New York" 中,则需要在 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")
我发现了一篇有趣的文章,描述了如何从多边形中提取地理点。
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'
以上问题的答案如下。
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')
如果地理点在多边形内,则提取多边形名称?。我有两个数据集,一个带有多边形名称和多边形,另一个带有位置名称和经纬度。
数据 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 存在于任何 "New York" 中,则需要在 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")
我发现了一篇有趣的文章,描述了如何从多边形中提取地理点。
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'
以上问题的答案如下。
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')