Skip to content

Commit

Permalink
Allow only EPSG in the CRS URN
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsdukai committed Apr 30, 2019
1 parent 4e85629 commit 328b505
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
7 changes: 6 additions & 1 deletion cjio/cityjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from cjio import geom_help
from cjio import convert
from cjio.errors import InvalidOperation
from cjio.utils import print_cmd_warning


CITYJSON_VERSIONS_SUPPORTED = ['0.6', '0.8', '0.9']
Expand Down Expand Up @@ -151,7 +152,11 @@ def get_epsg(self):
return self.j["metadata"]["crs"]["epsg"]
elif "referenceSystem" in self.j["metadata"]:
s = self.j["metadata"]["referenceSystem"]
return int(s[s.find("::")+2:])
if "epsg" in s.lower():
return int(s[s.find("::")+2:])
else:
print_cmd_warning("Only EPSG codes are supported in the URN. CRS is set to undefined.")
return None
else:
return None

Expand Down
60 changes: 28 additions & 32 deletions cjio/cjio.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ def handle_parse_result(self, ctx, opts, args):
ctx, opts, args)


def print_cmd_status(s):
click.echo(click.style(s, bg='cyan', fg='black'))


@click.group(chain=True)
@click.version_option(version=cjio.__version__)
@click.argument('input', cls=PerCommandArgWantSubCmdHelp)
Expand Down Expand Up @@ -67,15 +63,15 @@ def process_pipeline(processors, input, ignore_duplicate_keys):
raise IOError("File type not supported (only .json, .off, and .poly).")
#-- OFF file
if (extension == '.off'):
print_cmd_status("Converting %s to CityJSON" % (input))
utils.print_cmd_status("Converting %s to CityJSON" % (input))
cm = cityjson.off2cj(f)
#-- POLY file
elif (extension == '.poly'):
print_cmd_status("Converting %s to CityJSON" % (input))
utils.print_cmd_status("Converting %s to CityJSON" % (input))
cm = cityjson.poly2cj(f)
#-- CityJSON file
else:
print_cmd_status("Parsing %s" % (input))
utils.print_cmd_status("Parsing %s" % (input))
cm = cityjson.reader(file=f, ignore_duplicate_keys=ignore_duplicate_keys)
if (cm.get_version() not in cityjson.CITYJSON_VERSIONS_SUPPORTED):
allv = ""
Expand Down Expand Up @@ -131,7 +127,7 @@ def exporter(cm):
else:
os.makedirs(os.path.dirname(output['path']), exist_ok=True)
if format.lower() == 'obj':
print_cmd_status("Exporting CityJSON to OBJ (%s)" % (output['path']))
utils.print_cmd_status("Exporting CityJSON to OBJ (%s)" % (output['path']))
try:
fo = click.open_file(output['path'], mode='w')
re = cm.export2obj()
Expand All @@ -143,7 +139,7 @@ def exporter(cm):
fname = os.path.splitext(os.path.basename(output['path']))[0]
bufferbin = fname + ".glb"
binfile = os.path.join(os.path.dirname(output['path']), bufferbin)
print_cmd_status("Exporting CityJSON to glb %s" % binfile)
utils.print_cmd_status("Exporting CityJSON to glb %s" % binfile)
glb = cm.export2gltf()
# TODO B: how many buffer can there be in the 'buffers'?
try:
Expand All @@ -166,21 +162,21 @@ def exporter(cm):
# if (cm.get_epsg() == None):
# raise click.ClickException("CityJSON has no EPSG defined, can't be reprojected.")
# elif cm.get_epsg() != 4326:
# print_cmd_status("Reprojecting CityJSON to EPSG:4326")
# utils.print_cmd_status("Reprojecting CityJSON to EPSG:4326")
# cm.reproject(3857)
fname = os.path.splitext(os.path.basename(output['path']))[0]
b3dmbin = fname + ".b3dm"
binfile = os.path.join(os.path.dirname(output['path']), b3dmbin)
tilesetfile = os.path.join(os.path.dirname(output['path']), 'tileset.json')
print_cmd_status("Converting CityJSON ot b3dm")
utils.print_cmd_status("Converting CityJSON ot b3dm")
b3dm = cm.export2b3dm()
bbox = cm.update_bbox()
bbox_root = [coordinate * 1.1 for coordinate in bbox] # methinks the root boundingVolume should be larger than that of the children, even when there is only one child
tileset['root']['boundingVolume']['box'] = tiling.compute_obb(bbox_root)
tileset['root']['content']['boundingVolume']['box'] = tiling.compute_obb(bbox)
tileset['root']['content']['uri'] = b3dmbin
del tileset['root']['children']
print_cmd_status("Exporting CityJSON to 3dtiles (%s, %s)" % (tilesetfile, binfile))
utils.print_cmd_status("Exporting CityJSON to 3dtiles (%s, %s)" % (tilesetfile, binfile))
try:
b3dm.seek(0)
with click.open_file(binfile, mode='wb') as bo:
Expand Down Expand Up @@ -231,7 +227,7 @@ def saver(cm):
else:
os.makedirs(os.path.dirname(output['path']), exist_ok=True)

