【问题标题】:Merging geodataframes in geopandas (CRS do not match)在 geopandas 中合并地理数据框(CRS 不匹配)
【发布时间】:2015-12-22 16:28:00
【问题描述】:

我正在尝试合并两个地理数据框(想查看每个点所在的多边形)。

下面的代码首先给我一个警告(“CRS does not match!”) 然后是一个错误(“RTreeError: Coordinates must not have minimums more than maximums”)。

那里到底出了什么问题? CRS是坐标系吗?如果是这样,为什么它们的加载方式不同?

import geopandas as gpd
from shapely.geometry import Point, mapping,shape
from geopandas import GeoDataFrame, read_file
#from geopandas.tools import overlay
from geopandas.tools import sjoin

print('Reading points...')
points=pd.read_csv(points_csv)
points['geometry'] = points.apply(lambda z: Point(z.Latitude, z.Longitude), axis=1)
PointsGeodataframe = gpd.GeoDataFrame(points)
print PointsGeodataframe.head()
print('Reading polygons...')
PolygonsGeodataframe = gpd.GeoDataFrame.from_file(china_shapefile+".shp")
print PolygonsGeodataframe.head()
print('Merging GeoDataframes...')
merged=sjoin(PointsGeodataframe, PolygonsGeodataframe, how='left', op='intersects')

#merged = PointsGeodataframe.merge(PolygonsGeodataframe, left_on='iso_alpha2', right_on='ISO2', how='left')
print(merged.head(5))

复制数据链接: Shapefile, GPS points

【问题讨论】:

  • 你能提供一个可重现的例子吗? (一些代码来制作重现问题的两个数据框)
  • CRS 确实是一个坐标参考系统。您可以使用 GeoDataFrame 的 .crs 属性进行检查。 PolygonsGeodataframe 将具有在 shapefile 中指定的 CRS,而 PointsGeodataframe 将没有 CRS。如果两者都有相同的CRS,你可以做PointsGeodataframe.crs = PolygonsGeodataframe.crs
  • @joris 代码有点棘手,因为我不知道如何重现 geopandas 用 shapefile 制作的“几何”列,但我已经编辑了问题以提供指向shapefile,以及我正在使用的简单 csv 的链接。
  • @joris using PointsGeodataframe.crs = PolygonsGeodataframe.crs 确实使警告消失了。然而,关于最小值大于最大值的错误仍然存​​在。

标签: pandas gis geopandas


【解决方案1】:

正如问题中的 cmets 所述,您可以通过手动设置 PointsGeodataframe.crs = PolygonsGeodataframe.crs 来消除 CRS does not match! 警告(假设两个数据集的 CRS 确实相同)。

但是,这并不涉及RTreeErrorpoints_csv 中可能缺少纬度/经度数据 - 在这种情况下,您最终会创建包含 NaN 值(即 Point(nan nan))的 Point 对象,这会继续导致 rtree 出现问题。我遇到了类似的问题,修复只是在加载 CSV 时过滤掉缺少坐标数据的行:

points=pd.read_csv(points_csv).dropna(subset=["Latitude", "Longitude"])

【讨论】:

    【解决方案2】:

    我将在这里添加一个答案,因为我最近一直在努力解决这个问题,在这里找不到一个很好的答案。 Geopandas documentation 有一个很好的例子来解决“CRS 不匹配”的问题。

    我从下面的文档中复制了整个代码块,但最相关的一行是这一行,其中to_crs() 方法用于重新投影地理数据框。您可以调用mygeodataframe.crs 查找每个数据帧的CRS,然后to_crs() 重新投影一个以匹配另一个,如下所示:

    world = world.to_crs({'init': 'epsg:3395'})
    

    简单地设置PointsGeodataframe.crs = PolygonsGeodataframe.crs 将阻止错误被抛出,但不会正确地重新投影几何体。

    完整的文档代码供参考:

    # load example data
    In [1]: world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
    
    # Check original projection
    # (it's Platte Carre! x-y are long and lat)
    In [2]: world.crs
    Out[2]: {'init': 'epsg:4326'}
    
    # Visualize
    In [3]: ax = world.plot()
    
    In [4]: ax.set_title("WGS84 (lat/lon)");
    
    # Reproject to Mercator (after dropping Antartica)
    In [5]: world = world[(world.name != "Antarctica") & (world.name != "Fr. S. Antarctic Lands")]
    
    In [6]: world = world.to_crs({'init': 'epsg:3395'}) # world.to_crs(epsg=3395) would also work
    
    In [7]: ax = world.plot()
    
    In [8]: ax.set_title("Mercator");
    

    【讨论】:

      猜你喜欢
      • 2020-05-16
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多