cityjson / cjio

CityJSON/io: Python CLI to process and manipulate CityJSON files
MIT License
126 stars 32 forks source link

Trying to convert to glb a cityjson failing at triangulation #168

Closed collaer closed 1 year ago

collaer commented 1 year ago

Describe the bug With a given a cityjson file (enclosed), I was provided, I am trying to convert it into a glb file in order to use in in open3d.

The process is going throw if I specify not to triangulate, but geometries are wrong (triangles mess) but were looking good in cityjsonninja, see screenshots.

If I specify do_triangulate=True (I beleive that could be the missing step) I receive the following error message:

Houston we have a problem...
Failed to triangulate face in CityObject T1402_1-0
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [81], in <cell line: 4>()
      2 model.filter_lod("2.2")
      3 temporarylocation = data_dir2 / 'tmp.glb'
----> 4 glb = model.export2glb(do_triangulate=True)
      5 glb.seek(0)
      6 with open(temporarylocation,'wb') as out:

File ~\x\lib\site-packages\cjio\cityjson.py:1576, in CityJSON.export2glb(self, do_triangulate)
   1574 def export2glb(self, do_triangulate=True):
   1575     self.decompress()
-> 1576     glb = convert.to_glb(self, do_triangulate=do_triangulate)
   1577     return glb

File ~\x\lib\site-packages\cjio\convert.py:398, in to_glb(cm, do_triangulate)
    396 accessor["count"] = len(vtx_idx_np)
    397 accessor["type"] = "SCALAR"
--> 398 accessor["max"] = [int(vtx_idx_np.max())]
    399 accessor["min"] = [int(vtx_idx_np.min())]
    400 accessors.append(accessor)

File ~\x\lib\site-packages\numpy\core\_methods.py:40, in _amax(a, axis, out, keepdims, initial, where)
     38 def _amax(a, axis=None, out=None, keepdims=False,
     39           initial=_NoValue, where=True):
---> 40     return umr_maximum(a, axis, None, out, keepdims, initial, where)

ValueError: zero-size array to reduction operation maximum which has no identity

My questions are:
Is the cityjson not well formed or has any quality issue I should check and filter?
Am I doing something wrong?
Where should I look first and any tips if I would like to help to fix that issue?
Is there another better way to load a cityjson file into open3d python, I'd like to compute some raytracing intersections?

cjio version What does cjio --version print? Or did you install from a git branch? It's a recent pip install cjio: cjio.version is '0.8.1'
Cityjson file version is 1.1

To Reproduce Steps to reproduce the behavior? What error message do you see in the console? The code snippet is the following:

from cjio import cityjson
model = cityjson.load('tile.city.json', transform=True)
model.filter_lod("2.2")
temporarylocation = 'tmp.glb'
glb = model.export2glb(do_triangulate=True)
glb.seek(0)
with open(temporarylocation,'wb') as out:
    out.write(glb.getvalue())

With that cityjson file: enclosed here tile.city.json.txt (added txt to upload it here) tile.city.json.txt

Expected behavior Being able to convert cityjson lod2.2 to a glb or other 3d format in order to load 2.2 into open3d python.

Screenshots In ninja: image

In glb viewer (loading glb created when triangulation is set to False): image

Desktop (please complete the following information):

collaer commented 1 year ago

I solved the issue, my bad.

The root cause was me not understanding that triangle wasn't installed: https://github.com/cityjson/cjio/blob/master/cjio/geom_help.py#L140

Maybe the error message when triangle is not installed (Houston we have a problem) could be reviewed. Either the error message could be improved or we could check if MODULE_TRIANGLE_AVAILABLE is True as a prerequisite to triangulate?

That's simple enough ticket that I could send as a MR if you think it's worth it?

So if someone experience this, just a pip install triangle will solve it

You could validate if triangle is intalled by

from cjio import geom_help
print(geom_help.MODULE_TRIANGLE_AVAILABLE )
balazsdukai commented 1 year ago

Hi, thanks for reporting. cjio does check if one of the required triangulator libraries are installed and gives a warning in the console if not. But only when you use cjio as a CLI, as cjio was intended to be used. When you use cjio as a library, as you do, then there is no warning, and there is not going to be anytime soon either. If we ever sort out (refactor) cjio so that it works well both as a CLI and an API, then API logging will be implemented as well. Because of this, please don't send a MR for this, we won't merge it.

collaer commented 1 year ago

Thanks for the explanations!