Coverage for plugins/geoJSON/server/features : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/env python # -*- coding: utf-8 -*-
############################################################################### # Copyright Kitware Inc. # # Licensed under the Apache License, Version 2.0 ( the "License" ); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ###############################################################################
This module contains helper classes for transforming arbitrary objects into geoJSON objects. The intended usage is to iterate on a mongo cursor to construct feature collections. For example, a girder plugin can transform an item query into a geoJSON object of point features. Assuming items are stored as follows:
{ "meta": { "lat": *, "lon": *, "someproperty": * } }
This module can be used as follows with cursor returned by a mongo query over some girder items:
>>> pointSpec = { \ "latitude": "meta.lat", \ "longitude": "meta.lon", \ "keys": ["meta"], \ "flatten": ["meta"] \ } >>> collection = FeatureCollection(myPoints=pointSpec) >>> obj = features(myPoints=cursor)
The resulting object will contain an array of point features with properties set to the item metadata as follows:
{ "type": "FeatureCollection", "features": [ { "geometry": { "type": "Point", "coordinates": [ *, * ] }, "type": "Feature", "properties": { "lat": *, "lon": *, "someproperty": * } }, ... ] } """
""" Base exception class for all errors raised by this module. """
""" This exception is raised when a method cannot iterate over an expected iterable argument. """
""" This exception is raised when an invalid accessor is encountered. """
""" This exception is raised when trying to access an invalid property via an accessor. """
""" This exception is raised when an abstract method or class is called. """
""" This exception is raised by a feature class when it is passed an invalid spec object. """
""" Base class for all classes in this module. """
def __call__(self, *arg, **kw): # pragma: no cover """ The call method is used by subclasses to transform and argument or iterable into a specific geoJSON component. It must be overriden by subclasses. """ raise AbstractMethodException
def map(cls, func, data): """ Call the method `func` on every element of the iterable `data`. Returns a list of the returned results. """ "Expected an iterable but got a '%s'" % type(data) )
def get(cls, acc, data): """ Get a property of a data elemennt determined by an accessor.
The data objects should implement the [] operator like a `dict` or `list`.
The accessor can either be a string or an int. If it is an int then `data[acc]` is returned. If it is a string and the data type is a tuple or list, then it is coerced into an int.
The accessor can also reference subobjects by using a '.' to seperate keys. For example:
>>> Base.get('a.b.1', {'a': {'b': [10, 11]}}) 11 """
"Invalid data type '%s' for accessor '%s'" % (type(data), acc) ) "Property '%s' not found in data" % str(acc) )
""" This class formats GIS coordinates into a geoJSON position array. """ """ Initialize the Position object with accessors to the longitude and latitude. """
""" Convert an object to a geoJSON position array. """
# more geoJSON coordinate types to be implemented """unimplemented"""
"""unimplemented"""
"""unimplemented"""
"""unimplemented"""
""" Base class for generating geoJSON geometries. """
""" Construct a Geometry object. Keyword arguments are passed to the coordinate class associated with the geometry. """ if self.coordinate is None: # pragma: no cover raise AbastractMethodException
""" Convert an object into a geoJSON geometry. """ if self.coordinate is None: # pragma: no cover raise AbstractMethodException
'coordinates': self.coord(d), 'type': self.typeName }
""" A geoJSON Point geometry generator class. """
# more geoJSON geometries to be implemented """unimplemented"""
"""unimplemented"""
"""unimplemented"""
""" A base class for generating geoJSON features. """
""" Initialize the feature object. Extra keyword arguments are passed to the associated geometry class.
:param keys: The keys from the object to add as properties to the geoJSON feature. If this is parameter is None, then all keys are used. :type keys: [str] :param flatten: An array of keys that map to objects that will be used to extend the properties. This is used to flatten hierarchical objects into a single object. :type flatten: [str] """ if self.geometry is None: # pragma: no cover raise AbstractMethodException
def filter(cls, d, keys): """ Return a copy of the object `d` with only the given keys. """
def flat(cls, d, key): """ Remove `key` from `d` and extend it by the dictionary referenced by it. """
""" Convert an object into a geoJSON feature. """ if self.geometry is None: # pragma: no cover raise AbstractMethodException
'type': self.typeName, 'properties': meta, 'geometry': self._geometry(d) }
""" A geoJSON point feature generator class. """
""" A geoJSON feature collection generator class. """ 'point': PointFeature }
def getFeatureClass(cls, name): """ Get the actual class for a given feature name string. """ except KeyError: # pragma: no cover raise InvalidKeyException('Unknown feature type "%s"' % name)
""" Initialize a FeatureCollection object. :param features: A dictionary of specifications for feature conversions. Each key represents a named specification that maps to a dictionary of the form {'type': featureType, ...}. Additional values in this dictionary are passed as keyword arguments to the feature constructor. :type features: dict """
""" Add a feature specification to the collection. """
""" Convert one or more iterables into geoJSON feature collection. Iterable datasets should be passed as keyword arguments to this method. The argument name must be a feature defined through the constructor or added later by addFeatureSpec. """
'type': self.typeName, 'features': features } |