-
Notifications
You must be signed in to change notification settings - Fork 0
/
raster_shp
executable file
·32 lines (25 loc) · 1.01 KB
/
raster_shp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
# based on http://pcjericks.github.io/py-gdalogr-cookbook/raster_layers.html
# changed the burn value in the last RasterizeLayer call to 1
import sys
from osgeo import gdal, ogr
# Define pixel_size and NoData value of new raster
pixel_size = 300
NoData_value = -9999
# Filename of input OGR file
vector_fn = sys.argv[1]
# Filename of the raster Tiff that will be created
raster_fn = vector_fn[:-4] + ".tiff"
# Open the data source and read in the extent
source_ds = ogr.Open(vector_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_fn, x_res, y_res, 1, gdal.GDT_Byte)
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=[1])