changeset 5015:86e79fbb8fa3 dc-km-filter-rr

Added km filter to dc-config. * Use a new macro to get the current range. * Use min/max km to filter dc data. * Extracted new macro in user part.
author Raimund Renkert <rrenkert@intevation.de>
date Tue, 19 Feb 2013 10:46:41 +0100
parents 63617e142dfe
children cad911029c9d b656568bcb2b
files flys-artifacts/doc/conf/meta-data.xml
diffstat 1 files changed, 600 insertions(+), 516 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/doc/conf/meta-data.xml	Fri Feb 15 10:56:08 2013 +0100
+++ b/flys-artifacts/doc/conf/meta-data.xml	Tue Feb 19 10:46:41 2013 +0100
@@ -1,7 +1,10 @@
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <dc:template xmlns:dc="http://www.intevation.org/2011/Datacage">
 <datacage>
-<dc:macro name="SQL-wst_columns_statement">
+  <dc:comment>
+    Statement to load data from wsts.
+  </dc:comment>
+  <dc:macro name="SQL-wst_columns_statement">
     <dc:statement>
         SELECT wst_columns.id          AS prot_column_id,
                wst_columns.name        AS prot_column_name,
@@ -10,10 +13,49 @@
                wst_ranges.a            AS deffrom,
                wst_ranges.b            AS defto
         FROM wst_columns, wst_ranges
-        WHERE wst_columns.wst_id = ${prot_id} AND wst_ranges.wst_column_id = wst_columns.id
+        WHERE wst_columns.wst_id = ${prot_id}
+           AND wst_ranges.wst_column_id = wst_columns.id
+           AND (${fromkm} BETWEEN wst_ranges.a AND wst_ranges.b
+           OR ${tokm} BETWEEN wst_ranges.a AND wst_ranges.b
+           OR wst_ranges.a BETWEEN ${fromkm} AND ${tokm}
+           OR wst_ranges.b BETWEEN ${fromkm} AND ${tokm})
         ORDER by wst_columns.position
     </dc:statement>
-</dc:macro>
+  </dc:macro>
+
+  <dc:comment>
+    Load user specific distance information from artifact.
+  </dc:comment>
+  <dc:macro name="user-range">
+    <dc:choose>
+      <dc:when test="dc:contains($parameters, 'user-id')">
+        <dc:context connection="user">
+        <dc:statement>
+          SELECT COALESCE(ld_mode, '')      AS ldm,
+                 COALESCE(ld_locations, '') AS ldl,
+                 COALESCE(ld_from, '')      AS ldf,
+                 COALESCE(ld_to, '')        AS ldt
+          FROM master_artifacts_range
+          WHERE gid = CAST(${artifact-id} as uuid)
+        </dc:statement>
+        <dc:elements>
+          <dc:variable name="fromkm" type="number" expr="dc:fromValue($ldm, $ldl, $ldf)"/>
+          <dc:variable name="tokm" type="number" expr="dc:toValue($ldm, $ldl, $ldt)"/>
+          <dc:macro-body/>
+        </dc:elements>
+        </dc:context>
+      </dc:when>
+      <dc:otherwise>
+        <dc:variable name="fromkm" type="number" expr="dc:fromValue('', '', '')"/>
+        <dc:variable name="tokm" type="number" expr="dc:toValue('', '', '')"/>
+        <dc:macro-body/>
+      </dc:otherwise>
+    </dc:choose>
+  </dc:macro>
+
+  <dc:comment>
+    System part. Load data for the given river.
+  </dc:comment>
   <dc:macro name="load-system">
     <dc:context connection="system">
       <dc:statement>
@@ -21,13 +63,15 @@
         WHERE lower(name) LIKE lower(${river})
       </dc:statement>
       <dc:elements>
+
         <dc:comment>
-          Base-data macros (mostly data imported from wst-files)
+          Base-data macros (mostly data imported from wst-files).
         </dc:comment>
         <dc:macro name="basedata_0">
+          <dc:call-macro name="user-range">
             <dc:comment comment=" BASEDATA ---------------------------"/>
             <basedata>
-              <dc:context>
+              <dc:context connection="system">
                 <dc:statement>
                   SELECT id          AS prot_id,
                          description AS prot_description
@@ -51,11 +95,14 @@
                 </dc:elements>
               </dc:context>
             </basedata>
+          </dc:call-macro>
         </dc:macro>
+
         <dc:macro name="basedata_0_wq">
+          <dc:call-macro name="user-range">
             <dc:comment comment=" BASEDATA ---------------------------"/>
             <basedata>
-              <dc:context>
+              <dc:context connection="system">
                 <dc:statement>
                   SELECT id          AS prot_id,
                          description AS prot_description
@@ -79,12 +126,14 @@
                 </dc:elements>
               </dc:context>
             </basedata>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="basedata_1_additionals_marks">
+          <dc:call-macro name="user-range">
             <dc:comment comment=".ZUS -------------------------------"/>
             <additionals>
-              <dc:context>
+              <dc:context connection="system">
                 <dc:statement>
                   SELECT id          AS prot_id,
                          description AS prot_description
@@ -108,12 +157,14 @@
                 </dc:elements>
               </dc:context>
             </additionals>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="basedata_1_additionals">
+          <dc:call-macro name="user-range">
             <dc:comment comment=".ZUS -------------------------------"/>
             <additionals>
-              <dc:context>
+              <dc:context connection="system">
                 <dc:statement>
                   SELECT id          AS prot_id,
                          description AS prot_description
@@ -137,12 +188,14 @@
                 </dc:elements>
               </dc:context>
             </additionals>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="basedata_1_additionals-relative_point">
+          <dc:call-macro name="user-range">
             <dc:comment comment=".ZUS -------------------------------"/>
             <additionals>
-              <dc:context>
+              <dc:context connection="system">
                 <dc:statement>
                   SELECT id          AS prot_id,
                          description AS prot_description
@@ -166,125 +219,135 @@
                 </dc:elements>
               </dc:context>
             </additionals>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="basedata_2_fixations_wst">
