File: spatial_topology_dataset_connector.py

package info (click to toggle)
grass 7.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 135,976 kB
  • ctags: 44,148
  • sloc: ansic: 410,300; python: 166,939; cpp: 34,819; sh: 9,358; makefile: 6,618; xml: 3,551; sql: 769; lex: 519; yacc: 450; asm: 387; perl: 282; sed: 17; objc: 7
file content (375 lines) | stat: -rw-r--r-- 13,057 bytes parent folder | download | duplicates (3)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# -*- coding: utf-8 -*-
"""
Spatial topology connector class

Usage:

>>> import grass.temporal as tgis
>>> tmr = tgis.SpatialTopologyDatasetConnector()

(C) 2012-2013 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.

:authors: Soeren Gebbert
"""
from __future__ import print_function
import copy


class SpatialTopologyDatasetConnector(object):
    """This class implements a spatial topology access structure to connect
       spatial related datasets

       This object will be set up by spatial topology creation method provided
       by the SpatioTemporalTopologyBuilder.

       The following spatial relations with access methods are supported:

       - equivalent
       - overlap
       - in
       - contain
       - meet
       - cover
       - covered

        Usage:

        .. code-block:: python

            >>> import grass.temporal as tgis
            >>> tgis.init()
            >>> map = tgis.RasterDataset("a@P")
            >>> tmr = tgis.SpatialTopologyDatasetConnector()
            >>> tmr.append_equivalent(map)
            >>> tmr.append_overlap(map)
            >>> tmr.append_in(map)
            >>> tmr.append_contain(map)
            >>> tmr.append_meet(map)
            >>> tmr.append_cover(map)
            >>> tmr.append_covered(map)
            >>> tmr.print_spatial_topology_info()
             +-------------------- Spatial Topology --------------------------------------+
             | Equivalent: ................ a@P
             | Cover: ..................... a@P
             | Covered: ................... a@P
             | Overlap: ................... a@P
             | In: ........................ a@P
             | Contain: ................... a@P
             | Meet: ...................... a@P
            >>> tmr.print_spatial_topology_shell_info()
            equivalent=a@P
            cover=a@P
            covered=a@P
            overlap=a@P
            in=a@P
            contain=a@P
            meet=a@P
            >>> rlist = tmr.get_spatial_relations()
            >>> if "COVER" in rlist.keys():
            ...    print(rlist["COVER"][0].get_id())
            a@P

    """

    def __init__(self):
        self.reset_spatial_topology()

    def reset_spatial_topology(self):
        """Reset any information about temporal topology"""
        self._spatial_topology = {}
        self._has_spatial_topology = False

    def get_spatial_relations(self):
        """Return the dictionary of spatial relationships

           Keys are the spatial relationships in upper case,
           values are abstract map objects.

           :return: The spatial relations dictionary
        """
        return copy.copy(self._spatial_topology)

    def get_number_of_spatial_relations(self):
        """ Return a dictionary in which the keys are the relation names and the value
            are the number of relations.

            The following relations are available:

            - equivalent
            - overlap
            - in
            - contain
            - meet
            - cover
            - covered

            To access topological information the spatial topology must be
            build first using the SpatialTopologyBuilder.

            :return: the dictionary with relations as keys and number as
                     values or None in case the topology wasn't build
        """
        if self._has_spatial_topology is False:
            return None

        relations = {}
        try:
            relations["equivalent"] = len(self._spatial_topology["EQUIVALENT"])
        except:
            relations["equivalent"] = 0
        try:
            relations["overlap"] = len(self._spatial_topology["OVERLAP"])
        except:
            relations["overlap"] = 0
        try:
            relations["in"] = len(self._spatial_topology["IN"])
        except:
            relations["in"] = 0
        try:
            relations["contain"] = len(self._spatial_topology["CONTAIN"])
        except:
            relations["contain"] = 0
        try:
            relations["meet"] = len(self._spatial_topology["MEET"])
        except:
            relations["meet"] = 0
        try:
            relations["cover"] = len(self._spatial_topology["COVER"])
        except:
            relations["cover"] = 0
        try:
            relations["covered"] = len(self._spatial_topology["COVERED"])
        except:
            relations["covered"] = 0

        return relations

    def set_spatial_topology_build_true(self):
        """Same as name"""
        self._has_spatial_topology = True

    def set_spatial_topology_build_false(self):
        """Same as name"""
        self._has_spatial_topology = False

    def is_spatial_topology_build(self):
        """Check if the temporal topology was build"""
        return self._has_spatial_topology

    def append_equivalent(self, map):
        """Append a map with equivalent spatial extent as this map

           :param map: This object should be of type AbstractMapDataset
                       or derived classes
        """
        if "EQUIVALENT" not in self._spatial_topology:
            self._spatial_topology["EQUIVALENT"] = []
        self._spatial_topology["EQUIVALENT"].append(map)

    def get_equivalent(self):
        """Return a list of map objects with equivalent spatial extent as this map

           :return: A list of map objects or None
        """
        if "EQUIVALENT" not in self._spatial_topology:
            return None
        return self._spatial_topology["EQUIVALENT"]

    def append_overlap(self, map):
        """Append a map that this spatial overlap with this map

           :param map: This object should be of type AbstractMapDataset
                       or derived classes
        """
        if "OVERLAP" not in self._spatial_topology:
            self._spatial_topology["OVERLAP"] = []
        self._spatial_topology["OVERLAP"].append(map)

    def get_overlap(self):
        """Return a list of map objects that this map spatial overlap with

           :return: A list of map objects or None
        """
        if "OVERLAP" not in self._spatial_topology:
            return None
        return self._spatial_topology["OVERLAP"]

    def append_in(self, map):
        """Append a map that this is spatial in this map

           :param map: This object should be of type AbstractMapDataset
                       or derived classes
        """
        if "IN" not in self._spatial_topology:
            self._spatial_topology["IN"] = []
        self._spatial_topology["IN"].append(map)

    def get_in(self):
        """Return a list of map objects that are spatial in this map

           :return: A list of map objects or None
        """
        if "IN" not in self._spatial_topology:
            return None
        return self._spatial_topology["IN"]

    def append_contain(self, map):
        """Append a map that this map spatially contains

           :param map: This object should be of type AbstractMapDataset
                       or derived classes
        """
        if "CONTAIN" not in self._spatial_topology:
            self._spatial_topology["CONTAIN"] = []
        self._spatial_topology["CONTAIN"].append(map)

    def get_contain(self):
        """Return a list of map objects that this map contains

           :return: A list of map objects or None
        """
        if "CONTAIN" not in self._spatial_topology:
            return None
        return self._spatial_topology["CONTAIN"]

    def append_meet(self, map):
        """Append a map that spatially meet with this map

           :param map: This object should be of type AbstractMapDataset
                       or derived classes
        """
        if "MEET" not in self._spatial_topology:
            self._spatial_topology["MEET"] = []
        self._spatial_topology["MEET"].append(map)

    def get_meet(self):
        """Return a list of map objects that spatially meet with this map

           :return: A list of map objects or None
        """
        if "MEET" not in self._spatial_topology:
            return None
        return self._spatial_topology["MEET"]

    def append_cover(self, map):
        """Append a map that spatially cover this map

           :param map: This object should be of type AbstractMapDataset
                       or derived classes
        """
        if "COVER" not in self._spatial_topology:
            self._spatial_topology["COVER"] = []
        self._spatial_topology["COVER"].append(map)

    def get_cover(self):
        """Return a list of map objects that spatially cover this map

           :return: A list of map objects or None
        """
        if "COVER" not in self._spatial_topology:
            return None
        return self._spatial_topology["COVER"]

    def append_covered(self, map):
        """Append a map that is spatially covered by this map

           :param map: This object should be of type AbstractMapDataset
                       or derived classes
        """
        if "COVERED" not in self._spatial_topology:
            self._spatial_topology["COVERED"] = []
        self._spatial_topology["COVERED"].append(map)

    def get_covered(self):
        """Return a list of map objects that are spatially covered by this map

           :return: A list of map objects or None
        """
        if "COVERED" not in self._spatial_topology:
            return None
        return self._spatial_topology["COVERED"]

    def _generate_map_list_string(self, map_list, line_wrap=True):
        count = 0
        string = ""
        for map_ in map_list:
            if line_wrap and count > 0 and count % 3 == 0:
                string += "\n | ............................ "
                count = 0
            if count == 0:
                string += map_.get_id()
            else:
                string += ",%s" % map_.get_id()
            count += 1

        return string

    # Set the properties
    equivalent = property(fget=get_equivalent, fset=append_equivalent)
    cover = property(fget=get_cover, fset=append_cover)
    covered = property(fget=get_covered, fset=append_covered)
    overlap = property(fget=get_overlap, fset=append_overlap)
    in_ = property(fget=get_in, fset=append_in)
    contain = property(fget=get_contain, fset=append_contain)
    meet = property(fget=get_meet, fset=append_meet)

    def print_spatial_topology_info(self):
        """Print information about this class in human readable style"""

        print(" +-------------------- Spatial Topology --------------------------------------+")
        #          0123456789012345678901234567890
        if self.equivalent is not None:
            print(" | Equivalent: ................ " +
                  self._generate_map_list_string(self.equivalent))
        if self.cover is not None:
            print(" | Cover: ..................... " +
                  self._generate_map_list_string(self.cover))
        if self.covered is not None:
            print(" | Covered: ................... " +
                  self._generate_map_list_string(self.covered))
        if self.overlap is not None:
            print(" | Overlap: ................... " +
                  self._generate_map_list_string(self.overlap))
        if self.in_ is not None:
            print(" | In: ........................ " +
                  self._generate_map_list_string(self.in_))
        if self.contain is not None:
            print(" | Contain: ................... " +
                  self._generate_map_list_string(self.contain))
        if self.meet is not None:
            print(" | Meet: ...................... " +
                  self._generate_map_list_string(self.meet))

    def print_spatial_topology_shell_info(self):
        """Print information about this class in shell style"""

        if self.equivalent is not None:
            print("equivalent=" + self._generate_map_list_string(self.equivalent,
                                                                 False))
        if self.cover is not None:
            print("cover=" + self._generate_map_list_string(
                  self.cover, False))
        if self.covered is not None:
            print("covered=" +
                  self._generate_map_list_string(self.covered, False))
        if self.overlap is not None:
            print("overlap=" +
                  self._generate_map_list_string(self.overlap))
        if self.in_ is not None:
            print("in=" +
                  self._generate_map_list_string(self.in_))
        if self.contain is not None:
            print("contain=" +
                  self._generate_map_list_string(self.contain))
        if self.meet is not None:
            print("meet=" +
                  self._generate_map_list_string(self.meet))

###############################################################################

if __name__ == "__main__":
    import doctest
    doctest.testmod()