3

My application is trying to export a KML file. The file should contain a series of polygons that represent sales territories. The territories cover the whole of the US.

I've followed the instruction for creating polygons on the KML tutorial. The file output from my application can be found here. I've tried to import this into QGIS and MyMaps. Both application parse the file but only show some of the territories.

My question is what am I missing? What are the minimum fields required to show multiple shapes with multiple polygons (and holes).

Steve Maughan
  • 203
  • 1
  • 7
  • The documentation contains valid samples. If you are having difficulty with a specific small file, post it in a code block here. – Vince Oct 31 '17 at 12:11
  • Have you tried to validate the KML output against the KML schema? When I try in my parser I get lots of errors like: cvc-complex-type.2.4.a: Invalid content was found starting with element '{"http://www.opengis.net/kml/2.2":outerBoundaryIs}'. One of '{"http://www.opengis.net/kml/2.2":innerBoundaryIs, "http://www.opengis.net/kml/2.2":PolygonSimpleExtensionGroup, "http://www.opengis.net/kml/2.2":PolygonObjectExtensionGroup}' is expected. – nmtoken Oct 31 '17 at 12:39

2 Answers2

2

The issue with your test.kml file is that it contains multiple <outerBoundaryIs> elements per <Polygon>.

If you look at the XSD schema for KML (that is the definition of what's allowed/required in a KML file) you can see that only one such element is allowed per Polygon element:

<element name="Polygon" type="kml:PolygonType" substitutionGroup="kml:AbstractGeometryGroup"/>
<complexType name="PolygonType" final="#all">
<complexContent>
  <extension base="kml:AbstractGeometryType">
    <sequence>
      <element ref="kml:extrude" minOccurs="0"/>
      <element ref="kml:tessellate" minOccurs="0"/>
      <element ref="kml:altitudeModeGroup" minOccurs="0"/>
      <element ref="kml:outerBoundaryIs" minOccurs="0"/>
      <element ref="kml:innerBoundaryIs" minOccurs="0"
        maxOccurs="unbounded"/>
      <element ref="kml:PolygonSimpleExtensionGroup" minOccurs="0"
        maxOccurs="unbounded"/>
      <element ref="kml:PolygonObjectExtensionGroup" minOccurs="0"
        maxOccurs="unbounded"/>
    </sequence>
  </extension>
</complexContent>

I'm not an KML expert, but looking at the reference/schema, I think you probably need to use <MultiGeometry> and not <Polygon> to enclose all your <LinearRing><coordinates>... sections per Placemark, so you would have something like:

   <Placemark>
        <name>305</name>
        <MultiGeometry>
            <LinearRing>
                <coordinates> -65.525367,18.356347,10 ...</coordinates>
            </LinearRing>
            <LinearRing>
                <coordinates> -65.281155,18.141504,10 ...</coordinates>
            </LinearRing>
            <LinearRing>
                <coordinates> -67.835002,18.114775,...</coordinates>
            </LinearRing>
            <LinearRing>
                <coordinates> -65.227244,18.318174,10 ...
                </coordinates>
            </LinearRing>
            <LinearRing>
                <coordinates> -66.513506,17.902906,10 -66.509611,17.898928,10
                    -66.529663,17.883756,10 -66.526144,17.893284,10 -66.513506,17.902906,10
                </coordinates>
            </LinearRing>
            <LinearRing>
                <coordinates> -67.477922,18.390544,10 ...</coordinates>
            </LinearRing>
        </MultiGeometry>
    </Placemark>
nmtoken
  • 13,355
  • 5
  • 38
  • 87
2

I figured it out. It's not complicated but it's also not well documented:

Simple Polygon has this structure:

<Polygon>
  <outerBoundary>
    <LinearRing>
      <coordinates>
        ...
      </coordinates>
    </LinearRing>
  </outerBoundary>
</Polygon>

A polygon with a hole has this format (there can be multiple innerBoundaries):

<Polygon>
  <outerBoundary>
    <LinearRing>
      <coordinates>
        ...
      </coordinates>
    </LinearRing>
  </outerBoundary>
  <innerBoundary>
    <LinearRing>
      <coordinates>
        ...
      </coordinates>
    </LinearRing>
  </innerBoundary>
</Polygon>

And a multi-part shapes has this format:

<MultiGeometry>
  <Polygon> 
    ...
  </Polygon>
  <Polygon> 
    ...
  </Polygon>
</MultiGeometry>
Steve Maughan
  • 203
  • 1
  • 7