【问题标题】:Make geopandas DataFrame plot on top a raster plot fit correctly使栅格图顶部的 geopandas DataFrame 图正确拟合
【发布时间】:2018-05-20 16:55:30
【问题描述】:

我正在尝试在同一图中this website 的 OSMap.tif 文件顶部绘制 Pumps.shp 数据。

我尝试使用 rasterio.plot() 和 geopandas.plot() 方法,以及 matplotlibs 子图。

问题是绘图不匹配,光栅文件被绘制在两个轴的范围(0,1000)内,而 shp 被绘制在实际坐标范围内(x 轴和周围大约 50000) .

两个对象的crs相等,坐标在同一范围内。为什么是这样?我做错了什么?

这是我的代码

   import rasterio as rast
   import rasterio.plot as rsplot
   import geopandas as gpd
   src=rast.open("OSMap.tif")
   data=gpd.read_file("Pumps.shp")
   fig,ax=plt.subplots()
   rsplot.show(src,ax=ax)
   data.plot(ax=ax)
   plt.show()

这是调用 src.bounds 的结果:

边界框(左=528765.0,下=180466.0,右=529934.0,上=181519.0)

这是 data.bounds 的结果

(528765.0, 180466.0, 529934.0, 181519.0)

这是两者的 crs:

CRS({'lon_0': -2, 'y_0': -100000, 'k': 0.9996012717, 'lat_0': 49, 'proj': 'tmerc', 'wktext': True, 'datum': 'OSGB36', 'no_defs': True, 'x_0': 400000, 'units': 'm'})

【问题讨论】:

  • 我在您链接的页面上找不到这两个输入文件。
  • 它在第一个 zip 上,上面写着:一个 zip 文件,其中矢量数据为 Shapefile... @ImportanceOfBeingErnest
  • 您使用的是哪个版本的rasterio?并不是说这似乎特别可能是问题,而是rasterio.plot.show 仅开始使用 0.32 中的栅格范围。
  • @jdmcbr 我使用的是 0.36 版

标签: python matplotlib geopandas rasterio


【解决方案1】:

rasterio 0.36.0 也有同样的问题。我首先尝试翻译和缩放栅格,但更喜欢翻译 shapefile。

我的代码如下:

import geopandas as gpd
import matplotlib.pyplot as plt
import rasterio

image = rasterio.open('input.tif') # with tgw world file
shapefile = gpd.read_file('input.shp')

# coordinates and scaling factors
scale_x = image.transform[1]
scale_y = image.transform[5]
x0 = image.transform[0]
y0 = image.transform[3]

# translates back shapefile
shapefile.geometry = shapefile.translate(-x0, -y0)
shapefile.geometry = shapefile.scale(-1.0/scale_x, -1.0/scale_y, origin=(0, 0, 0))

# plots both elements
fig, ax = plt.subplots()
ax = rasterio.plot.show(image.read(), with_bounds=True, ax=ax)
shapefile.plot(ax=ax)

【讨论】:

    【解决方案2】:

    使用 matplotlib imshow 代替 rasterio show。将光栅的边界作为 imshow 的“范围”参数传递。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多