Browse Source

New temporal algebra module t.select and temporal algebra base class added from GSoC 2013.
GSoC student and author is Thomas Leppelt. Modifications to merge the temporal algebra
into grass7 by Soeren Gebbert.


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@58729 15284696-431f-4ddb-bdfa-cd5b030d7da7

Soeren Gebbert 11 years ago
parent
commit
3d674fea5b

+ 1 - 1
lib/python/temporal/Makefile

@@ -8,7 +8,7 @@ PYDIR = $(ETC)/python
 GDIR = $(PYDIR)/grass
 DSTDIR = $(GDIR)/temporal
 
-MODULES = base core abstract_dataset abstract_map_dataset abstract_space_time_dataset space_time_datasets open_stds factory gui_support list_stds register sampling metadata spatial_extent temporal_extent datetime_math temporal_granularity spatio_temporal_relationships unit_tests aggregation stds_export stds_import extract mapcalc univar_statistics temporal_topology_dataset_connector spatial_topology_dataset_connector c_libraries_interface
+MODULES = base core abstract_dataset abstract_map_dataset abstract_space_time_dataset space_time_datasets open_stds factory gui_support list_stds register sampling metadata spatial_extent temporal_extent datetime_math temporal_granularity spatio_temporal_relationships unit_tests aggregation stds_export stds_import extract mapcalc univar_statistics temporal_topology_dataset_connector spatial_topology_dataset_connector c_libraries_interface temporal_algebra
 
 PYFILES := $(patsubst %,$(DSTDIR)/%.py,$(MODULES) __init__)
 PYCFILES := $(patsubst %,$(DSTDIR)/%.pyc,$(MODULES) __init__)

+ 1 - 0
lib/python/temporal/__init__.py

@@ -25,3 +25,4 @@ from stds_import import *
 from mapcalc import *
 from univar_statistics import *
 from c_libraries_interface import *
+from temporal_algebra import *

File diff suppressed because it is too large
+ 2264 - 0
lib/python/temporal/temporal_algebra.py


+ 7 - 0
temporal/t.select/Makefile

@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../../
+
+PGM = t.select
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

+ 280 - 0
temporal/t.select/t.select.html

@@ -0,0 +1,280 @@
+<h2>DESCRIPTION</h2>
+
+t.select performs selection of maps that are registered in space time datasets using temporal algebra.
+
+<h3>PROGRAM USE</h3>
+The module expects an <b>expression</b> as input parameter in the following form: <br>
+<br>
+<b> "result = expression" </b>
+<br>
+<br>
+
+The statement structure is similar to r.mapcalc, see <a href="r.mapcalc.html">r.mapcalc</a>.
+Where <b>result</b> represents the name of a space time dataset (STDS)that will
+contain the result of the calculation that is given as <b>expression</b>
+on the right side of the equality sign.
+These expression can be any valid or nested combination of temporal
+operations and functions that are provided by the temporal algebra.  <br>
+The temporal algebra works with space time datasets of any type (STRDS, STR3DS and STVDS).
+The algebra provides methods for map selection from STDS based on their temporal relations.
+It is also possible to temporally shift maps, to create temporal buffer and to snap time
+instances to create a valid temporal topology. Furthermore expressions can be nested and
+evaluated in conditional statements (if, else statements). Within if-statements the algebra
+provides temporal variables like start time, end time, day of year, time differences or
+number of maps per time interval to build up conditions. These operations can be assigned
+to space time datasets or to the results of operations between space time datasets.
+<br>
+<br>
+The type of the input space time datasets must be defined with the input
+parameter <b>type</b>. Possible options are STRDS, STVDS or STR3DS.
+The default is set to space time raster datasets (STRDS).
+<br>
+<br>
+As default, topological relationships between space time datasets will be
+evaluated only temporal. Use the <b>s</b> flag to activate the
+additionally spatial topology evaluation.
+<br>
+<br>
+The expression option must be passed as <b>quoted</b>
+expression, for example: <br>
+<div class="code"><pre>t.select expression="C = A : B"</pre></div>
+Where <b>C</b> is the new space time raster dataset that will contain maps
+from <b>A</b> that are selected by equal temporal relationships
+to the existing dataset <b>B</b> in this case. <br>
+<br>
+<h2>TEMPORAL ALGEBRA</h2>
+
+The temporal algebra provides a wide range of temporal operators and
+functions that will be presented in the following section. <br>
+<br>
+
+<h3>TEMPORAL RELATIONS</h3>
+
+Several temporal topology relations between registered maps of space
+time datasets are supported: <br>
+<div class="code"><pre>
+equals            A ------
+                  B ------
+
+during            A  ----
+                  B ------
+
+contains          A ------
+                  B  ----
+
+starts            A ----
+                  B ------
+
+started           A ------
+                  B ----
+
+finishs           A   ----
+                  B ------
+
+finished          A ------
+                  B   ----
+
+precedes          A ----
+                  B     ----
+
+follows           A     ----
+                  B ----
+
+overlapped        A   ------
+                  B ------
+
+overlaps          A ------
+                  B   ------
+
+over              booth overlaps and overlapped
+
+</pre></div>
+The relations must be read as: A is related to B, like - A equals B - A is
+during B - A contains B <br>
+<br>
+Topological relations must be specified in {} parentheses. <br>
+
+<h3>TEMPORAL SELECTION</h3>
+
+The temporal selection simply selects parts of a space time dataset without
+processing raster or vector data.
+
+The algebra provides a selection operator <b>:</b> that selects parts
+of a space time dataset that are temporally equal to parts of a second one
+by default. The following expression
+<div class="code"><pre>
+C = A : B
+</pre></div>
+means: Select all parts of space time dataset A that are equal to B and store
+it in space time dataset C. The parts are time stamped maps. <br>
+<br>
+In addition the inverse selection operator <b>!:</b> is defined as the complement of
+the selection operator, hence the following expression
+<div class="code"><pre>
+C = A !: B
+</pre></div>
+means: select all parts of space time time dataset A that are not equal to B
+and store it in space time dataset (STDS) C. <br>
+<br>
+To select parts of a STDS by different topological relations to other STDS,
+the temporal topology selection operator can be used. The operator consists of
+topological relations, that must be separated by the logical OR operator
+<b>||</b> and the temporal selection operator. Both parts are separated by
+comma and surrounded by curly braces:
+{"topological relations", "temporal selection operator"}  <br>
+<br>
+Examples:
+<div class="code"><pre>
+C = A {equals,:} B
+C = A {equals,!:} B
+</pre></div>
+We can now define arbitrary topological relations using the OR operator "|"
+to connect them:
+<div class="code"><pre>
+C = A {equals|during|overlaps,:} B
+</pre></div>
+Select all parts of A that are equal to B, during B or overlaps B. <br>
+<br>
+The selection operator is implicitly contained in the temporal topology
+selection operator, so that the following statements are exactly the same:
+<div class="code"><pre>
+C = A : B
+C = A {:} B
+C = A {equal,:} B
+</pre></div>
+
+Same for the complementary selection:
+<div class="code"><pre>
+C = A !: B
+C = A {!:} B
+C = A {equal,!:} B
+</pre></div>
+
+<h3>CONDITIONAL STATEMENTS</h3>
+
+Selection operations can be evaluated within conditional statements.
+<br>
+<div class="code"><pre>
+Note A and B can either be space time datasets or expressions.
+
+if statement                         decision option                        temporal relations
+  if(if, then, else)
+  if(conditions, A)                    A if conditions are True;              temporal topological relation between if and then is equal.
+  if(conditions, A, B)                 A if conditions are True, B otherwise; temporal topological relation between if, then and else is equal.
+  if(topologies, conditions, A)        A if conditions are True;              temporal topological relation between if and then is explicit specified by topologies.
+  if(topologies, conditions, A, B)     A if conditions are True, B otherwise; temporal topological relation between if, then and else is explicit specified by topologies.
+</pre></div>
+The conditions are comparison expressions that are used to evaluate
+space time datasets. Specific values of temporal variables are
+compared by logical operators and evaluated for each map of the STDS.
+<br>
+<br>
+The supported logical operators:
+<div class="code"><pre>
+Symbol  description
+
+  ==    equal
+  !=    not equal
+  >     greater than
+  >=    greater than or equal
+  <     less than
+  <=    less than or equal
+  &&    and
+  ||    or
+</pre></div>
+
+Temporal functions: <br>
+<div class="code"><pre>
+
+td(A)                   Returns a list of time intervals of STDS A
+
+start_time()            Start time as HH::MM:SS
+start_date()            Start date as yyyy-mm-DD
+start_datetime()        Start datetime as yyyy-mm-DD HH:MM:SS
+end_time()              End time as HH:MM:SS
+end_date()              End date as yyyy-mm-DD
+end_datetime()          End datetime as  yyyy-mm-DD HH:MM
+
+start_doy()             Day of year (doy) from the start time [1 - 366]
+start_dow()             Day of week (dow) from the start time [1 - 7], the start of the week is Monday == 1
+start_year()            The year of the start time [0 - 9999]
+start_month()           The month of the start time [1 - 12]
+start_week()            Week of year of the start time [1 - 54]
+start_day()             Day of month from the start time [1 - 31]
+start_hour()            The hour of the start time [0 - 23]
+start_minute()          The minute of the start time [0 - 59]
+start_second()          The second of the start time [0 - 59]
+end_doy()               Day of year (doy) from the end time [1 - 366]
+end_dow()               Day of week (dow) from the end time [1 - 7], the start of the week is Monday == 1
+end_year()              The year of the end time [0 - 9999]
+end_month()             The month of the end time [1 - 12]
+end_week()              Week of year of the end time [1 - 54]
+end_day()               Day of month from the start time [1 - 31]
+end_hour()              The hour of the end time [0 - 23]
+end_minute()            The minute of the end time [0 - 59]
+end_second()            The second of the end time [0 - 59]
+</pre></div>
+
+Additionally the number of maps in intervals can be computed and
+used in conditional statements. <br>
+The operator to count the number of maps
+is the hash <b>#</b>.
+<div class="code"><pre>
+A{contains,#}B
+</pre></div>
+This expression computes the number of maps from space
+time dataset B which are during the time intervals of maps from
+space time dataset A.<br>
+A list of integers (scalars) corresponding to the maps of A
+that contain maps from B will be returned. <br>
+<br>
+
+Furthermore the temporal algebra allows temporal buffering, shifting
+and snapping with the functions buff_t(), tshift() and tsnap()
+respectively.
+<div class="code"><pre>
+buff_t(A, size)         Buffer STDS A with granule ("1 month" or 5)
+tshift(A, size)         Shift STDS A with granule ("1 month" or 5)
+tsnap(A)                Snap time instances and intervals of STDS A
+</pre></div>
+
+<h3>Examples: </h3>
+
+Select all maps from space time dataset A which have equal time stamps
+with space time dataset B and C and are ealier that Jan. 1. 2005 and
+store them in space time dataset D.
+<div class="code"><pre>
+D = if(start_date() < "2005-01-01", A : B : C)
+</pre></div>
+
+Select all maps from space time dataset A which contains more than three
+maps of space time dataset B, else select maps from C with time
+stamps that are not equal to A and store them in space time dataset D.
+<div class="code"><pre>
+D = if(A {contain, #} B > 3, A {contain, :} B, C)
+</pre></div>
+
+Select all maps from space time dataset B which are during the temporal
+buffered space time dataset A with a map interval of three days, else
+select maps from C and store them in space time dataset D.
+<div class="code"><pre>
+D = if(contain, td(buff_t(A, "1 days")) == 3, B, C)
+</pre></div>
+
+<h2>REFERENCES</h2>
+
+<tt><a href="http://www.dabeaz.com/ply/">PLY(Python-Lex-Yacc)</a></tt>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="r.mapcalc.html">r.mapcalc</a>
+</em>
+
+
+<h2>AUTHORS</h2>
+
+Thomas Leppelt, Soeren Gebbert, Thuenen Institut, Germany <br>
+
+<p><i>Last changed: $Date: 2013-07-24 17:35:20 +0100 (Thu, 24 Jul 2013) $</i>
+

+ 70 - 0
temporal/t.select/t.select.py

@@ -0,0 +1,70 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:       t.select
+# AUTHOR(S):    Thomas Leppelt, Soeren Gebbert
+#
+# PURPOSE:      Select maps from space time datasets by topological relationships to
+#               other space time datasets using temporal algebra.
+# COPYRIGHT:    (C) 2011 by the GRASS Development Team
+#
+#               This program is free software under the GNU General Public
+#               License (version 2). Read the file COPYING that comes with GRASS
+#               for details.
+#
+#############################################################################
+
+#%module
+#% description: Select maps from space time datasets by topological relationships to other space time datasets using temporal algebra.
+#% keywords: temporal
+#% keywords: metadata
+#%end
+
+#%option G_OPT_STDS_TYPE
+#% guidependency: input
+#% guisection: Required
+#%end
+
+#%option
+#% key: expression
+#% type: string
+#% description: The temporal mapcalc expression
+#% key_desc: expression
+#% required : yes
+#%end
+
+#%flag
+#% key: s
+#% description: Activate spatial topology.
+#%end
+
+
+import grass.script as grass
+import grass.temporal as tgis
+import sys
+
+def main():
+
+    expression = options['expression']
+    spatial = flags["s"]
+    stdstype = options["type"]
+
+    # Check for PLY istallation
+    try:
+        import ply.lex as lex
+        import ply.yacc as yacc
+    except:
+        grass.fatal(_("Please install PLY (Lex and Yacc Python implementation) to use the temporal algebra modules."))
+
+    tgis.init(True)
+    p = tgis.TemporalAlgebraParser(run=True, debug=False, spatial = spatial)
+    p.parse(expression, stdstype)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())
+
+
+
+

+ 101 - 0
temporal/t.select/test.t.select.sh

@@ -0,0 +1,101 @@
+#!/usr/bin/sh
+
+# Test for temporal algebra in LatLon location.
+n1=`g.tempfile pid=1 -d`
+n2=`g.tempfile pid=2 -d`
+n3=`g.tempfile pid=3 -d`
+n4=`g.tempfile pid=4 -d`
+
+export GRASS_OVERWRITE=1
+
+# Create random points test maps.
+for i in {1..60}
+  do
+    if [[ "$i" -le 20 ]]; then
+      echo testpoint1_$i >> ${n1}
+      v.random  -z output=testpoint1_$i n=3 seed=$i
+    elif [ "$i" -gt 20 ] && [ "$i" -le 40 ]; then
+      echo testpoint2_$i >>  ${n2}
+      v.random  -z output=testpoint2_$i n=3 seed=$i
+    else
+      echo testpoint3_$i >>  ${n3}
+      echo testpoint4_$i >>  ${n4}
+      v.random  -z output=testpoint3_$i n=3 seed=$i
+      v.random  -z output=testpoint4_$i n=3 seed=$i
+    fi
+  done
+
+# Create STVDS and register test maps.
+t.create  output=test1 type=stvds title="test dataset" descr="test dataset"
+t.create  output=test2 type=stvds title="test dataset" descr="test dataset"
+t.create  output=test3 type=stvds title="test dataset" descr="test dataset"
+t.create  output=test4 type=stvds title="test dataset" descr="test dataset"
+
+
+t.register -i  type=vect input=test1 file=${n1} increment="1 days" start="2013-01-01"
+t.register -i  type=vect input=test2 file=${n2} increment="1 days" start="2013-01-10"
+t.register -i  type=vect input=test3 file=${n3} increment="3 days" start="2013-01-01"
+t.register -i  type=vect input=test4 file=${n4} increment="3 days" start="2013-01-10"
+
+t.info type=stvds input=test1
+t.info type=stvds input=test2
+t.info type=stvds input=test3
+t.info type=stvds input=test4
+
+# Test different options.
+t.select  expression="A = test1 : test2" type="stvds"
+t.info type=stvds input=A
+t.select  expression="A = test1 !: test2" type="stvds"
+t.info type=stvds input=A
+t.select  expression="A = test2 {equal,:} test1" type="stvds"
+t.info type=stvds input=A
+t.select  expression='A = test1 : buff_t(test2, "1 days")' type="stvds"
+t.info type=stvds input=A
+t.select  expression='A = test1 {during,:} buff_t(test2, "1 days")' type="stvds"
+t.info type=stvds input=A
+t.select  expression='A = tsnap(test1 {during,:} buff_t(test2, "1 days"))' type="stvds"
+t.info type=stvds input=A
+t.select  expression='A = if(td(test1) == 1, test2)' type="stvds"
+t.info type=stvds input=A
+t.select  expression='A = if(td(test1) == 2, test2)' type="stvds"
+t.info type=stvds input=A
+t.select  expression='A = if(start_year() == 2013, test2)' type="stvds"
+t.info type=stvds input=A
+t.select  expression='A = if(start_year() == 2013 && start_day() < 20, test2)' type="stvds"
+t.info type=stvds input=A
+t.select  expression='A = if(start_year() == 2013 && start_day() < 20 && start_day() > 15, test2)' type="stvds"
+t.info type=stvds input=A
+t.select  expression='A = if(start_year() == 2013 || start_day() < 20 && start_day() > 15, test2)' type="stvds"
+t.info type=stvds input=A
+t.select  expression='A = if(td(test1) == 1 || start_day() < 20 && start_day() > 15, test2)' type="stvds"
+t.info type=stvds input=A
+t.select  expression='A = if(td(test1) == 1 || start_day() < 20, test2)' type="stvds"
+t.info type=stvds input=A
+t.select  expression='A = if(td(test1) == 1, if(start_day() > 15, test2))' type="stvds"
+t.vect.list input=A column=name,start_time
+t.select  expression='A = if(start_day() > 15, test2, test1)' type="stvds"
+t.vect.list input=A column=name,start_time
+t.select  expression='A = if({during}, start_day() > 15, test2, test1)' type="stvds"
+t.vect.list input=A column=name,start_time
+t.select  expression='A = if({during}, td(test1) == 1, test2, test1)' type="stvds"
+t.vect.list input=A column=name,start_time
+t.select  expression='A = if(test1 {#} test2 == 1, test1)' type="stvds"
+t.vect.list input=A column=name,start_time
+t.select  expression='A = if(test3 {contains,#} test2 > 1, test3)' type="stvds"
+t.vect.list input=A column=name,start_time
+t.select  expression='A = if({equal|during}, test3 {contains,#} test2 > 1, test3, test1)' type="stvds"
+t.vect.list input=A column=name,start_time
+t.select  expression="A = test1 {during,:} test3" type="stvds"
+t.vect.list input=A column=name,start_time,end_time
+t.select  expression="A = test1 {during,:} test4" type="stvds"
+t.vect.list input=A column=name,start_time,end_time
+t.select  expression="A = test1 {starts,:} test4" type="stvds"
+t.vect.list input=A column=name,start_time,end_time
+t.select  expression="A = test1 {finishes,:} test4" type="stvds"
+t.vect.list input=A column=name,start_time,end_time
+t.select  expression="A = test4 {finished,:} test1" type="stvds"
+t.vect.list input=A column=name,start_time,end_time
+t.select  expression="A = test4 {started,:} test1" type="stvds"
+t.vect.list input=A column=name,start_time,end_time
+
+t.remove -rf type=stvds input=test1,test2,test3,test4,A