-          <fixations>
-            <dc:context>
-              <dc:statement>
-                SELECT id          AS prot_id,
-                       description AS prot_description
-                FROM wsts WHERE kind = 2 AND river_id = ${river_id}
-              </dc:statement>
-              <dc:elements>
-                <fixation>
-                  <dc:attribute name="name" value="${prot_description}"/>
-                  <!--dc:attribute name="ids" value="fixations-wstv-A-${prot_id}"/-->
-                  <dc:context>
-                    <dc:call-macro name="SQL-wst_columns_statement"/>
-                    <dc:elements>
-                      <column>
-                        <dc:attribute name="name" value="${prot_column_name}"/>
-                        <dc:attribute name="ids" value="fixations-wstv-${prot_rel_pos}-${prot_id}"/>
-                        <dc:attribute name="factory" value="wqinterpol"/>
-                        <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
-                      </column>
-                    </dc:elements>
-                  </dc:context>
-                </fixation>
-              </dc:elements>
-            </dc:context>
-          </fixations>
+          <dc:call-macro name="user-range">
+            <fixations>
+              <dc:context connection="system">
+                <dc:statement>
+                  SELECT id          AS prot_id,
+                         description AS prot_description
+                  FROM wsts WHERE kind = 2 AND river_id = ${river_id}
+                </dc:statement>
+                <dc:elements>
+                  <fixation>
+                    <dc:attribute name="name" value="${prot_description}"/>
+                    <!--dc:attribute name="ids" value="fixations-wstv-A-${prot_id}"/-->
+                    <dc:context>
+                      <dc:call-macro name="SQL-wst_columns_statement"/>
+                      <dc:elements>
+                        <column>
+                          <dc:attribute name="name" value="${prot_column_name}"/>
+                          <dc:attribute name="ids" value="fixations-wstv-${prot_rel_pos}-${prot_id}"/>
+                          <dc:attribute name="factory" value="wqinterpol"/>
+                          <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
+                        </column>
+                      </dc:elements>
+                    </dc:context>
+                  </fixation>
+                </dc:elements>
+              </dc:context>
+            </fixations>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="basedata_2_fixations_wqkms">
-          <fixations>
-            <dc:context>
-              <dc:statement>
-                SELECT id          AS prot_id,
-                       description AS prot_description
-                FROM wsts WHERE kind = 2 AND river_id = ${river_id}
-              </dc:statement>
-              <dc:elements>
-                <fixation>
-                  <dc:attribute name="name" value="${prot_description}"/>
-                  <dc:context>
-                    <dc:call-macro name="SQL-wst_columns_statement"/>
-                    <dc:elements>
-                      <column>
-                        <dc:attribute name="name" value="${prot_column_name}"/>
-                        <dc:attribute name="ids" value="fixations-wstv-${prot_rel_pos}-${prot_id}"/>
-                        <dc:attribute name="factory" value="wqinterpol"/>
-                        <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
-                      </column>
-                    </dc:elements>
-                  </dc:context>
-                </fixation>
-              </dc:elements>
-            </dc:context>
-          </fixations>
+          <dc:call-macro name="user-range">
+            <fixations>
+              <dc:context connection="system">
+                <dc:statement>
+                  SELECT id          AS prot_id,
+                         description AS prot_description
+                  FROM wsts WHERE kind = 2 AND river_id = ${river_id}
+                </dc:statement>
+                <dc:elements>
+                  <fixation>
+                    <dc:attribute name="name" value="${prot_description}"/>
+                    <dc:context>
+                      <dc:call-macro name="SQL-wst_columns_statement"/>
+                      <dc:elements>
+                        <column>
+                          <dc:attribute name="name" value="${prot_column_name}"/>
+                          <dc:attribute name="ids" value="fixations-wstv-${prot_rel_pos}-${prot_id}"/>
+                          <dc:attribute name="factory" value="wqinterpol"/>
+                          <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
+                        </column>
+                      </dc:elements>
+                    </dc:context>
+                  </fixation>
+                </dc:elements>
+              </dc:context>
+            </fixations>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="basedata_2_fixations">
-          <fixations>
-            <dc:context>
-              <dc:statement>
-                SELECT id          AS prot_id,
-                       description AS prot_description
-                FROM wsts WHERE kind = 2 AND river_id = ${river_id}
-              </dc:statement>
-              <dc:elements>
-                <fixation>
-                  <dc:attribute name="name" value="${prot_description}"/>
-                  <dc:context>
-                    <dc:call-macro name="SQL-wst_columns_statement"/>
-                    <dc:elements>
-                      <column>
-                        <dc:attribute name="name" value="${prot_column_name}"/>
-                        <dc:attribute name="ids" value="fixations-wstv-${prot_rel_pos}-${prot_id}"/>
-                        <dc:attribute name="factory" value="staticwkms"/>
-                        <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
-                      </column>
-                    </dc:elements>
-                  </dc:context>
-                </fixation>
-              </dc:elements>
-            </dc:context>
-          </fixations>
+          <dc:call-macro name="user-range">
+            <fixations>
+              <dc:context connection="system">
+                <dc:statement>
+                  SELECT id          AS prot_id,
+                         description AS prot_description
+                  FROM wsts WHERE kind = 2 AND river_id = ${river_id}
+                </dc:statement>
+                <dc:elements>
+                  <fixation>
+                    <dc:attribute name="name" value="${prot_description}"/>
+                    <dc:context>
+                      <dc:call-macro name="SQL-wst_columns_statement"/>
+                      <dc:elements>
+                        <column>
+                          <dc:attribute name="name" value="${prot_column_name}"/>
+                          <dc:attribute name="ids" value="fixations-wstv-${prot_rel_pos}-${prot_id}"/>
+                          <dc:attribute name="factory" value="staticwkms"/>
+                          <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
+                        </column>
+                      </dc:elements>
+                    </dc:context>
+                  </fixation>
+                </dc:elements>
+              </dc:context>
+            </fixations>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="basedata_2_fixations_relative_point">
-          <fixations>
-            <dc:context>
-              <dc:statement>
-                SELECT id          AS prot_id,
-                       description AS prot_description
-                FROM wsts WHERE kind = 2 AND river_id = ${river_id}
-              </dc:statement>
-              <dc:elements>
-                <relativepoint>
-                  <dc:attribute name="name" value="${prot_description}"/>
-                  <dc:context>
-                    <dc:call-macro name="SQL-wst_columns_statement"/>
-                    <dc:elements>
-                      <column>
-                        <dc:attribute name="name" value="${prot_column_name}"/>
-                        <dc:attribute name="ids" value="fixations-wstv-${prot_rel_pos}-${prot_id}"/>
-                        <dc:attribute name="factory" value="staticwkms"/>
-                        <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
-                      </column>
-                    </dc:elements>
-                  </dc:context>
-                </relativepoint>
-              </dc:elements>
-            </dc:context>
-          </fixations>
+          <dc:call-macro name="user-range">
+            <fixations>
+              <dc:context connection="system">
+                <dc:statement>
+                  SELECT id          AS prot_id,
+                         description AS prot_description
+                  FROM wsts WHERE kind = 2 AND river_id = ${river_id}
+                </dc:statement>
+                <dc:elements>
+                  <relativepoint>
+                    <dc:attribute name="name" value="${prot_description}"/>
+                    <dc:context>
+                      <dc:call-macro name="SQL-wst_columns_statement"/>
+                      <dc:elements>
+                        <column>
+                          <dc:attribute name="name" value="${prot_column_name}"/>
+                          <dc:attribute name="ids" value="fixations-wstv-${prot_rel_pos}-${prot_id}"/>
+                          <dc:attribute name="factory" value="staticwkms"/>
+                          <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
+                        </column>
+                      </dc:elements>
+                    </dc:context>
+                  </relativepoint>
+                </dc:elements>
+              </dc:context>
+            </fixations>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="basedata_3_officials">
+          <dc:call-macro name="user-range">
             <dc:comment comment=".wst -------------------------------"/>
             <officiallines>
