예제 #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)