Question

I'm creating a geopositioning application and we intent to use KML as our import/export data scructure.
We need to store extra information on field definitions, but I'm having trouble understant how to use KML SimpleFieldExtension (in fact my problem is understant XML Schema and validation).
The Google KML tutorial https://developers.google.com/kml/documentation/extendeddata doesn't teach how to do it.

I understand that SimpleFieldExtension is an abstract element and there is no concrect element in the KML specfication.

<element name="SimpleFieldExtension" abstract="true"/>

So I need to extend it and create my own, rigth?
I would like to do something like this:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"> 
<Document>

  <Schema name="mySchemaName" id="mySchemaId">       
    <SimpleField type="xsd:int" name="myValue">       
      <displayName>MyValue</displayName>

        <mySimpleFieldExtension>
            <someExtraInfo>...</someExtraInfo>
            <otherExtraInfo>...</otherExtraInfo>
        </mySimpleFieldExtension>  

    </SimpleField>   
  </Schema>       

  <!-- Some placemarks with myValue fields -->

 </Document> 
</kml>

When I was trying to figure this out, I came with the impression that I need to create a .xsd file with my own mySimpleFieldExtension, and some how points the .kml file to it. But I'm not sure if that is the right path.

<element name="mySimpleFieldExtension" substitutionGroup="kml:SimpleFieldExtension"/>

Can some one give me an example? Thank you in advance.

I'm using http://www.kmlvalidator.com/ to check my files.

Was it helpful?

Solution

If you want to create SimpleFieldExtension elements and validate it then you will need to create an XML Schema (.xsd) and refer that file in your KML documents.

Example XML Schema with KML extension:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
  xmlns:kml="http://www.opengis.net/kml/2.2"
  xmlns:ext="http://myextension"
  targetNamespace="http://myextension"
  elementFormDefault="qualified"
  version="2.2.0"> 

 <import namespace="http://www.opengis.net/kml/2.2"
        schemaLocation="http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd" />

  <element name="SimpleMetadata" type="ext:SimpleMetadataType"
    substitutionGroup="kml:SimpleFieldExtension"/>

  <complexType name="SimpleMetadataType" final="#all">
    <sequence>

     <element name="description" type="string"/>

     <element name="observedProperty">
        <complexType>
        <simpleContent>
          <extension base="string">
            <attribute name="type" type="string" use="required"/>
          </extension>
        </simpleContent>
      </complexType>
     </element>

      <any namespace="##other" processContents="lax" minOccurs="0"
        maxOccurs="unbounded"/>
    </sequence>
  </complexType>

</schema>

Here's KML document:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:ext="http://myextension"  
     xsi:schemaLocation="http://myextension ext.xsd
     http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd">

    <Document>

        <Schema id="SensorTypesId" name="SensorTypes">
            <SimpleField name="model" type="string"/>
            <SimpleField name="reason" type="string"/>
            <SimpleField name="speed" type="double">
            <ext:SimpleMetadata>
                <ext:description>this is the true air speed of a given
                    aircraft in meters per second</ext:description>
                <ext:observedProperty type="urn:ogc:def:phenomenon:OGC:speed" />
            </ext:SimpleMetadata>
        </SimpleField>
        </Schema>
...

A proposed example of a SimpleFieldExtension and discussion can be found here.

Note that http://www.kmlvalidator.com/ checks the strict KML specification and doesn't check KML extensions such as Google's KML extensions so you won't be able to validate custom extensions either.

You can validate such a KML document using the XML Validator which is a standalone command-line validator.

You'll need to add the namespace definition in the XML Validator ns.map config file:

http://myextension=${XV_HOME}/schemas/ext.xsd

or absolute path like this:

http://myextension=C:/myPath/ext.xsd

Even though the SimpleFieldExtension is supported by the KML standard, adding a custom SimpleFieldExtension through a custom XML Schema requires more testing to verify it doesn't cause problems to applications using it especially if you plan to share your KML outside your organization. Applications like Google Earth will simply ignore your extensions so only use extensions when you absolutely must.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top