-              <dc:context>
+              <dc:context connection="system">
                 <dc:statement>
                   SELECT id          AS prot_id,
                          description AS prot_description
@@ -308,107 +371,149 @@
                 </dc:elements>
               </dc:context>
             </officiallines>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="basedata_4_heightmarks-points-relative_points">
-          <heightmarks>
-            <dc:context>
-              <dc:statement>
-                SELECT id          AS prot_id,
-                       description AS prot_description
-                FROM wsts WHERE kind = 4 AND river_id = ${river_id}
-              </dc:statement>
-              <dc:elements>
-                <relativepoint>
-                  <dc:attribute name="name" value="${prot_description}"/>
-                  <dc:context>
-                    <dc:call-macro name="SQL-wst_columns_statement"/>
-                    <dc:elements>
-                      <column>
-                        <dc:attribute name="name" value="${prot_column_name}"/>
-                        <dc:attribute name="ids" value="heightmarks_points-wstv-${prot_rel_pos}-${prot_id}"/>
-                        <dc:attribute name="factory" value="staticwkms"/>
-                        <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
-                      </column>
-                    </dc:elements>
-                  </dc:context>
-                </relativepoint>
-              </dc:elements>
-            </dc:context>
-          </heightmarks>
+          <dc:call-macro name="user-range">
+            <heightmarks>
+              <dc:context connection="system">
+                <dc:statement>
+                  SELECT id          AS prot_id,
+                         description AS prot_description
+                  FROM wsts WHERE kind = 4 AND river_id = ${river_id}
+                </dc:statement>
+                <dc:elements>
+                  <relativepoint>
+                    <dc:attribute name="name" value="${prot_description}"/>
+                    <dc:context>
+                      <dc:call-macro name="SQL-wst_columns_statement"/>
+                      <dc:elements>
+                        <column>
+                          <dc:attribute name="name" value="${prot_column_name}"/>
+                          <dc:attribute name="ids" value="heightmarks_points-wstv-${prot_rel_pos}-${prot_id}"/>
+                          <dc:attribute name="factory" value="staticwkms"/>
+                          <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
+                        </column>
+                      </dc:elements>
+                    </dc:context>
+                  </relativepoint>
+                </dc:elements>
+              </dc:context>
+            </heightmarks>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="basedata_4_heightmarks-points">
-          <heightmarks>
-            <dc:context>
-              <dc:statement>
-                SELECT id          AS prot_id,
-                       description AS prot_description
-                FROM wsts WHERE kind = 4 AND river_id = ${river_id}
-              </dc:statement>
-              <dc:elements>
-                <heightmark>
-                  <dc:attribute name="name" value="${prot_description}"/>
-                  <dc:context>
-                    <dc:call-macro name="SQL-wst_columns_statement"/>
-                    <dc:elements>
-                      <column>
-                        <dc:attribute name="name" value="${prot_column_name}"/>
-                        <dc:attribute name="ids" value="heightmarks_points-wstv-${prot_rel_pos}-${prot_id}"/>
-                        <dc:attribute name="factory" value="staticwkms"/>
-                        <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
-                      </column>
-                    </dc:elements>
-                  </dc:context>
-                </heightmark>
-              </dc:elements>
-            </dc:context>
-          </heightmarks>
+          <dc:call-macro name="user-range">
+            <heightmarks>
+              <dc:context connection="system">
+                <dc:statement>
+                  SELECT id          AS prot_id,
+                         description AS prot_description
+                  FROM wsts WHERE kind = 4 AND river_id = ${river_id}
+                </dc:statement>
+                <dc:elements>
+                  <heightmark>
+                    <dc:attribute name="name" value="${prot_description}"/>
+                    <dc:context>
+                      <dc:call-macro name="SQL-wst_columns_statement"/>
+                      <dc:elements>
+                        <column>
+                          <dc:attribute name="name" value="${prot_column_name}"/>
+                          <dc:attribute name="ids" value="heightmarks_points-wstv-${prot_rel_pos}-${prot_id}"/>
+                          <dc:attribute name="factory" value="staticwkms"/>
+                          <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
+                        </column>
+                      </dc:elements>
+                    </dc:context>
+                  </heightmark>
+                </dc:elements>
+              </dc:context>
+            </heightmarks>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="basedata_4_heightmarks-wq">
-          <heightmarks>
-            <dc:context>
-              <dc:statement>
-                SELECT id          AS prot_id,
-                       description AS prot_description
-                FROM wsts WHERE kind = 4 AND river_id = ${river_id}
-              </dc:statement>
-              <dc:elements>
-                <heightmark>
-                  <dc:attribute name="name" value="${prot_description}"/>
-                  <dc:context>
-                    <dc:call-macro name="SQL-wst_columns_statement"/>
-                    <dc:elements>
-                      <column>
-                        <dc:attribute name="name" value="${prot_column_name}"/>
-                        <dc:attribute name="ids" value="heightmarks_annotations-wstv-${prot_rel_pos}-${prot_id}"/>
-                        <dc:attribute name="factory" value="wqinterpol"/>
-                        <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
-                      </column>
-                    </dc:elements>
-                  </dc:context>
-                </heightmark>
-              </dc:elements>
-            </dc:context>
-          </heightmarks>
+          <dc:call-macro name="user-range">
+            <heightmarks>
+              <dc:context connection="system">
+                <dc:statement>
+                  SELECT id          AS prot_id,
+                         description AS prot_description
+                  FROM wsts WHERE kind = 4 AND river_id = ${river_id}
+                </dc:statement>
+                <dc:elements>
+                  <heightmark>
+                    <dc:attribute name="name" value="${prot_description}"/>
+                    <dc:context>
+                      <dc:call-macro name="SQL-wst_columns_statement"/>
+                      <dc:elements>
+                        <column>
+                          <dc:attribute name="name" value="${prot_column_name}"/>
+                          <dc:attribute name="ids" value="heightmarks_annotations-wstv-${prot_rel_pos}-${prot_id}"/>
+                          <dc:attribute name="factory" value="wqinterpol"/>
+                          <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
+                        </column>
+                      </dc:elements>
+                    </dc:context>
+                  </heightmark>
+                </dc:elements>
+              </dc:context>
+            </heightmarks>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="basedata_5_flood-protections_relative_points">
-          <flood_protections>
-            <dc:attribute name="id" value="flood-protections-${river_id}"/>
-            <dc:context>
-              <dc:statement>
-                SELECT id          AS prot_id,
-                       description AS prot_description
-                FROM wsts WHERE kind = 5 AND river_id = ${river_id}
-              </dc:statement>
-              <dc:elements>
-                <relativepoint>
-                  <dc:attribute name="name" value="${prot_description}"/>
-                  <dc:attribute name="db-id" value="${prot_id}"/>
-                  <dc:attribute name="factory" value="staticwkms"/>
-                  <columns>
+          <dc:call-macro name="user-range">
+            <flood_protections>
+              <dc:attribute name="id" value="flood-protections-${river_id}"/>
+              <dc:context connection="system">
+                <dc:statement>
+                  SELECT id          AS prot_id,
+                         description AS prot_description
+                  FROM wsts WHERE kind = 5 AND river_id = ${river_id}
+                </dc:statement>
+                <dc:elements>
+                  <relativepoint>
+                    <dc:attribute name="name" value="${prot_description}"/>
+                    <dc:attribute name="db-id" value="${prot_id}"/>
+                    <dc:attribute name="factory" value="staticwkms"/>
+                    <columns>
+                      <dc:context>
+                        <dc:call-macro name="SQL-wst_columns_statement"/>
+                        <dc:elements>
+                          <column>
+                            <dc:attribute name="name" value="${prot_column_name}"/>
+                            <dc:attribute name="ids" value="flood_protection-wstv-${prot_rel_pos}-${prot_id}"/>
+                            <dc:attribute name="factory" value="staticwkms"/>
+                            <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
+                          </column>
+                        </dc:elements>
+                      </dc:context>
+                    </columns>
+                  </relativepoint>
+                </dc:elements>
+              </dc:context>
+            </flood_protections>
+          </dc:call-macro>
+        </dc:macro>
+
+        <dc:macro name="basedata_5_flood-protections">
+          <dc:call-macro name="user-range">
+            <flood_protections>
+              <dc:attribute name="id" value="flood-protections-${river_id}"/>
+              <dc:context connection="system">
+                <dc:statement>
+                  SELECT id          AS prot_id,
+                         description AS prot_description
+                  FROM wsts WHERE kind = 5 AND river_id = ${river_id}
+                </dc:statement>
+                <dc:elements>
+                  <flood_protection>
+                    <dc:attribute name="name" value="${prot_description}"/>
+                    <dc:attribute name="db-id" value="${prot_id}"/>
+                    <dc:attribute name="factory" value="staticwkms"/>
                     <dc:context>
                       <dc:call-macro name="SQL-wst_columns_statement"/>
                       <dc:elements>
