forked from hotosm/GDAL_scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoffset_geotiff.py
More file actions
executable file
·57 lines (49 loc) · 1.55 KB
/
Copy pathoffset_geotiff.py
File metadata and controls
executable file
·57 lines (49 loc) · 1.55 KB
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import sys, os
from osgeo import gdal, ogr, osr
def shift(infile, xoffset, yoffset):
"""
Overwrite the GeoTransform of a GeoTIFF to translate it to a new position.
No rotation or scaling.
"""
ds = gdal.Open(infile, gdal.GA_Update)
gt = ds.GetGeoTransform()
newgt = (gt[0] + xoffset,
gt[1],
gt[2],
gt[3] + yoffset,
gt[4],
gt[5])
ds.SetGeoTransform(newgt)
ds = None
print(f'Old GeoTransform: {gt}'
f'\nOffset: {xoffset}, {yoffset}'
f'\nNew Geotransform: {newgt}')
def get_corners(infile):
"""
Grab the corners of a GeoTIFF.
"""
ds = gdal.Open(infile)
gt = ds.GetGeoTransform()
ul = (gt[0], gt[3])
width = ds.RasterXSize
height = ds.RasterYSize
lr = gdal.ApplyGeoTransform(gt, float(width), float(height))
# ulx uly lrx lry
return(ul[0], ul[1], lr[0], lr[1])
if __name__ == '__main__':
p = argparse.ArgumentParser()
# Positional arguments
# Flag arguments
p.add_argument('-i', '--input',
help='GeoTIFF raster to be georeferenced')
p.add_argument('-crs', '--coordinate_reference_system',
help='The coordinate reference system')
p.add_argument('-x', '--x_offset', type=float,
help='The x offset')
p.add_argument('-y', '--y_offset', type=float,
help='The y offset'),
args = p.parse_args()
shift = shift(args.input, args.x_offset, args.y_offset)