![](/img/trans.png)
[英]How to do area-weighted regridding of regular lat-lon data using python
[英]how to plot geotiff data in specific area (lat/lon) with python
我有一個帶有高程數據 init 的 geotiff 柵格數據集,我想在特定區域繪制它,例如 60°E - 70° E,70°S - 80°E。
我有一些來自這里的代碼,但是pcolormesh
似乎無法繪制我的 geotif。它都是紅色的。 圖片。 圖片由imshow
為真實圖片
當我嘗試使用以下代碼繪制圖時:
path = "F:\\Mosaic_h1112v28_ps.tif"
dataset = gdal.Open(path)
data = dataset.ReadAsArray()
x0, dx, dxdy, y0, dydx, dy = dataset.GetGeoTransform()
nrows, ncols = data.shape
londata = np.linspace(x0, x0+dx*ncols)
latdata = np.linspace(y0, y0+dy*nrows)
lons, lats = np.meshgrid(lonarray, latarray)
fig = plt.figure(figsize=(8, 8))
m = Basemap(projection='lcc', lon_0=67.5, lat_0=-68.5, height=950000,
width=580000, resolution='h')
m.drawcoastlines()
x, y = m(lons, lats)
然后我不知道如何繼續它。 我只想使用imshow
,但imshow
沒有指定區域(緯度/經度)。
我將非常感謝您的幫助。
這是我的解決方案。
from osgeo import gdal
pathToRaster = r'./xxxx.tif'
raster = gdal.Open(pathToRaster, gdal.GA_ReadOnly)
data = raster.GetRasterBand(1).ReadAsArray()
data = data[::-1]
kk = plt.pcolormesh(data,cmap = plt.cm.Reds,alpha = 0.45, zorder =2)
這是一個好問題,這是我的解決方案。
必需的軟件包: georaster及其依賴項(gdal等)。
可從http://dwtkns.com/srtm/下載用於演示目的的數據
import georaster
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
fig = plt.figure(figsize=(8,8))
# full path to the geotiff file
fpath = r"C:\\path_to_your\geotiff_file\srtm_57_10.tif" # Thailand east
# read extent of image without loading
# good for values in degrees lat/long
# geotiff may use other coordinates and projection
my_image = georaster.SingleBandRaster(fpath, load_data=False)
# grab limits of image's extent
minx, maxx, miny, maxy = my_image.extent
# set Basemap with slightly larger extents
# set resolution at intermediate level "i"
m = Basemap( projection='cyl', \
llcrnrlon=minx-2, \
llcrnrlat=miny-2, \
urcrnrlon=maxx+2, \
urcrnrlat=maxy+2, \
resolution='i')
m.drawcoastlines(color="gray")
m.fillcontinents(color='beige')
# load the geotiff image, assign it a variable
image = georaster.SingleBandRaster( fpath, \
load_data=(minx, maxx, miny, maxy), \
latlon=True)
# plot the image on matplotlib active axes
# set zorder to put the image on top of coastlines and continent areas
# set alpha to let the hidden graphics show through
plt.imshow(image.r, extent=(minx, maxx, miny, maxy), zorder=10, alpha=0.6)
plt.show()
結果圖:
編輯1
我最初的答案集中在如何使用底圖在最基本的投影上繪制簡單的geotiff圖像。 如果不訪問所有必需的資源(即geotiff文件),就不可能獲得更好的答案。
在這里,我嘗試改善答案。
我從整個世界的geotiff文件中剪了一小部分。 然后將其重新投影(扭曲)為要使用的Basemap()定義的LCC投影規范。 所有過程均使用GDAL軟件完成。 生成的文件名為“ lcc_2.tiff”。 使用此geotiff文件,使用以下代碼完成圖像的繪制。
最重要的部分是,geotiff文件必須與底圖使用的投影具有相同的坐標系(相同的投影)。
import georaster
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
fig = plt.figure(figsize=(8,8))
m = Basemap(projection='lcc', lon_0=67.5, lat_0=-68.5, \
height=950000, width=580000, resolution='h')
m.drawcoastlines()
m.fillcontinents(color='beige')
image = georaster.SingleBandRaster( "lcc_2.tiff", latlon=False)
plt.imshow(image.r, extent=image.extent, zorder=10, alpha=0.6)
plt.show()
輸出圖:
您可以使用 rioxarray
import rioxarray as rio
ds = rio.open_rasterio(path)
# Example lat lon range for subset
geometries = [
{
'type': 'Polygon',
'coordinates': [[
[33.97301017665958, -118.45830810580743],
[33.96660083660732, -118.37455741054782],
[33.92304171545437, -118.37151348516299],
[33.915042933806724, -118.42909440702563]
]]
}
]
clipped = ds.rio.clip(geometries)
clipped.plot()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.