@@ -420,42 +525,11 @@
                         </column>
                       </dc:elements>
                     </dc:context>
-                  </columns>
-                </relativepoint>
-              </dc:elements>
-            </dc:context>
-          </flood_protections>
-        </dc:macro>
-
-        <dc:macro name="basedata_5_flood-protections">
-          <flood_protections>
-            <dc:attribute name="id" value="flood-protections-${river_id}"/>
-            <dc:context>
-              <dc:statement>
-                SELECT id          AS prot_id,
-                       description AS prot_description
-                FROM wsts WHERE kind = 5 AND river_id = ${river_id}
-              </dc:statement>
-              <dc:elements>
-                <flood_protection>
-                  <dc:attribute name="name" value="${prot_description}"/>
-                  <dc:attribute name="db-id" value="${prot_id}"/>
-                  <dc:attribute name="factory" value="staticwkms"/>
-                  <dc:context>
-                    <dc:call-macro name="SQL-wst_columns_statement"/>
-                    <dc:elements>
-                      <column>
-                        <dc:attribute name="name" value="${prot_column_name}"/>
-                        <dc:attribute name="ids" value="flood_protection-wstv-${prot_rel_pos}-${prot_id}"/>
-                        <dc:attribute name="factory" value="staticwkms"/>
-                        <dc:attribute name="info" value="${info} [km ${deffrom} - ${defto}]"/>
-                      </column>
-                    </dc:elements>
-                  </dc:context>
-                </flood_protection>
-              </dc:elements>
-            </dc:context>
-          </flood_protections>
+                  </flood_protection>
+                </dc:elements>
+              </dc:context>
+            </flood_protections>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="mainvalues">
@@ -512,97 +586,129 @@
         </dc:macro>
 
         <dc:macro name="cross_sections">
-          <cross_sections>
-            <dc:attribute name="id" value="flood-protections-${river_id}"/>
-            <dc:context>
-              <dc:statement>
-                SELECT id          AS prot_id,
-                       description AS prot_description
-                FROM cross_sections WHERE river_id = ${river_id}
-              </dc:statement>
-              <dc:elements>
-                <cross_section>
-                  <dc:attribute name="name" value="${prot_description}"/>
-                  <dc:attribute name="ids" value="${prot_id}"/>
-                  <dc:attribute name="factory" value="crosssections"/>
-                </cross_section>
-              </dc:elements>
-            </dc:context>
-          </cross_sections>
+          <dc:call-macro name="user-range">
+            <cross_sections>
+              <dc:attribute name="id" value="flood-protections-${river_id}"/>
+              <dc:context connection="system">
+                <dc:statement>
+                  SELECT DISTINCT ON (cs.id)
+                         cs.id          AS prot_id,
+                         cs.description AS prot_description
+                  FROM cross_sections cs
+                    JOIN cross_section_lines csl ON csl.cross_section_id = cs.id
+                  WHERE cs.river_id = ${river_id}
+                    AND csl.km BETWEEN ${fromkm} AND ${tokm}
+                </dc:statement>
+                <dc:elements>
+                  <cross_section>
+                    <dc:attribute name="name" value="${prot_description}"/>
+                    <dc:attribute name="ids" value="${prot_id}"/>
+                    <dc:attribute name="factory" value="crosssections"/>
+                  </cross_section>
+                </dc:elements>
+              </dc:context>
+            </cross_sections>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="hyks">
-          <hyks>
-            <dc:attribute name="id" value="hyk-${river_id}"/>
-            <dc:context>
-              <dc:statement>
-                SELECT id          AS hyk_id,
-                       description AS hyk_description
-                FROM hyks WHERE river_id = ${river_id}
-              </dc:statement>
-              <dc:elements>
-                <hyk>
-                  <dc:attribute name="name" value="${hyk_description}"/>
-                  <dc:attribute name="ids" value="${hyk_id}"/>
-                  <dc:attribute name="factory" value="hyk"/>
-                </hyk>
-              </dc:elements>
-            </dc:context>
-          </hyks>
+          <dc:call-macro name="user-range">
+            <hyks>
+              <dc:attribute name="id" value="hyk-${river_id}"/>
+              <dc:context connection="system">
+                <dc:statement>
+                  SELECT DISTINCT ON (h.id)
+                         h.id          AS hyk_id,
+                         h.description AS hyk_description
+                  FROM hyks h
+                    JOIN hyk_entries he ON he.hyk_id = h.id
+                  WHERE river_id = ${river_id}
+                    AND he.km BETWEEN ${fromkm} AND ${tokm}
+                </dc:statement>
+                <dc:elements>
+                  <hyk>
+                    <dc:attribute name="name" value="${hyk_description}"/>
+                    <dc:attribute name="ids" value="${hyk_id}"/>
+                    <dc:attribute name="factory" value="hyk"/>
+                  </hyk>
+                </dc:elements>
+              </dc:context>
+            </hyks>
+          </dc:call-macro>
         </dc:macro>
 
         <dc:macro name="flow_velocity_measurements">
