gdal2isce_xml: move code from main() to sub-function
move the funcationality code from main() to a new function gdal2isce_xml() so that it can be called in a python way in other scripts, instead of using os.system().LT1AB
parent
d22bf1048f
commit
0b1b618363
|
@ -3,12 +3,14 @@
|
||||||
# Author: Bekaert David
|
# Author: Bekaert David
|
||||||
# Year: 2017
|
# Year: 2017
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import argparse
|
||||||
|
from osgeo import gdal
|
||||||
|
|
||||||
import isce
|
import isce
|
||||||
import isceobj
|
import isceobj
|
||||||
import sys
|
|
||||||
from osgeo import gdal
|
|
||||||
import argparse
|
|
||||||
import os
|
|
||||||
|
|
||||||
# command line parsing of input file
|
# command line parsing of input file
|
||||||
def cmdLineParse():
|
def cmdLineParse():
|
||||||
|
@ -20,17 +22,14 @@ def cmdLineParse():
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
# main script
|
def gdal2isce_xml(fname):
|
||||||
if __name__ == '__main__':
|
"""
|
||||||
'''
|
Generate ISCE xml file from gdal supported file
|
||||||
Main driver.
|
|
||||||
'''
|
Example: import isce
|
||||||
|
from applications.gdal2isce_xml import gdal2isce_xml
|
||||||
# Parse command line
|
xml_file = gdal2isce_xml(fname+'.vrt')
|
||||||
inps = cmdLineParse()
|
"""
|
||||||
# check if the input file exist
|
|
||||||
if not os.path.isfile(inps.fname):
|
|
||||||
raise Exception('Input file is not found ....')
|
|
||||||
|
|
||||||
# open the GDAL file and get typical data informationi
|
# open the GDAL file and get typical data informationi
|
||||||
GDAL2ISCE_DATATYPE = {
|
GDAL2ISCE_DATATYPE = {
|
||||||
|
@ -57,47 +56,46 @@ if __name__ == '__main__':
|
||||||
# }
|
# }
|
||||||
|
|
||||||
# check if the input file is a vrt
|
# check if the input file is a vrt
|
||||||
filename, file_extension = os.path.splitext(inps.fname)
|
fbase, fext = os.path.splitext(fname)
|
||||||
print(file_extension)
|
print(fext)
|
||||||
if file_extension == ".vrt":
|
if fext == ".vrt":
|
||||||
inps.outname = filename
|
outname = fbase
|
||||||
else:
|
else:
|
||||||
inps.outname = inps.fname
|
outname = fname
|
||||||
print(inps.outname)
|
print(outname)
|
||||||
|
|
||||||
# open the GDAL file and get typical data informationi
|
# open the GDAL file and get typical ds information
|
||||||
data = gdal.Open(inps.fname, gdal.GA_ReadOnly)
|
ds = gdal.Open(fname, gdal.GA_ReadOnly)
|
||||||
width = data.RasterXSize
|
width = ds.RasterXSize
|
||||||
length = data.RasterYSize
|
length = ds.RasterYSize
|
||||||
bands = data.RasterCount
|
bands = ds.RasterCount
|
||||||
# output to user
|
print("width: " + "\t" + str(width))
|
||||||
print("width: " + "\t" + str(width))
|
print("length: " + "\t" + str(length))
|
||||||
print("length: " + "\t" + str(length))
|
print("num of bands:" + "\t" + str(bands))
|
||||||
print("nof bands:" + "\t" + str(bands))
|
|
||||||
|
|
||||||
# getting the datatype information
|
# getting the datatype information
|
||||||
raster = data.GetRasterBand(1)
|
raster = ds.GetRasterBand(1)
|
||||||
dataTypeGdal = raster.DataType
|
dataTypeGdal = raster.DataType
|
||||||
|
|
||||||
# user look-up dictionary from gdal to isce format
|
# user look-up dictionary from gdal to isce format
|
||||||
dataType= GDAL2ISCE_DATATYPE[dataTypeGdal]
|
dataType= GDAL2ISCE_DATATYPE[dataTypeGdal]
|
||||||
# output to user
|
|
||||||
print("dataType: " + "\t" + str(dataType))
|
print("dataType: " + "\t" + str(dataType))
|
||||||
|
|
||||||
|
|
||||||
# transformation contains gridcorners (lines/pixels or lonlat and the spacing 1/-1 or deltalon/deltalat)
|
# transformation contains gridcorners (lines/pixels or lonlat and the spacing 1/-1 or deltalon/deltalat)
|
||||||
transform = data.GetGeoTransform()
|
transform = ds.GetGeoTransform()
|
||||||
# if a complex data type, then create complex image
|
# if a complex data type, then create complex image
|
||||||
# if a real data type, then create a regular image
|
# if a real data type, then create a regular image
|
||||||
|
|
||||||
img = isceobj.createImage()
|
img = isceobj.createImage()
|
||||||
img.setFilename(os.path.abspath(inps.outname))
|
img.setFilename(os.path.abspath(outname))
|
||||||
img.setWidth(width)
|
img.setWidth(width)
|
||||||
img.setLength(length)
|
img.setLength(length)
|
||||||
img.setAccessMode('READ')
|
img.setAccessMode('READ')
|
||||||
img.bands = bands
|
img.bands = bands
|
||||||
img.dataType = dataType
|
img.dataType = dataType
|
||||||
|
|
||||||
md = data.GetMetadata('IMAGE_STRUCTURE')
|
# interleave
|
||||||
|
md = ds.GetMetadata('IMAGE_STRUCTURE')
|
||||||
sch = md.get('INTERLEAVE', None)
|
sch = md.get('INTERLEAVE', None)
|
||||||
if sch == 'LINE':
|
if sch == 'LINE':
|
||||||
img.scheme = 'BIL'
|
img.scheme = 'BIL'
|
||||||
|
@ -110,10 +108,29 @@ if __name__ == '__main__':
|
||||||
print('Assuming default, BIP')
|
print('Assuming default, BIP')
|
||||||
img.scheme = 'BIP'
|
img.scheme = 'BIP'
|
||||||
|
|
||||||
|
|
||||||
img.firstLongitude = transform[0]
|
img.firstLongitude = transform[0]
|
||||||
img.firstLatitude = transform[3]
|
img.firstLatitude = transform[3]
|
||||||
img.deltaLatitude = transform[5]
|
img.deltaLatitude = transform[5]
|
||||||
img.deltaLongitude = transform[1]
|
img.deltaLongitude = transform[1]
|
||||||
img.dump(inps.outname + ".xml")
|
|
||||||
|
xml_file = outname + ".xml"
|
||||||
|
img.dump(xml_file)
|
||||||
|
|
||||||
|
return xml_file
|
||||||
|
|
||||||
|
|
||||||
|
# main script
|
||||||
|
if __name__ == '__main__':
|
||||||
|
'''
|
||||||
|
Main driver.
|
||||||
|
'''
|
||||||
|
|
||||||
|
# Parse command line
|
||||||
|
inps = cmdLineParse()
|
||||||
|
|
||||||
|
# check if the input file exist
|
||||||
|
if not os.path.isfile(inps.fname):
|
||||||
|
raise Exception('Input file is not found ....')
|
||||||
|
|
||||||
|
gdal2isce_xml(inps.fname)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue