Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
xmd5a2 committed Dec 18, 2020
1 parent 4454249 commit c1cf606
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
QGIS-xtopo v0.2.20201213
QGIS-xtopo v0.2.4
================
![GitHub Logo](/docs/splash.png)

Expand Down
4 changes: 3 additions & 1 deletion docker_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,7 @@ if [[ $(docker container ls | grep qgis-xtopo) ]] ; then
docker exec -it --user user qgis-xtopo /app/init_docker.sh
fi
if [[ $RUN_CHAIN == true ]] ; then
. docker_exec_qgis.sh
if [[ ! -f $config_dir/err_prepare_data.flag ]] && [[ ! -f $config_dir/err_populate_db.flag ]] ; then
. docker_exec_qgis.sh
fi
fi
86 changes: 81 additions & 5 deletions gui/qgis-xtopo-gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,24 @@ def main():
i = 0
while True:
event, values = window.read(timeout=300)
config_dir = values["qgis_projects_dir"] + slash_str + "qgisxtopo-config"
populate_db_flag_path = config_dir + slash_str + "err_populate_db.flag"
prepare_data_flag_path = config_dir + slash_str + "err_populate_db.flag"
if os.name == "nt":
if os.path.isfile(populate_db_flag_path):
raise_docker_errors(populate_db_flag_path, '')
try:
os.remove(populate_db_flag_path)
except Exception:
print("Error removing " + populate_db_flag_path)
if os.path.isfile(prepare_data_flag_path):
raise_docker_errors('', prepare_data_flag_path)
try:
os.remove(prepare_data_flag_path)
except Exception:
print("Error removing " + prepare_data_flag_path)
if i == 0:
config_dir = values["qgis_projects_dir"] + slash_str + "qgisxtopo-config"

if docker_installed:
print(translations.get("pulling_image", "Pulling image from DockerHub"))
runCommand(cmd="docker pull " + remote_repo_name, window=window)
Expand All @@ -319,6 +335,11 @@ def main():
update_free_text_color(values)
i += 1
if event in (sg.WIN_CLOSED, 'exit'):
try:
os.remove(values["qgis_projects_dir"] + slash_str + "qgisxtopo-config" + slash_str + "err_populate_db.flag")
os.remove(values["qgis_projects_dir"] + slash_str + "qgisxtopo-config" + slash_str + "err_prepare_data.flag")
except Exception:
pass
break

if timer_running:
Expand Down Expand Up @@ -464,6 +485,7 @@ def main():
terminal_command_params_list = get_terminal_command_params_list(
'docker exec -it --user user qgis-xtopo /app/populate_db.sh', '', True)
subprocess.Popen(terminal_command_params_list).wait()
raise_docker_errors(populate_db_flag_path, '')
else:
if os.name == "nt":
subprocess.Popen(
Expand All @@ -477,6 +499,7 @@ def main():
terminal_command_params_list = get_terminal_command_params_list(
'docker exec -it --user user qgis-xtopo /app/prepare_data.sh', '', True)
subprocess.Popen(terminal_command_params_list).wait()
raise_docker_errors('', prepare_data_flag_path)
else:
if os.name == "nt":
subprocess.Popen(
Expand Down Expand Up @@ -681,7 +704,7 @@ def start(values, run_chain):
sg.Popup(translations.get('bounding_box_too_large', 'Bounding box is too large. Creating a map will take a '
'long time'), title=translations.get('warning', 'Warning'))

init_docker(run_chain)
init_docker(run_chain, values)
config = values["qgis_projects_dir"] + slash_str + "qgisxtopo-config" + slash_str + "config.ini"
if values['terrain_src_dir']:
for v in range(0, 5):
Expand All @@ -703,10 +726,12 @@ def start(values, run_chain):
file.write(filedata)


def init_docker(run_chain):
def init_docker(run_chain, values):
if docker_installed:
if "qgis-xtopo" in str(
subprocess.check_output(['docker', 'ps'], stdin=subprocess.PIPE, stderr=subprocess.STDOUT)):
populate_db_flag_path = values["qgis_projects_dir"] + slash_str + "qgisxtopo-config" + slash_str + "err_populate_db.flag"
prepare_data_flag_path = values["qgis_projects_dir"] + slash_str + "qgisxtopo-config" + slash_str + "err_prepare_data.flag"
if os.name == "posix":
run_chain_filename = ''
if run_chain:
Expand All @@ -718,8 +743,11 @@ def init_docker(run_chain):
f = open(run_chain_filename, "w+")
f.write("#!/bin/bash\n")
f.write("docker exec --user user qgis-xtopo /app/init_docker.sh\n")
f.write("if [[ ! -f " + populate_db_flag_path + " ]] && [[ ! -f " + prepare_data_flag_path + " ]] ; "
"then\n")
f.write("xhost +local:docker\n")
f.write("docker exec -it --user user qgis-xtopo /app/exec_qgis.sh\n")
f.write("fi\n")
f.close()
os.chmod(run_chain_filename, 0o755)
terminal_command_params_list = get_terminal_command_params_list(
Expand All @@ -729,15 +757,63 @@ def init_docker(run_chain):
os.remove(run_chain_filename)
except OSError:
pass
raise_docker_errors(populate_db_flag_path, prepare_data_flag_path)
else:
if os.name == "nt":
subprocess.Popen(
['cmd', '/c start /wait cmd /c docker exec --user user qgis-xtopo /app/init_docker.sh'],
shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).wait()
if run_chain:
if run_chain and (not os.path.isfile(populate_db_flag_path) and not os.path.isfile(prepare_data_flag_path)):
run_qgis()


def raise_docker_errors(populate_db_flag_path, prepare_data_flag_path):
if os.path.isfile(populate_db_flag_path):
f = open(populate_db_flag_path, "r")
populate_db_error_code = f.read(1)
if populate_db_error_code:
if int(populate_db_error_code) == 1:
sg.Popup(translations.get('cropped_extract_is_empty_error', 'Cropped extract is empty. Check that bounding box matches the OSM data area.'), title=translations.get('error', 'Error'))
else:
sg.Popup(translations.get('populating_db_error', 'Error populating Overpass database'), title=translations.get('error', 'Error'))
if os.name == "posix":
try:
os.remove(populate_db_flag_path)
except Exception:
print("Error removing " + populate_db_flag_path)
if os.path.isfile(prepare_data_flag_path):
f = open(prepare_data_flag_path, "r")
prepare_data_error_code = f.read(1)
if prepare_data_error_code:
if int(prepare_data_error_code) == 1:
sg.Popup(translations.get('osmtogeojson_error', 'osmtogeojson error. Try reducing bbox.'), title=translations.get('error', 'Error'))
else:
if int(prepare_data_error_code) == 2:
sg.Popup(translations.get('vector_data_incomplete_error', 'Vector data is incomplete. It looks '
'like overpass server has interrupted '
'the transmission. Try again or use '
'another Overpass instance or Overpass '
'server inside docker.'),
title=translations.get('error', 'Error'))
else:
if int(prepare_data_error_code) == 3:
sg.Popup(translations.get('overpass_server_error',
'Overpass server error. Try again or use another Overpass instance '
'or Overpass server inside docker.'),
title=translations.get('error', 'Error'))
if int(prepare_data_error_code) == 4:
sg.Popup(translations.get('terrain_data_not_found_error',
'Terrain data not found. Check that terrain covered area matches bounding box.'),
title=translations.get('error', 'Error'))
else:
sg.Popup(translations.get('data_preparation_error', 'Data preparation error. Check parameters.'), title=translations.get('error', 'Error'))
if os.name == "posix":
try:
os.remove(prepare_data_flag_path)
except Exception:
print("Error removing " + prepare_data_flag_path)


def get_terminal_command_params_list(cmd, run_chain_filename, hold):
get_terminal_name()
terminal_command_params_list = []
Expand Down Expand Up @@ -1022,7 +1098,7 @@ def init_config(path, values):
params += get_working_repo_name()
command = command_to_run + params
runCommand(cmd=command, window=window)
init_docker(False)
init_docker(False, values)
# start(values, False)


Expand Down
9 changes: 8 additions & 1 deletion gui/translations_en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ or_use_external_overpass_instance=or use external Overpass instance.
bounding_box_too_large=Bounding box is too large. Creating a map will take a long time.
terrain_input_dir_empty_error=You have selected "Download terrain manually" but terrain directory is empty. Download the data and place it in the following directory.
invalid_overpass_endpoint_external_error=Invalid Overpass external endpoint
qgis_projects_dir_not_writable=QGIS projects directory is not writable. Choose another directory.
qgis_projects_dir_not_writable=QGIS projects directory is not writable. Choose another directory.
cropped_extract_is_empty_error=Cropped extract is empty. Check that bounding box matches the OSM data area.
populating_db_error=Error populating Overpass database
osmtogeojson_error=osmtogeojson error. Try reducing bbox.
data_preparation_error=Data preparation error. Check parameters.
vector_data_incomplete_error=Vector data is incomplete. It looks like Overpass server has interrupted the transmission. Try again or use another instance.
overpass_server_error=Overpass server error. Try again or use another instance.
terrain_data_not_found_error=Terrain data not found. Check that terrain covered area matches bounding box.
9 changes: 8 additions & 1 deletion gui/translations_ru.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ or_use_external_overpass_instance=или используйте внешний O
bounding_box_too_large=Границы зоны охвата слишком широки. Создание карты займёт много времени.
terrain_input_dir_empty_error=Вы выбрали "Cкачать рельеф вручную" но каталог с рельефом пуст. Скачайте данные и поместите их в следующий каталог.
invalid_overpass_endpoint_external_error=Ошибка в адресе внешнего сервера Overpass
qgis_projects_dir_not_writable=Не могу записать в каталог проектов QGIS. Выберите другой каталог.
qgis_projects_dir_not_writable=Не могу записать в каталог проектов QGIS. Выберите другой каталог.
cropped_extract_is_empty_error=Усечённый экстракт пуст. Проверьте что зона охвата находится внутри области, покрываемой данными OSM.
populating_db_error=Ошибка заполнения базы данных Overpass
osmtogeojson_error=Ошибка конвертации OSM в GeoJSON. Попробуйте уменьшить зону охвата.
data_preparation_error=Ошибка подготовки данных. Проверьте параметры.
vector_data_incomplete_error=Векторные данные неполные. Похоже что сервер Overpass прервал передачу. Попробуйте снова или используйте другой сервер.
overpass_server_error=Ошибка сервера Overpass. Попробуйте снова или используйте другой сервер.
terrain_data_not_found_error=Данные рельефа не найдены. Проверьте что область, покрываемая ими, входит в зону охвата.
6 changes: 5 additions & 1 deletion init_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ if [[ ! -f "$project_dir/$project_name.qgz" ]] ; then
else
echo -e "\033[93mQGIS project '"$project_dir/$project_name.qgz"' already exists. Usually it's ok.\033[0m"
fi
rm -f $config_dir/err_populate_db.flag
rm -f $config_dir/err_prepare_data.flag
if [[ $RUN_CHAIN == true ]] ; then
if [[ $OVERPASS_INSTANCE == docker ]] ; then
. /app/populate_db.sh
fi
. /app/prepare_data.sh
if [[ ! -f $config_dir/err_populate_db.flag ]] ; then
. /app/prepare_data.sh
fi
fi
22 changes: 16 additions & 6 deletions populate_db.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
#!/bin/bash
# Populate Overpass DB from local sources in osm_data_dir
config_dir=/mnt/qgis_projects/qgisxtopo-config
err_flag_name=err_populate_db.flag
rm -f $config_dir/$err_flag_name

function make_error_flag {
touch $config_dir/$err_flag_name
if [[ $1 ]] ; then
echo $1 > $config_dir/$err_flag_name
fi
}

if [[ ! -f /.dockerenv ]] ; then
echo -e "\033[91mThis script is not meant to run outside the docker container. Stopping.\033[0m" && exit 1;
echo -e "\033[91mThis script is not meant to run outside the docker container. Stopping.\033[0m" && make_error_flag && exit 1;
fi
if [[ -f $config_dir/config.ini ]] ; then
. $config_dir/config.ini
if [[ -f $config_dir/set_dirs.ini ]] ; then
. $config_dir/set_dirs.ini
fi
else
echo -e "\033[91mconfig.ini not found. Check project installation integrity. Stopping.\033[0m" && exit 1;
echo -e "\033[91mconfig.ini not found. Check project installation integrity. Stopping.\033[0m" && make_error_flag && exit 1;
fi
if [[ -f $config_dir/config_debug.ini ]] ; then
. $config_dir/config_debug.ini
Expand All @@ -19,7 +29,7 @@ fi
overpass_db_dir=$qgis_projects_dir/overpass_db
osm_tmp_dir=$osm_data_dir/tmp
if [[ ! -d $osm_data_dir ]] ; then
echo -e "\033[91mosm_data_dir in project_dir does not exist. Stopping.\033[0m" && exit 1;
echo -e "\033[91mosm_data_dir in project_dir does not exist. Stopping.\033[0m" && make_error_flag && exit 1;
fi
if [[ ! -d $osm_tmp_dir ]] ; then
mkdir $osm_tmp_dir
Expand Down Expand Up @@ -67,7 +77,7 @@ function merge_populate {
echo -e "\e[104mCropping extract by bbox $bbox\033[0m"
osmconvert -b=$bbox --complex-ways --complete-ways $osm_tmp_dir/input.pbf --out-osm | lbzip2 > $osm_tmp_dir/input.osm.bz2
if [[ $(wc -c <"$osm_tmp_dir/input.osm.bz2") -le 400 ]] ; then
echo -e "\033[91mError. Cropped extract is empty. Check that bbox parameter matches the OSM data area or turn off 'overpass_endpoint_docker_use_bbox' option.\033[0m" && exit 1;
echo -e "\033[91mError. Cropped extract is empty. Check that bbox parameter matches the OSM data area or turn off 'overpass_endpoint_docker_use_bbox' option.\033[0m" && make_error_flag 1 && exit 1;
fi
else
osmium cat $pbf_str $osm_str $osmbz2_str $o5m_str -o $osm_tmp_dir/input_tmp.pbf -f pbf
Expand All @@ -86,10 +96,10 @@ function merge_populate {
rm -f $osm_data_dir/*.osm
rm -f $osm_data_dir/*.osm.bz2
else
echo -e "\033[91mError populating Overpass database\033[0m" && exit 1;
echo -e "\033[91mError populating Overpass database\033[0m" && make_error_flag 2 && exit 1;
fi
else
echo -e "\033[91mError. No OSM data files found in $osm_data_dir.\033[0m" && exit 1;
echo -e "\033[91mError. No OSM data files found in $osm_data_dir.\033[0m" && make_error_flag && exit 1;
fi
}

Expand Down
Loading

0 comments on commit c1cf606

Please sign in to comment.