/usr/lib/python2.7/dist-packages/geojson/base.py is in python-geojson 1.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | from __future__ import unicode_literals
import geojson
from geojson.mapping import to_mapping
class GeoJSON(dict):
"""
A class representing a GeoJSON object.
"""
def __init__(self, iterable=(), **extra):
"""
Initialises a GeoJSON object
:param iterable: iterable from which to draw the content of the GeoJSON
object.
:type iterable: dict, array, tuple
:return: a GeoJSON object
:rtype: GeoJSON
"""
super(GeoJSON, self).__init__(iterable)
self["type"] = getattr(self, "type", type(self).__name__)
self.update(extra)
def __repr__(self):
return geojson.dumps(self, sort_keys=True)
__str__ = __repr__
def __getattr__(self, name):
"""
Permit dictionary items to be retrieved like object attributes
:param name: attribute name
:type name: str, int
:return: dictionary value
"""
try:
return self[name]
except KeyError:
raise AttributeError(name)
def __setattr__(self, name, value):
"""
Permit dictionary items to be set like object attributes.
:param name: key of item to be set
:type name: str
:param value: value to set item to
"""
self[name] = value
def __delattr__(self, name):
"""
Permit dictionary items to be deleted like object attributes
:param name: key of item to be deleted
:type name: str
"""
del self[name]
@property
def __geo_interface__(self):
if self.type != "GeoJSON":
return self
@classmethod
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.
:param cls: Dict containing the elements to be encoded into a GeoJSON
object.
:type cls: dict
:param ob: GeoJSON object into which to encode the dict provided in
`cls`.
:type ob: GeoJSON
:param default: A default instance to append the content of the dict
to if none is provided.
:type default: GeoJSON
:param strict: Raise error if unable to coerce particular keys or
attributes to a valid GeoJSON structure.
:type strict: bool
:return: A GeoJSON object with the dict's elements as its constituents.
:rtype: GeoJSON
:raises TypeError: If the input dict contains items that are not valid
GeoJSON types.
:raises UnicodeEncodeError: If the input dict contains items of a type
that contain non-ASCII characters.
:raises AttributeError: If the input dict contains items that are not
valid GeoJSON types.
"""
if ob is None and default is not None:
instance = default()
elif isinstance(ob, GeoJSON):
instance = ob
else:
mapping = to_mapping(ob)
d = {}
for k in mapping:
d[k] = mapping[k]
try:
type_ = d.pop("type")
try:
type_ = str(type_)
except UnicodeEncodeError:
# If the type contains non-ascii characters, we can assume
# it's not a valid GeoJSON type
raise AttributeError(
"{0} is not a GeoJSON type").format(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) as invalid:
if strict:
msg = "Cannot coerce %r into a valid GeoJSON structure: %s"
msg %= (ob, invalid)
raise ValueError(msg)
instance = ob
return instance
|