简体   繁体   English

如何重新投影和区域匹配netCDF和shapefile

[英]How to reproject and area match a netCDF and shapefile

I am new to GDAL and just getting my feet wet. 我是GDAL的新手,只是弄湿了我的脚。

I'm trying to compare rasters stored in netCDFs to shapefiles. 我正在尝试将存储在netCDF中的栅格与shapefile进行比较。 The shapefiles are sub-sections of the area covered by the netCDFs and the datasets use slightly different projections. shapefile是netCDF覆盖区域的子部分,数据集使用略有不同的投影。 I convert the shapefile dataset to the netCDF projection. 我将shapefile数据集转换为netCDF投影。 The netCDF file contains the raster array and 1d arrays for lat, lon, x, y. netCDF文件包含栅格阵列和lat,lon,x,y的1d阵列。

Right now, my code uses gdal.RasterizeLayer to rasterize a shapefile into a tiff, and then gdal.ReprojectImage to reproject that into a new tiff. 现在,我的代码使用gdal.RasterizeLayer将shapefile光栅化为tiff,然后使用gdal.ReprojectImage将其重新投影为新的tiff。 My issue is that I cannot figure out how to determine the extents of the second tiff - which I need to select the subsection of the netCDF data. 我的问题是我无法弄清楚如何确定第二次tiff的程度-我需要选择netCDF数据的小节。

Here is the relevant sections of my code: 这是我的代码的相关部分:

#Extract projection information
obs_driver = ogr.GetDriverByName('ESRI Shapefile')  
obs_dataset = obs_driver.Open(obsfiles[0])
layer = obs_dataset.GetLayer()
obs_proj = layer.GetSpatialRef()
mod_proj = obs_proj.SetProjParm('parameter',90)   #Hardcode param difference
xmin,xmax,ymin,ymax = layer.GetExtent()   #Extents pre-reproject

Rasterizing 光栅化

tiff1 = gdal.GetDriverByName('GTiff').Create('temp1.tif', 1000, 1000, 1, gdal.GDT_Byte)
tiff1.SetGeoTransform((xmin, pixelWidth, 0, ymin, 0, pixelHeight))
gdal.RasterizeLayer(tiff1,[1],layer,options = ['ATTRIBUTE=attribute'])

Re-projecting 重新投影

dst1 = gdal.GetDriverByName('GTiff').Create('temp3.tif', 1000, 1000, 1, gdal.GDT_Byte)
dst1.SetGeoTransform((xmin, pixelWidth, 0, ymin, 0, pixelHeight))
dst1.SetProjection(mod_proj.ExportToWkt())
gdal.ReprojectImage(gdal.Open('./temp1.tif'), dst1, obs_proj.ExportToWkt(), mod_proj.ExportToWkt(), gdalconst.GRA_Bilinear)

And convert raster to array for point-by-point comparison 并将栅格转换为数组以进行逐点比较

import matplotlib.pyplot as plt
obs = plt.imread('temp3.tif')

So now I need to get the extents of this array (in the new projection) so I can match it up with the right subsection of the netCDF array, and interpolate it to match. 因此,现在我需要获取该数组的范围(在新的投影中),以便可以将其与netCDF数组的正确子部分进行匹配,并对其进行插值以进行匹配。

EDIT:Now I am thinking that I need to individually transform the extents and use that to redefine the GeoTransform for the projection conversion. 编辑:现在,我在想我需要单独转换范围,并使用它来重新定义投影转换的GeoTransform。 Looking into it. 看着它。

Figured it out - you need to transform the extents to the new projection type before converting the data. 弄清楚了-在转换数据之前,您需要将范围转换为新的投影类型。

#Calculate new x,y positions of extents
tx = osr.CoordinateTransformation(obs_proj,mod_proj)
  #Projection corrected extent of area in question
corr_ext = [0,0,0,0]  #[min,max,ymin,ymax]
(corr_ext[0],corr_ext[2],ignore_z) = tx.TransformPoint(ext[0],ext[2]) #Ignore z componenet - data is 2D
(corr_ext[1],corr_ext[3],ignore_z) = tx.TransformPoint(ext[1],ext[3])

And then use those extents to set the GeoTransform of the object containing the reprojected data. 然后使用这些范围来设置包含重新投影的数据的对象的GeoTransform。 (The GeoTransform holds the spatial location information for the raster). (GeoTransform保留栅格的空间位置信息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM