view artifacts/doc/conf/meta-data.xml @ 6069:dcbc06ca5d19

meta-data.xml: Make a select a bit more human-resolvable.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 23 May 2013 11:18:51 +0200
parents 5db238b0176e
children cd8f77794a03
line wrap: on
line source
<?xml version="1.0" encoding="UTF-8"?>
<dc:template xmlns:dc="http://www.intevation.org/2011/Datacage">
  <datacage>

    <dc:comment>
      User specific part
      ------------------
    </dc:comment>
    <dc:if test="dc:contains($parameters, 'user-id') and not(dc:contains($artifact-outs, 'floodmap-hws'))">
      <dc:call-macro name="load-user"/>
    </dc:if>

    <dc:call-macro name="load-system"/>

    <dc:macro name="load-system">
      <dc:context connection="system">
        <dc:statement>
          SELECT id AS river_id, name AS river_name
          FROM rivers
          WHERE lower(name) LIKE lower(${river})
        </dc:statement>
        <dc:for-each>
          <dc:call-macro name="generate-system-content"/>
        </dc:for-each>
      </dc:context>
    </dc:macro>

    <dc:macro name="load-user">
      <old_calculations>
        <dc:call-macro name="generate-user-content"/>
      </old_calculations>
    </dc:macro>

    <dc:macro name="generate-system-content">
      <dc:call-macro name="user-range">
        <dc:call-macro name="km-filtered-wsts">
          <dc:choose>
            <dc:when test="dc:contains($parameters, 'recommended')">
              <dc:comment>
                Recommendations (client shall load immediately).
              </dc:comment>
              <dc:iterate var="out" container="artifact-outs">
                <dc:choose>
                  <dc:when test="$out = 'w_differences'">
                    <dc:call-macro name="annotations"/>
                  </dc:when>
                  <dc:when test="$out = 'discharge_longitudinal_section'">
                    <dc:call-macro name="annotations"/>
                  </dc:when>
                  <dc:when test="$out = 'historical_discharge_wq'">
                    <dc:call-macro name="mainvalues"/>
                  </dc:when>
                  <dc:when test="$out = 'cross_section'">
                    <dc:call-macro name="cross_sections"/>
                    <dc:call-macro name="hyks"/>
                  </dc:when>
                  <dc:when test="$out = 'discharge_curve'">
                    <dc:call-macro name="mainvalues"/>
                  </dc:when>
                  <dc:when test="$out = 'computed_discharge_curve'">
                    <dc:call-macro name="mainvalues"/>
                    <dc:call-macro name="basedata_2_fixations_wqkms"/>
                  </dc:when>
                  <dc:when test="$out = 'duration_curve'">
                    <dc:call-macro name="mainvalues"/>
                  </dc:when>
                  <dc:when test="$out = 'reference_curve'">
                    <dc:call-macro name="annotations"/>
                    <dc:call-macro name="mainvalues"/>
                  </dc:when>
                  <dc:when test="$out = 'fix_wq_curve'">
                    <dc:call-macro name="mainvalues"/>
                    <dc:call-macro name="qsectors"/>
                  </dc:when>
                  <dc:when test="$out = 'longitudinal_section'">
                    <dc:call-macro name="annotations"/>
                  </dc:when>
                  <dc:when test="$out = 'fix_longitudinal_section_curve'">
                    <dc:call-macro name="annotations"/>
                  </dc:when>
                  <dc:when test="$out = 'bed_difference_epoch'">
                    <dc:call-macro name="annotations"/>
                  </dc:when>
                  <dc:when test="$out = 'bed_difference_year'">
                    <dc:call-macro name="annotations"/>
                  </dc:when>
                  <dc:when test="$out = 'bed_difference_height_year'">
                    <dc:call-macro name="annotations"/>
                  </dc:when>
                  <dc:when test="$out = 'floodmap'">
                    <dc:call-macro name="flood-map-recommended"/>
                  </dc:when>
                  <dc:when test="$out = 'floodmap-hws'">
                    <dc:call-macro name="flood-map-recommended"/>
                  </dc:when>
                  <dc:when test="$out = 'minfo-heights'">
                    <dc:call-macro name="minfo-heights"/>
                  </dc:when>
                  <dc:when test="$out = 'minfo-heights-epoch'">
                    <bedheights>
                      <dc:call-macro name="bed-heights-epoch"/>
                    </bedheights>
                  </dc:when>
                </dc:choose>
              </dc:iterate>
            </dc:when>
            <dc:otherwise>
              <dc:comment>
                Non - Recommendations.
              </dc:comment>
              <dc:iterate var="out" container="artifact-outs">
                <dc:choose>
                  <dc:when test="$out = 'cross_section'">
                    <dc:call-macro name="basedata_0"/>
                    <dc:call-macro name="basedata_1_additionals"/>
                    <dc:call-macro name="basedata_2_fixations"/>
                    <dc:call-macro name="basedata_3_officials"/>
                    <dc:call-macro name="basedata_4_heightmarks-points"/>
                    <dc:call-macro name="cross_sections"/>
                    <dc:call-macro name="hyks"/>
                  </dc:when>
                  <dc:when test="$out = 'longitudinal_section'">
                    <dc:call-macro name="longitudinal-section-prototype"/>
                  </dc:when>
                  <dc:when test="$out = 'w_differences'">
                    <dc:call-macro name="longitudinal-section-prototype"/>
                  </dc:when>
                  <dc:when test="$out = 'discharge_longitudinal_section'">
                    <dc:call-macro name="longitudinal-section-prototype"/>
                  </dc:when>
                  <dc:when test="$out = 'discharge_curve'">
                    <dc:call-macro name="mainvalues"/>
                  </dc:when>
                  <dc:when test="$out = 'duration_curve'">
                    <dc:call-macro name="mainvalues"/>
                    <dc:call-macro name="basedata_2_fixations_relative_point"/>
                    <dc:call-macro name="basedata_4_heightmarks-points-relative_points"/>
                    <dc:call-macro name="basedata_5_flood-protections_relative_points"/>
                  </dc:when>
                  <dc:when test="$out = 'reference_curve'">
                    <dc:call-macro name="mainvalues"/>
                    <dc:call-macro name="annotations"/>
                    <dc:call-macro name="basedata_1_additionals-relative_point"/>
                    <dc:call-macro name="basedata_2_fixations_relative_point"/>
                    <dc:call-macro name="basedata_4_heightmarks-points-relative_points"/>
                    <dc:call-macro name="basedata_5_flood-protections_relative_points"/>
                  </dc:when>
                  <dc:when test="$out = 'fix_wq_curve'">
                    <dc:call-macro name="basedata_0_wq"/>
                    <dc:call-macro name="basedata_1_additionals_marks"/>
                    <dc:call-macro name="basedata_2_fixations_wqkms"/>
                    <dc:call-macro name="basedata_3_officials"/>
                    <dc:call-macro name="basedata_4_heightmarks-points"/>
                    <dc:call-macro name="basedata_5_flood-protections_relative_points"/>
                    <dc:call-macro name="discharge_table_gauge"/>
                    <dc:call-macro name="discharge_fix_wq"/>
                  </dc:when>
                  <dc:when test="$out = 'fix_longitudinal_section_curve'">
                    <dc:call-macro name="annotations"/>
                  </dc:when>
                  <dc:when test="$out = 'map'">
                    <dc:call-macro name="flood-map-complete"/>
                  </dc:when>
                  <dc:when test="$out = 'flow_velocity'">
                    <dc:call-macro name="annotations_per_type"/>
                    <dc:call-macro name="flow_velocity_measurements"/>
                  </dc:when>
                  <dc:when test="$out = 'bed_longitudinal_section'">
                    <dc:call-macro name="annotations_per_type"/>
                  </dc:when>
                  <dc:when test="$out = 'sedimentload_ls'">
                    <dc:call-macro name="annotations_per_type"/>
                    <dc:call-macro name="morph_width"/>
                  </dc:when>
                  <dc:when test="$out = 'bedheight_middle'">
                    <dc:call-macro name="sounding-width"/>
                    <dc:call-macro name="basedata_0"/>
                    <dc:call-macro name="basedata_1_additionals"/>
                    <dc:call-macro name="basedata_2_fixations"/>
                    <dc:call-macro name="basedata_3_officials"/>
                    <dc:call-macro name="annotations_per_type"/>
                  </dc:when>
                  <dc:when test="$out = 'bed_difference_year'">
                    <dc:call-macro name="basedata_0"/>
                    <dc:call-macro name="basedata_1_additionals"/>
                    <dc:call-macro name="basedata_2_fixations"/>
                    <dc:call-macro name="basedata_3_officials"/>
                    <dc:call-macro name="annotations_per_type"/>
                    <dc:call-macro name="morph_width"/>
                  </dc:when>
                  <dc:when test="$out = 'bed_difference_epoch'">
                    <dc:call-macro name="basedata_0"/>
                    <dc:call-macro name="basedata_1_additionals"/>
                    <dc:call-macro name="basedata_2_fixations"/>
                    <dc:call-macro name="basedata_3_officials"/>
                    <dc:call-macro name="annotations_per_type"/>
                    <dc:call-macro name="morph_width"/>
                  </dc:when>
                  <dc:when test="$out = 'floodmap'">
                    <dc:choose>
                      <dc:when test="dc:contains($parameters, 'dem')">
                        <dc:call-macro name="flood-map-dem"/>
                      </dc:when>
                      <dc:when test="dc:contains($parameters, 'hws')">
                        <hws>
                          <dc:call-macro name="flood-map-hws-lines"/>
                          <dc:call-macro name="flood-map-hws-points"/>
                        </hws>
                      </dc:when>
                      <dc:otherwise>
                        <dc:call-macro name="flood-map-complete"/>
                      </dc:otherwise>
                    </dc:choose>
                  </dc:when>
                  <dc:when test="$out = 'floodmap-hws'">
                    <dc:choose>
                      <dc:when test="dc:contains($parameters, 'dem')">
                        <dc:call-macro name="flood-map-dem"/>
                      </dc:when>
                      <dc:when test="dc:contains($parameters, 'hws')">
                        <hws>
                          <dc:call-macro name="flood-map-hws-lines"/>
                          <dc:call-macro name="flood-map-hws-points"/>
                        </hws>
                      </dc:when>
                      <dc:otherwise>
                        <dc:call-macro name="flood-map-complete"/>
                      </dc:otherwise>
                    </dc:choose>
                  </dc:when>
                  <dc:when test="$out = 'computed_discharge_curve'">
                    <dc:call-macro name="discharge_computed"/>
                    <dc:call-macro name="basedata_2_fixations_wqkms"/>
                    <dc:call-macro name="basedata_5_flood-protections"/>
                    <dc:call-macro name="basedata_0"/>
                    <dc:call-macro name="basedata_1_additionals"/>
                    <dc:call-macro name="basedata_4_heightmarks-points"/>
                    <computed_discharge_curve>
                      <dc:call-macro name="mainvalues"/>
                    </computed_discharge_curve>
                  </dc:when>
                  <dc:when test="$out = 'minfo-heights'">
                    <dc:call-macro name="minfo-heights"/>
                    <dc:macro name="minfo-heights">
                      <bedheights>
                        <dc:call-macro name="bed-heights-single"/>
                        <dc:call-macro name="bed-heights-epoch"/>
                      </bedheights>
                    </dc:macro>
                  </dc:when>
                  <dc:when test="$out = 'minfo-heights-epoch'">
                    <bedheights>
                      <dc:call-macro name="bed-heights-epoch"/>
                    </bedheights>
                  </dc:when>
                </dc:choose>
              </dc:iterate>
            </dc:otherwise>
          </dc:choose>
        </dc:call-macro>
      </dc:call-macro>
    </dc:macro>

    <dc:macro name="generate-user-content">
      <dc:call-macro name="km-filtered-userdata">
        <dc:iterate var="out" container="artifact-outs">
          <dc:choose>
            <dc:when test="$out = 'longitudinal_section'">
              <dc:call-macro name="longitudinal"/>
              <dc:call-macro name="differences"/>
              <dc:call-macro name="officiallines_user"/>
            </dc:when>
            <dc:when test="$out = 'discharge_longitudinal_section'">
              <dc:call-macro name="longitudinal"/>
              <dc:call-macro name="differences"/>
            </dc:when>
            <dc:when test="$out = 'w_differences'">
              <dc:call-macro name="longitudinal"/>
              <dc:call-macro name="differences"/>
            </dc:when>
            <dc:when test="$out = 'fix_deltawt_curve'">
              <dc:call-macro name="delta-wt"/>
            </dc:when>
            <dc:when test="$out = 'reference_curve'">
              <dc:call-macro name="reference-curves"/>
            </dc:when>
            <dc:when test="$out = 'computed_discharge_curve'">
              <dc:call-macro name="computed-discharge-curve"/>
            </dc:when>
            <dc:when test="$out = 'cross_section'">
              <dc:call-macro name="waterlevels"/>
            </dc:when>
            <dc:when test="$out = 'fix_longitudinal_section_curve'">
              <dc:call-macro name="longitudinal-section"/>
            </dc:when>
            <dc:when test="$out = 'fix_derivate_curve'">
              <dc:call-macro name="fix-derivate-curve"/>
            </dc:when>
            <dc:when test="$out = 'fix_wq_curve'">
              <dc:call-macro name="fix-wq-curve"/>
            </dc:when>
            <dc:when test="$out = 'duration_curve'">
              <dc:call-macro name="duration-curve"/>
            </dc:when>
            <dc:when test="$out = 'waterlevels'">
              <dc:call-macro name="basedata_0"/>
              <dc:call-macro name="basedata_1_additionals"/>
              <dc:call-macro name="basedata_2_fixations"/>
              <dc:call-macro name="basedata_4_heightmarks-points"/>
              <dc:call-macro name="basedata_5_flood-protections"/>
              <dc:call-macro name="waterlevels-fix"/>
            </dc:when>
            <dc:when test="$out = 'fix_wq_curve'">
              <dc:call-macro name="waterlevels-fix"/>
            </dc:when>
            <dc:when test="$out = 'floodmap'">
              <dc:call-macro name="flood-map"/>
            </dc:when>
            <dc:when test="$out = 'map'">
              <dc:call-macro name="flood-map"/>
            </dc:when>
            <dc:when test="$out = 'bedheight_middle'">
              <dc:call-macro name="waterlevels-discharge"/>
              <dc:call-macro name="waterlevels-fix"/>
            </dc:when>
            <dc:when test="$out = 'floodmap-hws'">
              <dc:call-macro name="floodmap-hws-user"/>
            </dc:when>
            <dc:when test="$out = 'flow_velocity'">
              <dc:call-macro name="bedquality-bed"/>
              <dc:call-macro name="bedquality-load"/>
            </dc:when>
            <dc:when test="$out = 'bed_longitudinal_section'">
              <dc:call-macro name="bedquality-bed"/>
              <dc:call-macro name="bedquality-load"/>
              <dc:call-macro name="bedquality-density"/>
              <dc:call-macro name="bedquality-porosity"/>
            </dc:when>
            <dc:when test="$out = 'sedimentload_ls'">
              <dc:call-macro name="differences"/>
              <dc:call-macro name="bedheight-differences"/>
              <dc:call-macro name="flow-velocity"/>
              <dc:call-macro name="sediment-load"/>
            </dc:when>
            <dc:when test="$out = 'bed_difference_year'">
              <dc:call-macro name="waterlevels-discharge"/>
              <dc:call-macro name="bedheight-differences"/>
              <dc:call-macro name="differences"/>
              <dc:call-macro name="waterlevels-fix"/>
              <dc:call-macro name="delta-wt-ls"/>
            </dc:when>
            <dc:when test="$out = 'bed_difference_epoch'">
              <dc:call-macro name="waterlevels-discharge"/>
              <dc:call-macro name="bedheight-differences"/>
              <dc:call-macro name="differences"/>
              <dc:call-macro name="waterlevels-fix"/>
              <dc:call-macro name="delta-wt-ls"/>
            </dc:when>
          </dc:choose>
        </dc:iterate>
      </dc:call-macro>
    </dc:macro>

    <!-- Macros to load user data -->

    <dc:macro name="select-facets">
      <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
          FROM artifacts a
            JOIN outs o   ON o.artifact_id = a.id
            JOIN facets f ON f.out_id = o.id
          WHERE a.id = ${a_id}
            AND f.name = ${facet_type}
        </dc:statement>
        <dc:if test="dc:has-result()">
          <dc:macro-body/>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="differences">
      <dc:variable name="facet_type" value="w_differences"/>
      <dc:call-macro name="select-facets">
        <differences>
          <dc:for-each>
            <w_differences
              description="{$facet_description}"
              factory="winfo"
              artifact-id="{$aid}"
              ids="{$aid}"
              out="w_differences"/>
          </dc:for-each>
        </differences>
      </dc:call-macro>
    </dc:macro>

    <dc:macro name="reference-curves">
      <dc:variable name="facet_type" value="reference_curve"/>
      <dc:call-macro name="select-facets">
        <reference_curves>
          <dc:for-each>
            <dc:element name="${facet_name}">
              <dc:attribute name="description" value="${facet_description}"/>
              <dc:attribute name="factory" value="winfo"/>
              <dc:attribute name="artifact-id" value="${aid}"/>
              <dc:attribute name="ids" value="${aid}"/>
              <dc:attribute name="out" value="reference_curve"/>
            </dc:element>
          </dc:for-each>
        </reference_curves>
      </dc:call-macro>
    </dc:macro>

    <dc:macro name="computed-discharge-curve">
      <dc:variable name="facet_type" value="computed_discharge_curve.q"/>
      <dc:call-macro name="select-facets">
        <computed_discharge_curves>
          <dc:for-each>
            <dc:element name="${facet_name}">
              <dc:attribute name="description" value="${facet_description}"/>
              <dc:attribute name="factory" value="winfo"/>
              <dc:attribute name="artifact-id" value="${aid}"/>
              <dc:attribute name="ids" value="${aid}"/>
              <dc:attribute name="out" value="computed_discharge_curve"/>
            </dc:element>
          </dc:for-each>
        </computed_discharge_curves>
      </dc:call-macro>
    </dc:macro>

    <dc:macro name="flood-map">
      <dc:variable name="facet_type" value="floodmap.wsplgen"/>
      <dc:call-macro name="select-facets">
        <floodmap>
          <dc:for-each>
            <dc:element name="${facet_name}">
              <dc:attribute name="description" value="${facet_description}"/>
              <dc:attribute name="factory" value="winfo"/>
              <dc:attribute name="artifact-id" value="${aid}"/>
              <dc:attribute name="ids" value="${aid}"/>
              <dc:attribute name="out" value="floodmap"/>
            </dc:element>
          </dc:for-each>
        </floodmap>
      </dc:call-macro>
    </dc:macro>

    <dc:macro name="waterlevels">
      <dc:context>
        <dc:statement>
          SELECT id AS out_id
          FROM outs
          WHERE artifact_id = ${a_id} AND name = 'cross_section'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:for-each>
              <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 description="{$river} {$a_creation}">
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}"/>
                      <dc:attribute name="factory" value="winfo"/>
                      <dc:attribute name="artifact-id" value="${a_gid}"/>
                      <dc:attribute name="out" value="cross_section"/>
                    </dc:element>
                  </dc:for-each>
                </longitudinal_section_columns>
              </dc:context>
            </dc:for-each>
          </waterlevels>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="longitudinal">
      <dc:context>
        <dc:statement>
          SELECT id AS out_id
          FROM outs
          WHERE artifact_id = ${a_id}
            AND name = 'longitudinal_section'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:for-each>
              <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 description="{$river} {$a_creation}">
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}"/>
                      <dc:attribute name="factory" value="winfo"/>
                      <dc:attribute name="artifact-id" value="${a_gid}"/>
                      <dc:attribute name="out" value="longitudinal_section"/>
                    </dc:element>
                  </dc:for-each>
                </longitudinal_section_columns>
              </dc:context>
            </dc:for-each>
          </waterlevels>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="longitudinal-section">
      <dc:context>
        <dc:statement>
          SELECT id AS out_id
          FROM outs
          WHERE artifact_id = ${a_id}
            AND name = 'fix_longitudinal_section_curve'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:for-each>
              <dc:context>
                <dc:statement>
                  SELECT name        AS facet_name,
                         num         AS facet_num,
                         description AS facet_description
                  FROM facets
                  WHERE out_id = ${out_id} AND (
                      name LIKE 'fix_deviation_ls%'       OR
                      name LIKE 'fix_sector_average_ls%'  OR
                      name LIKE 'fix_analysis_events_ls%' OR
                      name LIKE 'fix_reference_events_ls%')
                  ORDER BY num ASC, name DESC
                </dc:statement>
                <waterlevels description="{$river} {$a_creation} {$collection_name}">
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}"/>
                      <dc:attribute name="factory" value="fixanalysis"/>
                      <dc:attribute name="artifact-id" value="${a_gid}"/>
                      <dc:attribute name="out" value="fix_longitudinal_section_curve"/>
                    </dc:element>
                  </dc:for-each>
                </waterlevels>
              </dc:context>
            </dc:for-each>
          </waterlevels>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="delta-wt">
      <dc:context>
        <dc:statement>
          SELECT id AS out_id
          FROM outs
          WHERE artifact_id = ${a_id}
            AND name = 'fix_deltawt_curve'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:for-each>
              <dc:context>
                <dc:statement>
                  SELECT name        AS facet_name,
                         num         AS facet_num,
                         description AS facet_description
                  FROM facets
                  WHERE out_id = ${out_id} and (
                    name LIKE 'fix_sector_average_dwt%' OR
                    name LIKE 'fix_deviation_dwt%'      OR
                    name = 'fix_analysis_events_dwt'    OR
                    name = 'fix_reference_events_dwt'   OR
                    name = 'fix_analysis_periods_dwt')
                  ORDER BY num ASC, name DESC
                </dc:statement>
                <waterlevels description="{$river} {$a_creation} {$collection_name}">
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}"/>
                      <dc:attribute name="factory" value="fixanalysis"/>
                      <dc:attribute name="artifact-id" value="${a_gid}"/>
                      <dc:attribute name="out" value="fix_deltawt_curve"/>
                    </dc:element>
                  </dc:for-each>
                </waterlevels>
              </dc:context>
            </dc:for-each>
          </waterlevels>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="delta-wt-ls">
      <dc:context>
        <dc:statement>
          SELECT id AS out_id
          FROM outs
          WHERE artifact_id = ${a_id} AND name = 'fix_deltawt_curve'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:for-each>
              <dc:context>
                <dc:statement>
                  SELECT name AS facet_name, num AS facet_num, description AS facet_description
                  FROM facets
                  WHERE out_id = ${out_id} and (
                    name LIKE 'fix_sector_average_dwt%' OR
                    name LIKE 'fix_deviation_dwt%')
                  ORDER BY num ASC, name DESC
                </dc:statement>
                <waterlevels description="{$river} {$a_creation} {$collection_name}">
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}"/>
                      <dc:attribute name="factory" value="fixanalysis"/>
                      <dc:attribute name="artifact-id" value="${a_gid}"/>
                      <dc:attribute name="out" value="fix_deltawt_curve"/>
                    </dc:element>
                  </dc:for-each>
                </waterlevels>
              </dc:context>
            </dc:for-each>
          </waterlevels>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="fix-derivate-curve">
      <dc:context>
        <dc:statement>
          SELECT id AS out_id
          FROM outs
          WHERE artifact_id = ${a_id} AND name = 'fix_derivate_curve'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:for-each>
              <dc:context>
                <dc:statement>
                  SELECT name AS facet_name, num AS facet_num, description AS facet_description
                  FROM facets
                  WHERE out_id = ${out_id} and name = 'fix_derivate_curve'
                  ORDER BY num ASC, name DESC
                </dc:statement>
                <waterlevels description="{$river} {$a_creation} {$collection_name}">
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}"/>
                      <dc:attribute name="factory" value="fixanalysis"/>
                      <dc:attribute name="artifact-id" value="${a_gid}"/>
                      <dc:attribute name="out" value="fix_derivate_curve"/>
                    </dc:element>
                  </dc:for-each>
                </waterlevels>
              </dc:context>
            </dc:for-each>
          </waterlevels>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="fix-wq-curve">
      <dc:context>
        <dc:statement>
          SELECT id AS out_id
          FROM outs
          WHERE artifact_id = ${a_id} AND name = 'fix_wq_curve'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:for-each>
              <dc:context>
                <dc:statement>
                  SELECT name AS facet_name, num AS facet_num, description AS facet_description
                  FROM facets
                  WHERE out_id = ${out_id} and (
                    name LIKE 'fix_sector_average_wq%'  OR
                    name = 'fix_wq_curve'               OR
                    name LIKE 'fix_analysis_events_wq%' OR
                    name LIKE 'fix_reference_events_wq%' )
                  ORDER BY num ASC, name DESC
                </dc:statement>
                <waterlevels description="{$river} {$a_creation} {$collection_name}">
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}"/>
                      <dc:attribute name="factory" value="fixanalysis"/>
                      <dc:attribute name="artifact-id" value="${a_gid}"/>
                      <dc:attribute name="out" value="fix_wq_curve"/>
                    </dc:element>
                  </dc:for-each>
                </waterlevels>
              </dc:context>
            </dc:for-each>
          </waterlevels>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="duration-curve">
      <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
          FROM outs AS o, facets AS f, artifacts AS a
          WHERE
              (f.name = 'duration_curve.q' OR f.name = 'duration_curve.w') AND
              f.out_id = o.id         AND
              o.artifact_id = ${a_id} AND
              a.id = ${a_id}
        </dc:statement>
        <dc:if test="dc:has-result()">
          <computed_discharge_curves>
            <dc:for-each>
              <dc:element name="${facet_name}">
                <dc:attribute name="description" value="${facet_description}"/>
                <dc:attribute name="factory" value="winfo"/>
                <dc:attribute name="artifact-id" value="${aid}"/>
                <dc:attribute name="ids" value="${aid}"/>
                <dc:attribute name="out" value="duration_curve"/>
              </dc:element>
            </dc:for-each>
          </computed_discharge_curves>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:comment>TODO doesnt work nicely for fix/wq-diags.</dc:comment>
    <dc:macro name="waterlevels-fix">
      <dc:context>
        <dc:statement>
          SELECT id AS out_id
          FROM outs
          WHERE artifact_id = ${a_id} AND name = 'longitudinal_section'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:for-each>
              <dc:context>
                <dc:statement>
                  SELECT name AS facet_name, num AS facet_num, description AS facet_description
                  FROM facets
                  WHERE out_id = ${out_id} and name = 'longitudinal_section.w'
                  ORDER BY num ASC, name DESC
                </dc:statement>
                <waterlevels description="{$river} {$a_creation} {$collection_name}">
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}"/>
                      <dc:attribute name="factory" value="winfo"/>
                      <dc:attribute name="artifact-id" value="${a_gid}"/>
                      <dc:attribute name="out" value="longitudinal_section"/>
                    </dc:element>
                  </dc:for-each>
                </waterlevels>
              </dc:context>
            </dc:for-each>
          </waterlevels>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="floodmap-hws-user">
      <dc:context>
        <dc:statement>
          SELECT id AS out_id
          FROM outs
          WHERE artifact_id = ${a_id} AND name = 'floodmap'
        </dc:statement>
        <dc:for-each>
          <dc:context>
            <dc:statement>
              SELECT name        AS facet_name,
                     num         AS facet_num,
                     description AS facet_description
              FROM facets
              WHERE out_id = ${out_id} AND name = 'floodmap.usershape'
              ORDER BY num ASC, name DESC
            </dc:statement>
            <own-hws>
              <dc:for-each>
                <dc:element name="${facet_name}">
                  <dc:attribute name="description" value="${facet_description}"/>
                  <dc:attribute name="ids" value="${facet_num}"/>
                  <dc:attribute name="factory" value="winfo"/>
                  <dc:attribute name="artifact-id" value="${a_gid}"/>
                  <dc:attribute name="out" value="floodmap"/>
                </dc:element>
              </dc:for-each>
            </own-hws>
          </dc:context>
        </dc:for-each>
      </dc:context>
    </dc:macro>

    <dc:macro name="bedquality-bed">
      <dc:context>
        <dc:statement>
          SELECT id AS oid
          FROM outs AS o
          WHERE
            o.artifact_id = ${a_id} AND
            o.name='bed_longitudinal_section'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <bed_quality_bed>
            <dc:for-each>
              <quality-bed description="{$river} {$a_creation} {$collection_name}">
                <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
                    FROM outs AS o, facets AS f, artifacts AS a
                    WHERE
                      (f.name = 'bed_longitudinal_section.bed_diameter_toplayer'
                       OR f.name = 'bed_longitudinal_section.bed_diameter_sublayer') AND
                       f.out_id = o.id         AND
                       o.artifact_id = ${a_id} AND
                       a.id = ${a_id}
                  </dc:statement>
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="factory" value="minfo"/>
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                      <dc:attribute name="artifact-id" value="${aid}"/>
                      <dc:attribute name="out" value="bed_longitudinal_section"/>
                    </dc:element>
                  </dc:for-each>
                </dc:context>
              </quality-bed>
            </dc:for-each>
          </bed_quality_bed>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="bedquality-load">
      <dc:context>
        <dc:statement>
          SELECT id AS oid
          FROM outs AS o
          WHERE
            o.artifact_id = ${a_id} AND
            o.name='bed_longitudinal_section'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <bed_quality_load>
            <dc:for-each>
              <quality-load description="{$river} {$a_creation} {$collection_name}">
                <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
                    FROM outs AS o, facets AS f, artifacts AS a
                    WHERE
                       f.name = 'bed_longitudinal_section.bedload_diameter' AND
                       f.out_id = o.id         AND
                       o.artifact_id = ${a_id} AND
                       a.id = ${a_id}
                  </dc:statement>
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="factory" value="minfo"/>
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                      <dc:attribute name="artifact-id" value="${aid}"/>
                      <dc:attribute name="out" value="bed_longitudinal_section"/>
                    </dc:element>
                  </dc:for-each>
                </dc:context>
              </quality-load>
            </dc:for-each>
          </bed_quality_load>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="bedquality-density">
      <dc:context>
        <dc:statement>
          SELECT id AS oid
          FROM outs AS o
          WHERE
            o.artifact_id = ${a_id} AND
            o.name='bed_longitudinal_section'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <bed_quality_density>
            <dc:for-each>
              <density description="{$river} {$a_creation} {$collection_name}">
                <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
                    FROM outs AS o, facets AS f, artifacts AS a
                    WHERE
                       (f.name = 'bed_longitudinal_section.sediment_density_toplayer' OR
                        f.name = 'bed_longitudinal_section.sediment_density_sublayer') AND
                       f.out_id = o.id         AND
                       o.artifact_id = ${a_id} AND
                       a.id = ${a_id}
                  </dc:statement>
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="factory" value="minfo"/>
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                      <dc:attribute name="artifact-id" value="${aid}"/>
                      <dc:attribute name="out" value="bed_longitudinal_section"/>
                    </dc:element>
                  </dc:for-each>
                </dc:context>
              </density>
            </dc:for-each>
          </bed_quality_density>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="bedquality-porosity">
      <dc:context>
        <dc:statement>
          SELECT id AS oid
          FROM outs AS o
          WHERE
            o.artifact_id = ${a_id} AND
            o.name='bed_longitudinal_section'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <bed_quality_porosity>
            <dc:for-each>
              <porosity description="{$river} {$a_creation} {$collection_name}">
                <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
                    FROM outs AS o, facets AS f, artifacts AS a
                    WHERE
                       (f.name = 'bed_longitudinal_section.porosity_toplayer' OR
                        f.name = 'bed_longitudinal_section.porosity_sublayer') AND
                       f.out_id = o.id         AND
                       o.artifact_id = ${a_id} AND
                       a.id = ${a_id}
                  </dc:statement>
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="factory" value="minfo"/>
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                      <dc:attribute name="artifact-id" value="${aid}"/>
                      <dc:attribute name="out" value="bed_longitudinal_section"/>
                    </dc:element>
                  </dc:for-each>
                </dc:context>
              </porosity>
            </dc:for-each>
          </bed_quality_porosity>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="flow-velocity">
      <dc:context>
        <dc:statement>
          SELECT id AS oid
          FROM outs AS o
          WHERE
            o.artifact_id = ${a_id} and o.name='flow_velocity'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <flow-velocity>
            <dc:for-each>
              <flow description="{$river} {$a_creation} {$collection_name}">
                <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
                    FROM outs AS o, facets AS f, artifacts AS a
                    WHERE
                      (f.name = 'flow_velocity.totalchannel' OR
                       f.name = 'flow_velocity.mainchannel' OR
                       f.name = 'flow_velocity.totalchannel.filtered' OR
                       f.name = 'flow_velocity.mainchannel.filtered') AND
                       f.out_id = o.id         AND
                       o.artifact_id = ${a_id} AND
                       a.id = ${a_id}
                  </dc:statement>
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="factory" value="minfo"/>
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                      <dc:attribute name="artifact-id" value="${aid}"/>
                      <dc:attribute name="out" value="flow_velocity"/>
                    </dc:element>
                  </dc:for-each>
                </dc:context>
              </flow>
            </dc:for-each>
          </flow-velocity>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="sediment-load">
      <dc:context>
        <dc:statement>
          SELECT id AS oid
          FROM outs AS o
          WHERE
            o.artifact_id = ${a_id} and o.name='sedimentload_ls'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <sediment-load>
            <dc:for-each>
              <load description="{$river} {$a_creation} {$collection_name}">
                <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
                    FROM outs AS o, facets AS f, artifacts AS a
                    WHERE f.name LIKE 'sedimentload%' AND
                          f.out_id = o.id         AND
                          o.artifact_id = ${a_id} AND
                          a.id = ${a_id}
                  </dc:statement>
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="factory" value="minfo"/>
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                      <dc:attribute name="artifact-id" value="${aid}"/>
                      <dc:attribute name="out" value="sedimentload_ls"/>
                    </dc:element>
                  </dc:for-each>
                </dc:context>
              </load>
            </dc:for-each>
          </sediment-load>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="bedheight-differences">
      <dc:context>
        <dc:statement>
          SELECT id AS oid,
                 name AS o_name
          FROM outs AS o
          WHERE
            o.artifact_id = ${a_id} AND
            (o.name = 'bed_difference_year' OR
             o.name = 'bed_differnece_epoch')
        </dc:statement>
        <dc:if test="dc:has-result()">
          <bedheight-differences>
            <dc:for-each>
              <difference description="{$river} {$a_creation} {$collection_name}">
                <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
                    FROM outs AS o, facets AS f, artifacts AS a
                    WHERE (f.name LIKE 'bedheight_difference.year%' OR
                           f.name LIKE 'bedheight_difference.epoch%') AND
                          f.out_id = o.id         AND
                          o.artifact_id = ${a_id} AND
                          a.id = ${a_id}
                  </dc:statement>
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="factory" value="minfo"/>
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                      <dc:attribute name="artifact-id" value="${aid}"/>
                      <dc:choose>
                        <dc:when test="dc:contains($o_name, 'bed_difference_year')">
                          <dc:attribute name="out" value="bed_difference_year"/>
                        </dc:when>
                        <dc:otherwise>
                          <dc:attribute name="out" value="bed_difference_epoch"/>
                        </dc:otherwise>
                      </dc:choose>
                    </dc:element>
                  </dc:for-each>
                </dc:context>
              </difference>
            </dc:for-each>
          </bedheight-differences>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="waterlevels-discharge">
      <dc:context>
        <dc:statement>
          SELECT id AS oid
          FROM outs AS o
          WHERE
            o.artifact_id = ${a_id} AND
            o.name = 'discharge_longitudinal_section'
        </dc:statement>
        <dc:if test="dc:has-result()">
          <waterlevels-discharge>
            <dc:for-each>
              <discharge description="{$oid} {$river} {$a_id} {$a_creation} {$collection_name}">
                <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
                    FROM outs AS o, facets AS f, artifacts AS a
                    WHERE f.name = 'discharge_longitudinal_section.w' AND
                          f.out_id = o.id         AND
                          o.artifact_id = ${a_id} AND
                          a.id = ${a_id}
                  </dc:statement>
                  <dc:for-each>
                    <dc:element name="${facet_name}">
                      <dc:attribute name="factory" value="winfo"/>
                      <dc:attribute name="description" value="${facet_description}"/>
                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                      <dc:attribute name="artifact-id" value="${aid}"/>
                      <dc:attribute name="out" value="longitudinal_section"/>
                    </dc:element>
                  </dc:for-each>
                </dc:context>
              </discharge>
            </dc:for-each>
          </waterlevels-discharge>
        </dc:if>
      </dc:context>
    </dc:macro>
    <!-- Macros to load system data -->

    <dc:macro name="annotations">
      <annotation factory="annotations" ids="{$river_id}"/>
    </dc:macro>

    <dc:macro name="mainvalues">
      <wmainvalue factory="mainvalue" ids="{$river_id}:w"/>
      <qmainvalue factory="mainvalue" ids="{$river_id}:q"/>
    </dc:macro>

    <dc:macro name="cross_sections">
      <cross_sections id="flood-protections-{$river_id}">
        <dc:context connection="system">
          <dc:statement>
            SELECT DISTINCT
                   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:for-each>
            <cross_section name="{$prot_description}"
                           ids="{$prot_id}"
                           factory="crosssections"/>
          </dc:for-each>
        </dc:context>
      </cross_sections>
    </dc:macro>

    <dc:macro name="hyks">
      <hyks id="hyk-{$river_id}">
        <dc:context connection="system">
          <dc:statement>
            SELECT DISTINCT
                   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:for-each>
            <hyk name="{$hyk_description}" 
                 ids="{$hyk_id}"
                 factory="hyk"/>
          </dc:for-each>
        </dc:context>
      </hyks>
    </dc:macro>

    <dc:macro name="fixings-macro">
      <dc:filter expr="$kind=2">
        <dc:if test="dc:has-result()">
          <fixations>
            <dc:group expr="dc:replace($wst_description, 'Fixierungen/', '')">
              <fixation name="{dc:group-key()}">
                <dc:for-each>
                  <dc:macro-body/>
                </dc:for-each>
              </fixation>
            </dc:group>
          </fixations>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="basedata_2_fixations_wqkms">
      <dc:call-macro name="fixings-macro">
        <column name="{$wst_column_name}"
                ids="fixations-wstv-{$wst_column_position}-{$wst_id}"
                factory="wqinterpol"
                info="{$info} [km {$deffrom} - {$defto}]"/>
      </dc:call-macro>
    </dc:macro>

    <dc:macro name="basedata_2_fixations">
      <dc:call-macro name="fixings-macro">
        <column name="{$wst_column_name}"
                ids="fixations-wstv-{$wst_column_position}-{$wst_id}"
                factory="staticwqkms"
                info="{$info} [km {$deffrom} - {$defto}]"/>
      </dc:call-macro>
    </dc:macro>

    <dc:macro name="basedata_3_officials">
      <dc:filter expr="$kind=3">
        <dc:if test="dc:has-result()">
          <officiallines>
            <dc:group expr="$wst_description">
              <official name="{dc:group-key()}">
                <dc:for-each>
                  <column name="{$wst_column_name}"
                          ids="additionals-wstv-{$wst_column_position}-{$wst_id}"
                          factory="staticwqkms"
                          info="{$info} [km {$deffrom} - {$defto}]"/>
                </dc:for-each>
              </official>
            </dc:group>
          </officiallines>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="basedata_4_heightmarks-points">
      <dc:filter expr="$kind=4">
        <dc:if test="dc:has-result()">
          <heightmarks>
            <dc:group expr="{dc:replace($wst_description, 'HW-Marken/', '')}">
              <heightmark name="dc:group-key()">
                <dc:for-each>
                  <column name="{$wst_column_name}"
                          ids="heightmarks_points-wstv-{$wst_column_position}-{$wst_id}"
                          factory="staticwkms"
                          info="{$info} [km {$deffrom} - {$defto}]"/>
                </dc:for-each>
              </heightmark>
            </dc:group>
          </heightmarks>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="basedata_4_heightmarks-points-relative_points">
      <dc:filter expr="$kind=4">
        <dc:if test="dc:has-result()">
          <heightmarks>
            <dc:group expr="{dc:replace($wst_description, 'HW-Marken/', '')}">
              <relativepoint name="dc:group-key()">
                <dc:for-each>
                  <column name="{$wst_column_name}"
                          ids="heightmarks_points-wstv-{$wst_column_position}-{$wst_id}"
                          factory="staticwkms"
                          info="{$info} [km {$deffrom} - {$defto}]"/>
                </dc:for-each>
              </relativepoint>
            </dc:group>
          </heightmarks>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="longitudinal-section-prototype">
      <dc:call-macro name="basedata_0"/>
      <dc:call-macro name="basedata_1_additionals"/>
      <dc:call-macro name="basedata_2_fixations"/>
      <dc:call-macro name="basedata_4_heightmarks-points"/>
      <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:macro name="basedata_5_flood-protections">
      <dc:filter expr="$kind=5">
        <dc:if test="dc:has-result()">
          <flood_protections>
            <dc:group expr="dc:replace($wst_description, 'HW-Schutzanlagen/', '')">
              <flood_protection name="{dc:group-key()}">
                <dc:for-each>
                  <column name="{$wst_column_name}"
                          ids="flood_protection-wstv-{$wst_column_position}-{$wst_id}"
                          factory="staticwkms"
                          info="{$info} [km {$deffrom} - {$defto}]"/>
                </dc:for-each>
              </flood_protection>
            </dc:group>
          </flood_protections>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="annotations_per_type">
      <annotations>
        <dc:context>
          <all_annotations factory="annotations" ids="{$river_id}"/>
          <dc:statement>
            SELECT id   AS anno_id,
                   name AS anno_description
            FROM annotation_types
          </dc:statement>
          <dc:for-each>
            <annotation name="{$anno_description}"
                        factory="annotations"
                        ids="{$river_id}:{$anno_description}"/>
          </dc:for-each>
        </dc:context>
      </annotations>
    </dc:macro>

    <dc:macro name="basedata_2_fixations_relative_point">
      <dc:filter expr="$kind=2">
        <dc:if test="dc:has-result()">
          <fixations>
            <dc:group expr="dc:replace($wst_description, 'Fixierungen/', '')">
              <relativepoint name="{dc:group-key()}">
                <dc:for-each>
                  <column name="{$wst_column_name}"
                          ids="fixations-wstv-{$wst_column_position}-{$wst_id}"
                          factory="staticwkms"
                          info="{$info} [km {$deffrom} - {$defto}]"/>
                </dc:for-each>
              </relativepoint>
            </dc:group>
          </fixations>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="basedata_5_flood-protections_relative_points">
      <dc:filter expr="$kind=5">
        <dc:if test="dc:has-result()">
          <flood_protections>
            <dc:group expr="$wst_description">
              <relativepoint name="{dc:group-key()}">
                <dc:for-each>
                  <column name="{$wst_column_name}"
                          ids="flood_protection-wstv-{$wst_column_position}-{$wst_id}"
                          factory="staticwkms"
                          info="{$info} [km {$deffrom} - {$defto}]"/>
                </dc:for-each>
              </relativepoint>
            </dc:group>
          </flood_protections>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="basedata_1_additionals-relative_point">
      <dc:filter expr="$kind=1">
        <dc:if test="dc:has-result()">
          <additionals>
            <dc:group expr="$wst_description">
              <relativepoint name="{dc:group-key()}">
                <dc:for-each>
                  <column name="{$wst_column_name}"
                          ids="additionals-wstv-{$wst_column_position}-{$wst_id}"
                          factory="staticwkms"
                          info="{$info} [km {$deffrom} - {$defto}]"/>
                </dc:for-each>
              </relativepoint>
            </dc:group>
          </additionals>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="discharge_table_gauge">
      <dc:context>
        <dc:statement>
          SELECT id   AS gauge_id,
                 name AS gauge_name
          FROM gauges WHERE river_id = ${river_id}
        </dc:statement>
        <dc:if test="dc:has-result()">
          <discharge_table_nn>
            <discharge_table_gauge>
              <dc:for-each>
                <gauge name="{$gauge_name}"
                  factory="gaugedischarge"
                  from="{$g_start}"
                  to="{$g_stop}"
                  ids="{$gauge_name}"/>
              </dc:for-each>
            </discharge_table_gauge>
          </discharge_table_nn>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:comment>TODO: Why is this just a copy of the discharge_table_gauge?</dc:comment>
    <dc:macro name="discharge_computed">
      <dc:call-macro name="discharge_table_gauge"/>
    </dc:macro>

    <dc:comment>TODO: Why is this just a copy of the discharge_table_gauge?</dc:comment>
    <dc:macro name="discharge_fix_wq">
      <dc:call-macro name="discharge_table_gauge"/>
    </dc:macro>

    <dc:macro name="qsectors">
      <qsector factory="qsectors" ids="{$river_id}"/>
    </dc:macro>

    <dc:macro name="flood-map-recommended">
      <kilometrage>
        <riveraxis factory="riveraxis" ids="{$river_id}"/>
      </kilometrage>
      <rastermap>
        <background factory="wmsbackground" ids="{$river_id}"/>
      </rastermap>
    </dc:macro>

    <dc:macro name="minfo-heights">
      <bedheights>
        <dc:call-macro name="bed-heights-single"/>
        <dc:call-macro name="bed-heights-epoch"/>
      </bedheights>
    </dc:macro>

    <dc:macro name="bed-heights-single">
      <single>
        <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:for-each>
            <height factory="bedheight"
                    ids="bedheight-single-{$bedh_id}-{$bedh_year}"
                    description="{$bedh_descr}"/>
          </dc:for-each>
        </dc:context>
      </single>
    </dc:macro>

    <dc:macro name="bed-heights-epoch">
      <epoch>
        <dc:context>
          <dc:statement>
            SELECT id               AS bedh_id,
                   time_interval_id AS bedh_interval_id,
                   description      AS bedh_descr
            FROM bed_height_epoch WHERE river_id = ${river_id}
          </dc:statement>
          <dc:for-each>
            <height factory="bedheight"
                    ids="bedheight-epoch-{$bedh_id}-{$bedh_interval_id}"
                    description="{$bedh_descr}"/>
          </dc:for-each>
        </dc:context>
      </epoch>
    </dc:macro>

    <dc:macro name="flow_velocity_measurements">
      <dc:context>
        <dc:statement>
          SELECT id          AS fvmid,
                 description AS fvmd
          FROM flow_velocity_measurements WHERE river_id = ${river_id}
        </dc:statement>
        <dc:if test="dc:has-result()">
          <flowvelocitymeasurement>
            <dc:for-each>
              <dc:comment>TODO: Why has this an id and factory?</dc:comment>
              <flow_velocity_measurement name="{$fvmd}"
                                         ids="{$fvmid}"
                                         factory="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:for-each>
                    <measurement_value name="{$description} - {$station} - {$datetime}"
                                       ids="{$id}"
                                       factory="flowvelocity"/>
                  </dc:for-each>
                </dc:context>
              </flow_velocity_measurement>
            </dc:for-each>
          </flowvelocitymeasurement>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="morph_width">
      <dc:context>
        <dc:statement>
          SELECT id   AS width_id
          FROM morphologic_width
          WHERE river_id = ${river_id}
        </dc:statement>
        <dc:if test="dc:has-result()">
          <morph_width>
            <dc:for-each>
              <dc:context>
                <dc:statement>
                  SELECT min(station) AS from_station,
                         max(station) AS to_station
                  FROM morphologic_width_values
                  WHERE morphologic_width_id = ${width_id}
                </dc:statement>
                <dc:for-each>
                  <morphologic-width name="{$from_station} - {$to_station}"
                                     ids="{$width_id}"
                                     factory="morph-width"/>
                </dc:for-each>
              </dc:context>
            </dc:for-each>
          </morph_width>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="sounding-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:if test="dc:has-result()">
          <soundings_width>
            <dc:for-each>
              <height factory="bedheight"
                      ids="bedheight-singlevalues-{$bedh_id}-{$bedh_year}"
                      description="{$bedh_descr}"/>
            </dc:for-each>
          </soundings_width>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="basedata_0_macro">
      <basedata>
        <dc:filter expr="$kind = 0">
          <dc:group expr="$wst_description">
            <basedata name="{dc:group-key()}">
              <dc:for-each>
                <dc:macro-body/>
              </dc:for-each>
            </basedata>
          </dc:group>
        </dc:filter>
      </basedata>
    </dc:macro>

    <dc:macro name="basedata_0">
      <dc:call-macro name="basedata_0_macro">
        <column name="{$wst_column_name}"
                ids="base_data-wstv-{$wst_column_position}-{$wst_id}"
                factory="staticwqkms"
                info="{$info} [km {$deffrom} - {$defto}]"/>
      </dc:call-macro>
    </dc:macro>

    <dc:macro name="basedata_0_wq">
      <dc:call-macro name="basedata_0_macro">
        <column name="{$wst_column_name}"
                ids="base_data-wstv-{$wst_column_position}-{$wst_id}"
                factory="wqinterpol"
                info="{$info} [km {$deffrom} - {$defto}]"/>
      </dc:call-macro>
    </dc:macro>

    <dc:macro name="basedata_1_additionals_macro">
      <additionals>
        <dc:filter expr="$kind=1">
          <dc:group expr="dc:replace($wst_description, 'Zus.Längsschnitte/', '')">
            <additional name="{dc:group-key()}">
              <dc:macro-body/>
            </additional>
          </dc:group>
        </dc:filter>
      </additionals>
    </dc:macro>

    <dc:macro name="basedata_1_additionals">
      <dc:call-macro name="basedata_1_additionals_macro">
        <column name="{$wst_column_name}"
                ids="additionals-wstv-{$wst_column_position}-{$wst_id}"
                factory="staticwqkms"
                info="{$info} [km {$deffrom} - {$defto}]"/>
      </dc:call-macro>
    </dc:macro>

    <dc:macro name="basedata_1_additionals_marks">
      <dc:call-macro name="basedata_1_additionals_macro">
        <column name="{$wst_column_name}"
                ids="additionalsmarks-wstv-{$wst_column_position}-{$wst_id}"
                factory="staticwkms"
                info="{$info} [km {$deffrom} - {$defto}]"/>
      </dc:call-macro>
    </dc:macro>

    <!-- Floodmap part -->
    <dc:macro name="flood-map-complete">
      <dc:call-macro name="flood-map-buildings"/>
      <dc:call-macro name="catchments"/>
      <dc:call-macro name="floodmarks"/>
      <hws>
        <dc:call-macro name="hwslines"/>
        <dc:call-macro name="hwspoints"/>
      </hws>
      <dc:call-macro name="flood-map-routing"/>
      <hydrboundaries>
        <dc:call-macro name="flood-map-floodplain"/>
        <dc:call-macro name="flood-map-hydr-boundaries"/>
      </hydrboundaries>
      <dc:call-macro name="flood-map-uesk"/>
    </dc:macro>

    <dc:macro name="flood-map-routing">
      <route_data>
        <dc:call-macro name="flood-map-qps"/>
        <dc:call-macro name="flood-map-fixpoints"/>
        <dc:call-macro name="flood-map-km"/>
        <dc:call-macro name="flood-map-riveraxis"/>
      </route_data>
    </dc:macro>

    <dc:macro name="flood-map-hydr-boundaries">
      <bfg_model>
        <dc:call-macro name="flood-map-hydr-boundaries-poly"/>
        <dc:call-macro name="flood-map-hydr-boundaries-lines"/>
      </bfg_model>
      <dc:call-macro name="flood-map-hydr-boundaries-state"/>
    </dc:macro>

    <dc:macro name="flood-map-recommended">
      <dc:comment>
         FIXME: Following two macros look identical to me.
      </dc:comment>
      <kilometrage>
        <riveraxis factory="riveraxis" ids="{$river_id}"/>
      </kilometrage>
      <rastermap>
        <background factory="wmsbackground" ids="{$river_id}"/>
      </rastermap>
    </dc:macro>

    <dc:macro name="flood-map-dem">
      <dems>
        <dc:context>
          <dc:statement>
            SELECT d.id    AS dem_id,
                   r.a     AS dem_lower,
                   r.b     AS dem_upper,
                   d.name  AS name,
                   t.start_time AS start_time,
                   t.stop_time AS stop_time,
                   'Projektion: ' || d.projection || '$' ||
                   'Rasterweite: '   || d.resolution || 'm$' ||
                   'Format: '  || d.format || '$' ||
                   'Zeitraum: '
                   AS info
            FROM dem d
              JOIN ranges r ON d.range_id = r.id
              LEFT JOIN time_intervals t ON d.time_interval_id = t.id
            WHERE d.river_id = ${river_id}
          </dc:statement>
          <dc:for-each>
            <dem factory="demfactory" 
              ids="{$dem_id}"
              name="{$name}"
              info="{dc:replace($info, '$', '&lt;BR&gt;')}{dc:date-format('yyyy', $start_time)} - {dc:date-format('yyyy', $stop_time)}"/>
          </dc:for-each>
        </dc:context>
      </dems>
    </dc:macro>

    <dc:macro name="filter_hws_ddg">
      <dc:macro name="durchlass_damm_graben">
        <dc:macro name="ddg_factory">
          <dc:for-each>
            <hws factory="hwsfactory" name="{$hws_name}"/>
          </dc:for-each>
        </dc:macro>

        <dc:filter expr="$hws_kind=1">
          <dc:if test="dc:has-result()">
            <Durchlass><dc:call-macro name="ddg_factory"/></Durchlass>
          </dc:if>
        </dc:filter>

        <dc:filter expr="$hws_kind=2">
          <dc:if test="dc:has-result()">
            <Damm><dc:call-macro name="ddg_factory"/></Damm>
          </dc:if>
        </dc:filter>

        <dc:filter expr="$hws_kind=3">
          <dc:if test="dc:has-result()">
            <Graben><dc:call-macro name="ddg_factory"/></Graben>
          </dc:if>
        </dc:filter>
      </dc:macro>

      <dc:filter expr="$hws_official=1">
        <dc:if test="dc:has-result()">
          <official>
            <dc:call-macro name="durchlass_damm_graben"/>
          </official>
        </dc:if>
      </dc:filter>

      <dc:filter expr="$hws_official=0">
        <dc:if test="dc:has-result()">
          <inofficial>
            <dc:call-macro name="durchlass_damm_graben"/>
          </inofficial>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="flood-map-hws-lines">
      <dc:context>
        <dc:statement>
          SELECT DISTINCT
          name     AS hws_name,
          official AS hws_official,
          kind_id  AS hws_kind
          FROM hws_lines
          WHERE river_id = ${river_id}
        </dc:statement>
        <dc:if test="dc:has-result()">
          <lines>
            <dc:call-macro name="filter_hws_ddg"/>
          </lines>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="flood-map-hws-points">
      <dc:context>
        <dc:statement>
          SELECT DISTINCT
          name     AS hws_name,
          official AS hws_official,
          kind_id  AS hws_kind
          FROM hws_points
          WHERE river_id = ${river_id}
        </dc:statement>
        <dc:if test="dc:has-result()">
          <points>
            <dc:call-macro name="filter_hws_ddg"/>
          </points>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="flood-map-hydr-boundaries-state">
      <federal>
        <dc:context>
          <dc:statement>
            SELECT DISTINCT
              name
            FROM hydr_boundaries
            WHERE river_id = ${river_id}
              AND kind = 2
          </dc:statement>
          <dc:for-each>
            <line factory="wmshydrboundariesfactory" 
                  ids="{$river_id};{$name};2"
                  name="{$name}"/>
          </dc:for-each>
        </dc:context>
        <dc:context>
          <dc:statement>
            SELECT DISTINCT
              name
            FROM hydr_boundaries_poly
            WHERE river_id = ${river_id}
              AND kind = 2
          </dc:statement>
          <dc:for-each>
            <line factory="wmshydrboundariespolyfactory"
                  ids="{$river_id};{$name};2"
                  name="{$name}"/>
          </dc:for-each>
        </dc:context>
      </federal>
    </dc:macro>

    <dc:macro name="flood-map-hydr-boundaries-lines">
      <dc:context>
        <dc:statement>
          SELECT DISTINCT
            name
          FROM hydr_boundaries
          WHERE river_id = ${river_id}
            AND kind = 1
        </dc:statement>
        <dc:comment> What about all other line kinds?</dc:comment>
        <dc:if test="dc:has-result()">
          <lines>
            <dc:for-each>
              <line factory="wmshydrboundariesfactory"
                    ids="{$river_id};{$name};1"
                    name="{$name}"/>
            </dc:for-each>
          </lines>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="flood-map-hydr-boundaries-poly">
      <areas>
        <dc:context>
          <dc:statement>
            SELECT DISTINCT
              b.sectie AS sectie_id,
              sk.name AS sectie
            FROM hydr_boundaries_poly b
              JOIN sectie_kinds sk ON b.sectie = sk.id
            WHERE b.river_id = ${river_id}
              AND b.kind = 1
          </dc:statement>
          <dc:if test="dc:has-result()">
            <sobek_areas>
              <dc:for-each>
                <boundary name="{$sectie}"
                          factory="wmshydrboundariespolyfactory"
                          ids="{$river_id};{$sectie};1;{$sectie_id};-1"/>
              </dc:for-each>
            </sobek_areas>
          </dc:if>
        </dc:context>
        <dc:context>
          <dc:statement>
            SELECT DISTINCT
              b.sobek AS sobek_id,
              sk.name AS sobek
            FROM hydr_boundaries_poly b
              JOIN sobek_kinds sk ON b.sobek = sk.id
            WHERE b.river_id = ${river_id}
              AND b.kind = 1
          </dc:statement>
          <dc:if test="dc:has-result()">
            <sobek_flooded>
              <dc:for-each>
                <boundary name="{$sobek}"
                          factory="wmshydrboundariespolyfactory"
                          ids="{$river_id};{$sobek};1;-1;{$sobek_id}"/>
              </dc:for-each>
            </sobek_flooded>
          </dc:if>
        </dc:context>
        <dc:context>
          <dc:statement>
            SELECT DISTINCT
              b.name AS name
            FROM hydr_boundaries_poly b
            WHERE b.river_id = ${river_id}
              AND b.kind = 1
              AND b.sobek IS NULL
              AND b.sectie is NULL
          </dc:statement>
          <dc:for-each>
            <boundary name="{$name}"
                      factory="wmshydrboundariespolyfactory"
                      ids="{$river_id};{$name}"/>
          </dc:for-each>
        </dc:context>
      </areas>
    </dc:macro>

    <dc:macro name="flood-map-uesk">
      <dc:context>
        <dc:statement>
          SELECT DISTINCT 1 FROM floodmaps WHERE river_id = ${river_id}
        </dc:statement>
        <dc:if test="dc:has-result()">
        <uesk>
          <calculations>
            <dc:context>
              <dc:statement>
                SELECT DISTINCT 1 FROM floodmaps WHERE river_id = ${river_id}
                AND (kind = 112 OR kind = 111)
              </dc:statement>
              <dc:if test="dc:has-result()">
                <current>
                  <dc:context>
                    <dc:statement>
                      SELECT DISTINCT name AS name
                      FROM floodmaps
                      WHERE river_id = ${river_id} AND kind = 111
                    </dc:statement>
                    <dc:if test="dc:has-result()">
                      <bfg>
                        <dc:for-each>
                          <floodmaps factory="wmsfloodmapsfactory"
                            ids="{$river_id};{$name}"
                            name="{$name}"/>
                        </dc:for-each>
                      </bfg>
                    </dc:if>
                  </dc:context>
                  <dc:context>
                    <dc:statement>
                      SELECT DISTINCT name AS name
                      FROM floodmaps
                      WHERE river_id = ${river_id} AND kind = 112
                    </dc:statement>
                    <dc:if test="dc:has-result()">
                      <federal>
                        <dc:for-each>
                          <floodmaps factory="wmsfloodmapsfactory"
                            ids="{$river_id};{$name}"
                            name="{$name}"/>
                        </dc:for-each>
                      </federal>
                    </dc:if>
                  </dc:context>
                </current>
              </dc:if>
            </dc:context>
            <dc:context>
              <dc:statement>
                SELECT DISTINCT 1 from floodmaps where river_id = ${river_id}
                AND (kind = 122 OR kind = 121)
              </dc:statement>
              <dc:if test="dc:has-result()">
                <potential>
                  <dc:context>
                    <dc:statement>
                      SELECT DISTINCT name AS name
                      FROM floodmaps
                      WHERE river_id = ${river_id} AND kind = 121
                    </dc:statement>
                    <dc:if test="dc:has-result()">
                      <bfg>
                        <dc:for-each>
                          <floodmaps factory="wmsfloodmapsfactory"
                            ids="{$river_id};{$name}"
                            name="{$name}"/>
                        </dc:for-each>
                      </bfg>
                    </dc:if>
                  </dc:context>
                  <dc:context>
                    <dc:statement>
                      SELECT DISTINCT
                        name AS name
                      FROM floodmaps
                      WHERE river_id = ${river_id} AND kind = 122
                    </dc:statement>
                    <dc:if test="dc:has-result()">
                      <federal>
                        <dc:for-each>
                          <floodmaps factory="wmsfloodmapsfactory"
                            ids="{$river_id};{$name}"
                            name="{$name}"/>
                        </dc:for-each>
                      </federal>
                    </dc:if>
                  </dc:context>
                </potential>
              </dc:if>
            </dc:context>
          </calculations>
          <dc:context>
            <dc:statement>
              SELECT DISTINCT
                source AS source
              FROM floodmaps
              WHERE river_id = ${river_id} AND kind = 200
            </dc:statement>
            <dc:if test="dc:has-result()">
              <measurements>
                <dc:for-each>
                  <year name="{$source}">
                    <dc:context>
                      <dc:statement>
                        SELECT DISTINCT
                          name AS name
                        FROM floodmaps
                        WHERE river_id = ${river_id} AND kind = 200 AND source =
                        ${source}
                      </dc:statement>
                      <dc:for-each>
                        <floodmaps factory="wmsfloodmapsfactory"
                          ids="{$river_id};{$name}"
                          name="{$name}"/>
                      </dc:for-each>
                    </dc:context>
                  </year>
                </dc:for-each>
              </measurements>
            </dc:if>
          </dc:context>
        </uesk>
      </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="flood-map-floodplain">
      <dc:context>
        <dc:statement>
          SELECT DISTINCT
            fp.kind_id AS kind_id,
            flk.name AS kind_name
          FROM floodplain fp
            JOIN floodplain_kinds flk on fp.kind_id = flk.id
          WHERE river_id = ${river_id}
            AND kind_id=1
        </dc:statement>
        <dc:if test="dc:has-result()">
          <floodplain>
            <dc:filter expr="$kind_id=1">
              <dc:for-each>
                <floody factory="wmsfloodplainfactory"
                        description="{$kind_name}"
                        ids="{$river_id};{$kind_name};{$kind_id}"/>
              </dc:for-each>
            </dc:filter>
            <dc:filter expr="kind_id!=1">
              <other>
                <dc:for-each>
                  <floody factory="wmsfloodplainfactory"
                          description="{$layer_name}"
                          ids="{$river_id};{$layer_name};{$kind_id}"/>
                </dc:for-each>
              </other>
            </dc:filter>
          </floodplain>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="hwspoints_by_kind">
      <dc:comment>
        Call from a context where fed_name hws_kind hws_name and river_id is
        availble
      </dc:comment>

      <dc:macro name="hwspoints_by_kind_factory">
        <dc:for-each>
          <hws factory="wmshwspointsfactory"
               ids="{$river_id};{$hws_name}"
               name="{$hws_name}"/>
        </dc:for-each>
      </dc:macro>

      <dc:filter expr="$hws_kind=1">
        <dc:if test="dc:has-result()">
          <Durchlass>
            <dc:call-macro name="hwspoints_by_kind_factory"/>
          </Durchlass>
        </dc:if>
      </dc:filter>

      <dc:filter expr="$hws_kind=2">
        <dc:if test="dc:has-result()">
          <Damm>
            <dc:call-macro name="hwspoints_by_kind_factory"/>
          </Damm>
        </dc:if>
      </dc:filter>

      <dc:filter expr="$hws_kind=3">
        <dc:if test="dc:has-result()">
          <Graben>
            <dc:call-macro name="hwspoints_by_kind_factory"/>
          </Graben>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="hwspoints">
      <hws_points>
        <official>
          <dc:context>
            <dc:statement>
              SELECT DISTINCT
                fs.name AS fed_name,
                fs.id AS fed_id
              FROM hws_points hws
                JOIN fed_states fs ON hws.fed_state_id = fs.id
              WHERE river_id = ${river_id}
                AND hws.official=1
            </dc:statement>
            <dc:for-each>
              <dc:context>
                <dc:statement>
                  SELECT DISTINCT
                    name AS hws_name,
                    kind_id AS hws_kind
                  FROM hws_points
                  WHERE river_id = ${river_id}
                    AND official=1
                    AND fed_state_id = ${fed_id} ORDER BY name
                </dc:statement>
                <fedstate description="{$fed_name}">
                  <dc:call-macro name="hwspoints_by_kind"/>
                </fedstate>
              </dc:context>
            </dc:for-each>
          </dc:context>
          <dc:context>
            <dc:statement>
              SELECT distinct
                name AS hws_name,
                kind_id AS hws_kind
              FROM hws_points
              WHERE river_id = ${river_id}
                AND official=1
                AND fed_state_id IS NULL
                ORDER BY name
            </dc:statement>
            <hws_fed_unknown>
              <dc:call-macro name="hwspoints_by_kind"/>
            </hws_fed_unknown>
          </dc:context>
        </official>
        <inofficial>
          <dc:context>
            <dc:statement>
              SELECT DISTINCT
                fs.name AS fed_name,
                fs.id AS fed_id
              FROM hws_points hws
                JOIN fed_states fs ON hws.fed_state_id = fs.id
              WHERE river_id = ${river_id}
                AND hws.official=0
            </dc:statement>
            <dc:for-each>
              <dc:context>
                <dc:statement>
                  SELECT DISTINCT
                    name AS hws_name,
                    kind_id AS hws_kind
                  FROM hws_points
                  WHERE river_id = ${river_id}
                    AND official=0
                    AND fed_state_id = ${fed_id} ORDER BY name
                </dc:statement>
                <fedstate description="{$fed_name}">
                  <dc:call-macro name="hwspoints_by_kind"/>
                </fedstate>
              </dc:context>
            </dc:for-each>
          </dc:context>
          <dc:context>
            <dc:statement>
              SELECT distinct
                name AS hws_name,
                kind_id AS hws_kind
              FROM hws_points
              WHERE river_id = ${river_id}
                AND official=0
                AND fed_state_id IS NULL ORDER BY name
            </dc:statement>
            <hws_fed_unknown>
              <dc:call-macro name="hwspoints_by_kind"/>
            </hws_fed_unknown>
          </dc:context>
        </inofficial>
      </hws_points>
    </dc:macro>

    <dc:macro name="hwslines_by_kind">
      <dc:comment>
        Call from a context where fed_name hws_kind hws_name and river_id is
        availble
      </dc:comment>

      <dc:macro name="hwslines_by_kind_factory">
        <dc:for-each>
          <hws factory="wmshwslinesfactory"
               ids="{$river_id};{$hws_name}"
               name="{$hws_name}"/>
        </dc:for-each>
      </dc:macro>

      <dc:filter expr="$hws_kind=1">
        <dc:if test="dc:has-result()">
          <Durchlass>
            <dc:call-macro name="hwslines_by_kind_factory"/>
          </Durchlass>
        </dc:if>
      </dc:filter>

      <dc:filter expr="$hws_kind=2">
        <dc:if test="dc:has-result()">
          <Damm>
            <dc:call-macro name="hwslines_by_kind_factory"/>
          </Damm>
        </dc:if>
      </dc:filter>

      <dc:filter expr="$hws_kind=3">
        <dc:if test="dc:has-result()">
          <Graben>
            <dc:call-macro name="hwslines_by_kind_factory"/>
          </Graben>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="hwslines">
      <hws_lines>
        <official>
          <dc:context>
            <dc:statement>
              SELECT DISTINCT
                fs.name AS fed_name,
                fs.id AS fed_id
              FROM hws_lines hws
                JOIN fed_states fs ON hws.fed_state_id = fs.id
              WHERE river_id = ${river_id}
                AND hws.official=1
            </dc:statement>
            <dc:for-each>
              <dc:context>
                <dc:statement>
                  SELECT DISTINCT
                    name AS hws_name,
                    kind_id AS hws_kind
                  FROM hws_lines
                  WHERE river_id = ${river_id}
                    AND official=1
                    AND fed_state_id = ${fed_id} ORDER BY name
                </dc:statement>
                <fedstate description="{$fed_name}">
                  <dc:call-macro name="hwslines_by_kind"/>
                </fedstate>
              </dc:context>
            </dc:for-each>
          </dc:context>
          <dc:context>
            <dc:statement>
              SELECT distinct
                name AS hws_name,
                kind_id AS hws_kind
              FROM hws_lines
              WHERE river_id = ${river_id}
                AND official=1
                AND fed_state_id IS NULL
                ORDER BY name
            </dc:statement>
            <hws_fed_unknown>
              <dc:call-macro name="hwslines_by_kind"/>
            </hws_fed_unknown>
          </dc:context>
        </official>
        <inofficial>
          <dc:context>
            <dc:statement>
              SELECT DISTINCT
                fs.name AS fed_name,
                fs.id AS fed_id
              FROM hws_lines hws
                JOIN fed_states fs ON hws.fed_state_id = fs.id
              WHERE river_id = ${river_id}
                AND hws.official=0
            </dc:statement>
            <dc:for-each>
              <dc:context>
                <dc:statement>
                  SELECT DISTINCT
                    name AS hws_name,
                    kind_id AS hws_kind
                  FROM hws_lines
                  WHERE river_id = ${river_id}
                    AND official=0
                    AND fed_state_id = ${fed_id} ORDER BY name
                </dc:statement>
                <fedstate description="{$fed_name}">
                  <dc:call-macro name="hwslines_by_kind"/>
                </fedstate>
              </dc:context>
            </dc:for-each>
          </dc:context>
          <dc:context>
            <dc:statement>
              SELECT distinct
                name AS hws_name,
                kind_id AS hws_kind
              FROM hws_lines
              WHERE river_id = ${river_id}
                AND official=0
                AND fed_state_id IS NULL ORDER BY name
            </dc:statement>
            <hws_fed_unknown>
              <dc:call-macro name="hwslines_by_kind"/>
            </hws_fed_unknown>
          </dc:context>
        </inofficial>
      </hws_lines>
    </dc:macro>

    <dc:macro name="floodmarks">
      <dc:context>
        <dc:statement>
          SELECT DISTINCT
            COALESCE(CAST (year AS VARCHAR(64)), 'Unbekanntes Jahr') AS year
          FROM flood_marks
          WHERE river_id = ${river_id}
        </dc:statement>
        <dc:if test="dc:has-result()">
          <floodmarks>
            <dc:for-each>
              <floodmark name="{$year}"
                factory="wmsfloodmarkfactory"
                ids="{$river_id};{$year};{$year}"/>
            </dc:for-each>
          </floodmarks>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="catchments">
      <catchments>
        <catchment_wms factory="externalwmsfactory"
                       ids="http://geoportal.bafg.de/wmsproxy/INSPIRE/DrainageBasin;HY.PHYSICALWATERS.CATCHMENTS;Einzugsgebiete (WMS)" />
        <dc:call-macro name="flood-map-gaugelocations"/>
      </catchments>
    </dc:macro>

    <dc:macro name="flood-map-gaugelocations">
      <gaugelocations>
        <gauge_points factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelpunkte;Pegelpunkte (WSV)"/>
        <gauge_names factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelnamen;Pegelnamen (WSV)"/>
        <gauge_level factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelwasserstand;Aktueller Wasserstand (WSV)"/>
        <gauge_tendency factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;TendenzWasserstand;Tendenz des Wasserstands (WSV)"/>
      </gaugelocations>
    </dc:macro>

    <dc:macro name="flood-map-riveraxis">
      <dc:context>
        <dc:statement>
          SELECT DISTINCT
            ak.name    AS kind_name,
            ax.kind_id AS kind_id,
            ax.name    AS layer_name
          FROM river_axes ax
            JOIN axis_kinds ak on ax.kind_id = ak.id
          WHERE river_id = ${river_id}
        </dc:statement>
        <dc:if test="dc:has-result()">
          <axis>
            <dc:filter expr="$kind_id=1">
              <dc:for-each>
                <actual description="{$kind_name}"
                        ids="{$river_id};{$kind_name};{$kind_id}"
                        factory="riveraxis"/>
              </dc:for-each>
            </dc:filter>
            <dc:filter expr="$kind_id!=1">
              <other>
                <dc:for-each>
                  <misc-axis description="{$layer_name}"
                             ids="{$river_id};{$layer_name};{$kind_id}"
                             factory="riveraxis"/>
                </dc:for-each>
              </other>
            </dc:filter>
          </axis>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="flood-map-km">
      <dc:context>
        <dc:statement>
          SELECT DISTINCT 1 FROM river_axes_km WHERE river_id = ${river_id}
        </dc:statement>
         <dc:for-each>
            <kilometrage factory="wmskmfactory" ids="{$river_id}"/>
        </dc:for-each>
      </dc:context>
    </dc:macro>

    <dc:macro name="flood-map-fixpoints">
      <dc:context>
        <dc:statement>
          SELECT name AS name
          FROM fixpoints
          WHERE river_id = ${river_id}
            GROUP BY name
        </dc:statement>
        <dc:for-each>
          <fixpoints factory="wmsfixpointsfactory"
            ids="{$river_id};{$name}"/>
        </dc:for-each>
      </dc:context>
    </dc:macro>

    <dc:macro name="flood-map-qps">
      <dc:context>
        <dc:statement>
          SELECT DISTINCT
            cs.kind_id AS kind_id,
            cs.name    AS layer_name,
            ck.name    AS kind_name
          FROM cross_section_tracks cs
            JOIN cross_section_track_kinds ck ON cs.kind_id = ck.id
          WHERE river_id = ${river_id}
        </dc:statement>
        <dc:if test="dc:has-result()">
          <qps>
            <dc:filter expr="$kind_id=1">
              <dc:for-each>
                <actual description="{$kind_name}"
                        factory="wmsqpsfactory"
                        ids="{$river_id};{$kind_name};{$kind_id}"/>
              </dc:for-each>
            </dc:filter>
            <dc:filter expr="kind_id=0">
              <other>
                <dc:for-each>
                  <misc-qps description="{$layer_name}"
                            factory="wmsqpsfactory"
                            ids="{$river_id};{$layer_name};{$kind_id}"/>
                </dc:for-each>
              </other>
            </dc:filter>
          </qps>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="flood-map-buildings">
      <buildings>
        <dc:context>
          <dc:statement>
            SELECT DISTINCT
              b.name AS building_name,
              bk.name AS building_kind,
              b.kind_id AS building_kind_id
            FROM buildings b
              JOIN building_kinds bk ON b.kind_id = bk.id
            WHERE river_id = ${river_id}
          </dc:statement>
          <dc:if test="dc:has-result()">
            <dc:filter expr="$building_kind_id!=0">
              <dc:for-each>
                <buildings description="{$building_kind}"
                           factory="wmsbuildingsfactory"
                           ids="{$river_id};{$building_kind};{$building_kind_id}"/>
              </dc:for-each>
            </dc:filter>
            <dc:filter expr="$building_kind_id=0">
              <other>
                <dc:for-each>
                  <buildings description="{$building_name}"
                             factory="wmsbuildingsfactory"
                             ids="{$river_id};{$building_name}"/>
                </dc:for-each>
              </other>
            </dc:filter>
          </dc:if>
        </dc:context>
        <dc:context>
          <dc:statement>
            SELECT DISTINCT
              j.kind_id AS jetty_kind_id,
              jk.name AS jetty_kind
            FROM jetties j
              JOIN jetty_kinds jk ON j.kind_id = jk.id
            WHERE river_id = ${river_id}
          </dc:statement>
          <dc:if test="dc:has-result()">
            <jetties>
              <dc:for-each>
                <jetty description="{$jetty_kind}"
                       factory="wmsjettiesfactory"
                       ids="{$river_id};{$jetty_kind};{$jetty_kind_id}"/>
              </dc:for-each>
            </jetties>
          </dc:if>
        </dc:context>
      </buildings>
    </dc:macro>

    <dc:macro name="officiallines_user">
      <dc:comment comment=".wst -------------------------------"/>
      <officiallines>
        <dc:for-each>
          <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})
            </dc:statement>
            <dc:for-each>
              <dc:context connection="system">
                <dc:statement>
                  SELECT ol.wst_id         AS wstid,
                         ol.wst_column_pos AS wstcolpos,
                         ol.name           AS olname,
                         ol.value          AS oval
                  FROM official_q_values ol
                  WHERE ol.value = CAST(${wqsingle} AS NUMERIC(10,2))
                  AND ol.gauge_name = ${gaugy}
                </dc:statement>
                <dc:for-each>
                  <dc:element name="${olname}">
                    <dc:attribute name="name" value="${olname}"/>
                    <dc:attribute name="ids" value="additionals-wstv-${wstcolpos}-${wstid}"/>
                    <dc:attribute name="factory" value="staticwkms"/>
                    <dc:attribute name="out" value="${out}"/>
                  </dc:element>
                </dc:for-each>
              </dc:context>
            </dc:for-each>
          </dc:context>
        </dc:for-each>
      </officiallines>
    </dc:macro>

    <!-- Common stuff -->

    <dc:macro name="km-filtered-wsts">
      <dc:context connection="system">
        <dc:statement>
          SELECT w.kind         AS kind,
                 w.id           AS wst_id,
                 wc.id          AS wst_column_id,
                 wc.position    AS wst_column_position,
                 wc.description AS info,
                 w.description  AS wst_description,
                 wc.name        AS wst_column_name,
                 wr.a           AS deffrom,
                 wr.b           AS defto
          FROM wst_columns wc
            JOIN wsts w        ON wc.wst_id = w.id
            JOIN wst_ranges wr ON wc.id = wr.wst_column_id
          WHERE w.river_id = ${river_id} AND
            NOT((${tokm} &lt; wr.a) or (${fromkm} &gt; wr.b))
        </dc:statement>
        <dc:macro-body/>
      </dc:context>
    </dc:macro>

    <dc:macro name="km-filtered-userdata">
      <dc:context connection="user">
        <dc:statement>
          SELECT c.name                     AS collection_name,
                 ma.id                      AS a_id,
                 ma.state                   AS a_state,
                 ma.gid                     AS a_gid,
                 ma.creation                AS a_creation,
                 COALESCE(ma.ld_mode, '')      AS ld_m,
                 COALESCE(ma.ld_locations, '') AS ld_l,
                 COALESCE(ma.ld_from, '')      AS ld_f,
                 COALESCE(ma.ld_to, '')        AS ld_t
          FROM users u
            JOIN collections c             ON c.user_id = u.id
            JOIN master_artifacts_range ma ON ma.collection_id = c.id
          WHERE  c.id = ${collection_id}
            AND ma.gid &lt;&gt; CAST(${artifact-id} AS uuid)
            AND EXISTS (
              SELECT id
              FROM artifact_data ad
              WHERE ad.artifact_id = ma.id
                AND k = 'river'
                AND v = ${river})
        </dc:statement>
          <dc:for-each>
            <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="!(($tokm &lt; $from) or $fromkm &gt; $tokm)">
              <dc:macro-body/>
            </dc:if>
          </dc:for-each>
      </dc:context>
    </dc:macro>

    <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:for-each>
              <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:for-each>
          </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>

  </datacage>
</dc:template>

http://dive4elements.wald.intevation.org