簡體   English   中英

geopandas 無法識別多邊形中的點

[英]geopandas not recognizing point in polygon

我有兩個數據框。 一個具有建築物的多邊形(大約 70K),另一個具有可能位於或不在多邊形內的點(大約 100K)。 我需要確定一個點是否在多邊形內。

當我繪制兩個數據框(下面的示例)時,該圖顯示某些點在多邊形內,而其他點不在多邊形內。 但是,當我使用 .within() 時,結果表明沒有任何點在多邊形內。

我重新創建了“手動”創建一個多邊形和一個點的示例,而不是導入數據,在這種情況下,.within() 確實識別出該點在多邊形中。 因此,我認為我犯了一個錯誤,但我不知道在哪里。

示例:(為簡單起見,我將只發布對應於一個點和一個多邊形的部分。在這種情況下,每個數據框包含一個點或一個多邊形)

1) 使用導入的數據。 數據框 dmR 有點,數據框 dmf 有多邊形

import pandas as pd
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
from shapely import wkt
from shapely.geometry import Point, Polygon
plt.style.use("seaborn")

# I'm skipping the data manipulation stage and 
# going to the point where the data are used.

print(dmR)

               geometry
35  POINT (-95.75207 29.76047)

print(dmf)
               geometry
41964  POLYGON ((-95.75233 29.76061, -95.75194 29.760...

# Plot
fig, ax = plt.subplots(figsize=(5,5))
minx, miny, maxx, maxy = ([-95.7525, 29.7603, -95.7515, 29.761])
ax.set_xlim(minx, maxx)
ax.set_ylim(miny, maxy)
dmR.plot(ax=ax, c='Red')
dmf.plot(ax=ax, alpha=0.5)
plt.savefig('imported_data.png')

結果顯示該點在多邊形內 然而,

print(dmR.within(dmf))
35       False
41964    False
dtype: bool

2)如果我嘗試手動重新創建它,它將如下(可能有更好的方法來做到這一點,但我無法弄清楚):

# Get the vertices of the polygon to create it by hand
poly1 = dmf['geometry']
g = [i for i in poly1]
x,y = g[0].exterior.coords.xy
x,y

(array('d', [-95.752332508564, -95.75193554162979, -95.75193151831627, -95.75232848525047, -95.752332508564]),
 array('d', [29.760606530637265, 29.760607694859385, 29.76044470363038, 29.76044237518235, 29.760606530637265]))

# Create the polygon by hand using the corresponding vertices
coords = [(-95.752332508564, 29.760606530637265),
          (-95.75193554162979, 29.760607694859385),
          (-95.75193151831627, 29.7604447036303),
          (-95.75232848525047, 29.76044237518235),
         (-95.752332508564, 29.760606530637265)]
poly = Polygon(coords)

# Create point by hand (just copy the point from 1) above
p1 = Point(-95.75207, 29.76047)

# Create the GeoPandas data frames from the point and polygon
ex = gpd.GeoDataFrame()
ex['geometry']=[poly]
ex = ex.set_geometry('geometry')
ex_p = gpd.GeoDataFrame()
ex_p['geometry'] = [p1]
ex_p = ex_p.set_geometry('geometry')

# Plot and print
fig, ax = plt.subplots(figsize=(5,5))
ax.set_xlim(minx, maxx)
ax.set_ylim(miny, maxy)
ex_p.plot(ax=ax, c='Red')
ex.plot(ax = ax, alpha=0.5)
plt.savefig('by_hand.png')

在這種情況下,結果還顯示了多邊形中的點 然而,

ex_p.within(ex)
0    True
dtype: bool

它識別出該點在多邊形中。 感謝所有關於做什么的建議! 謝謝。

我不知道這是否是最有效的方法,但我能夠在 Python 中並使用 Geopandas 做我需要的事情。

我沒有使用point.within(polygon)方法,而是進行了空間連接( geopandas.sjoin(df_1, df_2, how = 'inner', op = 'contains') )這會產生一個包含點的新數據框位於多邊形內並排除不在多邊形內的多邊形。 可以在此處找到有關如何執行此操作的更多信息。

我認為您的坐標參考系統(crs)有些可疑。 我不能告訴dmr因為它沒有提供,但ex_p是一個簡單的幾何體,因為你從點生成它而不指定 crs。 您可以使用以下方法檢查 crs:

dmr.crs

讓我們假設它在 4326,然后它會返回:

<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich

在這種情況下,您需要首先使用以下命令為ex_p設置 CRS:

ex_p = ex_p.set_crs(epsg=4326)

如果你想動態繼承dmr的 crs 你也可以使用:

ex_p = ex_p.set_crs(dmr.crs)

設置 crs 后,您可以使用以下命令從一個 crs 重新投影到另一個 crs:

ex_p = ex_p.to_crs(epsg=3395)

有關該主題的更多信息: https : //geopandas.org/projections.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM