本文整理汇总了Python中osgeo.gdal.GDT_Float32方法的典型用法代码示例。如果您正苦于以下问题:Python gdal.GDT_Float32方法的具体用法?Python gdal.GDT_Float32怎么用?Python gdal.GDT_Float32使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类osgeo.gdal
的用法示例。
在下文中一共展示了gdal.GDT_Float32方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_tif
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [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 GDT_Float32 [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: test_gdal_create_dataset
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [as 别名]
def test_gdal_create_dataset(self):
testfunc = io.gdal.gdal_create_dataset
tmp = tempfile.NamedTemporaryFile(mode="w+b").name
with pytest.raises(TypeError):
testfunc("AIG", tmp)
from osgeo import gdal
with pytest.raises(TypeError):
testfunc(
"AAIGrid", tmp, cols=10, rows=10, bands=1, gdal_type=gdal.GDT_Float32
)
testfunc("GTiff", tmp, cols=10, rows=10, bands=1, gdal_type=gdal.GDT_Float32)
testfunc(
"GTiff",
tmp,
cols=10,
rows=10,
bands=1,
gdal_type=gdal.GDT_Float32,
remove=True,
)
示例4: write_geotiff
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [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
示例5: getNumpyType
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [as 别名]
def getNumpyType(self, pixelType = gdal.GDT_Byte):
'''
Translates the gdal raster type to numpy type
pixelType: gdal raster type
'''
if pixelType == gdal.GDT_Byte:
return numpy.uint8
elif pixelType == gdal.GDT_UInt16:
return numpy.uint16
elif pixelType == gdal.GDT_Int16:
return numpy.int16
elif pixelType == gdal.GDT_UInt32:
return numpy.uint32
elif pixelType == gdal.GDT_Int32:
return numpy.int32
elif pixelType == gdal.GDT_Float32:
return numpy.float32
elif pixelType == gdal.GDT_Float64:
return numpy.float64
示例6: getNumpyType
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [as 别名]
def getNumpyType(self, pixelType = gdal.GDT_Byte):
"""
Translates the gdal raster type to numpy type
pixelType: gdal raster type
"""
if pixelType == gdal.GDT_Byte:
return numpy.uint8
elif pixelType == gdal.GDT_UInt16:
return numpy.uint16
elif pixelType == gdal.GDT_Int16:
return numpy.int16
elif pixelType == gdal.GDT_UInt32:
return numpy.uint32
elif pixelType == gdal.GDT_Int32:
return numpy.int32
elif pixelType == gdal.GDT_Float32:
return numpy.float32
elif pixelType == gdal.GDT_Float64:
return numpy.float64
示例7: save_geotiff
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [as 别名]
def save_geotiff(self,filename, dtype=gdal.GDT_Float32, **kwargs):
"""
Save a GeoTIFF of the raster currently loaded.
Georeferenced and subset according to the current raster.
:params filename: the path and filename to save the file to.
:type filename: str
:params dtype: GDAL datatype, defaults to Float32.
:type dtype: int
:returns: 1 on success.
:rtype: int
"""
if self.r is None:
raise ValueError('There are no image data loaded. No file can be created.')
simple_write_geotiff(filename, self.r, self.trans,
wkt=self.srs.ExportToWkt(), dtype=dtype, **kwargs)
示例8: _create_geotiff_file
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [as 别名]
def _create_geotiff_file(outfn, driver, shape, metadata, num_bands):
dst = driver.Create(
outfn,
shape[1],
shape[0],
num_bands,
gdal.GDT_Float32,
["COMPRESS=DEFLATE", "PREDICTOR=3"],
)
sx = (metadata["x2"] - metadata["x1"]) / shape[1]
sy = (metadata["y2"] - metadata["y1"]) / shape[0]
dst.SetGeoTransform([metadata["x1"], sx, 0.0, metadata["y2"], 0.0, -sy])
sr = osr.SpatialReference()
sr.ImportFromProj4(metadata["projection"])
dst.SetProjection(sr.ExportToWkt())
return dst
示例9: make_tif2
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [as 别名]
def make_tif2(path, reso=(1., -1.), rsize=(10, 10), tl=(0., 10.),
proj=SRS[0]['wkt'], channel_count=1, dtype=gdal.GDT_Float32,
nodata=-32000, nodata_border_size=(0, 0, 0, 0)):
"""Create a tiff files"""
reso = np.asarray(reso)
fp = buzz.Footprint(tl=tl, rsize=rsize, size=np.abs(reso * rsize))
x, y = fp.meshgrid_raster
a = x + y
if nodata_border_size != 0:
l, r, t, b = nodata_border_size
if t != 0:
a[None:t, None:None] = nodata
if b != 0:
a[-b:None, None:None] = nodata
if l != 0:
a[None:None, None:l] = nodata
if r != 0:
a[None:None, -r:None] = nodata
LOGGER.info('TIFF ARRAY:%s\n', a)
gdal.UseExceptions()
driver = gdal.GetDriverByName('GTiff')
dataset = driver.Create(path, int(rsize[0]), int(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(nodata)
dataset.FlushCache()
示例10: _str_of_gdt
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [as 别名]
def _str_of_gdt(gdt):
return {
gdal.GDT_Byte: 'GDT_Byte',
gdal.GDT_Int16: 'GDT_Int16',
gdal.GDT_Int32: 'GDT_Int32',
gdal.GDT_UInt16: 'GDT_UInt16',
gdal.GDT_UInt32: 'GDT_UInt32',
gdal.GDT_Float32: 'GDT_Float32',
gdal.GDT_Float64: 'GDT_Float64',
gdal.GDT_CFloat32: 'GDT_CFloat32',
gdal.GDT_CFloat64: 'GDT_CFloat64',
gdal.GDT_CInt16: 'GDT_CInt16',
gdal.GDT_CInt32: 'GDT_CInt32',
}[gdt]
示例11: array2raster
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [as 别名]
def array2raster(rasterfn,newRasterfn,array,driver_name = "ENVI", noDataValue = -9999):
"""Takes an array and writes to a GDAL compatible raster. It needs another raster to map the dimensions.
Args:
FileName (str): The filename (with path and extension) of a raster that has the same dimensions as the raster to be written.
newRasterfn (str): The filename (with path and extension) of the new raster.
array (np.array): The array to be written
driver_name (str): The type of raster to write. Default is ENVI since that is the LSDTOpoTools format
noDataValue (float): The no data value
Return:
np.array: A numpy array with the data from the raster.
Author: SMM
"""
raster = gdal.Open(rasterfn)
geotransform = raster.GetGeoTransform()
originX = geotransform[0]
originY = geotransform[3]
pixelWidth = geotransform[1]
pixelHeight = geotransform[5]
cols = raster.RasterXSize
rows = raster.RasterYSize
driver = gdal.GetDriverByName(driver_name)
outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Float32)
outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
outRaster.GetRasterBand(1).SetNoDataValue( noDataValue )
outband = outRaster.GetRasterBand(1)
outband.WriteArray(array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromWkt(raster.GetProjectionRef())
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outband.FlushCache()
#==============================================================================
示例12: polygonize
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [as 别名]
def polygonize(img,shp_path):
# mapping between gdal type and ogr field type
type_mapping = {gdal.GDT_Byte: ogr.OFTInteger,
gdal.GDT_UInt16: ogr.OFTInteger,
gdal.GDT_Int16: ogr.OFTInteger,
gdal.GDT_UInt32: ogr.OFTInteger,
gdal.GDT_Int32: ogr.OFTInteger,
gdal.GDT_Float32: ogr.OFTReal,
gdal.GDT_Float64: ogr.OFTReal,
gdal.GDT_CInt16: ogr.OFTInteger,
gdal.GDT_CInt32: ogr.OFTInteger,
gdal.GDT_CFloat32: ogr.OFTReal,
gdal.GDT_CFloat64: ogr.OFTReal}
ds = gdal.Open(img)
prj = ds.GetProjection()
srcband = ds.GetRasterBand(1)
dst_layername = "Shape"
drv = ogr.GetDriverByName("ESRI Shapefile")
dst_ds = drv.CreateDataSource(shp_path)
srs = osr.SpatialReference(wkt=prj)
dst_layer = dst_ds.CreateLayer(dst_layername, srs=srs)
raster_field = ogr.FieldDefn('id', type_mapping[srcband.DataType])
dst_layer.CreateField(raster_field)
gdal.Polygonize(srcband, srcband, dst_layer, 0, [], callback=None)
del img, ds, srcband, dst_ds, dst_layer
# convert images in a selected folder to shapefiles
示例13: polygonize
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [as 别名]
def polygonize(img,shp_path):
# mapping between gdal type and ogr field type
type_mapping = {gdal.GDT_Byte: ogr.OFTInteger,
gdal.GDT_UInt16: ogr.OFTInteger,
gdal.GDT_Int16: ogr.OFTInteger,
gdal.GDT_UInt32: ogr.OFTInteger,
gdal.GDT_Int32: ogr.OFTInteger,
gdal.GDT_Float32: ogr.OFTReal,
gdal.GDT_Float64: ogr.OFTReal,
gdal.GDT_CInt16: ogr.OFTInteger,
gdal.GDT_CInt32: ogr.OFTInteger,
gdal.GDT_CFloat32: ogr.OFTReal,
gdal.GDT_CFloat64: ogr.OFTReal}
ds = gdal.Open(img)
prj = ds.GetProjection()
srcband = ds.GetRasterBand(1)
dst_layername = "Shape"
drv = ogr.GetDriverByName("ESRI Shapefile")
dst_ds = drv.CreateDataSource(shp_path)
srs = osr.SpatialReference(wkt=prj)
dst_layer = dst_ds.CreateLayer(dst_layername, srs=srs)
# raster_field = ogr.FieldDefn('id', type_mapping[srcband.DataType])
raster_field = ogr.FieldDefn('id', type_mapping[gdal.GDT_Int32])
dst_layer.CreateField(raster_field)
gdal.Polygonize(srcband, srcband, dst_layer, 0, [], callback=None)
del img, ds, srcband, dst_ds, dst_layer
# extract sinks from dem
示例14: dump_raster
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [as 别名]
def dump_raster(rast, rast_path, driver='GTiff', gdt=None, nodata=None):
'''
Creates a raster file from a given gdal.Dataset instance. Arguments:
rast A gdal.Dataset; does NOT accept NumPy array
rast_path The path of the output raster file
driver The name of the GDAL driver to use (determines file type)
gdt The GDAL data type to use, e.g., see gdal.GDT_Float32
nodata The NoData value; defaults to -9999.
'''
if gdt is None:
gdt = rast.GetRasterBand(1).DataType
driver = gdal.GetDriverByName(driver)
sink = driver.Create(
rast_path, rast.RasterXSize, rast.RasterYSize, rast.RasterCount, int(gdt))
assert sink is not None, 'Cannot create dataset; there may be a problem with the output path you specified'
sink.SetGeoTransform(rast.GetGeoTransform())
sink.SetProjection(rast.GetProjection())
for b in range(1, rast.RasterCount + 1):
dat = rast.GetRasterBand(b).ReadAsArray()
sink.GetRasterBand(b).WriteArray(dat)
sink.GetRasterBand(b).SetStatistics(*map(np.float64,
[dat.min(), dat.max(), dat.mean(), dat.std()]))
if nodata is None:
nodata = rast.GetRasterBand(b).GetNoDataValue()
if nodata is None:
nodata = -9999
sink.GetRasterBand(b).SetNoDataValue(np.float64(nodata))
sink.FlushCache()
示例15: write
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import GDT_Float32 [as 别名]
def write(arr, filename):
"""
Write image array to a file with the given filename.
Args:
arr (numpy.ndarray)
filename (str)
"""
driver = gdal.GetDriverByName('GTiff')
x_size = arr.shape[1]
y_size = arr.shape[0]
dataset = driver.Create(filename, x_size, y_size,
eType=gdal.GDT_Float32)
dataset.GetRasterBand(1).WriteArray(arr)