簡體   English   中英

柵格和形狀文件未使用 Geopandas、Rasterio 和 Contextily 排列

[英]Raster and Shapefiles not lining up using Geopandas, Rasterio, and Contextily

我試圖讓 DEM 柵格與 Python 中的 shapefile 對齊,但無論我做什么它都不會顯示。 這是實驗室練習,整個 rest 練習都依賴於這些排列,因為我將從柵格和多邊形圖層中提取數據到點圖層。

我知道如何在 ArcGIS 中“手動”完成所有這些,但練習的重點是使用 R 或 Python(教授用 R 做了一個例子,但我們可以使用任何一個,我一直在學習 Python 過去的一對幾個月的工作項目)。 在 class 的筆記中,他說這兩個文件都在 EPSG 3847 中,但是 shapefile 缺少 CRS,所以我在 geopandas 中添加了 CRS。

DEM 似乎是 EPSG 3006(盡管它應該是 3847),所以我嘗試將它轉換為 EPSG 3847,但它仍然沒有出現。 因此,我嘗試換一種方式將 shapefile 轉換為 EPSG 3006,這也無濟於事。

import contextily as cx  
import geopandas as gpd  
import rasterio  
from rasterio.plot import show  
from rasterio.crs import CRS  
from rasterio.plot import show as rioshow  
import matplotlib.pyplot as plt  
#data files
abisveg = gpd.read_file(r'/content/drive/MyDrive/Stackoverflow/Sweden/abisveg_polygon.shp')  
abisveg_3847 = abisveg.set_crs(epsg = 3847)  

abisveg_3006 = abisveg_3847.to_crs(epsg = 3006)  

src = rasterio.open(r'/content/drive/MyDrive/Stackoverflow/Sweden/nh_75_6.tif')  
DEM = src.read()  
### creating plot grid  
fig = plt.figure(figsize = (20,20), constrained_layout = True)  
gs = fig.add_gridspec(1,3)  
ax1 = fig.add_subplot(gs[0,0])  
ax2 = fig.add_subplot(gs[0,1], sharex = ax1, sharey = ax1)  
ax3 = fig.add_subplot(gs[0,2], sharex = ax1, sharey = ax1)  

### Plot 1 - Basemap Only  
abisveg_3006.plot(ax = ax1, color = 'none')  
cx.add_basemap(ax1, crs = 3006)  
ax1.set_aspect('equal')  
ax1.set_title("Basemap of AOI")  


### Plot 2 - DEM  
# abisveg_3847.plot(ax = ax2, color = 'none')  
show(DEM, ax=ax2, cmap = "Greys")  
cx.add_basemap(ax2, crs = 3006)  
ax2.set_aspect('equal')  
ax2.set_title('Digitial Elevation Model of AOI')  


### Plot 3 - Vegetation Types  
abisveg_3006.plot(ax = ax3, column = "VEGKOD", cmap = "viridis")  
cx.add_basemap(ax3, crs = 3006)  
ax3.set_aspect('equal')  
ax3.set_title("Vegetation Types")  

3 面板 map 缺少 DEM: https://i.imgur.com/taG2U9Q.jpg

嘗試 plot Matplotlib 中的文件沒有用,b/c 它們根本不對齊。 我在上下文中使用底圖,並將底圖 CRS 設置為 EPSG 3847(或 3006,具體取決於我使用的 GIS 文件版本)。 無論投影如何,shapefile 都會顯示在正確的位置,但不會顯示光柵。 奇怪的是,如果我在 ArcGIS 中打開所有內容,它們都會正確排列。

如果我 plot 只是 DEM 本身,它就會顯示出來,盡管我不知道它在地球上的哪個地方繪制。

fig = plt.figure(figsize = (10,10), constrained_layout = True)  
show(DEM, cmap = "Greys")  

DEM 本身: https://i.imgur.com/KyYu7jc.jpg

我在這里的 Colab 筆記本中有我的代碼:

https://colab.research.google.com/drive/1VAZ3dgf0QS2PPBOl8KJ2FXtB2oRj0qJ8?usp=share_link

文件在這里:

https://drive.google.com/drive/folders/1t-xvpIcLOIR9uYXOguJ7KyKqt7wuYSNc?usp=share_link

你可以試一試 EOmaps...它使用 matplotlib/cartopy 進行繪圖並處理將數據和形狀重新投影到 plot-crs

from pathlib import Path
from eomaps import Maps
import geopandas as gpd

p = Path(r"path to the data folder")
# read shapefile
abisveg = gpd.read_file(p / 'abisveg_polygon.shp').set_crs(epsg = 3847)

# create a map in epsg=3006
m = Maps(crs=3006, figsize=(10, 8))
# add stamen-terrain basemap
m.add_wms.OpenStreetMap.add_layer.stamen_terrain()
# plot shapefile (zorder=2 to be on top of the DEM)
m.add_gdf(abisveg, column=abisveg.VEGKOD, cmap="viridis", ec="k", lw=0.2, alpha=0.5, zorder=2)
# plot DEM
m2 = m.new_layer_from_file.GeoTIFF(p / "nh_75_6.tif", cmap="Greys", zorder=1)

m.ax.set_extent((589913.0408156103, 713614.6619114348, 7495264.310799116, 7618965.93189494),
                Maps.CRS.epsg(3006))

在此處輸入圖像描述

暫無
暫無

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

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