<?xml version="1.0" encoding="UTF-8" standalone="no"?>
{% import 'common/iso19139-charstring.j2' as cs %}
<gmd:MD_Metadata xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gml="http://www.opengis.net/gml" xmlns:gmx="http://www.isotc211.org/2005/gmx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://www.isotc211.org/2005/gmd/gmd.xsd http://www.isotc211.org/2005/gmx http://www.isotc211.org/2005/gmx/gmx.xsd">
  <gmd:fileIdentifier>
    <gco:CharacterString>{{ record['metadata']['identifier'] }}</gco:CharacterString>
  </gmd:fileIdentifier>
  <gmd:language>
    <gmd:LanguageCode codeList="http://www.loc.gov/standards/iso639-2/" codeSpace="ISO 639-2" codeListValue="{{ record['metadata']['language'] }}">{{ record['metadata']['language'] }}</gmd:LanguageCode>
  </gmd:language>
  <gmd:characterSet>
    <gmd:MD_CharacterSetCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_CharacterSetCode" codeSpace="ISOTC211/19115" codeListValue="{{ record['metadata']['charset'] }}">{{ record['metadata']['charset'] }}</gmd:MD_CharacterSetCode>
  </gmd:characterSet>
  {% if record['metadata']['parentidentifier'] %}
  <gmd:parentIdentifier>
    <gco:CharacterString>{{ record['metadata']['parentidentifier'] }}</gco:CharacterString>
  </gmd:parentIdentifier>
  {% endif %}
  <gmd:hierarchyLevel>
    <gmd:MD_ScopeCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_ScopeCode" codeSpace="ISOTC211/19115" codeListValue="{{ record['metadata']['hierarchylevel'] }}">{{ record['metadata']['hierarchylevel'] }}</gmd:MD_ScopeCode>
  </gmd:hierarchyLevel>
  {% set contact = record['contact']['main'] %}
  {% set role = 'pointOfContact' %}
  <gmd:contact>
  {% include "contact.j2" %}
  </gmd:contact>
  <gmd:dateStamp>
    {% set datestamp = record['metadata']['datestamp']|normalize_datestring %}
    {% if datestamp|length > 11 %}
    <gco:DateTime>{{ datestamp }}</gco:DateTime>
    {% else %}
    <gco:Date>{{ datestamp }}</gco:Date>
    {% endif %}
  </gmd:dateStamp>
  <gmd:metadataStandardName>
    <gco:CharacterString>ISO 19115:2003 - Geographic information - Metadata</gco:CharacterString>
  </gmd:metadataStandardName>
  <gmd:metadataStandardVersion>
    <gco:CharacterString>ISO 19115:2003</gco:CharacterString>
  </gmd:metadataStandardVersion>
  <gmd:dataSetURI>
    <gco:CharacterString>{{ record['metadata']['dataseturi']|e }}</gco:CharacterString>
  </gmd:dataSetURI>
  {% if record['metadata']['language_alternate'] %}
  <gmd:locale>
    <gmd:PT_Locale id="locale-fr">
      <gmd:languageCode>
        <gmd:LanguageCode codeList="http://www.loc.gov/standards/iso639-2/" codeSpace="ISO 639-2" codeListValue="{{ record['metadata']['language_alternate'] }}">{{ record['metadata']['language_alternate'] }}</gmd:LanguageCode>
      </gmd:languageCode>
      <gmd:characterEncoding>
        <gmd:MD_CharacterSetCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_CharacterSetCode" codeSpace="ISOTC211/19115" codeListValue="{{ record['metadata']['charset'] }}">{{ record['metadata']['charset'] }}</gmd:MD_CharacterSetCode>
      </gmd:characterEncoding>
    </gmd:PT_Locale>
  </gmd:locale>
  {% endif %}
  <gmd:spatialRepresentationInfo>
    {% if record['spatial']['datatype'] == 'vector' %}
    <gmd:MD_VectorSpatialRepresentation>
      <gmd:topologyLevel>
        <gmd:MD_TopologyLevelCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopologyLevelCode" codeSpace="ISOTC211/19115" codeListValue="geometryOnly">geometryOnly</gmd:MD_TopologyLevelCode>
      </gmd:topologyLevel>
      <gmd:geometricObjects>
        <gmd:MD_GeometricObjects>
          <gmd:geometricObjectType>
            <gmd:MD_GeometricObjectTypeCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_GeometricObjectTypeCode" codeSpace="ISOTC211/19115" codeListValue="{{ record['spatial']['geomtype'] }}">{{ record['spatial']['geomtype'] }}</gmd:MD_GeometricObjectTypeCode>
          </gmd:geometricObjectType>
        </gmd:MD_GeometricObjects>
      </gmd:geometricObjects>
    </gmd:MD_VectorSpatialRepresentation>
    {% endif %}
  </gmd:spatialRepresentationInfo>
  <gmd:referenceSystemInfo>
    <gmd:MD_ReferenceSystem>
      <gmd:referenceSystemIdentifier>
        <gmd:RS_Identifier>
          <gmd:authority>
            <gmd:CI_Citation>
              <gmd:title>
                <gco:CharacterString>European Petroleum Survey Group (EPSG) Geodetic Parameter Registry</gco:CharacterString>
              </gmd:title>
              <gmd:date>
                <gmd:CI_Date>
                  <gmd:date>
                    <gco:Date>2008-11-12</gco:Date>
                  </gmd:date>
                  <gmd:dateType>
                    <gmd:CI_DateTypeCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode" codeSpace="ISOTC211/19115" codeListValue="publication">publication</gmd:CI_DateTypeCode>
                  </gmd:dateType>
                </gmd:CI_Date>
              </gmd:date>
              <gmd:citedResponsibleParty>
                <gmd:CI_ResponsibleParty>
                  <gmd:organisationName>
                    <gco:CharacterString>European Petroleum Survey Group</gco:CharacterString>
                  </gmd:organisationName>
                  <gmd:contactInfo>
                    <gmd:CI_Contact>
                      <gmd:onlineResource>
                        <gmd:CI_OnlineResource>
                          <gmd:linkage>
                            <gmd:URL>http://www.epsg-registry.org</gmd:URL>
                          </gmd:linkage>
                        </gmd:CI_OnlineResource>
                      </gmd:onlineResource>
                    </gmd:CI_Contact>
                  </gmd:contactInfo>
                  <gmd:role>
                    <gmd:CI_RoleCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_RoleCode" codeSpace="ISOTC211/19115" codeListValue="originator">originator</gmd:CI_RoleCode>
                  </gmd:role>
                </gmd:CI_ResponsibleParty>
              </gmd:citedResponsibleParty>
            </gmd:CI_Citation>
          </gmd:authority>
          <gmd:code>
            <gco:CharacterString>urn:ogc:def:crs:EPSG:{{ record['spatial']['crs'] }}</gco:CharacterString>
          </gmd:code>
          <gmd:version>
            <gco:CharacterString>6.18.3</gco:CharacterString>
          </gmd:version>
        </gmd:RS_Identifier>
      </gmd:referenceSystemIdentifier>
    </gmd:MD_ReferenceSystem>
  </gmd:referenceSystemInfo>
  <gmd:identificationInfo>
    <gmd:MD_DataIdentification>
      <gmd:citation>
        <gmd:CI_Citation>
          {{ cs.get_freetext('title', record['metadata']['language_alternate'], get_charstring('title', record['identification'], record['metadata']['language'], record['metadata']['language_alternate'])) }}
          {% for date_type, date in record['identification']['dates'].items() %}
          {% set datestamp = date|normalize_datestring %}
          <gmd:date>
            <gmd:CI_Date>
              <gmd:date>
                {% if datestamp|length > 11 %}
                <gco:DateTime>{{ datestamp }}</gco:DateTime>
                {% else %}
                <gco:Date>{{ datestamp }}</gco:Date>
                {% endif %}
              </gmd:date>
              <gmd:dateType>
                <gmd:CI_DateTypeCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode" codeSpace="ISOTC211/19115" codeListValue="{{ date_type }}">{{ date_type }}</gmd:CI_DateTypeCode>
              </gmd:dateType>
            </gmd:CI_Date>
          </gmd:date>
          {% endfor %}
        </gmd:CI_Citation>
      </gmd:citation>
      {{ cs.get_freetext('abstract', record['metadata']['language_alternate'], get_charstring('abstract', record['identification'], record['metadata']['language'], record['metadata']['language_alternate'])) }}
      <gmd:status>
        <gmd:MD_ProgressCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_ProgressCode" codeSpace="ISOTC211/19115" codeListValue="{{ record['identification']['status'] }}">{{ record['identification']['status'] }}</gmd:MD_ProgressCode>
      </gmd:status>
      <gmd:resourceMaintenance>
        <gmd:MD_MaintenanceInformation>
          <gmd:maintenanceAndUpdateFrequency>
            <gmd:MD_MaintenanceFrequencyCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_MaintenanceFrequencyCode" codeSpace="ISOTC211/19115" codeListValue="{{ record['identification']['maintenancefrequency'] }}">{{ record['identification']['maintenancefrequency'] }}</gmd:MD_MaintenanceFrequencyCode>
          </gmd:maintenanceAndUpdateFrequency>
        </gmd:MD_MaintenanceInformation>
      </gmd:resourceMaintenance>
      {% if record['identification']['browsegraphic'] %}
      <gmd:graphicOverview>
        <gmd:MD_BrowseGraphic>
          <gmd:fileName>
            <gco:CharacterString>{{ record['identification']['browsegraphic']|e }}</gco:CharacterString>
            </gmd:fileName>
        </gmd:MD_BrowseGraphic>
      </gmd:graphicOverview>
      {% endif %}
      {% for key, value in record['identification']['keywords'].items() %}
      <gmd:descriptiveKeywords>
        <gmd:MD_Keywords>
        {% set keywords = get_charstring('keywords', value, record['metadata']['language'], record['metadata']['language_alternate']) %}
        {% if keywords[1] is none %}
          {# No language_alternate is specified #}
          {% for kw1 in keywords %}
          {{ cs.get_freetext('keyword', None, [kw1]) }}
          {% endfor %}
        {% else %}
          {% for kw1, kw2 in zip(keywords[0], keywords[1]) %}
          {{ cs.get_freetext('keyword', record['metadata']['language_alternate'], [kw1, kw2]) }}
          {% endfor %}
        {% endif %}
          <gmd:type>
            <gmd:MD_KeywordTypeCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_KeywordTypeCode" codeSpace="ISOTC211/19115" codeListValue="{{ value['keywords_type'] }}">{{ value['keywords_type'] }}</gmd:MD_KeywordTypeCode>
          </gmd:type>
        </gmd:MD_Keywords>
      </gmd:descriptiveKeywords>
      {% endfor %}
      <gmd:resourceConstraints>
        <gmd:MD_LegalConstraints>
          <gmd:accessConstraints>
            <gmd:MD_RestrictionCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_RestrictionCode" codeSpace="ISOTC211/19115" codeListValue="{{ record['identification']['accessconstraints'] }}">{{ record['identification']['accessconstraints'] }}</gmd:MD_RestrictionCode>
          </gmd:accessConstraints>
        </gmd:MD_LegalConstraints>
      </gmd:resourceConstraints>
      <gmd:spatialRepresentationType>
        <gmd:MD_SpatialRepresentationTypeCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_SpatialRepresentationTypeCode" codeSpace="ISOTC211/19115" codeListValue="{{ record['spatial']['datatype'] }}">{{ record['spatial']['datatype'] }}</gmd:MD_SpatialRepresentationTypeCode>
      </gmd:spatialRepresentationType>
      {% if record['identification']['language'] in ['inapplicable', 'missing', 'template', 'unknown', 'withheld'] %}
      <gmd:language gco:nilReason="{{ record['identification']['language'] }}"/>
      {% else %}
      <gmd:language>
        <gmd:LanguageCode codeList="http://www.loc.gov/standards/iso639-2/" codeSpace="ISO 639-2" codeListValue="{{ record['identification']['language'] }}">{{ record['identification']['language'] }}</gmd:LanguageCode>
      </gmd:language>
      {% endif %}
      <gmd:characterSet>
        <gmd:MD_CharacterSetCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_CharacterSetCode" codeSpace="ISOTC211/19115" codeListValue="{{ record['identification']['charset'] }}">{{ record['identification']['charset'] }}</gmd:MD_CharacterSetCode>
      </gmd:characterSet>
      {% for tc in record['identification']['topiccategory'] %}
      <gmd:topicCategory>
        <gmd:MD_TopicCategoryCode>{{ tc }}</gmd:MD_TopicCategoryCode>
      </gmd:topicCategory>
      {% endfor %}
      <gmd:extent>
        {% set bbox = record['spatial']['bbox'].split(',') %}
        <gmd:EX_Extent>
          <gmd:geographicElement>
            {% if record['spatial']['crs'] != 4326 %}
            <gmd:EX_BoundingPolygon>
              <gmd:extentTypeCode>
                <gco:Boolean>1</gco:Boolean>
              </gmd:extentTypeCode>
              <gmd:polygon>
                <gml:Polygon gml:id="P001" srsName="{{ record['spatial']['crs'] }}" srsDimension="2">
                  <gml:exterior>
                    <gml:LinearRing>
                      <gml:posList srsName="{{ record['spatial']['crs'] }}" srsDimension="2">{{ bbox[0] }} {{ bbox[1] }} {{ bbox[0] }} {{ bbox[3] }} {{ bbox[2] }} {{ bbox[3] }} {{ bbox[2] }} {{ bbox[1] }} {{ bbox[0] }} {{ bbox[1] }}</gml:posList>
                    </gml:LinearRing>
                  </gml:exterior>
                </gml:Polygon>
              </gmd:polygon>
            </gmd:EX_BoundingPolygon>
            {% else %}
            <gmd:EX_GeographicBoundingBox>
              <gmd:extentTypeCode>
                <gco:Boolean>1</gco:Boolean>
              </gmd:extentTypeCode>
              <gmd:westBoundLongitude>
                <gco:Decimal>{{ bbox[0] }}</gco:Decimal>
              </gmd:westBoundLongitude>
              <gmd:eastBoundLongitude>
                <gco:Decimal>{{ bbox[2] }}</gco:Decimal>
              </gmd:eastBoundLongitude>
              <gmd:southBoundLatitude>
                <gco:Decimal>{{ bbox[1] }}</gco:Decimal>
              </gmd:southBoundLatitude>
              <gmd:northBoundLatitude>
                <gco:Decimal>{{ bbox[3] }}</gco:Decimal>
              </gmd:northBoundLatitude>
            </gmd:EX_GeographicBoundingBox>
            {% endif %}
          </gmd:geographicElement>
          <gmd:temporalElement>
            <gmd:EX_TemporalExtent>
              <gmd:extent>
                <gml:TimePeriod gml:id="T001">
                  <gml:beginPosition>{{ record['identification']['temporal_begin'] }}</gml:beginPosition>
                  {% if record['identification']['temporal_end'] == 'now' %}
                  <gml:endPosition indeterminatePosition="now"/>
                  {% else %}
                  <gml:endPosition>{{ record['identification']['temporal_end'] }}</gml:endPosition>
                  {% endif %}
                </gml:TimePeriod>
              </gmd:extent>
            </gmd:EX_TemporalExtent>
          </gmd:temporalElement>
        </gmd:EX_Extent>
      </gmd:extent>
      {% if record['identification']['url'] %}
      {{ cs.get_freetext('supplementalInformation', record['metadata']['language_alternate'], get_charstring('url', record['identification'], record['metadata']['language'], record['metadata']['language_alternate'])) }}
      {% endif %}
    </gmd:MD_DataIdentification>
  </gmd:identificationInfo>
  <gmd:distributionInfo>
    <gmd:MD_Distribution>
      {% for k, v in record['distribution'].items() %}
      {% if v['format'] and v['format_version'] %}
      <gmd:distributionFormat>
        <gmd:MD_Format>
          <gmd:name>{{ v['format'] }}</gmd:name>
          <gmd:version>{{ v['format_version'] }}</gmd:version>
        </gmd:MD_Format>
      </gmd:distributionFormat>
      {% endif %}
      {% endfor %}
      <gmd:distributor>
        <gmd:MD_Distributor>
          <gmd:distributorContact>
          {% set contact = record['contact']['distribution'] %}
          {% set role = 'distributor' %}
          {% include "contact.j2" %}
          </gmd:distributorContact>
        </gmd:MD_Distributor>
      </gmd:distributor>
      <gmd:transferOptions>
        <gmd:MD_DigitalTransferOptions>
        {% set formats = prune_transfer_option(record['distribution'], record['identification']['language']) %}
        {% for v in formats %}
          <gmd:onLine>
            <gmd:CI_OnlineResource>
              <gmd:linkage>
                <gmd:URL>{{ v['url']|e }}</gmd:URL>
              </gmd:linkage>
              <gmd:protocol>
                <gco:CharacterString>{{ v['type'] }}</gco:CharacterString>
              </gmd:protocol>
              {{ cs.get_freetext('name', record['metadata']['language_alternate'], get_charstring('name', v, record['metadata']['language'], record['metadata']['language_alternate'])) }}
              {{ cs.get_freetext('description', record['metadata']['language_alternate'], get_charstring('description', v, record['metadata']['language'], record['metadata']['language_alternate'])) }}
              <gmd:function>
                <gmd:CI_OnLineFunctionCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_OnLineFunctionCode" codeSpace="ISOTC211/19115" codeListValue="{{ v['function'] }}">{{ v['function'] }}</gmd:CI_OnLineFunctionCode>
              </gmd:function>
            </gmd:CI_OnlineResource>
          </gmd:onLine>
        {% endfor %}
        </gmd:MD_DigitalTransferOptions>
      </gmd:transferOptions>
    </gmd:MD_Distribution>
  </gmd:distributionInfo>
  <gmd:metadataMaintenance>
    <gmd:MD_MaintenanceInformation>
      <gmd:maintenanceAndUpdateFrequency>
        <gmd:MD_MaintenanceFrequencyCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_MaintenanceFrequencyCode" codeSpace="ISOTC211/19115" codeListValue="{{ record['identification']['maintenancefrequency'] }}">{{ record['identification']['maintenancefrequency'] }}</gmd:MD_MaintenanceFrequencyCode>
      </gmd:maintenanceAndUpdateFrequency>
      <gmd:maintenanceNote>
        <gco:CharacterString>This metadata record was generated by pygeometa-{{ pygeometa_version }} (https://github.com/geopython/pygeometa)</gco:CharacterString>
      </gmd:maintenanceNote>
    </gmd:MD_MaintenanceInformation>
  </gmd:metadataMaintenance>
</gmd:MD_Metadata>
