Python gdal.RasterizeLayer() Examples
The following are 7
code examples of gdal.RasterizeLayer().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
gdal
, or try the search function
.
Example #1
Source File: function_dataraster.py From dzetsaka with GNU General Public License v3.0 | 8 votes |
def rasterize(data, vectorSrc, field, outFile): dataSrc = gdal.Open(data) import ogr shp = ogr.Open(vectorSrc) lyr = shp.GetLayer() driver = gdal.GetDriverByName('GTiff') dst_ds = driver.Create( outFile, dataSrc.RasterXSize, dataSrc.RasterYSize, 1, gdal.GDT_UInt16) dst_ds.SetGeoTransform(dataSrc.GetGeoTransform()) dst_ds.SetProjection(dataSrc.GetProjection()) if field is None: gdal.RasterizeLayer(dst_ds, [1], lyr, None) else: OPTIONS = ['ATTRIBUTE=' + field] gdal.RasterizeLayer(dst_ds, [1], lyr, None, options=OPTIONS) data, dst_ds, shp, lyr = None, None, None, None return outFile
Example #2
Source File: terrain_helper.py From CityEnergyAnalyst with MIT License | 6 votes |
def calc_raster_terrain_fixed_elevation(crs, elevation, grid_size, raster_path, locator, x_max, x_min, y_max, y_min): # local variables: temp_shapefile = locator.get_temporary_file("terrain.shp") cols = int((x_max - x_min) / grid_size) rows = int((y_max - y_min) / grid_size) shapes = Polygon([[x_min, y_min], [x_max, y_min], [x_max, y_max], [x_min, y_max], [x_min, y_min]]) geodataframe = Gdf(index=[0], crs=crs, geometry=[shapes]) geodataframe.to_file(temp_shapefile) # 1) opening the shapefile source_ds = ogr.Open(temp_shapefile) source_layer = source_ds.GetLayer() target_ds = gdal.GetDriverByName('GTiff').Create(raster_path, cols, rows, 1, gdal.GDT_Float32) ##COMMENT 2 target_ds.SetGeoTransform((x_min, grid_size, 0, y_max, 0, -grid_size)) ##COMMENT 3 # 5) Adding a spatial reference ##COMMENT 4 target_dsSRS = osr.SpatialReference() target_dsSRS.ImportFromProj4(crs) target_ds.SetProjection(target_dsSRS.ExportToWkt()) band = target_ds.GetRasterBand(1) band.SetNoDataValue(-9999) ##COMMENT 5 gdal.RasterizeLayer(target_ds, [1], source_layer, burn_values=[elevation]) ##COMMENT 6 target_ds = None # closing the file
Example #3
Source File: Exploratory Spatial Data Analysis in PySAL.py From python-urbanPlanning with MIT License | 5 votes |
def Feature_to_Raster(input_shp, output_tiff, cellsize, field_name=False, NoData_value=-9999): """ Converts a shapefile into a raster """ # Input inp_driver = ogr.GetDriverByName('ESRI Shapefile') inp_source = inp_driver.Open(input_shp, 0) inp_lyr = inp_source.GetLayer() inp_srs = inp_lyr.GetSpatialRef() # Extent x_min, x_max, y_min, y_max = inp_lyr.GetExtent() x_ncells = int((x_max - x_min) / cellsize) y_ncells = int((y_max - y_min) / cellsize) # Output out_driver = gdal.GetDriverByName('GTiff') if os.path.exists(output_tiff): out_driver.Delete(output_tiff) out_source = out_driver.Create(output_tiff, x_ncells, y_ncells,1, gdal.GDT_Int16) print("+"*50) print(x_ncells, y_ncells,1, gdal.GDT_Int16) out_source.SetGeoTransform((x_min, cellsize, 0, y_max, 0, -cellsize)) out_source.SetProjection(inp_srs.ExportToWkt()) out_lyr = out_source.GetRasterBand(1) out_lyr.SetNoDataValue(NoData_value) # Rasterize # print(inp_lyr) if field_name: gdal.RasterizeLayer(out_source, [1], inp_lyr,options=["ATTRIBUTE={0}".format(field_name)]) else: gdal.RasterizeLayer(out_source, [1], inp_lyr, burn_values=[1]) # Save and/or close the data sources inp_source = None out_source = None # Return return output_tiff #geo_silhouettes
Example #4
Source File: pointsClustering.py From python-urbanPlanning with MIT License | 5 votes |
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() #批量计算
Example #5
Source File: functions.py From hants with Apache License 2.0 | 4 votes |
def Feature_to_Raster(input_shp, output_tiff, cellsize, field_name=False, NoData_value=-9999): """ Converts a shapefile into a raster """ # Input inp_driver = ogr.GetDriverByName('ESRI Shapefile') inp_source = inp_driver.Open(input_shp, 0) inp_lyr = inp_source.GetLayer() inp_srs = inp_lyr.GetSpatialRef() # Extent x_min, x_max, y_min, y_max = inp_lyr.GetExtent() x_ncells = int((x_max - x_min) / cellsize) y_ncells = int((y_max - y_min) / cellsize) # Output out_driver = gdal.GetDriverByName('GTiff') if os.path.exists(output_tiff): out_driver.Delete(output_tiff) out_source = out_driver.Create(output_tiff, x_ncells, y_ncells, 1, gdal.GDT_Int16) out_source.SetGeoTransform((x_min, cellsize, 0, y_max, 0, -cellsize)) out_source.SetProjection(inp_srs.ExportToWkt()) out_lyr = out_source.GetRasterBand(1) out_lyr.SetNoDataValue(NoData_value) # Rasterize if field_name: gdal.RasterizeLayer(out_source, [1], inp_lyr, options=["ATTRIBUTE={0}".format(field_name)]) else: gdal.RasterizeLayer(out_source, [1], inp_lyr, burn_values=[1]) # Save and/or close the data sources inp_source = None out_source = None # Return return output_tiff
Example #6
Source File: raster_conversions.py From wa with Apache License 2.0 | 4 votes |
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)
Example #7
Source File: functions.py From wa with Apache License 2.0 | 4 votes |
def Feature_to_Raster(input_shp, output_tiff, cellsize, field_name=False, NoData_value=-9999): """ Converts a shapefile into a raster """ # Input inp_driver = ogr.GetDriverByName('ESRI Shapefile') inp_source = inp_driver.Open(input_shp, 0) inp_lyr = inp_source.GetLayer() inp_srs = inp_lyr.GetSpatialRef() # Extent x_min, x_max, y_min, y_max = inp_lyr.GetExtent() x_ncells = int((x_max - x_min) / cellsize) y_ncells = int((y_max - y_min) / cellsize) # Output out_driver = gdal.GetDriverByName('GTiff') if os.path.exists(output_tiff): out_driver.Delete(output_tiff) out_source = out_driver.Create(output_tiff, x_ncells, y_ncells, 1, gdal.GDT_Int16) out_source.SetGeoTransform((x_min, cellsize, 0, y_max, 0, -cellsize)) out_source.SetProjection(inp_srs.ExportToWkt()) out_lyr = out_source.GetRasterBand(1) out_lyr.SetNoDataValue(NoData_value) # Rasterize if field_name: gdal.RasterizeLayer(out_source, [1], inp_lyr, options=["ATTRIBUTE={0}".format(field_name)]) else: gdal.RasterizeLayer(out_source, [1], inp_lyr, burn_values=[1]) # Save and/or close the data sources inp_source = None out_source = None # Return return output_tiff