poliastro / czml3

Python 3 library to write CZML
https://pypi.org/project/czml3/
MIT License
38 stars 30 forks source link

IntervalValue does not accept integer or float #69

Closed khoohuibo closed 3 years ago

khoohuibo commented 3 years ago

I seem to be unable to insert values other than booleans into an IntervalValue, defined within types.py

I essentially run into this error upon hitting the following function:

    obj_dict.update(**self._value.to_json())

AttributeError: 'int' object has no attribute 'to_json'

And another exception occurs as well

    key = TYPE_MAPPING[type(self._value)]

KeyError: <class 'int'>

This is interesting because I am inserting a number '1' as the value, and json.dumps works on that number.

Now i noticed that TYPE_MAPPING is a dictionary defined at the top.

TYPE_MAPPING = {bool: "boolean"}

Obviously to circumvent the error, all one needs to do is insert the keys for int and/or float

Now my question is, is my integer and float type meant to fail? Does Cesium's interval values not accept intervalvalues of numbers apart from boolean values?

I am trying to create a time-varying width of polylines using the IntervalValues, considering that IntervalValues can accept BooleanValues. I would like to vary the width of the polylines between 1-5 dependent on another parameter in my simulation.

I am using Spyder 4.1.4, Python 3.7,7, My operating system is windows 10.

Here is the class in question

@attr.s(repr=False, frozen=True, kw_only=True)
class IntervalValue(BaseCZMLObject):
    """Value over some interval."""

    _start = attr.ib()
    _end = attr.ib()
    _value = attr.ib()

    def to_json(self):
        obj_dict = {"interval": TimeInterval(start=self._start, end=self._end)}

        try:
            obj_dict.update(**self._value.to_json())
        except AttributeError:
            key = TYPE_MAPPING[type(self._value)]
            obj_dict[key] = self._value

        return obj_dict
astrojuanlu commented 3 years ago

Hi @khoohuibo , thanks for your interest in czml3! I guess you refer to this piece of code:

https://github.com/poliastro/czml3/blob/d7f0b72193087811780da65bae4a986eb6eab0e7/src/czml3/types.py#L318-L335

And here are a couple of examples of how to use IntervalValue from the tests:

https://github.com/poliastro/czml3/blob/f0136f46e03d9d9134a5bfa06164c10d95315ac4/tests/test_properties.py#L540-L543

https://github.com/poliastro/czml3/blob/d7f0b72193087811780da65bae4a986eb6eab0e7/src/czml3/examples/simple.py#L31-L33

Would you please paste here what code is producing the error you describe, so we can try to reproduce it? For my understanding everything is working as expected, but CZML documentation is sparse and it's also possible that I'm missing something.

astrojuanlu commented 3 years ago

Closing because of inactivity.