【问题标题】:Python, pandas data frame, conditional formatting for coordinatesPython,熊猫数据框,坐标的条件格式
【发布时间】:2018-08-03 15:23:17
【问题描述】:

我有数据框,上面有坐标(记录的路线)。 数据框结构是这样的(有更多的列):

没有纬度经度高度速度课程日期时间等。

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

.

.

列表还在继续……

我正在尝试从数据框中解析不需要的点。图片上的例子。红线代表数据框中的坐标点,我只想获取绿色字段上的点。

Route

示例代码:

#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 个南点(坐标点)内的数据被包含在新数据框中。

使用该代码,我设法解析了数据,但它远离南北点(仅包括一半街道)。所以它确实过度解析它或发生了一些奇怪的事情..

有没有更好或更有效的方法来做到这一点?

【问题讨论】:

  • df1 应该是单行吗?
  • df1 是 pandas DataFrame,具有多列和多行。

标签: python pandas gps coordinates


【解决方案1】:

矩形未与经度和纬度对齐,因此您不能使用简单的经度/纬度检查。这样做的simple way 将考虑一条来自给定经度/纬度的线,并将其沿随机方向(可能是基本方向)延伸几英里(比矩形大得多)。

然后,编写一个intersect function intersect(Point1, Point2, Point3, Point4),如果 Line(P1, P2) 与 Line(P1, P2) 相交,则返回 true。然后,用你的延长线,检查你的边界框有多少条边与它相交。如果答案是一个,那么你很好,你在盒子里。

【讨论】:

  • 这会在本初子午线或两极造成奇怪的边缘情况。我认为您不关心两极,但是如果您靠近本初子午线,请在经度上添加一些非常大的值,这样您就没有负数。您也可以将延伸设置为 500 英里(从而处理本初子午线 600 英里范围内的本初子午线情况),以免遇到大的矩形问题。
  • 是的,我确实尝试过这种方法,但对于我正在尝试做的事情来说似乎有点笨拙。但是你用这个想法指引我走向正确的道路。
【解决方案2】:

我确实通过以下方式解决了这个问题..

首先我创建了 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 空间连接。 (op) 在这个 cos 中并不重要,因为我将点连接到多边形。如果这些是线条,那会有所作为。

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

在此之后,我留下了 DataFrame,其中包含所需位置的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多