Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: list indices must be integers or slices, not str #138

Closed
balazsdukai opened this issue Apr 28, 2022 · 11 comments
Closed

TypeError: list indices must be integers or slices, not str #138

balazsdukai opened this issue Apr 28, 2022 · 11 comments

Comments

@balazsdukai
Copy link
Member

Discussed in cityjson/specs#115

Originally posted by AdrianKriger April 25, 2022
Hi.

In an attempt to be clever; I sought to update some packages which corrupted an environment. Believing it a sign I further took this as an opportunity to reduce the number of dependencies. All going well.

I now have a TypeError I have no idea how to resolve.

  Input In [5] in <cell line: 1>
    runfile('C:/Adrian/outLinesOnly/osm3DMain2.py', wdir='C:/Adrian/osm_lod1_3dModel/outLinesOnly')

  File ~\miniconda3\envs\osm3D_vc-env\lib\site-packages\debugpy\_vendored\pydevd\_pydev_bundle\pydev_umd.py:175 in runfile
    execfile(filename, namespace)

  File ~\miniconda3\envs\osm3D_vc-env\lib\site-packages\debugpy\_vendored\pydevd\_pydev_bundle\_pydev_execfile.py:25 in execfile
    exec(compile(contents + "\n", file, 'exec'), glob, loc)

  File C:/Adrian/osm_lod1_3dModel/outLinesOnly/osm3DMain2.py:102 in <module>
    main()

  File C:/Adrian/osm_lod1_3dModel/outLinesOnly/osm3DMain2.py:88 in main
    output_cityjson(extent, minz, maxz, t, pts, jparams)

  File C:\Adrian\osm_lod1_3dModel\outLinesOnly\osm3DCode2.py:623 in output_cityjson
    cm = cityjson.load(jparams['cjsn_out'])

  File ~\miniconda3\envs\osm3D_vc-env\lib\site-packages\cjio\cityjson.py:71 in load
    cm.load_from_j(transform=transform)

  File ~\miniconda3\envs\osm3D_vc-env\lib\site-packages\cjio\cityjson.py:251 in load_from_j
    models.Geometry(

  File ~\miniconda3\envs\osm3D_vc-env\lib\site-packages\cjio\models.py:96 in __init__
    self.boundaries = self._dereference_boundaries(type, boundaries, vertices, transform)

  File ~\miniconda3\envs\osm3D_vc-env\lib\site-packages\cjio\models.py:260 in _dereference_boundaries
    s.append([self._vertex_mapper(ring, vertices, transform) for ring in surface])

  File ~\miniconda3\envs\osm3D_vc-env\lib\site-packages\cjio\models.py:260 in <listcomp>
    s.append([self._vertex_mapper(ring, vertices, transform) for ring in surface])

  File ~\miniconda3\envs\osm3D_vc-env\lib\site-packages\cjio\models.py:157 in _vertex_mapper
    return list(map(lambda v_i: Geometry._transform_vertex(vertices[v_i], transform), ring))

  File ~\miniconda3\envs\osm3D_vc-env\lib\site-packages\cjio\models.py:157 in <lambda>
    return list(map(lambda v_i: Geometry._transform_vertex(vertices[v_i], transform), ring))

  File ~\miniconda3\envs\osm3D_vc-env\lib\site-packages\cjio\models.py:174 in _transform_vertex
    x = deepcopy((vertex[0] * transform["scale"][0]) + transform["translate"][0])

TypeError: list indices must be integers or slices, not str

I set the transform Object to:

    cm["transform"] = {
        "scale": [0.0, 0.0, 0.0],
        "translate": [1.0, 1.0, 1.0]
        },

Could this the fault?
Have I made the same mistake with the depth of the lists in the boundaries; as an earlier discussion?

Minimum-working-example here - your help is appreciated.

@balazsdukai balazsdukai transferred this issue from cityjson/specs Apr 28, 2022
@balazsdukai
Copy link
Member Author

@AdrianKriger which version of cjio are you using? See cjio --version

@AdrianKriger
Copy link

AdrianKriger commented Apr 28, 2022

@balazsdukai from the requirements2.txt -> cjio = 0.7.3 # - python CLI to process and manipulate CityJSON files.

@AdrianKriger
Copy link

AdrianKriger commented May 11, 2022

@balazsdukai this is possibly a conda / pip conflict ---on my side.
Tried recreating environment with pip only.

The challenge is Fiona. -> StackExchange.
gdal install's successfully.

I don't know. Pretty much stuck.

@balazsdukai
Copy link
Member Author

Ok, it's not clear to me what causes the issue, but if you set the transform object yourself on the citymodel that you've already loaded with cityjson.load or created yourself, then you need to add it to the .j member of the citymodel.

Like this:

    cm.j["transform"] = {
        "scale": [0.0, 0.0, 0.0],
        "translate": [1.0, 1.0, 1.0]
        },

I know that this is less than self-explanatory and it will change in the future at some point.

For now, I recommend to go through these materials if you are using cjio as a library: https://github.com/3DGI/3dbag-workshop-foss4gnl2021

Let me know if this fixes it.

@AdrianKriger
Copy link

AdrianKriger commented May 13, 2022

Thank you for the response @balazsdukai.

I did make a mistake with the depth of the allsurfaces; as per #79.
Perhaps the new environment adds/removes an extra [...] where the other did not.

The model creates and cm.remove_duplicate_vertices() succeeds without:

    cm["transform"] = {
        "scale": [0.0, 0.0, 0.0],
       "translate": [1.0, 1.0, 1.0]
},

I now want to do as you suggest and add the transform object with:

cm = cityjson.load(jparams['cjsn_out'])
cm.j["transform"] = {
    "scale": [0.0, 0.0, 0.0],
    "translate": [1.0, 1.0, 1.0]},
cityjson.save(cm, jparams['cjsn_CleanOut'])

and

`Traceback (most recent call last):

  Input In [131] in <cell line: 5>
    cm.remove_duplicate_vertices()

  File ~\miniconda3\envs\osm3D_vc-env\lib\site-packages\cjio\cityjson.py:1098 in remove_duplicate_vertices
    a = list(map(int, v.split()))

ValueError: invalid literal for int() with base 10: '281331.92'

@balazsdukai
Copy link
Member Author

Ok, I'm closing this issue then since the original problem is resolved.

Disregard what I wrote about the transform.
If you want to set a transform yourself, then you won't be able to use cityjson.save(), but you'll have to replicate what is in cityjson.save() plus do the vertex transformation yourself. Check the source of cityjson.save for the details.

@AdrianKriger
Copy link

AdrianKriger commented May 14, 2022

@balazsdukai I do not understand what you are saying.

  • Is the transform object not compulsory? Do I have a choice whether to add it or not?
  • What cityjson.save() source?

forgive me for doing this here---
I want to connect with @chchatzi based on Thesis_Master; in reference to this discussion. Can you initiate an introduction please?

@balazsdukai
Copy link
Member Author

Yes, vertex transformation is mandatory per cityjson v1.1, but cityjson.save() does not transform the vertices, because it is not implemented. You need to transform (compress) the vertices manually if you are using cityjson as a library. Check the source code of cityjson.compress() to see how compression works.

Check the source code of cityjson.save() to see the details of what it does

cjio/cjio/cityjson.py

Lines 75 to 95 in 444c696

def save(citymodel, path: str, indent: bool = False):
"""Save a city model to a CityJSON file
:param citymodel: A CityJSON object
:param path: Absolute path to a CityJSON file
"""
citymodel.add_to_j()
if citymodel.is_transformed:
# FIXME: here should be compression, however the current compression does not work with immutable tuples, but requires mutable lists for the points
pass
citymodel.remove_duplicate_vertices()
citymodel.remove_orphan_vertices()
try:
with open(path, 'w') as fout:
if indent:
json_str = json.dumps(citymodel.j, indent="\t")
else:
json_str = json.dumps(citymodel.j, separators=(',',':'))
fout.write(json_str)
except IOError as e:
raise IOError('Invalid output file: %s \n%s' % (path, e))
. But as it says in the comment, you'll need to work around the coordinate tuples in some way if you want to compress them with cityjson.compress().

@AdrianKriger
Copy link

AdrianKriger commented May 18, 2022

Thank you @balazsdukai. I think I understand. I should go something like:

#clean cityjson
cm = cityjson.load(jparams['cjsn_out'])
cm.remove_duplicate_vertices()
cm.remove_orphan_vertices()
cm.j["transform"] = {
    "scale": [0.0, 0.0, 0.0],
    "translate": [1.0, 1.0, 1.0]},
#save/write
try: 
     with open(jparams['cjsn_CleanOut'], 'w') as fout: 
          if indent: 
             json_str = json.dumps(cm, indent="\t") 
          else: 
             json_str = json.dumps(cm, separators=(',',':')) 
          fout.write(json_str) 

I will respond with an affirmative / negative when I have an opportunity.

@balazsdukai
Copy link
Member Author

I really recommend to go through the materials that I mentioned before: https://github.com/3DGI/3dbag-workshop-foss4gnl2021
It explains parts that are missing for your code here, namely the citymodel.add_to_j() function and what that is needed.

Long story short, currently there is some disconnect between the CLI and API methods in cjio. So if you use cjio as a library, you need to be aware of this.

@AdrianKriger
Copy link

@balazsdukai I am doing something wrong.
Right now; without the transform object the result has duplicate_vertices() and orphan_vertices() removed. The file size reflects this.
With the transform object the file is bloated and does not open.
I am not including the transform object.

You have been very patient and generous with me @balazsdukai. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants