File: wellknowntext.py

package info (click to toggle)
thuban 1.2.2-14
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,176 kB
  • sloc: python: 30,410; ansic: 6,181; xml: 4,234; cpp: 1,595; makefile: 145
file content (135 lines) | stat: -rw-r--r-- 4,559 bytes parent folder | download | duplicates (6)
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
128
129
130
131
132
133
134
135
# Copyright (C) 2002, 2003 Intevation GmbH <intevation@intevation.de>
# Author: Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file GPL coming with the software for details.

"""Convert Well-Known Text format to python objects

Main entry point is the convert_well_known_text function which takes a
geometry in Well-Known Text format and returns a python object with the
geometry.
"""

__version__ = "$Revision: 1605 $"
# $Source$
# $Id: wellknowntext.py 1605 2003-08-19 11:00:40Z bh $

import re


_open_parens = r"[ \t]*(\([ \t]*)*"
_close_parens = r"[ \t]*(\)[ \t]*)+"
rx_point_list = re.compile(_open_parens + r"(?P<coords>[^\)]+)"
                           + _close_parens + ",?")


def parse_coordinate_lists(wkt):
    """Return the coordinates in wkt as a list of lists of coordinate pairs.

    The wkt parameter is the coordinate part of a geometry in well-known
    text format.
    """
    geometry = []
    while wkt:
        match = rx_point_list.match(wkt)
        if match:
            poly = []
            wktcoords = match.group("coords")
            for pair in wktcoords.split(","):
                # a pair may be a triple actually. For now we just
                # ignore any third value
                x, y = map(float, pair.split())[:2]
                poly.append((x, y))
            geometry.append(poly)
            wkt = wkt[match.end(0):].strip()
        else:
            raise ValueError("Invalid well-known-text (WKT) syntax")
    return geometry


def parse_multipolygon(wkt):
    """
    Return the MULTIPOLYGON geometry wkt as a list of lists of float pairs
    """
    return parse_coordinate_lists(wkt)

def parse_polygon(wkt):
    """Return the POLYGON geometry in wkt as a list of float pairs"""
    return parse_coordinate_lists(wkt)

def parse_multilinestring(wkt):
    """
    Return the MULTILINESTRING geometry wkt as a list of lists of float pairs
    """
    return parse_coordinate_lists(wkt)

def parse_linestring(wkt):
    """Return the LINESTRING geometry in wkt as a list of float pairs"""
    return parse_coordinate_lists(wkt)[0]

def parse_point(wkt):
    """Return the POINT geometry in wkt format as pair of floats"""
    return parse_coordinate_lists(wkt)[0][0]


# map geometry types to parser functions
_function_map = [
    ("MULTIPOLYGON", parse_multipolygon),
    ("POLYGON", parse_polygon),
    ("MULTILINESTRING", parse_multilinestring),
    ("LINESTRING", parse_linestring),
    ("POINT", parse_point),
    ]

def convert_well_known_text(wkt):
    """Return the geometry given in well-known text format as python objects

    The function accepts only 2D data and supports the POINT, POLYGON,
    MULTIPOLYGON, LINESTRING and MULTILINESTRING geometries.

    The structure of the return value depends on the geometry type. For
    MULTIPOLYGON and MULTILINESTRING return a list of lists of
    coordinate pairs. For POLYGON and LINESTRING return a list of
    coordinate pairs. For POINT return a coordinate pair. All
    coordinates are floats.

    The string wkt may contain an SRID specification in addition to the
    actual geometry. This SRID is ignored.
    """
    parts = wkt.split(";")
    for part in parts:
        part = part.strip()
        if part.startswith("SRID"):
            # ignore SRIDs
            continue
        else:
            for geotype, function in _function_map:
                if part.startswith(geotype):
                    return function(part[len(geotype):])
            else:
                raise ValueError("Unsupported WKT-part %s" % repr(part[:20]))
    else:
        # there were no recognized geometries in the WKT string
        raise ValueError("No recognized geometry in WKT string")

def parse_wkt_thuban(wkt):
    """Like convert_well_known_text, but return lists of lists of pairs"""
    parts = wkt.split(";")
    for part in parts:
        part = part.strip()
        if part.startswith("SRID"):
            # ignore SRIDs
            continue
        else:
            # split on "(" to separate the geometry type from the
            # coordinate values
            components = part.split("(", 1)
            if len(components) > 1:
                return parse_coordinate_lists(components[1])
            else:
                raise ValueError("WKT part %r doesn't contain opening"
                                 " parenthesis" % part)
    else:
        # there were no recognized geometries in the WKT string
        raise ValueError("No recognized geometry in WKT string")