![](/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.