#!/usr/bin/env python#-*-coding:utf-8-*- fromOSGeoImportogr fromOSGeoImportGdal#Set pixel sizePixel_size = 0.00002No_data_value=-9999#Shapefile input Name#input projection must is in Cartesian system in meters#input WGS or epsg:4326 won't work!!!INPUT_SHP = R'.. /GEODATA/DATA/PLY_GOLFCOURSE-STRASSLACH3857.SHP'#TIF Raster file to be createdOutput_raster = R'.. /geodata/data/ply_golfcourse-strasslach.img'#Open The data source get the Layer object#assign extent coordinates#shp_driver = ogr. Getdriverbyname (' ESRI shapefiles ')OPEN_SHP =ogr. Open (input_shp) Shp_layer=open_shp. Getlayer () x_min, X_max, y_min, Y_max=Shp_layer. GetExtent ()Print(x_min)Print(X_max)#Calculate Raster Resolutionx_res = Int ((x_max-x_min)/pixel_size) Y_res= Int ((y_max-y_min)/pixel_size)Print(x_res)Print(y_res)#set the image type for exportImage_type ='Gtiff'Image_type='HFA'Driver=Gdal. Getdriverbyname (Image_type) driver. Register ()#Create a new raster takes Parameters#Filename The name of the dataset to create. UTF-8 encoded.#nxsize width of created raster in pixels.#nysize height of created raster in pixels.#nbands Number of bands.#eType type of raster.New_raster= driver. Create (Output_raster, X_res, Y_res, 1, Gdal. Gdt_byte) New_raster. Setgeotransform (x_min, pixel_size, 0, Y_max, 0,-pixel_size))#Get the Raster band we want to export tooRaster_band = New_raster. Getrasterband (1)#assign the no data value to empty cellsRaster_band. Setnodatavalue (No_data_value)#run vector to raster on new raster with input ShapefileGdal. Rasterizelayer (New_raster, [1], Shp_layer, burn_values=[255])
Vector to Grid