-          <flowvelocitymeasurement>
-          <dc:context>
-              <dc:statement>
-                SELECT id          AS fvmid,
-                       description AS fvmd
-                FROM flow_velocity_measurements WHERE river_id = ${river_id}
-              </dc:statement>
-              <dc:elements>
-                <flow_velocity_measurement>
-                  <dc:attribute name="name" value="${fvmd}"/>
-                  <dc:attribute name="ids" value="${fvmid}"/>
-                  <dc:attribute name="factory" value="flowvelocity"/>
-                  <dc:context>
-                      <dc:statement>
-                              SELECT id, description, station, datetime, v, w, q
-                              FROM flow_velocity_measure_values
-                              WHERE measurements_id = ${fvmid}
-                      </dc:statement>
-                      <dc:elements>
-                         <measurement_value>
-                           <dc:attribute name="name" value="${id}-${description}-${station}-${datetime}"/>
-                           <dc:attribute name="ids" value="${id}"/>
-                           <dc:attribute name="factory" value="flowvelocity"/>
-                         </measurement_value>
-                       </dc:elements>
-                  </dc:context>
-                </flow_velocity_measurement>
+          <dc:call-macro name="user-range">
+            <flowvelocitymeasurement>
+            <dc:context connection="system">
+                <dc:statement>
+                  SELECT id          AS fvmid,
+                         description AS fvmd
+                  FROM flow_velocity_measurements WHERE river_id = ${river_id}
+                </dc:statement>
+                <dc:elements>
+                  <flow_velocity_measurement>
+                    <dc:attribute name="name" value="${fvmd}"/>
+                    <dc:attribute name="ids" value="${fvmid}"/>
+                    <dc:attribute name="factory" value="flowvelocity"/>
+                    <dc:context>
+                        <dc:statement>
+                                SELECT id, description, station, datetime, v, w, q
+                                FROM flow_velocity_measure_values
+                                WHERE measurements_id = ${fvmid}
+                                  AND station BETWEEN ${fromkm} AND ${tokm}
+                        </dc:statement>
+                        <dc:elements>
+                           <measurement_value>
+                             <dc:attribute name="name" value="${id}-${description}-${station}-${datetime}"/>
+                             <dc:attribute name="ids" value="${id}"/>
+                             <dc:attribute name="factory" value="flowvelocity"/>
+                           </measurement_value>
+                         </dc:elements>
+                    </dc:context>
+                  </flow_velocity_measurement>
               </dc:elements>
           </dc:context>
           </flowvelocitymeasurement>
+        </dc:call-macro>
+        </dc:macro>
+
+        <dc:macro name="sounding-width">
+          <soundings_width>
+            <dc:context>
+              <dc:statement>
+                  SELECT id          AS bedh_id,
+                         year        AS bedh_year,
+                         description AS bedh_descr
+                  FROM bed_height_single WHERE river_id = ${river_id}
+              </dc:statement>
+              <dc:elements>
+                <height>
+                  <dc:attribute name="factory" value="bedheight"/>
+                  <dc:attribute name="ids" value="bedheight-singlevalues-${bedh_id}-${bedh_year}"/>
+                  <dc:attribute name="description" value="${bedh_descr}"/>
+                </height>
+              </dc:elements>
+            </dc:context>
+          </soundings_width>
         </dc:macro>
 
         <dc:macro name="longitudinal-section-prototype">
-                 <dc:call-macro name="basedata_0"/>
-                 <dc:call-macro name="basedata_1_additionals"/>
-                 <dc:comment comment=" FIXATIONS ---------------------------"/>
-                 <dc:call-macro name="basedata_2_fixations"/>
-                 <dc:comment comment=" HOEHENMARKEN ---------------------------"/>
-                 <dc:call-macro name="basedata_4_heightmarks-points"/>
-                 <dc:comment comment=" AMTL LINIEN  ---------------------------"/>
-                 <dc:call-macro name="basedata_3_officials"/>
-                 <dc:call-macro name="basedata_5_flood-protections"/>
-                 <dc:call-macro name="annotations_per_type"/>
+          <dc:call-macro name="basedata_0"/>
+          <dc:call-macro name="basedata_1_additionals"/>
+          <dc:comment comment=" FIXATIONS ---------------------------"/>
+          <dc:call-macro name="basedata_2_fixations"/>
+          <dc:comment comment=" HOEHENMARKEN ---------------------------"/>
+          <dc:call-macro name="basedata_4_heightmarks-points"/>
+          <dc:comment comment=" AMTL LINIEN  ---------------------------"/>
+          <dc:call-macro name="basedata_3_officials"/>
+          <dc:call-macro name="basedata_5_flood-protections"/>
+          <dc:call-macro name="annotations_per_type"/>
         </dc:macro>
 
         <dc:comment>
-
         + River-Node
-
         </dc:comment>
-
         <river>
           <dc:attribute name="name" value="${river_name}"/>
 
@@ -709,23 +815,7 @@
                 MINFO bedheight middle
               </dc:comment>
               <dc:if test="dc:contains($artifact-outs, 'bedheight_middle')">
-                <soundings_width>
-                  <dc:context>
-                    <dc:statement>
-                        SELECT id          AS bedh_id,
-                               year        AS bedh_year,
-                               description AS bedh_descr
-                        FROM bed_height_single WHERE river_id = ${river_id}
-                    </dc:statement>
-                    <dc:elements>
-                      <height>
-                        <dc:attribute name="factory" value="bedheight"/>
-                        <dc:attribute name="ids" value="bedheight-singlevalues-${bedh_id}-${bedh_year}"/>
-                        <dc:attribute name="description" value="${bedh_descr}"/>
-                      </height>
-                    </dc:elements>
-                  </dc:context>
-                </soundings_width>
+                <dc:call-macro name="sounding-width"/>
               </dc:if>
              <dc:comment comment="--- non-recommendations---"/>
             </dc:otherwise>
