File: examples.py

package info (click to toggle)
python-geojson 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 288 kB
  • sloc: python: 1,081; makefile: 7
file content (69 lines) | stat: -rw-r--r-- 2,059 bytes parent folder | download
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
"""
SimpleWebFeature is a working example of a class that satisfies the Python geo
interface.
"""


class SimpleWebFeature:

    """
    A simple, Atom-ish, single geometry (WGS84) GIS feature.
    """

    def __init__(self, id=None, geometry=None, title=None, summary=None,
                 link=None):
        """
        Initialises a SimpleWebFeature from the parameters provided.

        :param id: Identifier assigned to the object.
        :type id: int, str
        :param geometry: The geometry on which the object is based.
        :type geometry: Geometry
        :param title: Name of the object
        :type title: str
        :param summary: Short summary associated with the object.
        :type summary: str
        :param link: Link associated with the object.
        :type link: str
        :return: A SimpleWebFeature object
        :rtype: SimpleWebFeature
        """
        self.id = id
        self.geometry = geometry
        self.properties = {'title': title, 'summary': summary, 'link': link}

    def as_dict(self):
        return {
            "type": "Feature",
            "id": self.id,
            "properties": self.properties,
            "geometry": self.geometry
            }

    __geo_interface__ = property(as_dict)


def create_simple_web_feature(o):
    """
    Create an instance of SimpleWebFeature from a dict, o. If o does not
    match a Python feature object, simply return o. This function serves as a
    json decoder hook. See coding.load().

    :param o: A dict to create the SimpleWebFeature from.
    :type o: dict
    :return: A SimpleWebFeature from the dict provided.
    :rtype: SimpleWebFeature
    """
    try:
        id = o['id']
        g = o['geometry']
        p = o['properties']
        return SimpleWebFeature(str(id), {
            'type': str(g.get('type')),
            'coordinates': g.get('coordinates', [])},
            title=p.get('title'),
            summary=p.get('summary'),
            link=str(p.get('link')))
    except (KeyError, TypeError):
        pass
    return o