Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building large KML file

I generate KML files which may have 50,000 placemarks or more, arranged in Folders based on a domain-specific grouping. The KML file uses custom images which are packed in to a KMZ file.

I'm looking to breakup the single KML file in to multiple files, partitioned based on the grouping, so rather than having 1 large document with folders, i'd have a root/index KML file with folders linking to the smaller KML files.

Is this possible though? I think that a KMZ file can contain only 1 KML file, regardless of where it's located or its name, in the zip. Furthermore, I'm not exactly sure how a KML file can link to another KML file. Is the only way to have it as a <NetworkLink> to a local file? Can a <NetworkLink> work to link to a file local in the same KMZ?

like image 698
Stealth Rabbi Avatar asked Dec 16 '22 09:12

Stealth Rabbi


1 Answers

By design you can have multiple KML files within a single KMZ file. That way you can bundle many KML files in a single KMZ file that is downloaded, sent via e-mail or accessed offline.

Google Earth can scale to a large number of features (50,000 or much larger) in a single KMZ file if the features are split into multiple KML files and the KML is defined such that all the sub-KML files are not displayed at once. KML provides mechanisms to control which features or sub-KML files are displayed using time, region, or altitude level filtering.

Large KML files can scale using any of the following techniques:

  1. NetworkLinks
  2. Regions
  3. Folder Radio style or explicit visiblity=0
  4. Number of Points per feature and geometry simplification

NetworkLinks

You can have any level of NetworkLinks from within your root KML file from flat (single KML file with Networklinks to all other KML files within the KMZ) to deep (with each KML file with a NetworkLink to other KML files each with its own NetworkLink). Depends on how you need to structure your KML and how large the data is.

The key is that Google Earth chooses the first KML as the root KML file so you must ensure that the first file (typically named doc.kml) is the root KML file that loads the other KML files via network links. A common structure is to include additional KML files in a "kml" sub-folder to differentiate it from the root KML file.

Here's a KMZ example with 4 file entries: root KML file (doc.kml) that contains a NetworkLink to "kml/sub1.kml" and another to "kml/sub2.kml", which in turn has a NetworkLink to "sub3.kml" also in "kml" sub-folder.

== test.kmz ==

+doc.kml
   NetworkLink > kml/sub1.kml
   NetworkLink > kml/sub2.kml
+kml/sub1.kml
+kml/sub2.kml
   NetworkLink > sub3.kml
+kml/sub3.kml

Here is the structure of such a doc.kml file:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <NetworkLink>
        <name>NetworkLinked sub-item</name>
        <Link>
          <href> kml/sub1.kml </href>
        </Link>
    </NetworkLink>
    <NetworkLink>
        <name>NetworkLinked sub-item</name>
        <Link>
          <href> kml/sub2.kml </href>
        </Link>
    </NetworkLink>
    ...
  </Document>
</kml>

As a best practice if you include more than one NetworkLink with time-based features in a parent KML file then add a <TimeSpan> element to the NetworkLinks including the full range of time for that collection of features otherwise Google Earth will automatically load the entire file at startup.

<NetworkLink>
  <TimeSpan>
    <begin>2007-01-14T01:00:00Z</begin>
    <end>2007-01-14T02:00:00Z</end>
  </TimeSpan>
  <Link>
    <href>...</href>
  </Link>
</NetworkLink>

Regions

A Region affects visibility of a Placemark's geometry or an Overlay's image. Regions combined with NetworkLinks enable access to massive amounts of data in KML files. A region can optionally have a min and max altitude for altitude level filtering.

For more details, here's a tutorial on Regions in KML
https://developers.google.com/kml/documentation/regions

Radio Folders

You can further restrict what is displayed at a given time using radio folders.

Here's a radio folder example allowing the user to only choose one of the NetworkLinks at a time. This is used when the content is mutually exclusive and only one set of features should appear at any given time.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
    <Document>
        <Style id="rf">
            <ListStyle>
                <listItemType>radioFolder</listItemType>
            </ListStyle>
        </Style>
        
        <Folder>        
            <name>One at a time example</name>
            <open>1</open>
            <description>Link 1 visible by default</description>
            <styleUrl>#rf</styleUrl>
            
            <NetworkLink>
                <name>NetworkLinked sub-item-1</name>
                <Link>
                    <href> kml/sub1.kml </href>
                </Link>
            </NetworkLink>
            
            <NetworkLink>
                <name>NetworkLinked sub-item-2</name>
                <visibility>0</visibility>
                <Link>
                    <href> kml/sub2.kml </href>
                </Link>
            </NetworkLink>
            
        </Folder>
    </Document>
</kml>

Number of Points per feature and geometry simplification

Size of the KML file and number of features is not the only issue to consider. A KML file with a single hi-res polygon having 350K points and 7000 inner holes can cause Google Earth performance issues. Such geometries would need to be simplified and the number of points reduced. You can use QGIS to open a KML file then apply a simplify algorithm on the polygon. In QGIS, select Vector menu -> Geometry tools -> Simplify then save the result.

like image 197
CodeMonkey Avatar answered Feb 01 '23 20:02

CodeMonkey