簡體   English   中英

Python,pandas數據框,坐標的條件格式

[英]Python, pandas data frame, conditional formatting for coordinates

我有數據框上有坐標(記錄的路線)。 數據框結構如下所示(具有更多列):

沒有緯度經度海拔速度課程日期時間等。

0 59.303758 18.078915 NaN 0.0 114.9 2017/04/01 13:21:48

1 59.303758 18.078915 -8.5 0.0 114.9 2017/04/01 13:21:49

2 59.303758 18.078915 -8.5 0.0 114.9 2017/04/01 13:21:50

清單還在繼續...

我正在嘗試從數據框中解析不需要的點。 圖片示例。 紅線表示數據框中的坐標點,我只想獲取綠色字段上的點。

路線

示例代碼:

#north
y_1n=59.33551 #point 1 latitude
x_1n=18.02649 #point 1 longitude
y_2n=59.33327 #point 2 latitude
x_2n=18.04500 #point 2 longitude
#south
y_1s=59.33478 #point 3 latitude
x_1s=18.02645 #point 3 longitude
y_2s=59.33246 #point 4 latitude
x_2s=18.04422 #point 4 longitude
#
test = df1[(df1['Latitude'] <= y_1n) & (df1['Latitude'] >= y_2n) &
            (df1['Latitude'] <= y_1s) & (df1['Latitude'] >= y_2s) &
            (df1['Longitude'] >= x_1n) & (df1['Longitude'] <= x_2n) &
            (df1['Longitude'] >= x_1s) & (df1['Longitude'] <= x_2s)
          ]

因此,我們的想法是,僅將這些預定義的2個北點和2個南點(坐標點)內的數據包括在新數據框中。

使用該代碼,我設法解析了數據,但是它離北和南點很遠(僅包括一半的街道)。 所以它確實解析了它,或者發生了奇怪的事情。

有什么更好或有效的方法可以做到這一點嗎?

矩形未與經度和緯度對齊,因此無法使用簡單的經/緯度檢查。 一種簡單的方法是從給定的經度/緯度考慮一條線,然后將其沿隨機方向(為方便起見,可能是基數方向)延伸數英里(比矩形長一些)。

然后,編寫一個相交函數 intersect(Point1, Point2, Point3, Point4) ,如果Line(P1,P2)與Line(P1,P2) 相交 ,則該函數返回true。 然后,用您的延長線檢查邊界框相交的邊緣。 如果答案是一個,那您就很好,您就在盒子里。

我確實按照以下方式解決了..

首先,我創建了Geopandas Dataframe,並使用Shapely創建了多邊形。 然后我將多邊形添加到數據框。 還添加了與多邊形對應的位置。

import geopandas as gpd
from shapely.geometry import Point, Polygon, LineString
polygon = gpd.GeoDataFrame()
coord = [(18.02649,59.33551),(18.04500,59.33327),(18.02645,59.33478), 
         (18.04422,59.33246)]

polygon.loc[0, 'geometry'] = Polygon(coord)
polygon.loc[0, 'Location'] = 'Fleminggatan'

然后,我從Pandas DataFrame復制到Geopandas Dataframe。

df2 = gpd.GeoDataFrame(df1)

之后,我為結合了經度和緯度系列的DataFrame制作了新系列。

df2['geometry'] = [Point(xy) for xy in zip(df2.Longitude, df2.Latitude)]

然后我使用了Geopandas Spatial Join。 (op)無關緊要,因為我將點連接到多邊形。 如果這些是台詞,那將會有所作為。

df3 = gpd.sjoin(df2,polygon, how='inner', op='intersects')

之后,我離開了DataFrame,並在想要的位置放置了數據。

暫無
暫無

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

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