def create_mask_from_vector(vector_data_path, cols, rows, geo_transform, projection, target_value=1,
output_fname='', dataset_format='MEM'):
"""
Rasterize the given vector (wrapper for gdal.RasterizeLayer). Return a gdal.Dataset.
:param vector_data_path: Path to a shapefile
:param cols: Number of columns of the result
:param rows: Number of rows of the result
:param geo_transform: Returned value of gdal.Dataset.GetGeoTransform (coefficients for
transforming between pixel/line (P,L) raster space, and projection
coordinates (Xp,Yp) space.
:param projection: Projection definition string (Returned by gdal.Dataset.GetProjectionRef)
:param target_value: Pixel value for the pixels. Must be a valid gdal.GDT_UInt16 value.
:param output_fname: If the dataset_format is GeoTIFF, this is the output file name
:param dataset_format: The gdal.Dataset driver name. [default: MEM]
"""
data_source = gdal.OpenEx(vector_data_path, gdal.OF_VECTOR)
if data_source is None:
report_and_exit("File read failed: %s", vector_data_path)
layer = data_source.GetLayer(0)
driver = gdal.GetDriverByName(dataset_format)
target_ds = driver.Create(output_fname, cols, rows, 1, gdal.GDT_UInt16)
target_ds.SetGeoTransform(geo_transform)
target_ds.SetProjection(projection)
gdal.RasterizeLayer(target_ds, [1], layer, burn_values=[target_value])
return target_ds
python类GDT_UInt16()的实例源码
classify.py 文件源码
项目:python_scripting_for_spatial_data_processing
作者: upsdeepak
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
GPP_C6_for_cattle.py 文件源码
项目:Global_GPP_VPM_NCEP_C3C4
作者: zhangyaonju
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def write_file(output_name,output_array,GeoT,xsize,ysize,proJ,driverName='GTiff'):
print "creating", output_name
dr=gdal.GetDriverByName(driverName)
dr.Register()
do=dr.Create(output_name,xsize,ysize,1,gdal.GDT_UInt16,options = [ 'COMPRESS=LZW' ])
do.SetGeoTransform(GeoT)
do.SetProjection(proJ)
do.GetRasterBand(1).WriteArray(output_array)
do.GetRasterBand(1).SetNoDataValue(65535)
do=None
# Function to wirte multi-dimesion array to tiff file
def write_file(output_name, output_array, GeoT, xsize, ysize, proJ, driverName='GTiff'):
print "creating", output_name
dr = gdal.GetDriverByName(driverName)
dr.Register()
do = dr.Create(output_name, xsize, ysize, 1, gdal.GDT_UInt16, options=['COMPRESS=LZW'])
do.SetGeoTransform(GeoT)
do.SetProjection(proJ)
do.GetRasterBand(1).WriteArray(output_array)
do.GetRasterBand(1).SetNoDataValue(32767)
do = None
def write_file(output_name, output_array, GeoT, xsize, ysize, proJ, driverName='GTiff'):
print "creating", output_name
dr = gdal.GetDriverByName(driverName)
dr.Register()
do = dr.Create(output_name, xsize, ysize, 1, gdal.GDT_UInt16, options=['COMPRESS=LZW'])
do.SetGeoTransform(GeoT)
do.SetProjection(proJ)
do.GetRasterBand(1).WriteArray(output_array)
do.GetRasterBand(1).SetNoDataValue(65535)
do = None
def merge_pile_into_mosaic(mosaic_ds,
pile_md_filename,
selected_i,
selected_img,
processing_options):
pile_parts = os.path.basename(pile_md_filename).split('_')[0:3]
assert pile_parts[0] == 'uf'
uf_i = int(pile_parts[1])
uf_j = int(pile_parts[2])
if processing_options.get('normalize_intensity',False):
pile_md = json.load(open(pile_md_filename))
selected_img = selected_img * 1000.0 \
/ pile_md['intensity_median'][selected_i]
mosaic_ds.GetRasterBand(1).WriteArray(
selected_img, uf_i * 256, uf_j * 256)
alpha_band = mosaic_ds.GetRasterBand(mosaic_ds.RasterCount)
if alpha_band.GetColorInterpretation() == gdal.GCI_AlphaBand:
if alpha_band.DataType == gdal.GDT_UInt16:
opaque = 65535
else:
opaque = 255
alpha_band.WriteArray(
numpy.ones(selected_img.shape) * opaque,
uf_i * 256, uf_j * 256)
def make_metatile(pile_directory):
mosaic_filename = os.path.join(pile_directory,'mosaic.tif')
mosaic_ds = gdal.GetDriverByName('GTiff').Create(
mosaic_filename, 4096, 4096, 1, gdal.GDT_UInt16)
# TODO: Try to add georeferencing...
return mosaic_filename, mosaic_ds
def open_data(filename):
'''
The function open and load the image given its name.
The type of the data is checked from the file and the scipy array is initialized accordingly.
Input:
filename: the name of the file
Output:
im: the data cube
GeoTransform: the geotransform information
Projection: the projection information
'''
data = gdal.Open(filename,gdal.GA_ReadOnly)
if data is None:
print 'Impossible to open '+filename
exit()
nc = data.RasterXSize
nl = data.RasterYSize
d = data.RasterCount
# Get the type of the data
gdal_dt = data.GetRasterBand(1).DataType
if gdal_dt == gdal.GDT_Byte:
dt = 'uint8'
elif gdal_dt == gdal.GDT_Int16:
dt = 'int16'
elif gdal_dt == gdal.GDT_UInt16:
dt = 'uint16'
elif gdal_dt == gdal.GDT_Int32:
dt = 'int32'
elif gdal_dt == gdal.GDT_UInt32:
dt = 'uint32'
elif gdal_dt == gdal.GDT_Float32:
dt = 'float32'
elif gdal_dt == gdal.GDT_Float64:
dt = 'float64'
elif gdal_dt == gdal.GDT_CInt16 or gdal_dt == gdal.GDT_CInt32 or gdal_dt == gdal.GDT_CFloat32 or gdal_dt == gdal.GDT_CFloat64 :
dt = 'complex64'
else:
print 'Data type unkown'
exit()
# Initialize the array
if d == 1:
im = sp.empty((nl,nc),dtype=dt)
else:
im = sp.empty((nl,nc,d),dtype=dt)
if d == 1:
im[:,:]=data.GetRasterBand(1).ReadAsArray()
else :
for i in range(d):
im[:,:,i]=data.GetRasterBand(i+1).ReadAsArray()
GeoTransform = data.GetGeoTransform()
Projection = data.GetProjection()
data = None
return im,GeoTransform,Projection
def write_data(outname,im,GeoTransform,Projection):
'''
The function write the image on the hard drive.
Input:
outname: the name of the file to be written
im: the image cube
GeoTransform: the geotransform information
Projection: the projection information
Output:
Nothing --
'''
nl = im.shape[0]
nc = im.shape[1]
if im.ndim == 2:
d=1
else:
d = im.shape[2]
driver = gdal.GetDriverByName('GTiff')
dt = im.dtype.name
# Get the data type
if dt == 'bool' or dt == 'uint8':
gdal_dt=gdal.GDT_Byte
elif dt == 'int8' or dt == 'int16':
gdal_dt=gdal.GDT_Int16
elif dt == 'uint16':
gdal_dt=gdal.GDT_UInt16
elif dt == 'int32':
gdal_dt=gdal.GDT_Int32
elif dt == 'uint32':
gdal_dt=gdal.GDT_UInt32
elif dt == 'int64' or dt == 'uint64' or dt == 'float16' or dt == 'float32':
gdal_dt=gdal.GDT_Float32
elif dt == 'float64':
gdal_dt=gdal.GDT_Float64
elif dt == 'complex64':
gdal_dt=gdal.GDT_CFloat64
else:
print 'Data type non-suported'
exit()
dst_ds = driver.Create(outname,nc,nl, d, gdal_dt)
dst_ds.SetGeoTransform(GeoTransform)
dst_ds.SetProjection(Projection)
if d==1:
out = dst_ds.GetRasterBand(1)
out.WriteArray(im)
out.FlushCache()
else:
for i in range(d):
out = dst_ds.GetRasterBand(i+1)
out.WriteArray(im[:,:,i])
out.FlushCache()
dst_ds = None