Created
August 19, 2021 02:16
-
-
Save stephendade/75583a3138c4deb9e4b26b3380681eed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Rotate a DTED into Gazebo format (270deg rotate) | |
# So it matches the Gazebo axis format | |
# Requires GDAL to be installed | |
from osgeo import gdal, osr | |
import numpy as np | |
import argparse, os | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='DTED rotator for Gazebo simulator') | |
parser.add_argument('-file', action="store", dest="file", default="HoganIsland2.tif") | |
args = parser.parse_args() | |
dataset = gdal.Open(args.file) | |
band = dataset.GetRasterBand(1) | |
array = dataset.ReadAsArray() | |
array_rot90 = (list(list(x) for x in zip(*array))[::-1]) | |
array_rot180 = (list(list(x) for x in zip(*array_rot90))[::-1]) | |
array_rot270 = (list(list(x) for x in zip(*array_rot180))[::-1]) | |
geotransform = dataset.GetGeoTransform() | |
wkt = dataset.GetProjection() | |
# Create gtif file | |
driver = gdal.GetDriverByName("GTiff") | |
output_file = "{0}_{2}{1}".format(*os.path.splitext(args.file) + ("rot270",)) | |
dst_ds = driver.Create(output_file, | |
band.XSize, | |
band.YSize, | |
1, | |
gdal.GDT_Float32) | |
array_rot270 = np.array(array_rot270) | |
#writting output raster | |
dst_ds.GetRasterBand(1).WriteArray( array_rot270 ) | |
#setting extension of output raster | |
# top left x, w-e pixel resolution, rotation, top left y, rotation, n-s pixel resolution | |
dst_ds.SetGeoTransform(geotransform) | |
# setting spatial reference of output raster | |
srs = osr.SpatialReference() | |
srs.ImportFromWkt(wkt) | |
dst_ds.SetProjection( srs.ExportToWkt() ) | |
#Close output raster dataset | |
dataset = None | |
dst_ds = None | |
print("Generated: {0}_{2}{1}".format(*os.path.splitext(args.file) + ("rot270",))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment