本文整理匯總了Python中osgeo.gdal.GetDriverByName方法的典型用法代碼示例。如果您正苦於以下問題:Python gdal.GetDriverByName方法的具體用法?Python gdal.GetDriverByName怎麽用?Python gdal.GetDriverByName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類osgeo.gdal
的用法示例。
在下文中一共展示了gdal.GetDriverByName方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: make_tif
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def make_tif(path, tloffset=(0, 0), reso=(0.25, -0.25), rsize=(20, 10),
proj=SRS[0]['wkt'], channel_count=1, dtype=gdal.GDT_Float32):
"""Create a tiff files and return info about it"""
tl = ROOT_TL + tloffset
reso = np.asarray(reso)
fp = buzz.Footprint(tl=tl, rsize=rsize, size=np.abs(reso * rsize))
x, y = fp.meshgrid_spatial
x = np.abs(x) - abs(ROOT_TL[0])
y = abs(ROOT_TL[1]) - np.abs(y)
x *= 15
y *= 15
a = x / 2 + y / 2
a = np.around(a).astype('float32')
driver = gdal.GetDriverByName('GTiff')
dataset = driver.Create(path, rsize[0], rsize[1], channel_count, dtype)
dataset.SetGeoTransform(fp.gt)
dataset.SetProjection(proj)
for i in range(channel_count):
dataset.GetRasterBand(i + 1).WriteArray(a)
dataset.GetRasterBand(i + 1).SetNoDataValue(-32000.)
dataset.FlushCache()
return path, fp, a
示例2: writeRaster
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def writeRaster(arr, out_path, template):
no_data = 0
# First of all, gather some information from the template file
data = gdal.Open(template)
[cols, rows] = arr.shape
trans = data.GetGeoTransform()
proj = data.GetProjection()
# nodatav = 0 #data.GetNoDataValue()
# Create the file, using the information from the template file
outdriver = gdal.GetDriverByName("GTiff")
# http://www.gdal.org/gdal_8h.html
# GDT_Byte = 1, GDT_UInt16 = 2, GDT_UInt32 = 4, GDT_Int32 = 5, GDT_Float32 = 6,
outdata = outdriver.Create(str(out_path), rows, cols, 1, gdal.GDT_UInt32)
# Write the array to the file, which is the original array in this example
outdata.GetRasterBand(1).WriteArray(arr)
# Set a no data value if required
outdata.GetRasterBand(1).SetNoDataValue(no_data)
# Georeference the image
outdata.SetGeoTransform(trans)
# Write projection information
outdata.SetProjection(proj)
return arr
# raster to vector
示例3: CreateMultiBandGeoTiff
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def CreateMultiBandGeoTiff(OutPath, Array):
'''
Author: Jake Shermeyer
Array has shape:
Channels, Y, X?
'''
driver = gdal.GetDriverByName('GTiff')
DataSet = driver.Create(OutPath, Array.shape[2], Array.shape[1],
Array.shape[0], gdal.GDT_Byte,
['COMPRESS=LZW'])
for i, image in enumerate(Array, 1):
DataSet.GetRasterBand(i).WriteArray(image)
del DataSet
return OutPath
###############################################################################
示例4: createImageDS
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def createImageDS(filename, x_min, y_min, x_max, y_max, pixel_size, srs=None):
# Create the destination data source
x_res = int((x_max - x_min) / pixel_size) # resolution
y_res = int((y_max - y_min) / pixel_size) # resolution
ds = gdal.GetDriverByName('GTiff').Create(filename, x_res,
y_res, 1, gdal.GDT_Byte)
ds.SetGeoTransform((
x_min, pixel_size, 0,
y_max, 0, -pixel_size,
))
if srs:
# Make the target raster have the same projection as the source
ds.SetProjection(srs.ExportToWkt())
else:
# Source has no projection (needs GDAL >= 1.7.0 to work)
ds.SetProjection('LOCAL_CS["arbitrary"]')
# Set nodata
band = ds.GetRasterBand(1)
band.SetNoDataValue(0)
return ds
示例5: to_tiff
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def to_tiff(self, filename):
'''
geo.to_tiff(filename)
Saves GeoRaster as geotiff filename.tif with type datatype
If GeoRaster does not have datatype, then it tries to assign a type.
You can assign the type yourself by setting
geo.datatype = 'gdal.GDT_'+type
'''
if self.datatype is None:
self.datatype = gdal_array.NumericTypeCodeToGDALTypeCode(self.raster.data.dtype)
if self.datatype is None:
if self.raster.data.dtype.name.find('int') !=- 1:
self.raster = self.raster.astype(np.int32)
self.datatype = gdal_array.NumericTypeCodeToGDALTypeCode(self.raster.data.dtype)
else:
self.raster = self.raster.astype(np.float64)
self.datatype = gdal_array.NumericTypeCodeToGDALTypeCode(self.raster.data.dtype)
self.raster.data[self.raster.mask] = self.nodata_value
create_geotiff(filename, self.raster, gdal.GetDriverByName('GTiff'), self.nodata_value,
self.shape[1], self.shape[0], self.geot, self.projection, self.datatype)
示例6: make_raster
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def make_raster(in_ds, fn, data, data_type, nodata=None):
"""Create a one-band GeoTIFF.
in_ds - datasource to copy projection and geotransform from
fn - path to the file to create
data - NumPy array containing data to write
data_type - output data type
nodata - optional NoData value
"""
driver = gdal.GetDriverByName('GTiff')
out_ds = driver.Create(
fn, in_ds.RasterXSize, in_ds.RasterYSize, 1, data_type)
out_ds.SetProjection(in_ds.GetProjection())
out_ds.SetGeoTransform(in_ds.GetGeoTransform())
out_band = out_ds.GetRasterBand(1)
if nodata is not None:
out_band.SetNoDataValue(nodata)
out_band.WriteArray(data)
out_band.FlushCache()
out_band.ComputeStatistics(False)
return out_ds
示例7: open_raster
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def open_raster(filename, driver=None):
"""Open raster file, return gdal.Dataset
Parameters
----------
filename : string
raster file name
driver : string
gdal driver string
Returns
-------
dataset : gdal.Dataset
dataset
"""
dataset = gdal.OpenEx(filename)
if driver:
gdal.GetDriverByName(driver)
return dataset
示例8: write_geotiff
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def write_geotiff(tiff_path, data, upper_left_lon, upper_left_lat, step, AREA_OR_POINT='Point'):
"""
寫入geotiff。默認pixel is point,默認WGS84
"""
rows, columns = data.shape
bands = 1
pixel_width = step
pixel_height = -step
half_step = step / 2
upper_left_x = upper_left_lon - half_step
upper_left_y = upper_left_lat + half_step
driver = gdal.GetDriverByName('GTiff')
dataset = driver.Create(tiff_path, columns, rows, bands, gdal.GDT_Float32)
dataset.SetMetadataItem('AREA_OR_POINT', AREA_OR_POINT)
dataset.SetGeoTransform([upper_left_x, pixel_width, 0,
upper_left_y, 0, pixel_height])
# 獲取地理坐標係統信息,用於選取需要的地理坐標係統
sr = osr.SpatialReference()
sr.SetWellKnownGeogCS('WGS84')
dataset.SetProjection(sr.ExportToWkt()) # 給新建圖層賦予投影信息
dataset.GetRasterBand(1).WriteArray(data)
dataset.FlushCache() # 將數據寫入硬盤
dataset = None
示例9: createRaster
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def createRaster(self, srcraster, destfile, pixelType):
'''
Creates a raster file
srcraster: source raster
destfile: destination file
pixelType: raster pixel type (e.g byte)
'''
cols = srcraster.RasterXSize
rows = srcraster.RasterYSize
(xOrigin, yOrigin, pixelWidth, pixelHeight) = self.getGeoreferenceInfo(srcraster)
targetSR = self.getCRS(srcraster)
driver = gdal.GetDriverByName('GTiff')
outRaster = driver.Create(destfile, cols, rows, 3, pixelType)
outRaster.SetGeoTransform((xOrigin, pixelWidth, 0, yOrigin, 0, pixelHeight))
outRaster.SetProjection(targetSR.ExportToWkt())
return outRaster
示例10: createRaster
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def createRaster(self, srcraster, destfile, pixelType):
"""
Creates a raster file
srcraster: source raster
destfile: destination file
pixelType: raster pixel type (e.g byte)
"""
cols = srcraster.RasterXSize
rows = srcraster.RasterYSize
(xOrigin, yOrigin, pixelWidth, pixelHeight) = self.getGeoreferenceInfo(srcraster)
targetSR = self.getCRS(srcraster)
driver = gdal.GetDriverByName('GTiff')
outRaster = driver.Create(destfile, cols, rows, 3, pixelType)
outRaster.SetGeoTransform((xOrigin, pixelWidth, 0, yOrigin, 0, pixelHeight))
outRaster.SetProjection(targetSR.ExportToWkt())
return outRaster
示例11: write_data
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def write_data(self, data, fileName, colorTable):
fileName = os.path.join(self.outpath, fileName)
driver = gdal.GetDriverByName( "GTiff" )
dst_ds = driver.Create( fileName, self.RasterXSize, self.RasterYSize, 1, gdal.GDT_Byte, [ 'COMPRESS=DEFLATE' ] )
band = dst_ds.GetRasterBand(1)
if self.geotransform:
dst_ds.SetGeoTransform( self.geotransform )
if self.projection:
dst_ds.SetProjection( self.projection )
if colorTable:
print "Add colortable"
band.SetRasterColorTable(self.ct)
band.WriteArray(data, 0, 0)
if verbose:
print "Written", fileName
ds = None
示例12: main
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def main():
args = parse_arguments()
src_ds = gdal.Open(args.src_file[0])
driver = gdal.GetDriverByName("GTiff")
dst_ds = driver.CreateCopy(args.out_file[0], src_ds, 0)
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(float(args.position[0]), float(args.position[1]))
point.Transform(transform)
gt = [point.GetX(), float(args.pixelsize[0]), 0,
point.GetY(), 0, -float(args.pixelsize[1])]
dst_ds.SetGeoTransform(gt)
to_sys = osr.SpatialReference()
to_sys.ImportFromEPSG(to_srs)
dest_wkt = to_sys.ExportToWkt()
dst_ds.SetProjection(dest_wkt)
示例13: toGDAL
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def toGDAL(self):
'''Get GDAL memory driver dataset'''
w, h = self.size
n = self.nbBands
dtype = str(self.dtype)
if dtype == 'uint8': dtype = 'byte'
dtype = gdal.GetDataTypeByName(dtype)
mem = gdal.GetDriverByName('MEM').Create('', w, h, n, dtype)
#writearray is available only at band level
if self.isOneBand:
mem.GetRasterBand(1).WriteArray(self.data)
else:
for bandIdx in range(n):
bandArray = self.data[:,:,bandIdx]
mem.GetRasterBand(bandIdx+1).WriteArray(bandArray)
#write georef
if self.isGeoref:
mem.SetGeoTransform(self.georef.toGDAL())
if self.georef.crs is not None:
mem.SetProjection(self.georef.crs.getOgrSpatialRef().ExportToWkt())
return mem
示例14: fillNodata
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def fillNodata(self):
#if not self.noData in self.data:
if not np.ma.is_masked(self.data):
#do not process it if its not necessary
return
if self.IFACE == 'GDAL':
# gdal.FillNodata need a band object to apply on
# so we create a memory datasource (1 band, float)
height, width = self.data.shape
ds = gdal.GetDriverByName('MEM').Create('', width, height, 1, gdal.GetDataTypeByName('float32'))
b = ds.GetRasterBand(1)
b.SetNoDataValue(self.noData)
self.data = np.ma.filled(self.data, self.noData)# Fill mask with nodata value
b.WriteArray(self.data)
gdal.FillNodata(targetBand=b, maskBand=None, maxSearchDist=max(self.size.xy), smoothingIterations=0)
self.data = b.ReadAsArray()
ds, b = None, None
else: #Call the inpainting function
# Cast to float
self.cast2float()
# Fill mask with NaN (warning NaN is a special value for float arrays only)
self.data = np.ma.filled(self.data, np.NaN)
# Inpainting
self.data = replace_nans(self.data, max_iter=5, tolerance=0.5, kernel_size=2, method='localmean')
示例15: create_ds
# 需要導入模塊: from osgeo import gdal [as 別名]
# 或者: from osgeo.gdal import GetDriverByName [as 別名]
def create_ds(file_name, xsize, ysize, band_count, compress=True):
options = ['PHOTOMETRIC=RGB']
if compress:
options.append('COMPRESS=DEFLATE')
options.append('PREDICTOR=2')
datatype = gdal.GDT_UInt16
gdal_ds = gdal.GetDriverByName('GTIFF').Create(
file_name, xsize, ysize, band_count, datatype,
options=options)
return gdal_ds