-
Notifications
You must be signed in to change notification settings - Fork 0
/
shp2tiff.py
executable file
·74 lines (52 loc) · 1.88 KB
/
shp2tiff.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
import os, ogr, osr, gdal, numpy, shutil
import sys, argparse
def main(args):
pixel_size = args.resolution
NoData_value = 0 #args.no_data
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS('EPSG:4326')
min_x = -180
max_x = 180
min_y = -90
max_y = 90
# Create the destination data source
x_res = max(int((max_x - min_x) / pixel_size), 1)
y_res = max(int((max_y - min_y) / pixel_size), 1)
target_ds = gdal.GetDriverByName('GTiff').Create(
os.path.join(os.getcwd(), args.outfile), x_res, y_res, 1, gdal.GDT_UInt16)
target_ds.SetProjection(srs.ExportToWkt())
target_ds.SetGeoTransform((min_x, pixel_size, 0, max_y, 0, -pixel_size))
band = target_ds.GetRasterBand(1)
band.SetNoDataValue(NoData_value)
source = ogr.Open(os.path.join(os.getcwd(), args.infile))
source_layer = source.GetLayer()
print('Writing %s from %s at %s ' % (
args.outfile, args.infile, args.resolution))
# Rasterize
gdal.RasterizeLayer(target_ds, [1], source_layer, options=[
"ATTRIBUTE=%s" % args.attribute,
"SPARSE_OK=TRUE",
"COMPRESS=LZW"])
print('Wrote %s' % args.outfile)
del target_ds
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--attribute',
dest='attribute', default="OBJECTID", metavar='ATTRIBUTE', type=str,
help='an attribute to burn to raster')
parser.add_argument('--src',
dest='infile', metavar='INFILE', type=str,
help='an attribute to burn to raster')
parser.add_argument('--dest',
dest='outfile', metavar='OUTFILE', type=str,
help='an attribute to burn to raster')
parser.add_argument('--resolution',
default=0.01, dest='resolution', metavar='OUTFILE', type=float,
help='an attribute to burn to raster')
args = parser.parse_args()
if args.infile and args.outfile:
print os.path.join(os.getcwd(), args.outfile)
main(args)
else:
print(parser.print_help())