示例#1
0
文件: base.py 项目: sungkomp/SAMBRO-1
 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
文件: base.py 项目: Akanksha18/eden
 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
文件: codec.py 项目: BIGGANI/eden-1
 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
文件: codec.py 项目: BIGGANI/eden-1
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
文件: codec.py 项目: AkulGupta/eden
def dump(obj, fp, cls=GeoJSONEncoder, **kwargs):
    return simplejson.dump(to_mapping(obj), fp, cls=cls, **kwargs)