File: open_stds.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 (253 lines) | stat: -rw-r--r-- 8,477 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
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
"""
Functions to open or create space time datasets

Usage:

.. code-block:: python

    import grass.temporal as tgis

    tgis.register_maps_in_space_time_dataset(type, name, maps)


(C) 2012-2014 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 .factory import *

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


def open_old_stds(name, type, dbif=None):
    """This function opens an existing space time dataset and return the
       created and initialized object of the specified type.

       This function will call exit() or raise a
       grass.pygrass.messages.FatalError in case the type is wrong,
       or the space time dataset was not found.

       :param name: The name of the space time dataset, if the name does not
                    contain the mapset (name@mapset) then the current mapset
                    will be used to identifiy the space time dataset
       :param type: The type of the space time dataset (strd, str3ds, stvds,
                    raster, vector, raster3d)
       :param dbif: The optional database interface to be used

       :return: New stds object

    """
    mapset = get_current_mapset()
    msgr = get_tgis_message_interface()

    # Check if the dataset name contains the mapset as well
    if name.find("@") < 0:
        id = name + "@" + mapset
    else:
        id = name

    if type == "strds" or type == "rast" or type == "raster":
        sp = dataset_factory("strds", id)
    elif type == "str3ds" or type == "raster3d" or type == "rast3d" or type == "raster_3d":
        sp = dataset_factory("str3ds", id)
    elif type == "stvds" or type == "vect" or type == "vector":
        sp = dataset_factory("stvds", id)
    else:
        msgr.fatal(_("Unknown type: %s") % (type))

    dbif, connected = init_dbif(dbif)

    if not sp.is_in_db(dbif):
        dbif.close()
        msgr.fatal(_("Space time %(sp)s dataset <%(name)s> not found") %
                   {'sp': sp.get_new_map_instance(None).get_type(),
                    'name': name})

    # Read content from temporal database
    sp.select(dbif)
    if connected:
        dbif.close()

    return sp

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


def check_new_stds(name, type, dbif=None, overwrite=False):
    """Check if a new space time dataset of a specific type can be created

       :param name: The name of the new space time dataset
       :param type: The type of the new space time dataset (strd, str3ds,
                    stvds, raster, vector, raster3d)
       :param dbif: The temporal database interface to be used
       :param overwrite: Flag to allow overwriting

       :return: A space time dataset object that must be filled with
               content before insertion in the temporal database

       This function will raise a FatalError in case of an error.
    """

    # Get the current mapset to create the id of the space time dataset

    mapset = get_current_mapset()
    msgr = get_tgis_message_interface()

    if name.find("@") < 0:
        id = name + "@" + mapset
    else:
        n, m = name.split("@")
        if mapset != m:
            msgr.fatal(_("Space time datasets can only be created in the "
                         "current mapset"))
        id = name

    if type == "strds" or type == "rast" or type == "raster":
        sp = dataset_factory("strds", id)
    elif type == "str3ds" or type == "raster3d" or type == "rast3d "or type == "raster_3d":
        sp = dataset_factory("str3ds", id)
    elif type == "stvds" or type == "vect" or type == "vector":
        sp = dataset_factory("stvds", id)
    else:
        msgr.error(_("Unknown type: %s") % (type))
        return None

    dbif, connected = init_dbif(dbif)

    if sp.is_in_db(dbif) and overwrite is False:
        msgr.fatal(_("Space time %(sp)s dataset <%(name)s> is already in the"
                     " database. Use the overwrite flag.") % {
                   'sp': sp.get_new_map_instance(None).get_type(),
                   'name': name})
    if connected:
        dbif.close()

    return sp

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


def open_new_stds(name, type, temporaltype, title, descr, semantic,
                  dbif=None, overwrite=False):
    """Create a new space time dataset of a specific type

       :param name: The name of the new space time dataset
       :param type: The type of the new space time dataset (strd, str3ds,
                    stvds, raster, vector, raster3d)
       :param temporaltype: The temporal type (relative or absolute)
       :param title: The title
       :param descr: The dataset description
       :param semantic: Semantical information
       :param dbif: The temporal database interface to be used
       :param overwrite: Flag to allow overwriting

       :return: The new created space time dataset

       This function will raise a FatalError in case of an error.
    """
    dbif, connected = init_dbif(dbif)
    msgr = get_tgis_message_interface()
    sp = check_new_stds(name, type, dbif, overwrite)

    if sp.is_in_db(dbif):
        msgr.warning(_("Overwriting space time %(sp)s dataset <%(name)s> and "
                       "unregistering all maps") % {
                     'sp': sp.get_new_map_instance(None).get_type(),
                     'name': name})
        id = sp.get_id()
        sp.delete(dbif)
        sp = sp.get_new_instance(id)

    msgr.verbose(_("Creating a new space time %s dataset") %
                 sp.get_new_map_instance(None).get_type())

    sp.set_initial_values(temporal_type=temporaltype, semantic_type=semantic,
                          title=title, description=descr)

    sp.insert(dbif)

    if connected:
        dbif.close()

    return sp

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


def check_new_map_dataset(name, layer=None, type="raster",
                          overwrite=False, dbif=None):
    """Check if a new map dataset of a specific type can be created in
        the temporal database

       :param name: The name of the new map dataset
       :param layer: The layer of the new map dataset
       :param type: The type of the new map dataset (raster, vector, raster3d)
       :param dbif: The temporal database interface to be used
       :param overwrite: Flag to allow overwriting

       :return: A map dataset object

       This function will raise a FatalError in case of an error.
    """
    mapset = get_current_mapset()
    msgr = get_tgis_message_interface()

    dbif, connected = init_dbif(dbif)
    map_id = AbstractMapDataset.build_id(name, mapset, layer)

    new_map = dataset_factory(type, map_id)
    # Check if new map is in the temporal database
    if new_map.is_in_db(dbif):
        if not overwrite:
            if connected:
                dbif.close()
            msgr.fatal(_("Map <%s> is already in temporal database,"
                         " use overwrite flag to overwrite") % (map_id))

    if connected:
        dbif.close()

    return new_map

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


def open_new_map_dataset(name, layer=None, type="raster",
                         temporal_extent=None, overwrite=False,
                         dbif=None):
    """Create a new map dataset object of a specific type that can be
        registered in the temporal database

       :param name: The name of the new map dataset
       :param layer: The layer of the new map dataset
       :param type: The type of the new map dataset (raster, vector, raster3d)
       :param dbif: The temporal database interface to be used
       :param overwrite: Flag to allow overwriting

       :return: A map dataset object

    """

    mapset = get_current_mapset()

    dbif, connected = init_dbif(dbif)
    new_map = check_new_map_dataset(name, layer, type, overwrite, dbif)

    # Check if new map is in the temporal database
    if new_map.is_in_db(dbif):
        # Remove the existing temporal database entry
        map_id = new_map.get_id()
        new_map.delete(dbif)
        new_map = new_map.get_new_instance(map_id)

    if temporal_extent:
        new_map.set_temporal_extent(temporal_extent)

    if connected:
        dbif.close()

    return new_map