Python GDAL: Write new raster using projection from old
来源:互联网 发布:网络感情骗术 台湾 编辑:程序博客网 时间:2024/06/15 01:49
http://gis.stackexchange.com/questions/57005/python-gdal-write-new-raster-using-projection-from-old
This is the routine I developed to convertISIS3 cubes to GTiffs. I expect a similar approach should work between anytypes of drivers (though I think the driver.Create() method might limit thechoice of output file).
import numpy as np
import gdal
from gdalconst import *
from osgeo import osr
# Function to read the original file's projection:
def GetGeoInfo(FileName):
SourceDS =gdal.Open(FileName, GA_ReadOnly)
NDV =SourceDS.GetRasterBand(1).GetNoDataValue()
xsize =SourceDS.RasterXSize
ysize =SourceDS.RasterYSize
GeoT =SourceDS.GetGeoTransform()
Projection= osr.SpatialReference()
Projection.ImportFromWkt(SourceDS.GetProjectionRef())
DataType =SourceDS.GetRasterBand(1).DataType
DataType =gdal.GetDataTypeName(DataType)
return NDV,xsize, ysize, GeoT, Projection, DataType
# Function to write a new file.
def CreateGeoTiff(Name, Array, driver, NDV,
xsize, ysize, GeoT, Projection, DataType):
if DataType== 'Float32':
DataType = gdal.GDT_Float32
NewFileName= Name+'.tif'
# Set nansto the original No Data Value
Array[np.isnan(Array)] = NDV
# Set upthe dataset
DataSet =driver.Create( NewFileName, xsize, ysize, 1, DataType )
#the '1' is for band 1.
DataSet.SetGeoTransform(GeoT)
DataSet.SetProjection(Projection.ExportToWkt() )
# Write thearray
DataSet.GetRasterBand(1).WriteArray( Array )
DataSet.GetRasterBand(1).SetNoDataValue(NDV)
returnNewFileName
# Open the original file
FileName = 'I29955002trim.cub' # This is the ISIS3 cube file
# It's aninfra-red photograph
# taken bythe 2001 Mars Odyssey orbiter.
DataSet = gdal.Open(FileName, GA_ReadOnly)
# Get the first (and only) band.
Band = DataSet.GetRasterBand(1)
# Open as an array.
Array = Band.ReadAsArray()
# Get the No Data Value
NDV = Band.GetNoDataValue()
# Convert No Data Points to nans
Array[Array == NDV] = np.nan
# Now I do some processing on Array, it's prettycomplex
# but for this example I'll just add 20 to eachpixel.
NewArray = Array + 20 # If only it were that easy
# Now I'm ready to save the new file, in the meantimeI have
# closed the original, so I reopen it to get theprojection
# information...
NDV, xsize, ysize, GeoT, Projection, DataType =GetGeoInfo(FileName)
# Set up the GTiff driver
driver = gdal.GetDriverByName('GTiff')
# Now turn the array into a GTiff.
NewFileName = CreateGeoTiff('I29955002trim',NewArray, driver, NDV,
xsize, ysize, GeoT, Projection, DataType)
And that's it. I can open both images inQGIS. And gdalinfo on either file shows that I have the same projectioninformation and georeferencing.
- Python GDAL: Write new raster using projection from old
- ORA-600 [16606] Error From Database Level Triggers Using :OLD or :NEW Syntax [ID 1089801.1]
- Mosaic/Mosaic To New Raster
- Write Excel files with Python using xlwt
- mysql 触发器 NEW OLD
- PLSQL Trigger :OLD :NEW
- HtmlParser 错误解决 character mismatch (new: [] != old: []) for encoding change from ...
- Adding new storage disks and Dropping old storage disks from OCR ,Vote diskgroup
- Odoo ORM API(七)- Porting from the old API to the new API
- Dome projection using a spherical mirror
- MySQL 4.1+ using old authentication
- Python 实现ARCGIS中raster to ascii
- AJAX - Old Dog, New Tricks?
- The old new thing翻译
- 触发器中 :new 和 :old
- old 10.10 to new linux
- :new与:old的用法
- tensorflow new/old version function
- 作业 2.30
- [OpenCV] -- Win8.1下配置OpenCV的Qt(MSVC2013编译器)开发环境
- 求一个整型数组中,只有一个数出现一次,其他的数都出现2次,求这个数?
- Scala语言
- UVA 315 Network (割点)
- Python GDAL: Write new raster using projection from old
- Hdu 三角形分区
- 计蒜客--第20题:跳跃游戏二
- action通过VM生成文件并导出
- jQuery的用途和作用
- nyoj41(三个数从小到大排序)
- MySQL max_allowed_packet设置及问题
- 第四周项目二 分数类的雏形
- 如何在java类中获取javaWeb的根路径