本文整理汇总了Python中osgeo.gdal.RasterizeLayer方法的典型用法代码示例。如果您正苦于以下问题:Python gdal.RasterizeLayer方法的具体用法?Python gdal.RasterizeLayer怎么用?Python gdal.RasterizeLayer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类osgeo.gdal
的用法示例。
在下文中一共展示了gdal.RasterizeLayer方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_mask_from_vector
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import RasterizeLayer [as 别名]
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]
"""
driver = ogr.GetDriverByName('ESRI Shapefile')
data_source = driver.Open(vector_data_path, 0)
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
示例2: pts2raster
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import RasterizeLayer [as 别名]
def pts2raster(shapefile,RASTER_PATH,cellSize,field_name=False):
from osgeo import gdal, ogr
# Define pixel_size and NoData value of new raster
pixel_size = cellSize
NoData_value = -9999
# Filename of input OGR file
vector_ptsShp_fn = shapefile
# Filename of the raster Tiff that will be created
raster_ptsShp_fn = RASTER_PATH
# Open the data source and read in the extent
source_ds = ogr.Open(vector_ptsShp_fn)
source_layer = source_ds.GetLayer()
x_min, x_max, y_min, y_max = source_layer.GetExtent()
# Create the destination data source
x_res = int((x_max - x_min) / pixel_size)
y_res = int((y_max - y_min) / pixel_size)
target_ds = gdal.GetDriverByName('GTiff').Create(raster_ptsShp_fn, x_res, y_res, 1, gdal.GDT_Int32 )
target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
band = target_ds.GetRasterBand(1)
band.SetNoDataValue(NoData_value)
# Rasterize
# gdal.RasterizeLayer(target_ds, [1], source_layer, burn_values=[0])
# Rasterize
if field_name:
gdal.RasterizeLayer(target_ds,[1], source_layer,options=["ATTRIBUTE={0}".format(field_name)])
# print("write:",field_name)
else:
gdal.RasterizeLayer(target_ds,[1], source_layer,burn_values=[-1])
return gdal.Open(RASTER_PATH).ReadAsArray()
#批量计算
示例3: Vector_to_Raster
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import RasterizeLayer [as 别名]
def Vector_to_Raster(Dir, shapefile_name, reference_raster_data_name):
"""
This function creates a raster of a shp file
Keyword arguments:
Dir --
str: path to the basin folder
shapefile_name -- 'C:/....../.shp'
str: Path from the shape file
reference_raster_data_name -- 'C:/....../.tif'
str: Path to an example tiff file (all arrays will be reprojected to this example)
"""
from osgeo import gdal, ogr
geo, proj, size_X, size_Y=Open_array_info(reference_raster_data_name)
x_min = geo[0]
x_max = geo[0] + size_X * geo[1]
y_min = geo[3] + size_Y * geo[5]
y_max = geo[3]
pixel_size = geo[1]
# Filename of the raster Tiff that will be created
Dir_Basin_Shape = os.path.join(Dir,'Basin')
if not os.path.exists(Dir_Basin_Shape):
os.mkdir(Dir_Basin_Shape)
Basename = os.path.basename(shapefile_name)
Dir_Raster_end = os.path.join(Dir_Basin_Shape, os.path.splitext(Basename)[0]+'.tif')
# Open the data source and read in the extent
source_ds = ogr.Open(shapefile_name)
source_layer = source_ds.GetLayer()
# Create the destination data source
x_res = int(round((x_max - x_min) / pixel_size))
y_res = int(round((y_max - y_min) / pixel_size))
# Create tiff file
target_ds = gdal.GetDriverByName('GTiff').Create(Dir_Raster_end, x_res, y_res, 1, gdal.GDT_Float32, ['COMPRESS=LZW'])
target_ds.SetGeoTransform(geo)
srse = osr.SpatialReference()
srse.SetWellKnownGeogCS(proj)
target_ds.SetProjection(srse.ExportToWkt())
band = target_ds.GetRasterBand(1)
target_ds.GetRasterBand(1).SetNoDataValue(-9999)
band.Fill(-9999)
# Rasterize the shape and save it as band in tiff file
gdal.RasterizeLayer(target_ds, [1], source_layer, None, None, [1], ['ALL_TOUCHED=TRUE'])
target_ds = None
# Open array
Raster_Basin = Open_tiff_array(Dir_Raster_end)
return(Raster_Basin)