简体   繁体   English

Python,pandas数据框,坐标的条件格式

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

I have got data frame witch has coordinates on it (recorded route). 我有数据框上有坐标(记录的路线)。 Data frame structure is something like this (has more columns): 数据框结构如下所示(具有更多列):

No Latitude Longitude Altitude Speed Course Date Time etc.. 没有纬度经度海拔速度课程日期时间等。

0 59.303758 18.078915 NaN 0.0 114.9 2017/04/01 13:21:48 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 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 2 59.303758 18.078915 -8.5 0.0 114.9 2017/04/01 13:21:50

.

.

and list goes on... 清单还在继续...

I'm trying to parse unwanted points from the data frame. 我正在尝试从数据框中解析不需要的点。 Example at picture. 图片示例。 Red line represents coordinate points from data frame, i want to get only points on the Greenish fields. 红线表示数据框中的坐标点,我只想获取绿色字段上的点。

Route 路线

Example code: 示例代码:

#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)
          ]

So the idea is that only the data, inside these predefined 2 North and 2 South points (coordinate points) are included in the new data frame. 因此,我们的想法是,仅将这些预定义的2个北点和2个南点(坐标点)内的数据包括在新数据框中。

With that code i managed to parse the data, but it was faraway from the North & South points (only half of the street was included). 使用该代码,我设法解析了数据,但是它离北和南点很远(仅包括一半的街道)。 So it did over parse it or something odd happened.. 所以它确实解析了它,或者发生了奇怪的事情。

Is there some better or efficient way to do this? 有什么更好或有效的方法可以做到这一点吗?

The rectangle isn't aligned with longitude and latitude, so you can't use your simple long/lat check. 矩形未与经度和纬度对齐,因此无法使用简单的经/纬度检查。 A simple way to do this would be to consider a line from a given longitude/latitude, and extend it several miles (Some amount much larger than the rectangle) in a random direction (Probably a cardinal direction for ease). 一种简单的方法是从给定的经度/纬度考虑一条线,然后将其沿随机方向(为方便起见,可能是基数方向)延伸数英里(比矩形长一些)。

Then, write an intersect function intersect(Point1, Point2, Point3, Point4) that returns true if Line(P1, P2) intersects Line(P1, P2). 然后,编写一个相交函数 intersect(Point1, Point2, Point3, Point4) ,如果Line(P1,P2)与Line(P1,P2) 相交 ,则该函数返回true。 Then, with your extended line, check how many edges of your bounding box that it intersects. 然后,用您的延长线检查边界框相交的边缘。 If the answer is one, then you're good, you're inside of the box. 如果答案是一个,那您就很好,您就在盒子里。

I did solve this following way.. 我确实按照以下方式解决了..

First i created Geopandas Dataframe and used Shapely to create polygon. 首先,我创建了Geopandas Dataframe,并使用Shapely创建了多边形。 Then i added the polygon to the dataframe. 然后我将多边形添加到数据框。 Also added location to correspond the polygon. 还添加了与多边形对应的位置。

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'

Then i made copy from the Pandas DataFrame to Geopandas Dataframe. 然后,我从Pandas DataFrame复制到Geopandas Dataframe。

df2 = gpd.GeoDataFrame(df1)

After that i made new series to the DataFrame witch combined Latitude & Longitude series. 之后,我为结合了经度和纬度系列的DataFrame制作了新系列。

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

Then i used Geopandas Spatial Join. 然后我使用了Geopandas Spatial Join。 (op) doesn't matter in this cos i'm joining points to polygon. (op)无关紧要,因为我将点连接到多边形。 if these were lines it would make a difference. 如果这些是台词,那将会有所作为。

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

After this i was left with DataFrame with data in the location wanted. 之后,我离开了DataFrame,并在想要的位置放置了数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM