Exemple #1
0
    def maybe(cls, x):
        if isinstance(x, Rectangle):
            return x

        if com.is_listlike_2elem(x):
            x = com._flatten_list_of_listlike(x)
        if com.is_listlike(x) and len(x) == 4:
            return Rectangle.fromDegrees(*x)
        return x
Exemple #2
0
    def maybe(cls, x, degrees=False):
        """ Convert list or tuple to Cartesian2 """
        if isinstance(x, Cartesian2):
            return x

        x = shapefile._maybe_shapely_point(x)
        if com.is_listlike(x) and len(x) == 2:
            return Cartesian2(*x, degrees=degrees)
        return x
Exemple #3
0
 def _fill_by(self, x, length, key, default=None):
     if com.is_listlike(x):
         if len(x) != length:
             msg = "{key} length must be {length}: {x}"
             raise ValueError(msg.format(key=key, length=length, x=x))
     else:
         if x is None:
             # fill by other default if specified
             x = default
         x = [x] * length
     return x
Exemple #4
0
    def maybe(cls, x, degrees=False):
        """ Convert list or tuple to Cartesian3 """
        if isinstance(x, Cartesian3):
            return x

        x = shapefile._maybe_shapely_point(x)

        # currently, only Cartesian3 tries to geocode passed loc
        x = geocode._maybe_geocode(x, height=0)
        if com.is_listlike(x):
            if len(x) == 3:
                return Cartesian3(*x, degrees=degrees)
            elif len(x) == 2 and degrees:
                # if degrees is True, z can filled by 0
                # otherwise raise (non-degrees Cartesian is used in Box)
                return Cartesian3(x=x[0], y=x[1], z=0, degrees=degrees)
        return x
Exemple #5
0
    def maybe(cls, x):
        """ Convert str or tuple to ColorConstant """
        if isinstance(x, Color):
            return x

        if isinstance(x, six.string_types):
            cname = x.upper()
            cname = _SINGLE_COLORS.get(cname, cname)

            if cname in _COLORS:
                return ColorConstant(name=cname)
        elif com.is_listlike(x):
            if len(x) in (3, 4):
                return Color(*x)

        msg = 'Unable to convert to Color instance: {x}'
        raise ValueError(msg.format(x=x))
Exemple #6
0
    def add(self, item, **kwargs):
        if com.is_listlike(item):
            for i in item:
                self.add(i, **kwargs)
        elif isinstance(item, self._allowed):
            for key, value in six.iteritems(kwargs):
                setattr(item, key, value)
            self._items.append(item)
        else:
            msg = 'item must be {allowed} instance: {item}'

            if isinstance(self._allowed, tuple):
                allowed = ', '.join([a.__name__ for a in self._allowed])
            else:
                allowed = self._allowed

            raise ValueError(msg.format(allowed=allowed, item=item))
Exemple #7
0
def _maybe_geocode(x, height=None):
    """
    geocode passed str or its list-like
    height can be used to create base data for Cartesian3
    """
    if isinstance(x, six.string_types):
        loc = _GEOCODER.geocode(x)
        if loc is not None:
            if height is None:
                # return x, y order
                return loc.longitude, loc.latitude
            else:
                return loc.longitude, loc.latitude, height
    elif com.is_listlike(x):
        return [_maybe_geocode(e) for e in x]

    return x
Exemple #8
0
def _maybe_cartesian2_list(x, key):
    """
    Convert list or tuple to list of Cartesian2 instances. Used by PolylineVolume
    """
    if com.is_listlike(x):
        if all(isinstance(e, Cartesian2) for e in x):
            return x
        elif all(isinstance(e, _Cartesian) for e in x):
            # for better error message
            msg = '{key} must be a listlike of Cartesian2: {x}'
            raise ValueError(msg.format(key=key, x=x))

        if com.is_listlike_2elem(x):
            x = com._flatten_list_of_listlike(x)

    x = com.validate_listlike_even(x, key=key)
    x = [Cartesian2(i, j) for (i, j) in zip(x[::2], x[1::2])]
    return x
Exemple #9
0
    def flyTo(self, destination, orientation=None):
        from cesiumpy.entities.entity import _CesiumEntity
        import cesiumpy.extension.geocode as geocode

        if isinstance(destination, _CesiumEntity):
            # if entity has a position (not positions), use it
            if destination.position is not None:
                destination = destination.position

        destination = geocode._maybe_geocode(destination, height=100000)

        if com.is_listlike(destination) and len(destination) == 4:
            destination = cartesian.Rectangle.maybe(destination)
        else:
            destination = cartesian.Cartesian3.maybe(destination, degrees=True)
        self.destination = destination
        self.orientation = com.notimplemented(orientation)

        return self