2015-12-01 16:26:13 +00:00
|
|
|
import ecto
|
|
|
|
|
|
|
|
from opendm import io
|
|
|
|
from opendm import log
|
|
|
|
from opendm import system
|
|
|
|
from opendm import context
|
2016-02-23 17:47:43 +00:00
|
|
|
from opendm import types
|
|
|
|
|
2015-12-01 16:26:13 +00:00
|
|
|
|
|
|
|
class ODMOrthoPhotoCell(ecto.Cell):
|
2015-12-10 11:01:41 +00:00
|
|
|
def declare_params(self, params):
|
|
|
|
params.declare("resolution", 'Orthophoto ground resolution in pixels/meter', 20)
|
2015-12-01 16:26:13 +00:00
|
|
|
|
|
|
|
def declare_io(self, params, inputs, outputs):
|
2015-12-10 11:01:41 +00:00
|
|
|
inputs.declare("tree", "Struct with paths", [])
|
2015-12-01 16:26:13 +00:00
|
|
|
inputs.declare("args", "The application arguments.", {})
|
2015-12-10 11:01:41 +00:00
|
|
|
inputs.declare("reconstruction", "list of ODMReconstructions", [])
|
2015-12-01 16:26:13 +00:00
|
|
|
|
|
|
|
def process(self, inputs, outputs):
|
|
|
|
|
|
|
|
log.ODM_INFO('Running OMD OrthoPhoto Cell')
|
|
|
|
|
|
|
|
# get inputs
|
|
|
|
args = self.inputs.args
|
2015-12-10 11:01:41 +00:00
|
|
|
tree = self.inputs.tree
|
2015-12-01 16:26:13 +00:00
|
|
|
|
|
|
|
# define paths and create working directories
|
2015-12-10 11:01:41 +00:00
|
|
|
system.mkdir_p(tree.odm_orthophoto)
|
2015-12-01 16:26:13 +00:00
|
|
|
|
2015-12-01 16:52:18 +00:00
|
|
|
# check if we rerun cell or not
|
2015-12-10 11:01:41 +00:00
|
|
|
rerun_cell = args['rerun'] is not None \
|
|
|
|
and args['rerun'] == 'odm_orthophoto'
|
|
|
|
|
|
|
|
if not io.file_exists(tree.odm_orthophoto_file) or rerun_cell:
|
|
|
|
|
2016-02-23 17:47:43 +00:00
|
|
|
# odm_orthophoto definitions
|
2015-12-10 11:01:41 +00:00
|
|
|
kwargs = {
|
|
|
|
'bin': context.odm_modules_path,
|
|
|
|
'model_geo': tree.odm_textured_model_obj_geo,
|
|
|
|
'log': tree.odm_orthophoto_log,
|
|
|
|
'ortho': tree.odm_orthophoto_file,
|
|
|
|
'corners': tree.odm_orthophoto_corners,
|
|
|
|
'res': self.params.resolution
|
|
|
|
}
|
2015-12-01 16:52:18 +00:00
|
|
|
|
2016-02-23 17:47:43 +00:00
|
|
|
# run odm_orthophoto
|
2015-12-11 21:27:16 +00:00
|
|
|
system.run('{bin}/odm_orthophoto -inputFile {model_geo} ' \
|
|
|
|
'-logFile {log} -outputFile {ortho} -resolution {res} ' \
|
|
|
|
'-outputCornerFile {corners}'.format(**kwargs))
|
2016-02-23 17:47:43 +00:00
|
|
|
|
|
|
|
# Create georeferenced GeoTiff
|
|
|
|
geoTiffCreated = False
|
|
|
|
georef = types.ODM_GeoRef()
|
|
|
|
# creates the coord refs # TODO I don't want to have to do this twice- after odm_georef
|
|
|
|
georef.parse_coordinate_system(tree.odm_georeferencing_coords)
|
|
|
|
|
|
|
|
if (georef.epsg and georef.utm_east_offset and georef.utm_north_offset):
|
|
|
|
ulx = uly = lrx = lry = 0.0
|
|
|
|
with open(tree.odm_orthophoto_corners) as f:
|
|
|
|
for lineNumber, line in enumerate(f):
|
|
|
|
if lineNumber == 0:
|
|
|
|
tokens = line.split(' ')
|
|
|
|
if len(tokens) == 4:
|
|
|
|
ulx = float(tokens[0]) + \
|
|
|
|
float(georef.utm_east_offset)
|
|
|
|
lry = float(tokens[1]) + \
|
|
|
|
float(georef.utm_north_offset)
|
|
|
|
lrx = float(tokens[2]) + \
|
|
|
|
float(georef.utm_east_offset)
|
|
|
|
uly = float(tokens[3]) + \
|
|
|
|
float(georef.utm_north_offset)
|
|
|
|
log.ODM_INFO('Creating GeoTIFF')
|
|
|
|
|
|
|
|
kwargs = {
|
|
|
|
'ulx': ulx,
|
|
|
|
'uly': uly,
|
|
|
|
'lrx': lrx,
|
|
|
|
'lry': lry,
|
|
|
|
'epsg': georef.epsg,
|
|
|
|
'png': tree.odm_orthophoto_file,
|
|
|
|
'tiff': tree.odm_orthophoto_tif
|
|
|
|
}
|
|
|
|
|
|
|
|
system.run('gdal_translate -a_ullr {ulx} {uly} {lrx} {lry} '
|
|
|
|
'-a_srs \"EPSG:{epsg}\" {png} {tiff}'.format(**kwargs))
|
|
|
|
geoTiffCreated = True
|
|
|
|
if not geoTiffCreated:
|
|
|
|
log.ODM_WARNING('No geo-referenced orthophoto created due '
|
|
|
|
'to missing geo-referencing or corner coordinates.')
|
|
|
|
|
2015-12-01 16:52:18 +00:00
|
|
|
else:
|
2015-12-10 11:01:41 +00:00
|
|
|
log.ODM_WARNING('Found a valid orthophoto in: %s' % tree.odm_orthophoto_file)
|
2015-12-01 16:26:13 +00:00
|
|
|
|
|
|
|
log.ODM_INFO('Running OMD OrthoPhoto Cell - Finished')
|
|
|
|
return ecto.OK if args['end_with'] != 'odm_orthophoto' else ecto.QUIT
|
|
|
|
|
2015-11-16 14:52:47 +00:00
|
|
|
|
|
|
|
def odm_orthophoto():
|
|
|
|
"""Run odm_orthophoto"""
|
|
|
|
print "\n - running orthophoto generation - " + system.now()
|
|
|
|
|
|
|
|
os.chdir(jobOptions["jobDir"])
|
|
|
|
try:
|
|
|
|
os.mkdir(jobOptions["jobDir"] + "/odm_orthophoto")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
run("\"" + BIN_PATH + "/odm_orthophoto\" -inputFile " + jobOptions["jobDir"] + \
|
|
|
|
"-results/odm_texturing/odm_textured_model_geo.obj -logFile " + jobOptions["jobDir"] \
|
|
|
|
+ "/odm_orthophoto/odm_orthophoto_log.txt -outputFile " + jobOptions["jobDir"] \
|
|
|
|
+ "-results/odm_orthphoto.png -resolution 20.0 -outputCornerFile " + jobOptions["jobDir"] \
|
|
|
|
+ "/odm_orthphoto_corners.txt")
|
|
|
|
|
|
|
|
if "csString" not in jobOptions:
|
2016-02-23 17:47:43 +00:00
|
|
|
parse_coordinate_system() # Writes the coord string to the jobOptions object
|
2015-11-16 14:52:47 +00:00
|
|
|
|
|
|
|
geoTiffCreated = False
|
|
|
|
if ("csString" in jobOptions and
|
|
|
|
"utmEastOffset" in jobOptions and "utmNorthOffset" in jobOptions):
|
|
|
|
ulx = uly = lrx = lry = 0.0
|
|
|
|
with open(jobOptions["jobDir"] +
|
2016-02-23 17:47:43 +00:00
|
|
|
"/odm_orthphoto_corners.txt") as f: # Open tree.odm_orthophoto_corners
|
2015-11-16 14:52:47 +00:00
|
|
|
for lineNumber, line in enumerate(f):
|
|
|
|
if lineNumber == 0:
|
|
|
|
tokens = line.split(' ')
|
|
|
|
if len(tokens) == 4:
|
|
|
|
ulx = float(tokens[0]) + \
|
|
|
|
float(jobOptions["utmEastOffset"])
|
|
|
|
lry = float(tokens[1]) + \
|
|
|
|
float(jobOptions["utmNorthOffset"])
|
|
|
|
lrx = float(tokens[2]) + \
|
|
|
|
float(jobOptions["utmEastOffset"])
|
|
|
|
uly = float(tokens[3]) + \
|
|
|
|
float(jobOptions["utmNorthOffset"])
|
|
|
|
|
|
|
|
print(" Creating GeoTIFF...")
|
|
|
|
sys.stdout.write(" ")
|
|
|
|
run("gdal_translate -a_ullr " + str(ulx) + " " + str(uly) + " " +
|
|
|
|
str(lrx) + " " + str(lry) + " -a_srs \"" + jobOptions["csString"] +
|
|
|
|
"\" " + jobOptions["jobDir"] + "-results/odm_orthphoto.png " +
|
|
|
|
jobOptions["jobDir"] + "-results/odm_orthphoto.tif")
|
|
|
|
geoTiffCreated = True
|
|
|
|
|
|
|
|
if not geoTiffCreated:
|
|
|
|
|
|
|
|
print " Warning: No geo-referenced orthophoto created due to missing geo-referencing or corner coordinates."
|