print_cmd_status("Saving CityJSON to a file %s" % output['path'])
utils.print_cmd_status("Saving CityJSON to a file %s" % output['path'])
try:
fo = click.open_file(output['path'], mode='w')
if textures:
Expand Down Expand Up @@ -262,7 +258,7 @@ def update_bbox_cmd():
If there is none then it is added.
"""
def processor(cm):
print_cmd_status("Updating bbox")
utils.print_cmd_status("Updating bbox")
cm.update_bbox()
return cm
return processor
Expand Down Expand Up @@ -292,9 +288,9 @@ def processor(cm):
click.echo(click.style("Folder for schemas unknown. Validation aborted.", fg='red'))
return cm
else:
print_cmd_status('===== Validation (schemas: %s) =====' % (folder_schemas))
utils.print_cmd_status('===== Validation (schemas: %s) =====' % (folder_schemas))
else:
print_cmd_status('===== Validation (schemas v%s) =====' % (cm.j['version']))
utils.print_cmd_status('===== Validation (schemas v%s) =====' % (cm.j['version']))
#-- validate
bValid, woWarnings, errors, warnings = cm.validate(skip_schema=skip_schema, folder_schemas=folder_schemas)
click.echo('=====')
Expand Down Expand Up @@ -333,7 +329,7 @@ def merge_cmd(filepattern):
$ cjio myfile.json merge '/home/elvis/temp/*.json' info
"""
def processor(cm):
print_cmd_status('Merging files')
utils.print_cmd_status('Merging files')
lsCMs = []
g = glob.glob(filepattern)
for i in g:
Expand Down Expand Up @@ -366,7 +362,7 @@ def processor(cm):
click.echo(click.style("Folder for output unknown. Partitioning aborted.", fg='red'))
return cm
else:
print_cmd_status('===== Partitioning CityJSON (output: %s) =====' % (folder_output))
utils.print_cmd_status('===== Partitioning CityJSON (output: %s) =====' % (folder_output))
bbox = cm.update_bbox()
grid_idx = tiling.create_grid(bbox, depth)
partitions = tiling.partitioner(cm, grid_idx)
Expand All @@ -385,7 +381,7 @@ def processor(cm):
f = os.path.basename(filename)
d = os.path.abspath(folder_output)
p = os.path.join(d, f)
print_cmd_status("Saving CityJSON partition to a file (%s)" % (p))
utils.print_cmd_status("Saving CityJSON partition to a file (%s)" % (p))
try:
fo = click.open_file(p, mode='w')
if textures:
Expand Down Expand Up @@ -423,7 +419,7 @@ def subset_cmd(id, bbox, random, cotype, invert):
Option '--invert' inverts the selection, thus delete the selected object(s).
"""
def processor(cm):
print_cmd_status('Subset of CityJSON')
utils.print_cmd_status('Subset of CityJSON')
s = copy.deepcopy(cm)
if random is not None:
s = s.get_subset_random(random, invert=invert)
Expand All @@ -446,7 +442,7 @@ def remove_duplicate_vertices_cmd():
and not those of the textures/templates.
"""
def processor(cm):
print_cmd_status('Remove duplicate vertices')
utils.print_cmd_status('Remove duplicate vertices')
cm.remove_duplicate_vertices()
return cm
return processor
Expand All @@ -460,7 +456,7 @@ def remove_orphan_vertices_cmd():
and not those of the textures/templates.
"""
def processor(cm):
print_cmd_status('Remove orphan vertices')
utils.print_cmd_status('Remove orphan vertices')
cm.remove_orphan_vertices()
return cm
return processor
Expand All @@ -472,7 +468,7 @@ def remove_materials_cmd():
Remove all materials from a CityJSON file.
"""
def processor(cm):
print_cmd_status('Remove all material')
utils.print_cmd_status('Remove all material')
cm.remove_materials()
return cm
return processor
Expand All @@ -485,7 +481,7 @@ def compress_cmd(digit):
Compress a CityJSON file, ie stores its vertices with integers.
"""
def processor(cm):
print_cmd_status('Compressing the CityJSON (with %d digit)' % digit)
utils.print_cmd_status('Compressing the CityJSON (with %d digit)' % digit)
try:
cm.compress(digit)
except Exception as e:
Expand All @@ -500,7 +496,7 @@ def decompress_cmd():
Decompress a CityJSON file, ie remove the "tranform".
"""
def processor(cm):
print_cmd_status('Decompressing the CityJSON')
utils.print_cmd_status('Decompressing the CityJSON')
if (cm.decompress() == False):
click.echo("File is not compressed, nothing done.")
return cm
Expand All @@ -513,7 +509,7 @@ def remove_textures_cmd():
Remove all textures from a CityJSON file.
"""
def processor(cm):
print_cmd_status('Remove all textures')
utils.print_cmd_status('Remove all textures')
cm.remove_textures()
return cm
return processor
Expand All @@ -529,7 +525,7 @@ def update_crs_cmd(newepsg):
To reproject (and thus modify all the values of the coordinates) use reproject().
"""
def processor(cm):
print_cmd_status('Assign EPSG:%d' % newepsg)
utils.print_cmd_status('Assign EPSG:%d' % newepsg)
cm.set_epsg(newepsg)
return cm
return processor
Expand All @@ -543,7 +539,7 @@ def update_crs_cmd(epsg):
The current file must have an EPSG defined (do it with function update_epsg()).
"""
def processor(cm):
print_cmd_status('Reproject to EPSG:%d' % epsg)
utils.print_cmd_status('Reproject to EPSG:%d' % epsg)
if (cm.get_epsg() == None):
click.echo("WARNING: CityJSON has no EPSG defined, can't be reprojected.")
else:
Expand All @@ -562,7 +558,7 @@ def upgrade_version_cmd():
"""
def processor(cm):
vlatest = cityjson.CITYJSON_VERSIONS_SUPPORTED[-1]
print_cmd_status('Upgrade CityJSON file to v%s' % vlatest)
utils.print_cmd_status('Upgrade CityJSON file to v%s' % vlatest)
re, reasons = cm.upgrade_version(vlatest)
if (re == False):
click.echo(click.style("WARNING: %s" % (reasons), fg='red'))
Expand All @@ -576,7 +572,7 @@ def locate_textures_cmd():
Output the location of the texture files.
"""
def processor(cm):
print_cmd_status('Locate the textures')
utils.print_cmd_status('Locate the textures')
loc = cm.get_textures_location()
click.echo(loc)
return cm
Expand All @@ -592,7 +588,7 @@ def update_textures_cmd(newlocation, relative):
Can be used if the texture files were moved to new directory.
"""
def processor(cm):
print_cmd_status('Update location of textures')
utils.print_cmd_status('Update location of textures')
cm.update_textures_location(newlocation, relative=relative)
return cm
return processor
Expand All @@ -608,7 +604,7 @@ def extract_lod_cmd(lod):
if a city object doesn't have this LoD then it is deleted.
"""
def processor(cm):
print_cmd_status('Extract LoD:%s' % lod)
utils.print_cmd_status('Extract LoD:%s' % lod)
cm.extract_lod(lod)
return cm
return processor
Expand Down
11 changes: 10 additions & 1 deletion cjio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os.path
from click import ClickException
from click import echo, style

def verify_filename(filename):
"""Verify if the provided output filename is a file or a directory"""
Expand All @@ -25,4 +26,12 @@ def verify_filename(filename):
res['dir'] = True
else:
res['dir'] = False
return res
return res


def print_cmd_status(s):
echo(style(s, bg='cyan', fg='black'))


def print_cmd_warning(s):
echo(style(s, fg='bright_yellow'))

0 comments on commit 328b505

Please sign in to comment.