Пример #1
0
 def to_instance(cls, ob, default=None, strict=False):
     """Encode a GeoJSON dict into an GeoJSON object.
     
     Assumes the caller knows that the dict should satisfy a GeoJSON type.
     """
     if ob is None and default is not None:
         instance = default()
     elif isinstance(ob, GeoJSON):
         instance = ob
     else:
         mapping = to_mapping(ob)
         d = dict((str(k), mapping[k]) for k in mapping)
         try:
             type_ = d.pop("type")
             geojson_factory = getattr(geojson.factory, type_)
             if not issubclass(geojson_factory, GeoJSON):
                 raise TypeError("""\
                 Not a valid GeoJSON type:
                 %r (geojson_factory: %r, cls: %r)
                 """ % (type_, geojson_factory, cls))
             instance = geojson_factory(**d)
         except (AttributeError, KeyError), invalid:
             if not strict:
                 instance = ob
             else:
                 msg = "Cannot coerce %r into a valid GeoJSON structure: %s"
                 msg %= (ob, invalid)
                 raise ValueError(msg)
Пример #2
0
 def to_instance(cls, ob, default=None, strict=False):
     """Encode a GeoJSON dict into an GeoJSON object.
     
     Assumes the caller knows that the dict should satisfy a GeoJSON type.
     """
     if ob is None and default is not None:
         instance = default()
     elif isinstance(ob, GeoJSON):
         instance = ob
     else:
         mapping = to_mapping(ob)
         d = dict((str(k), mapping[k]) for k in mapping)
         try:
             type_ = d.pop("type")
             geojson_factory = getattr(geojson.factory, type_)
             if not issubclass(geojson_factory, GeoJSON):
                 raise TypeError("""\
                 Not a valid GeoJSON type:
                 %r (geojson_factory: %r, cls: %r)
                 """ % (type_, geojson_factory, cls))
             instance = geojson_factory(**d)
         except (AttributeError, KeyError), invalid:
             if not strict:
                 instance = ob
             else:
                 msg = "Cannot coerce %r into a valid GeoJSON structure: %s"
                 msg %= (ob, invalid)
                 raise ValueError(msg)
Пример #3
0
 def default(self, obj):
     if isinstance(obj, Mapping):
         mapping = obj
     else:
         mapping = to_mapping(obj)
     d = dict(mapping)
     type_str = d.pop("type", None)
     if type_str:
         geojson_factory = getattr(factory, type_str, factory.GeoJSON)
         d = geojson_factory(**d).__geo_interface__
     return d
Пример #4
0
 def default(self, obj):
     if isinstance(obj, Mapping):
         mapping = obj
     else:
         mapping = to_mapping(obj)
     d = dict(mapping)
     type_str = d.pop("type", None)
     if type_str:
         geojson_factory = getattr(factory, type_str, factory.GeoJSON)
         d = geojson_factory(**d).__geo_interface__
     return d
Пример #5
0
def dumps(obj, cls=GeoJSONEncoder, **kwargs):
    return json.dumps(to_mapping(obj), cls=cls, **kwargs)
Пример #6
0
def dumps(obj, cls=GeoJSONEncoder, **kwargs):
    return json.dumps(to_mapping(obj), cls=cls, **kwargs)
Пример #7
0
def dump(obj, fp, cls=GeoJSONEncoder, **kwargs):
    return simplejson.dump(to_mapping(obj), fp, cls=cls, **kwargs)
Пример #8
0
def dump(obj, fp, cls=GeoJSONEncoder, **kwargs):
    return simplejson.dump(to_mapping(obj), fp, cls=cls, **kwargs)