Vector to Grid

Source: Internet
Author: User

#!/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.