view artifacts/doc/conf/meta-data.xml @ 6393:a8a97f27ca27

Draw sediment load fraction 'unknown' in chart.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 21 Jun 2013 15:36:48 +0200
parents abed7e091762
children f369d69e3488
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
      ------------------
      This is a hack because we currently have no way to supress empty
      folders (either in the client or in the datacage).
    </dc:comment>
    <dc:if test="dc:contains($parameters, 'user-id') and
      not(dc:contains($artifact-outs, 'floodmap-hws')) and
      not($current-state-id = 'state.winfo.uesk.dgm')">
      <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="artifact-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:when test="$out = 'sedimentload_ls'">
                    <dc:call-macro name="annotations"/>
                  </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="basedata_5_flood-protections"/>
                    <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_height_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="basedata_6_delta_w"/>
                    <dc:call-macro name="annotations_per_type"/>
                    <dc:call-macro name="morph_width"/>
                  </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="basedata_6_delta_w"/>
                    <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:when>
                  <dc:when test="$out = 'minfo-heights-epoch'">
                    <bedheights>
                      <dc:call-macro name="bed-heights-epoch"/>
                    </bedheights>
                  </dc:when>
                  <dc:when test="$out = 'waterlevels'">
                    <dc:if test="$current-state-id != 'state.winfo.uesk.wsp'">
                      <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:if>
                  </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="artifact-range">
        <dc:call-macro name="all-user-artifacts">
          <dc:call-macro name="km-filtered-user-artifacts">
            <dc:choose>
              <dc:when test="dc:contains($parameters, 'recommended')">
                <dc:comment>
                  Recommendations (user)
                </dc:comment>
                <dc:iterate var="out" container="artifact-outs">
                  <dc:choose>
                    <dc:when test="$out = 'longitudinal_section'">
                      <dc:call-macro name="officiallines_user"/>
                    </dc:when>
                  </dc:choose>
                </dc:iterate>
              </dc:when>
              <dc:otherwise>
                <dc:comment>
                  Non Recommendations (user)
                </dc:comment>
                <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: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="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:otherwise>
            </dc:choose>
          </dc:call-macro>
        </dc:call-macro>
      </dc:call-macro>
    </dc:macro>

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

    <dc:macro name="differences">
      <dc:filter expr="$facet_name = 'w_differences'">
        <dc:if test="dc:has-result()">
          <differences>
            <dc:for-each>
              <w_differences
                description="{$facet_description}"
                factory="winfo" target_out="{$out}"
                artifact-id="{$aid}"
                ids="{$aid}"
                out="w_differences"/>
            </dc:for-each>
          </differences>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="reference-curves">
      <dc:filter expr="$facet_name = 'reference_curve'">
        <dc:if test="dc:has-result()">
          <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="target_out" value="${out}"/>
                <dc:attribute name="artifact-id" value="${a_id}"/>
                <dc:attribute name="ids" value="${a_id}"/>
                <dc:attribute name="out" value="reference_curve"/>
              </dc:element>
            </dc:for-each>
          </reference_curves>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="computed-discharge-curve">
      <dc:filter expr="$facet_name = 'computed_discharge_curve.q'">
        <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="target_out" value="${out}"/>
                <dc:attribute name="artifact-id" value="${a_id}"/>
                <dc:attribute name="ids" value="${a_id}"/>
                <dc:attribute name="out" value="computed_discharge_curve"/>
              </dc:element>
            </dc:for-each>
          </computed_discharge_curves>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="flood-map">
      <dc:filter expr="$facet_name = 'floodmap.wsplgen'">
        <dc:if test="dc:has-result()">
          <floodmap>
            <dc:for-each>
              <dc:variable name="combined_desc" expr="concat($facet_description, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)"/>
              <dc:element name="${facet_name}">
                <dc:attribute name="description" value="${combined_desc}"/>
                <dc:attribute name="factory" value="winfo"/>
                <dc:attribute name="target_out" value="${out}"/>
                <dc:attribute name="artifact-id" value="${a_id}"/>
                <dc:attribute name="ids" value="${a_id}"/>
                <dc:attribute name="out" value="floodmap"/>
              </dc:element>
            </dc:for-each>
          </floodmap>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="waterlevels">
      <dc:filter expr="$out_name = 'cross_section'">
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation))">
              <dc:comment>Aheinecke: Why is this grouping different from the rest?</dc:comment>
              <longitudinal_section_columns description="{dc:group-key()}">
                <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="target_out" value="${out}"/>
                    <dc:attribute name="artifact-id" value="${a_gid}"/>
                    <dc:attribute name="out" value="cross_section"/>
                  </dc:element>
                </dc:for-each>
              </longitudinal_section_columns>
            </dc:group>
          </waterlevels>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="longitudinal">
      <dc:filter expr="$out_name = 'longitudinal_section'">
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation))">
              <dc:comment>Aheinecke: Why is this grouping different from the rest?</dc:comment>
              <longitudinal_section_columns description="{dc:group-key()}">
                <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="target_out" value="${out}"/>
                    <dc:attribute name="artifact-id" value="${a_gid}"/>
                    <dc:attribute name="out" value="longitudinal_section"/>
                  </dc:element>
                </dc:for-each>
              </longitudinal_section_columns>
            </dc:group>
          </waterlevels>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="longitudinal-section">
      <dc:filter expr="$out_name = 'fix_longitudinal_section_curve' and
        (starts-with($facet_name, 'fix_deviation_ls') or
        starts-with($facet_name, 'fix_sector_average_ls') or
        starts-with($facet_name, 'fix_analysis_events_ls') or
        starts-with($facet_name, 'fix_reference_events_ls'))">
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <waterlevels description="{dc:group-key()}">
                <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="target_out" value="${out}"/>
                    <dc:attribute name="artifact-id" value="${a_gid}"/>
                    <dc:attribute name="out" value="fix_longitudinal_section_curve"/>
                  </dc:element>
                </dc:for-each>
              </waterlevels>
            </dc:group>
          </waterlevels>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="delta-wt">
      <dc:filter expr="$out_name = 'fix_deltawt_curve' and
        ($facet_name = 'fix_analysis_events_dwt' or
        $facet_name = 'fix_analysis_periods_dwt' or
        $facet_name = 'fix_reference_events_dwt' or
        starts-with($facet_name, 'fix_deviation_dwt') or
        starts-with($facet_name, 'fix_sector_average_dwt'))">
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <waterlevels description="{dc:group-key()}">
                <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="target_out" value="${out}"/>
                    <dc:attribute name="artifact-id" value="${a_gid}"/>
                    <dc:attribute name="out" value="fix_deltawt_curve"/>
                  </dc:element>
                </dc:for-each>
              </waterlevels>
            </dc:group>
          </waterlevels>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="delta-wt-ls">
      <dc:filter expr="$out_name = 'fix_deltawt_curve' and
        (starts-with($facet_name, 'fix_sector_average_dwt') or
        starts-with($facet_name, 'fix_deviation_dwt'))">
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <waterlevels description="{dc:group-key()}">
                <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="target_out" value="${out}"/>
                    <dc:attribute name="artifact-id" value="${a_gid}"/>
                    <dc:attribute name="out" value="fix_deltawt_curve"/>
                  </dc:element>
                </dc:for-each>
              </waterlevels>
            </dc:group>
          </waterlevels>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="fix-derivate-curve">
      <dc:filter expr="$out_name = 'fix_derivate_curve' and $facet_name = 'fix_derivate_curve'">
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <waterlevels description="{dc:group-key()}">
                <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="target_out" value="${out}"/>
                    <dc:attribute name="artifact-id" value="${a_gid}"/>
                    <dc:attribute name="out" value="fix_derivate_curve"/>
                  </dc:element>
                </dc:for-each>
              </waterlevels>
            </dc:group>
          </waterlevels>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="fix-wq-curve">
      <dc:filter expr="$out_name = 'fix_wq_curve' and
        (starts-with($facet_name, 'fix_analysis_events_wq') or
        starts-with($facet_name, 'fix_reference_events_wq') or
        starts-with($facet_name, 'fix_sector_average_wq') or
        $facet_name = 'fix_wq_curve')">
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <waterlevels description="{dc:group-key()}">
                <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="target_out" value="${out}"/>
                    <dc:attribute name="artifact-id" value="${a_gid}"/>
                    <dc:attribute name="out" value="fix_wq_curve"/>
                  </dc:element>
                </dc:for-each>
              </waterlevels>
            </dc:group>
          </waterlevels>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="duration-curve">
      <dc:filter expr="$facet_name = 'duration_curve.q' or $facet_name = 'duration_curve.w'">
        <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="target_out" value="${out}"/>
                <dc:attribute name="artifact-id" value="${a_id}"/>
                <dc:attribute name="ids" value="${a_id}"/>
                <dc:attribute name="out" value="duration_curve"/>
              </dc:element>
            </dc:for-each>
          </computed_discharge_curves>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:comment>TODO doesnt work nicely for fix/wq-diags. Aheinecke (27.5.2013): Why?</dc:comment>
    <dc:macro name="waterlevels-fix">
      <dc:filter expr="($out_name = 'longitudinal_section' and $facet_name = 'longitudinal_section.w') and
        (not ($current-state-id = 'state.winfo.uesk.wsp' and $ld_m = 'location'))">
        <dc:if test="dc:has-result()">
          <waterlevels>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <waterlevels description="{dc:group-key()}">
                <dc:for-each>
                  <dc:choose>
                    <dc:when test="$ld_m = 'location'">
                      <dc:variable name="combined_desc" expr="concat($facet_description, ' an KM ', $deffrom)"/>
                    </dc:when>
                    <dc:otherwise>
                      <dc:variable name="combined_desc" expr="concat($facet_description, ' von KM ',
                        $deffrom, ' bis KM ', $defto)"/>
                    </dc:otherwise>
                  </dc:choose>
                  <dc:element name="${facet_name}">
                    <dc:attribute name="description" value="${combined_desc}"/>
                    <dc:attribute name="ids" value="${facet_num}"/>
                    <dc:attribute name="factory" value="winfo"/>
                    <dc:attribute name="target_out" value="${out}"/>
                    <dc:attribute name="artifact-id" value="${a_gid}"/>
                    <dc:attribute name="out" value="longitudinal_section"/>
                  </dc:element>
                </dc:for-each>
              </waterlevels>
            </dc:group>
          </waterlevels>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="floodmap-hws-user">
      <dc:comment>No grouping in this?</dc:comment>
      <dc:filter expr="$out_name = 'floodmap' and $facet_name = 'floodmap.usershape'">
        <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="target_out" value="${out}"/>
              <dc:attribute name="artifact-id" value="${a_gid}"/>
              <dc:attribute name="out" value="floodmap"/>
            </dc:element>
          </dc:for-each>
        </own-hws>
      </dc:filter>
    </dc:macro>

    <dc:macro name="bedquality-bed">
      <dc:filter expr="$out_name = 'bed_longitudinal_section' and
        ($facet_name = 'bed_longitudinal_section.bed_diameter_toplayer' or
        $facet_name = 'bed_longitudinal_section.bed_diameter_sublayer')">
        <dc:if test="dc:has-result()">
          <bed_quality_bed>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <quality-bed description="{dc:group-key()}">
                <dc:for-each>
                  <dc:element name="${facet_name}">
                    <dc:attribute name="factory" value="minfo"/>
                    <dc:attribute name="target_out" value="${out}"/>
                    <dc:attribute name="description" value="${facet_description}"/>
                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                    <dc:attribute name="artifact-id" value="${a_id}"/>
                    <dc:attribute name="out" value="bed_longitudinal_section"/>
                  </dc:element>
                </dc:for-each>
              </quality-bed>
            </dc:group>
          </bed_quality_bed>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="bedquality-load">
      <dc:filter expr="$out_name = 'bed_longitudinal_section' and
        $facet_name = 'bed_longitudinal_section.bedload_diameter'">
        <dc:if test="dc:has-result()">
          <bed_quality_load>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <quality-load description="{dc:group-key()}">
                <dc:for-each>
                  <dc:element name="${facet_name}">
                    <dc:attribute name="factory" value="minfo"/>
                    <dc:attribute name="target_out" value="${out}"/>
                    <dc:attribute name="description" value="${facet_description}"/>
                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                    <dc:attribute name="artifact-id" value="${a_id}"/>
                    <dc:attribute name="out" value="bed_longitudinal_section"/>
                  </dc:element>
                </dc:for-each>
              </quality-load>
            </dc:group>
          </bed_quality_load>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="bedquality-density">
      <dc:filter expr="$out_name = 'bed_longitudinal_section' and
        ($facet_name = 'bed_longitudinal_section.sediment_density_toplayer' or
         $facet_name = 'bed_longitudinal_section.sediment_density_sublayer')">
        <dc:if test="dc:has-result()">
          <bed_quality_density>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <density description="{dc:group-key()}">
                <dc:for-each>
                  <dc:element name="${facet_name}">
                    <dc:attribute name="factory" value="minfo"/>
                    <dc:attribute name="target_out" value="${out}"/>
                    <dc:attribute name="description" value="${facet_description}"/>
                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                    <dc:attribute name="artifact-id" value="${a_id}"/>
                    <dc:attribute name="out" value="bed_longitudinal_section"/>
                  </dc:element>
                </dc:for-each>
              </density>
            </dc:group>
          </bed_quality_density>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="bedquality-porosity">
      <dc:filter expr="$out_name = 'bed_longitudinal_section' and
        ($facet_name = 'bed_longitudinal_section.porosity_toplayer' or
        $facet_name = 'bed_longitudinal_section.porosity_sublayer')">
        <dc:if test="dc:has-result()">
          <bed_quality_porosity>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <porosity description="{dc:group-key()}">
                <dc:for-each>
                  <dc:element name="${facet_name}">
                    <dc:attribute name="factory" value="minfo"/>
                    <dc:attribute name="target_out" value="${out}"/>
                    <dc:attribute name="description" value="${facet_description}"/>
                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                    <dc:attribute name="artifact-id" value="${a_id}"/>
                    <dc:attribute name="out" value="bed_longitudinal_section"/>
                  </dc:element>
                </dc:for-each>
              </porosity>
            </dc:group>
          </bed_quality_porosity>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="flow-velocity">
      <dc:filter expr="$out_name = 'flow_velocity' and
        ($facet_name = 'flow_velocity.totalchannel' or
         $facet_name = 'flow_velocity.mainchannel' or
         $facet_name = 'flow_velocity.totalchannel.filtered' or
         $facet_name = 'flow_velocity.mainchannel.filtered')">
        <dc:if test="dc:has-result()">
          <flow-velocity>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <flow description="{dc:group-key()}">
                <dc:for-each>
                  <dc:element name="${facet_name}">
                    <dc:attribute name="factory" value="minfo"/>
                    <dc:attribute name="target_out" value="${out}"/>
                    <dc:attribute name="description" value="${facet_description}"/>
                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                    <dc:attribute name="artifact-id" value="${a_id}"/>
                    <dc:attribute name="out" value="flow_velocity"/>
                  </dc:element>
                </dc:for-each>
              </flow>
            </dc:group>
          </flow-velocity>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="sediment-load">
      <dc:filter expr="$out_name = 'sedimentload_ls' and starts-with($facet_name, 'sedimentload')">
        <dc:if test="dc:has-result()">
          <sediment-load>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <load description="{dc:group-key()}">
                <dc:for-each>
                  <dc:element name="${facet_name}">
                    <dc:attribute name="factory" value="minfo"/>
                    <dc:attribute name="target_out" value="${out}"/>
                    <dc:attribute name="description" value="${facet_description}"/>
                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                    <dc:attribute name="artifact-id" value="${a_id}"/>
                    <dc:attribute name="out" value="sedimentload_ls"/>
                  </dc:element>
                </dc:for-each>
              </load>
            </dc:group>
          </sediment-load>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="bedheight-differences">
      <dc:filter expr="($out_name = 'bed_difference_year' or $out_name = 'bed_difference_epoch') and
        (starts-with($facet_name, 'bedheight_difference.year') or starts-with($facet_name, 'bedheight_difference.epoch'))">
        <dc:if test="dc:has-result()">
          <bedheight-differences>
            <dc:group expr="concat($river, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <difference description="{dc:group-key()}">
                <dc:for-each>
                  <dc:element name="${facet_name}">
                    <dc:attribute name="factory" value="minfo"/>
                    <dc:attribute name="target_out" value="${out}"/>
                    <dc:attribute name="description" value="${facet_description}"/>
                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                    <dc:attribute name="artifact-id" value="${a_id}"/>
                    <dc:attribute name="out" value="${out}"/>
                  </dc:element>
                </dc:for-each>
              </difference>
            </dc:group>
          </bedheight-differences>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="waterlevels-discharge">
      <dc:filter expr="$out_name = 'discharge_longitudinal_section' and $facet_name = 'discharge_longitudinal_section.w">
        <dc:if test="dc:has-result()">
          <waterlevels-discharge>
            <dc:group expr="concat($oid, ' ', $river, ' ', $a_id, ' ', dc:date-format('dd.MM.yyyy - H:m:s', $a_creation), ' ', $collection_name)">
              <discharge description="{dc:group-key()}">
                <dc:for-each>
                  <dc:element name="${facet_name}">
                    <dc:attribute name="factory" value="winfo"/>
                    <dc:attribute name="target_out" value="${out}"/>
                    <dc:attribute name="description" value="${facet_description}"/>
                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
                    <dc:attribute name="artifact-id" value="${a_id}"/>
                    <dc:attribute name="out" value="longitudinal_section"/>
                  </dc:element>
                </dc:for-each>
              </discharge>
            </dc:group>
          </waterlevels-discharge>
        </dc:if>
      </dc:filter>
    </dc:macro>
    <!-- Macros to load system data -->

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

    <dc:macro name="mainvalues">
      <wmainvalue factory="mainvalue" ids="{$river_id}:w" target_out="{$out}" />
      <qmainvalue factory="mainvalue" ids="{$river_id}:q" target_out="{$out}" />
    </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" target_out="{$out}" />
          </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" target_out="{$out}" />
          </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" target_out="{$out}"
                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" target_out="{$out}"
                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" target_out="{$out}"
                          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" target_out="{$out}"
                          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" target_out="{$out}"
                          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" target_out="{$out}"
                          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}" target_out="{$out}" />
          <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" target_out="{$out}"
                        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" target_out="{$out}"
                          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" target_out="{$out}"
                          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" target_out="{$out}"
                          info="{$info} [km {$deffrom} - {$defto}]"/>
                </dc:for-each>
              </relativepoint>
            </dc:group>
          </additionals>
        </dc:if>
      </dc:filter>
    </dc:macro>

    <dc:macro name="basedata_6_delta_w">
      <dc:filter expr="$kind=6">
        <dc:if test="dc:has-result()">
          <delta_w>
            <dc:group expr="$wst_description">
              <relativepoint name="{dc:group-key()}">
                <dc:for-each>
                  <column name="{$wst_column_name}"
                          ids="delta_w-wstv-{$wst_column_position}-{$wst_id}"
                          factory="staticwkms" target_out="{$out}"
                          info="{$info} [km {$deffrom} - {$defto}]"/>
                </dc:for-each>
              </relativepoint>
            </dc:group>
          </delta_w>
        </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" target_out="{$out}"
                  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}" target_out="{$out}" />
    </dc:macro>

    <dc:macro name="flood-map-recommended">
      <kilometrage>
        <riveraxis factory="riveraxis" ids="{$river_id}" target_out="{$out}" />
      </kilometrage>
      <rastermap>
        <background factory="wmsbackground" ids="{$river_id}" target_out="{$out}" />
      </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}
              AND lower(description) NOT LIKE '%epoch%'
          </dc:statement>
          <dc:for-each>
            <height factory="bedheight" target_out="{$out}"
                    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,
                   year        AS bedh_year,
                   description AS bedh_descr
            FROM bed_height_single
            WHERE river_id = ${river_id}
              AND lower(description) LIKE '%epoch%'
          </dc:statement>
          <dc:for-each>
            <height factory="bedheight" target_out="{$out}"
                    ids="bedheight-single-{$bedh_id}-{$bedh_year}"
                    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" target_out="{$out}" >
                <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" target_out="{$out}" />
                  </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" target_out="{$out}" />
                </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" target_out="{$out}"
                      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">
      <dc:filter expr="$kind = 0">
        <dc:if test="dc:has-result()">
          <basedata>
            <dc:group expr="$wst_description">
              <basedata name="{dc:group-key()}">
                <dc:for-each>
                  <dc:macro-body/>
                </dc:for-each>
              </basedata>
            </dc:group>
          </basedata>
        </dc:if>
      </dc:filter>
    </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" target_out="{$out}"
                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" target_out="{$out}"
                info="{$info} [km {$deffrom} - {$defto}]"/>
      </dc:call-macro>
    </dc:macro>

    <dc:macro name="basedata_1_additionals_macro">
      <dc:filter expr="$kind=1">
        <dc:if test="dc:has-result()">
          <additionals>
            <dc:group expr="dc:replace($wst_description, 'Zus.Längsschnitte/', '')">
              <additional name="{dc:group-key()}">
                <dc:for-each>
                  <dc:macro-body/>
                </dc:for-each>
              </additional>
            </dc:group>
          </additionals>
        </dc:if>
      </dc:filter>
    </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" target_out="{$out}"
                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" target_out="{$out}"
                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}" target_out="{$out}" />
      </kilometrage>
      <rastermap>
        <background factory="wmsbackground" ids="{$river_id}" target_out="{$out}" />
      </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 || '&lt;BR&gt;' ||
                   'Rasterweite: '   || d.resolution || 'm&lt;BR&gt;' ||
                   'Format: '  || d.format || '&lt;BR&gt;' ||
                   '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}
              AND NOT((${tokm} &lt; r.a) or (${fromkm} &gt; r.b))
            </dc:statement>
            <dc:for-each>
            <dem factory="demfactory" target_out="{$out}"
              ids="{$dem_id}"
              name="{$name}"
              info="{$info}{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}" target_out="{$out}" />
          </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" target_out="{$out}"
                  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" target_out="{$out}"
                  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" target_out="{$out}"
                    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" target_out="{$out}"
                          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" target_out="{$out}"
                          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" target_out="{$out}"
                      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" target_out="{$out}"
                            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" target_out="{$out}"
                            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" target_out="{$out}"
                            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" target_out="{$out}"
                            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" target_out="{$out}"
                          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" target_out="{$out}"
                        description="{$kind_name}"
                        ids="{$river_id};{$kind_name};{$kind_id}"/>
              </dc:for-each>
            </dc:filter>
            <dc:filter expr="$kind_id != 1">
              <dc:if test="dc:has-result()">
                <other>
                  <dc:for-each>
                    <floody factory="wmsfloodplainfactory" target_out="{$out}"
                            description="{$layer_name}"
                            ids="{$river_id};{$layer_name};{$kind_id}"/>
                  </dc:for-each>
                </other>
              </dc:if>
            </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" target_out="{$out}"
               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" target_out="{$out}"
               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" target_out="{$out}"
                ids="{$river_id};{$year};{$year}"/>
            </dc:for-each>
          </floodmarks>
        </dc:if>
      </dc:context>
    </dc:macro>

    <dc:macro name="catchments">
      <catchments>
        <catchment_wms factory="externalwmsfactory" target_out="{$out}"
                       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)" target_out="{$out}" />
        <gauge_names factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelnamen;Pegelnamen (WSV)" target_out="{$out}" />
        <gauge_level factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelwasserstand;Aktueller Wasserstand (WSV)" target_out="{$out}" />
        <gauge_tendency factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;TendenzWasserstand;Tendenz des Wasserstands (WSV)" target_out="{$out}" />
      </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" target_out="{$out}" />
              </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" target_out="{$out}" />
                </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}" target_out="{$out}" />
        </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" target_out="{$out}"
            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" target_out="{$out}"
                        ids="{$river_id};{$kind_name};{$kind_id}"/>
              </dc:for-each>
            </dc:filter>
            <dc:filter expr="$kind_id = 0">
              <dc:if test="dc:has-result()">
                <other>
                  <dc:for-each>
                    <misc-qps description="{$layer_name}"
                              factory="wmsqpsfactory" target_out="{$out}"
                              ids="{$river_id};{$layer_name};{$kind_id}"/>
                  </dc:for-each>
                </other>
              </dc:if>
            </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" target_out="{$out}"
                           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" target_out="{$out}"
                             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" target_out="{$out}"
                       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 -------------------------------">
      <!-- =============== THIS IS BROKEN! ============== -->
      <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="target_out" value="${out}"/>
                    <dc:attribute name="out" value="${out}"/>
                  </dc:element>
                </dc:for-each>
              </dc:context>
            </dc:for-each>
          </dc:context>
        </dc:for-each>
      </officiallines>
    </dc:comment>
    </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,
                 COALESCE(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="all-user-artifacts">
        <dc:context connection="user">
        <dc:comment>Select collections and masterartifacts.</dc:comment>
        <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,
                 o.name                        AS out_name,
                 o.id                          AS out_id,
                 f.name                        AS facet_name,
                 f.num                         AS facet_num,
                 f.description                 AS facet_description,
                 f.id                          AS fid
          FROM users u
            JOIN collections c             ON c.user_id = u.id
            JOIN master_artifacts_range ma ON ma.collection_id = c.id
            JOIN outs o                    ON o.artifact_id = ma.id
            JOIN facets f                  ON f.out_id = o.id
          WHERE  u.gid = CAST(${user-id} AS UUID)
            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:virtual-column name="deffrom" type="number" expr="dc:fromValue($ld_m, $ld_l, $ld_f)">
          <dc:virtual-column name="defto" type="number" expr="dc:toValue($ld_m, $ld_l, $ld_t)">
            <dc:macro-body/>
          </dc:virtual-column>
        </dc:virtual-column>
      </dc:context>
    </dc:macro>

    <dc:macro name="km-filtered-user-artifacts">
      <dc:filter expr="not($deffrom &gt; $tokm or $defto &lt; $fromkm)">
        <dc:macro-body/>
      </dc:filter>
    </dc:macro>

    <dc:macro name="artifact-range">
      <dc:variable name="fromkm" type="number" expr="dc:fromValue(dc:get('ld_mode'), dc:get('ld_locations'), dc:get('ld_from'))"/>
      <dc:variable name="tokm" type="number" expr="dc:toValue(dc:get('ld_mode'), dc:get('ld_locations'), dc:get('ld_to'))"/>
      <dc:macro-body/>
    </dc:macro>

  </datacage>
</dc:template>

http://dive4elements.wald.intevation.org