@@ -1318,18 +1408,45 @@
     <dc:when test="dc:contains($parameters, 'user-id')">
 
 
-     <old_calculations>
-      <dc:context connection="user">
-        <dc:comment>
-           Get the user and collection-id.
-        </dc:comment>
-        <dc:statement>
+    <old_calculations>
+      <!--      <dc:macro name="load-user">-->
+        <dc:call-macro name="user-range">
+        <dc:context connection="user">
+          <dc:comment>
+            Get the user and collection-id.
+          </dc:comment>
+          <dc:statement>
             SELECT u.id AS user_id, c.id AS collection_id, c.name as collection_name
             FROM collections c JOIN users u ON c.user_id = u.id
             WHERE u.gid = CAST(${user-id} AS uuid)
             ORDER BY c.creation DESC
         </dc:statement>
 
+
+        <dc:macro name="range-filter">
+                 <dc:statement>
+                   SELECT m.id AS a_id,
+                          m.state AS a_state,
+                          m.gid AS a_gid,
+                          m.creation AS a_creation,
+                          COALESCE(ld_mode, '')      AS ld_m,
+                          COALESCE(ld_locations, '') AS ld_l,
+                          COALESCE(ld_from, '')      AS ld_f,
+                          COALESCE(ld_to, '')        AS ld_t
+                   FROM   master_artifacts_range m
+                   WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
+                   AND EXISTS (
+                       SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
+                 </dc:statement>
+                 <dc:elements>
+                   <dc:variable name="from" type="number" expr="dc:fromValue($ld_m, $ld_l, $ld_f)"/>
+                   <dc:variable name="to" type="number" expr="dc:toValue($ld_m, $ld_l, $ld_t)"/>
+                   <dc:if test="($from &gt;= $fromkm and $from &lt;= $tokm) or ($to &lt;= $tokm and $to &gt;= $fromkm) or ($from &lt;= $fromkm and $to &gt;= $tokm)">
+                     <dc:macro-body/>
+                   </dc:if>
+                 </dc:elements>
+               </dc:macro>
+ 
         <!-- OFFICIAL LINES -->
         <dc:if test="dc:contains($artifact-outs, 'longitudinal_section')">
           <dc:comment comment=".wst -------------------------------"/>
@@ -1337,11 +1454,27 @@
             <dc:elements>
                 <dc:context>
                   <dc:statement>
-                    SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation, ardg.v as gaugy, arv.v as wqsingle
-                    FROM   master_artifacts m, artifact_data ardg, artifact_data arv
-                    WHERE  m.collection_id = ${collection_id} AND m.gid = CAST(${artifact-id} AS uuid) AND ardg.artifact_id = m.id AND ardg.k = 'ld_gaugename' AND arv.artifact_id = m.id AND arv.k = 'wq_single'
-                    AND EXISTS (
-                        SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
+                    SELECT m.id AS a_id,
+                           m.state AS a_state,
+                           m.gid AS a_gid,
+                           m.creation AS a_creation,
+                           ardg.v AS gaugy,
+                           arv.v AS wqsingle
+                    FROM   master_artifacts m,
+                           artifact_data ardg,
+                           artifact_data arv
+                    WHERE  m.collection_id = ${collection_id}
+                      AND m.gid = CAST(${artifact-id} AS uuid)
+                      AND ardg.artifact_id = m.id
+                      AND ardg.k = 'ld_gaugename'
+                      AND arv.artifact_id = m.id
+                      AND arv.k = 'wq_single'
+                      AND EXISTS (
+                        SELECT id
+                        FROM artifact_data ad
+                        WHERE ad.artifact_id = m.id
+                          AND k = 'river'
+                          AND v = ${river})
                   </dc:statement>
                   <dc:elements>
                       <dc:context connection="system">
@@ -1369,18 +1502,11 @@
           SHOW W-DIFFERENCES
         </dc:comment>
 
-        <dc:if test="dc:contains($artifact-outs, 'longitudinal_section') or (dc:contains($artifact-outs, 'w_differences') or (dc:contains($artifact-outs, 'discharge_longitudinal_section')))">
+       <dc:macro name="differences">
           <differences>
             <dc:elements>
                 <dc:context>
-                  <dc:statement>
-                    SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                    FROM   master_artifacts m
-                    WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                    AND EXISTS (
-                        SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                  </dc:statement>
-                  <dc:elements>
+                  <dc:call-macro name="range-filter">
                       <dc:context>
                         <dc:statement>
                           SELECT a.gid as aid, f.id AS fid, f.name AS facet_name, f.num AS facet_num, f.description as facet_description
@@ -1397,28 +1523,22 @@
                           </dc:element>
                         </dc:elements>
                       </dc:context>
-                  </dc:elements>
+                    </dc:call-macro>
                 </dc:context>
             </dc:elements>
           </differences>
-        </dc:if>
+        </dc:macro>
 
         <dc:comment>
           SHOW REFERENCE CURVE
         </dc:comment>
 
-        <dc:if test="dc:contains($artifact-outs, 'reference_curve')">
+
+        <dc:macro name="reference-curves">
           <reference_curves>
             <dc:elements>
                 <dc:context>
-                  <dc:statement>
-                    SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                    FROM   master_artifacts m
-                    WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                    AND EXISTS (
-                        SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                  </dc:statement>
-                  <dc:elements>
+                  <dc:call-macro name="user-range">
                       <dc:context>
                         <dc:statement>
                           SELECT a.gid as aid, f.id AS fid, f.name AS facet_name, f.num AS facet_num, f.description as facet_description
@@ -1435,28 +1555,21 @@
                           </dc:element>
                         </dc:elements>
                       </dc:context>
-                  </dc:elements>
+                    </dc:call-macro>
                 </dc:context>
             </dc:elements>
           </reference_curves>
-        </dc:if>
+        </dc:macro>
 
         <dc:comment>
           SHOW COMPUTED DISCHARGE CURVES
         </dc:comment>
 
-        <dc:if test="dc:contains($artifact-outs, 'computed_discharge_curve')">
+        <dc:macro name="computed-discharge-curve">
           <computed_discharge_curves>
             <dc:elements>
                 <dc:context>
-                  <dc:statement>
-                    SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                    FROM   master_artifacts m
-                    WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                    AND EXISTS (
-                        SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                  </dc:statement>
-                  <dc:elements>
+                  <dc:call-macro name="range-filter">
                       <dc:context>
                         <dc:statement>
                           SELECT a.gid as aid, f.id AS fid, f.name AS facet_name, f.num AS facet_num, f.description as facet_description
@@ -1473,43 +1586,37 @@
                           </dc:element>
                         </dc:elements>
                       </dc:context>
-                  </dc:elements>
+                    </dc:call-macro>
                 </dc:context>
             </dc:elements>
           </computed_discharge_curves>
-        </dc:if>
+        </dc:macro>
 
         <dc:comment>
           CROSS SECTION
         </dc:comment>
 
-        <dc:if test="dc:contains($artifact-outs, 'cross_section')">
+
+        <dc:macro name="waterlevels">
           <waterlevels>
             <dc:elements>
               <dc:context>
-                 <dc:statement>
-                   SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                   FROM   master_artifacts m
-                   WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                   AND EXISTS (
-                       SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                 </dc:statement>
-                 <dc:elements>
-                   <dc:context>
-                     <dc:statement>
-                       SELECT id AS out_id
-                       FROM outs
-                       WHERE artifact_id = ${a_id} AND name = 'cross_section'
-                     </dc:statement>
-                     <dc:elements>
-                       <dc:context>
-                         <dc:statement>
-                           SELECT name AS facet_name, num as facet_num, description AS facet_description
-                           FROM facets
-                           WHERE out_id = ${out_id}
-                           ORDER BY num ASC, name DESC
-                         </dc:statement>
-                         <longitudinal_section_columns>
+                <dc:call-macro name="range-filter">
+                     <dc:context>
+                       <dc:statement>
+                         SELECT id AS out_id
+                         FROM outs
+                         WHERE artifact_id = ${a_id} AND name = 'cross_section'
+                       </dc:statement>
+                       <dc:elements>
+                         <dc:context>
+                           <dc:statement>
+                             SELECT name AS facet_name, num as facet_num, description AS facet_description
+                             FROM facets
+                             WHERE out_id = ${out_id}
+                             ORDER BY num ASC, name DESC
+                           </dc:statement>
+                           <longitudinal_section_columns>
                              <dc:attribute name="description" value="${river} ${a_creation}"/>
                              <dc:elements>
                                <dc:element name="${facet_name}">
@@ -1520,30 +1627,23 @@
                                  <dc:attribute name="out" value="cross_section"/>
                                </dc:element>
                              </dc:elements>
-                         </longitudinal_section_columns>
-                       </dc:context>
-                     </dc:elements>
-                   </dc:context>
-                 </dc:elements>
+                           </longitudinal_section_columns>
+                         </dc:context>
+                       </dc:elements>
+                     </dc:context>
+                   </dc:call-macro>
                </dc:context>
              </dc:elements>
           </waterlevels>
-        </dc:if>
+        </dc:macro>
 
 
-        <dc:if test="dc:contains($artifact-outs, 'longitudinal_section') or (dc:contains($artifact-outs, 'discharge_longitudinal_section') or (dc:contains($artifact-outs, 'w_differences')))">
+        <dc:macro name="longitudinal">
           <waterlevels>
             <dc:elements>
               <dc:context>
-                 <dc:statement>
-                   SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                   FROM   master_artifacts m
-                   WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                   AND EXISTS (
-                       SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                 </dc:statement>
-                 <dc:elements>
-                   <dc:context>
+                <dc:call-macro name="range-filter">
+                  <dc:context>
                      <dc:statement>
                        SELECT id AS out_id
                        FROM outs
@@ -1572,25 +1672,18 @@
                        </dc:context>
                      </dc:elements>
                    </dc:context>
-                 </dc:elements>
+                 </dc:call-macro>
                </dc:context>
              </dc:elements>
           </waterlevels>
-        </dc:if>
+        </dc:macro>
 
-        <dc:if test="dc:contains($artifact-outs, 'fix_longitudinal_section_curve')">
+
+        <dc:macro name="longitudinal-section">
           <waterlevels>
             <dc:elements>
               <dc:context>
-                 <dc:statement>
-                   SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                   FROM   master_artifacts m
-                   WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                   AND EXISTS (
-                       SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                 </dc:statement>
-                 <dc:elements>
-
+                <dc:call-macro name="range-filter">
                    <dc:context>
                    <dc:statement>
                      SELECT id AS out_id
@@ -1621,25 +1714,17 @@
                      </dc:context>
                    </dc:elements>
                   </dc:context>
-                 </dc:elements>
+                </dc:call-macro>
                 </dc:context>
              </dc:elements>
           </waterlevels>
-        </dc:if>
+        </dc:macro>
 
-        <dc:if test="dc:contains($artifact-outs, 'fix_deltawt_curve')">
+        <dc:macro name="delta-wt">
           <waterlevels>
             <dc:elements>
               <dc:context>
-                 <dc:statement>
-                   SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                   FROM   master_artifacts m
-                   WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                   AND EXISTS (
-                       SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                 </dc:statement>
-                 <dc:elements>
-
+                <dc:call-macro name="range-filter">
                    <dc:context>
                    <dc:statement>
                      SELECT id AS out_id
@@ -1669,25 +1754,18 @@
                      </dc:context>
                    </dc:elements>
                   </dc:context>
-                 </dc:elements>
+                </dc:call-macro>
                 </dc:context>
              </dc:elements>
           </waterlevels>
-        </dc:if>
+        </dc:macro>
 
-        <dc:if test="dc:contains($artifact-outs, 'fix_derivate_curve')">
+
+        <dc:macro name="fix-derivate-curve">
           <waterlevels>
             <dc:elements>
               <dc:context>
-                 <dc:statement>
-                   SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                   FROM   master_artifacts m
-                   WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                   AND EXISTS (
-                       SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                 </dc:statement>
-                 <dc:elements>
-
+                <dc:call-macro name="range-filter">
                    <dc:context>
                    <dc:statement>
                      SELECT id AS out_id
@@ -1717,25 +1795,18 @@
                      </dc:context>
                    </dc:elements>
                   </dc:context>
-                 </dc:elements>
+                </dc:call-macro>
                 </dc:context>
              </dc:elements>
           </waterlevels>
-        </dc:if>
+        </dc:macro>
 
-        <dc:if test="dc:contains($artifact-outs, 'fix_wq_curve')">
+
+        <dc:macro name="fix-wq-curve">
           <waterlevels>
             <dc:elements>
               <dc:context>
-                 <dc:statement>
-                   SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                   FROM   master_artifacts m
-                   WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                   AND EXISTS (
-                       SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                 </dc:statement>
-                 <dc:elements>
-
+                <dc:call-macro name="range-filter">
                    <dc:context>
                    <dc:statement>
                      SELECT id AS out_id
@@ -1765,24 +1836,18 @@
                      </dc:context>
                    </dc:elements>
                   </dc:context>
-                 </dc:elements>
+                </dc:call-macro>
                 </dc:context>
              </dc:elements>
           </waterlevels>
-        </dc:if>
+        </dc:macro>
 
-        <dc:if test="dc:contains($artifact-outs, 'duration_curve')">
+
+        <dc:macro name="duration-curve">
           <computed_discharge_curves>
             <dc:elements>
                 <dc:context>
-                  <dc:statement>
-                    SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                    FROM   master_artifacts m
-                    WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                    AND EXISTS (
-                        SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                  </dc:statement>
-                  <dc:elements>
+                  <dc:call-macro name="range-filter">
                       <dc:context>
                         <dc:statement>
                           SELECT a.gid as aid, f.id AS fid, f.name AS facet_name, f.num AS facet_num, f.description as facet_description
@@ -1799,29 +1864,23 @@
                           </dc:element>
                         </dc:elements>
                       </dc:context>
-                  </dc:elements>
+                    </dc:call-macro>
                 </dc:context>
             </dc:elements>
           </computed_discharge_curves>
-        </dc:if>
+        </dc:macro>
+
         <dc:comment>
            WATERLEVELS - ONLY SHOW Ws
         </dc:comment>
 
         <!-- TODO doesnt work nicely for fix/wq-diags. -->
-        <dc:if test="dc:contains($artifact-outs, 'waterlevels') or (dc:contains($artifact-outs, 'fix_wq_curve'))"> 
+
+        <dc:macro name="waterlevel-fix">
           <waterlevels>
             <dc:elements>
               <dc:context>
-                 <dc:statement>
-                   SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                   FROM   master_artifacts m
-                   WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                   AND EXISTS (
-                       SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                 </dc:statement>
-                 <dc:elements>
-
+                <dc:call-macro name="range-filter">
                    <dc:context>
                    <dc:statement>
                      SELECT id AS out_id
@@ -1851,28 +1910,22 @@
                      </dc:context>
                    </dc:elements>
                   </dc:context>
-                 </dc:elements>
+                </dc:call-macro>
                 </dc:context>
              </dc:elements>
           </waterlevels>
-        </dc:if>
+        </dc:macro>
 
         <dc:comment>
           SHOW FLOODMAPS
         </dc:comment>
 
-        <dc:if test="dc:contains($artifact-outs, 'floodmap') or dc:contains($artifact-outs, 'map')">
+
+        <dc:macro name="flood-map">
           <floodmap>
             <dc:elements>
                 <dc:context>
-                  <dc:statement>
-                    SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                    FROM   master_artifacts m
-                    WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                    AND EXISTS (
-                        SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                  </dc:statement>
-                  <dc:elements>
+                  <dc:call-macro name="range-filter">
                       <dc:context>
                         <dc:statement>
                           SELECT a.gid as aid, f.id AS fid, f.name AS facet_name, f.num AS facet_num, f.description as facet_description
@@ -1889,28 +1942,21 @@
                           </dc:element>
                         </dc:elements>
                       </dc:context>
-                  </dc:elements>
+                  </dc:call-macro>
                 </dc:context>
             </dc:elements>
           </floodmap>
-        </dc:if>
+        </dc:macro>
 
         <dc:comment>
           MINFO bedheight difference
         </dc:comment>
 
-        <dc:if test="dc:contains($artifact-outs, 'bed_difference_year') or dc:contains($artifact-outs, 'bed_difference_height_year')">
+        <dc:macro name="bed-difference">
           <fix_longitudinal_section_curve>
             <dc:elements>
                 <dc:context>
-                  <dc:statement>
-                    SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                    FROM   master_artifacts m
-                    WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                    AND EXISTS (
-                        SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                  </dc:statement>
-                  <dc:elements>
+                  <dc:call-macro name="range-filter">
                       <dc:context>
                         <dc:statement>
                           SELECT a.gid as aid, f.id AS fid, f.name AS facet_name, f.num AS facet_num, f.description as facet_description
@@ -1932,28 +1978,21 @@
                             </dc:elements>
                         </fix_longitudinal_section_curve>
                         </dc:context>
-                    </dc:elements>
+                    </dc:call-macro>
                 </dc:context>
             </dc:elements>
           </fix_longitudinal_section_curve>
-        </dc:if>
+        </dc:macro>
 
         <dc:comment>
           MINFO bedheight middle
         </dc:comment>
 
-        <dc:if test="dc:contains($artifact-outs, 'bedheight_middle')">
+        <dc:macro name="bed-height">
           <fix_vollmer_wq_curve>
             <dc:elements>
                 <dc:context>
-                  <dc:statement>
-                    SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
-                    FROM   master_artifacts m
-                    WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                    AND EXISTS (
-                        SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
-                  </dc:statement>
-                  <dc:elements>
+                  <dc:call-macro name="range-filter">
                       <dc:context>
                         <dc:statement>
                           SELECT a.gid as aid, f.id AS fid, f.name AS facet_name, f.num AS facet_num, f.description as facet_description
@@ -1974,13 +2013,58 @@
                             </dc:elements>
                         </fix_vollmer_wq_curve>
                         </dc:context>
-                    </dc:elements>
+                    </dc:call-macro>
                 </dc:context>
             </dc:elements>
           </fix_vollmer_wq_curve>
+        </dc:macro>
+
+        <dc:if test="dc:contains($artifact-outs, 'longitudinal_section') or (dc:contains($artifact-outs, 'discharge_longitudinal_section') or (dc:contains($artifact-outs, 'w_differences')))">
+          <dc:call-macro name="longitudinal"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'fix_deltawt_curve')">
+          <dc:call-macro name="delta-wt"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'longitudinal_section') or (dc:contains($artifact-outs, 'w_differences') or (dc:contains($artifact-outs, 'discharge_longitudinal_section')))">
+          <dc:call-macro name="differences"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'reference_curve')">
+          <dc:call-macro name="reference-curves"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'computed_discharge_curve')">
+          <dc:call-macro name="computed-discharge-curve"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'cross_section')">
+          <dc:call-macro name="waterlevels"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'fix_longitudinal_section_curve')">
+          <dc:call-macro name="longitudinal-section"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'fix_derivate_curve')">
+          <dc:call-macro name="fix-derivate-curve"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'fix_wq_curve')">
+          <dc:call-macro name="fix-wq-curve"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'duration_curve')">
+          <dc:call-macro name="duration-curve"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'waterlevels') or (dc:contains($artifact-outs, 'fix_wq_curve'))">
+          <dc:call-macro name="waterlevels-fix"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'floodmap') or dc:contains($artifact-outs, 'map')">
+          <dc:call-macro name="flood-map"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'bed_difference_year') or dc:contains($artifact-outs, 'bed_difference_height_year')">
+          <dc:call-macro name="bed-difference"/>
+        </dc:if>
+        <dc:if test="dc:contains($artifact-outs, 'bedheight_middle')">
+          <dc:call-macro name="bed-height"/>
         </dc:if>
 
       </dc:context>
+  </dc:call-macro>
+
       </old_calculations>
 
 

http://dive4elements.wald.intevation.org