Commit 0f1afa63 authored by Rashad Kanavath's avatar Rashad Kanavath
Browse files

check and create otbcli wrapper script on startup

parent 9f7a31a8
......@@ -103,12 +103,6 @@ class OTBAlgorithmProvider(QgsProcessingProvider):
OTBUtils.executeOtb(commands, feedback=None)
if isValid:
otb_app_path_env = os.pathsep.join(otb_app_dirs)
env_profile = os.path.join(folder, 'otbenv.profile')
if os.name == 'nt':
env_profile = os.path.join(folder, 'otbenv.bat')
if os.path.exists(env_profile) and os.path.isfile(env_profile):
self.write_otbcli_wrapper_script(folder, otb_app_path_env)
utils.iface.messageBar().pushInfo("OTB", "OTB provider is activated from '{}'.".format(folder))
else:
self.setActive(False)
......@@ -130,47 +124,54 @@ class OTBAlgorithmProvider(QgsProcessingProvider):
self.setActive(False)
raise ValueError(self.tr("Cannot find '{}'. OTB will be disabled".format(os.path.join(v,'bin', otb_exe_file('otbApplicationLauncherCommandLine')))))
def write_otbcli_wrapper_script(self, otb_install_dir, otb_app_path_env):
def write_otbcli_wrapper_script(self):
otb_folder = OTBUtils.appFolder()
otb_app_path_env = os.pathsep.join(self.otb_app_dir_list(OTBUtils.appFolder()))
gdal_data_dir = None
geotiff_csv_dir = None
if os.name == 'nt':
app_vargs = " %*"
export_cmd = 'SET '
first_line = ':: Setup environment for OTB package. Generated by QGIS plugin'
otb_app_launcher = os.path.join(otb_install_dir, 'bin', 'otbApplicationLauncherCommandLine.exe')
cli_filepath = os.path.join(QgsApplication.qgisSettingsDirPath(), 'processing', 'qgis_otb_cli.bat')
gdal_data_dir = os.path.join(otb_install_dir, 'share' , 'data')
geotiff_csv_dir = os.path.join(otb_install_dir, 'share' , 'epsg_csv')
else:
app_vargs = " $@"
export_cmd = 'export '
first_line = '#!/bin/sh'
otb_app_launcher = os.path.join(otb_install_dir, 'bin', 'otbApplicationLauncherCommandLine')
cli_filepath = os.path.join(QgsApplication.qgisSettingsDirPath(), 'processing', 'qgis_otb_cli')
lines = None
env_profile = os.path.join(otb_install_dir, 'otbenv.profile')
with open(env_profile) as f:
lines = f.readlines()
lines = [x.strip() for x in lines]
for line in lines:
if not line or line.startswith('#'):
continue
if 'GDAL_DATA=' in line:
gdal_data_dir = line.split("GDAL_DATA=")[1]
if 'GEOTIFF_CSV='in line:
geotiff_csv_dir = line.split("GEOTIFF_CSV=")[1]
with open(cli_filepath, 'w') as otb_cli_file:
otb_cli_file.write(first_line + os.linesep)
otb_cli_file.write(export_cmd + "LC_NUMERIC=C" + os.linesep)
otb_cli_file.write(export_cmd + "GDAL_DRIVER_PATH=disable" + os.linesep)
otb_cli_file.write(export_cmd + "\"GDAL_DATA=" + gdal_data_dir + "\"" + os.linesep)
otb_cli_file.write(export_cmd + "\"GEOTIFF_CSV=" + geotiff_csv_dir + "\"" + os.linesep)
otb_cli_file.write(export_cmd + "\"OTB_APPLICATION_PATH=" + otb_app_path_env + "\"" + os.linesep)
otb_cli_file.write("\"" + otb_app_launcher + "\"" + app_vargs + os.linesep)
if not os.name == 'nt':
os.chmod(cli_filepath, 0o744)
try:
if os.name == 'nt':
app_vargs = " %*"
export_cmd = 'SET '
first_line = ':: Setup environment for OTB package. Generated by QGIS plugin'
otb_app_launcher = os.path.join(otb_folder, 'bin', 'otbApplicationLauncherCommandLine.exe')
gdal_data_dir = os.path.join(otb_folder, 'share' , 'data')
geotiff_csv_dir = os.path.join(otb_folder, 'share' , 'epsg_csv')
else:
app_vargs = " $@"
export_cmd = 'export '
first_line = '#!/bin/sh'
otb_app_launcher = os.path.join(otb_folder, 'bin', 'otbApplicationLauncherCommandLine')
lines = None
env_profile = os.path.join(otb_folder, 'otbenv.profile')
with open(env_profile) as f:
lines = f.readlines()
lines = [x.strip() for x in lines]
for line in lines:
if not line or line.startswith('#'):
continue
if 'GDAL_DATA=' in line:
gdal_data_dir = line.split("GDAL_DATA=")[1]
if 'GEOTIFF_CSV='in line:
geotiff_csv_dir = line.split("GEOTIFF_CSV=")[1]
with open(OTBUtils.OTB_CLI_FILE, 'w') as otb_cli_file:
otb_cli_file.write(first_line + os.linesep)
otb_cli_file.write(export_cmd + "LC_NUMERIC=C" + os.linesep)
otb_cli_file.write(export_cmd + "GDAL_DRIVER_PATH=disable" + os.linesep)
otb_cli_file.write(export_cmd + "\"GDAL_DATA=" + gdal_data_dir + "\"" + os.linesep)
otb_cli_file.write(export_cmd + "\"GEOTIFF_CSV=" + geotiff_csv_dir + "\"" + os.linesep)
otb_cli_file.write(export_cmd + "\"OTB_APPLICATION_PATH=" + otb_app_path_env + "\"" + os.linesep)
otb_cli_file.write("\"" + otb_app_launcher + "\"" + app_vargs + os.linesep)
if not os.name == 'nt':
os.chmod(OTBUtils.OTB_CLI_FILE, 0o744)
except BaseException as e:
import traceback
os.remove(OTBUtils.OTB_CLI_FILE)
errmsg = "Cannot write:" + OTBUtils.OTB_CLI_FILE + "\nError:\n" + traceback.format_exc()
QgsMessageLog.logMessage(self.tr(errmsg), self.tr('Processing'), Qgis.Critical)
raise e
def load(self):
group = self.name()
......@@ -272,6 +273,8 @@ class OTBAlgorithmProvider(QgsProcessingProvider):
if os.path.exists(env_profile) and os.path.isfile(env_profile):
OTBUtils.OTB_CLI_FILE = os.path.join(QgsApplication.qgisSettingsDirPath(), 'processing', 'qgis_otb_cli' + cli_ext)
if not os.path.exists(OTBUtils.OTB_CLI_FILE):
self.write_otbcli_wrapper_script()
else:
OTBUtils.OTB_CLI_FILE = os.path.join(folder, 'bin', 'otbcli' + cli_ext)
......@@ -291,7 +294,10 @@ class OTBAlgorithmProvider(QgsProcessingProvider):
return os.path.join(self.descrFolder(d), f)
def otb_app_dir_list(self, v):
folders = v.split(';')
#!hack needed for QGIS < 3.2!#
v = v.replace(';',os.pathsep)
#!hack needed for QGIS < 3.2!#
folders = v.split(os.pathsep)
app_dirs = []
for f in folders:
if f is not None and os.path.exists(f):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment