Skip to content

Commit

Permalink
Add try-catch for export_tiles to catch file saving issues
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsdukai committed Jan 22, 2020
1 parent 66f5a76 commit ed1ee23
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions cjio_dbexport/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import sys
from pathlib import Path
from io import StringIO
import json

from psycopg2 import Error as pgError
from psycopg2 import sql
Expand Down Expand Up @@ -101,6 +102,7 @@ def export_tiles_cmd(ctx, tiles, merge, dir):
DIR is the path to the output directory.
"""
log = ctx.obj['log']
path = Path(dir).resolve()
if not Path(path.parent).exists():
raise NotADirectoryError(f"Directory {path.parent} not exists")
Expand All @@ -126,11 +128,29 @@ def export_tiles_cmd(ctx, tiles, merge, dir):
for tile in tile_list:
click.echo(f"Exporting tile {str(tile)}")
filepath = (path / str(tile)).with_suffix('.json')
cm = db3dnl.export(conn=conn,
cfg=ctx.obj['cfg'],
tile_list=(tile,))
cityjson.save(cm, path=filepath, indent=None)
click.echo(f"Saved CityJSON tile {str(tile)} to {filepath}")
try:
cm = db3dnl.export(conn=conn,
cfg=ctx.obj['cfg'],
tile_list=(tile,))
except Exception as e:
cm = None
log.error(f"Failed to export tile {str(tile)}\n{e}")
if cm is not None:
try:
cm.remove_duplicate_vertices()
except Exception as e:
log.error(f"Failed to remove duplicate vertices\n{e}")
try:
cm.remove_orphan_vertices()
except Exception as e:
log.error(f"Failed to remove orphan vertices\n{e}")
try:
with open(filepath, 'w') as fout:
json_str = json.dumps(cm.j, indent=None)
fout.write(json_str)
except IOError as e:
log.error(f"Invalid output file: {filepath}\n{e}")
click.echo(f"Saved CityJSON tile {str(tile)} to {filepath}")
except Exception as e:
raise click.exceptions.ClickException(e)
finally:
Expand Down

0 comments on commit ed1ee23

Please sign in to comment.