span8
span4
span8
span4
Q: What are KML Folders and how do I set them up?
A KML file displayed in Google Earth. The Places pane is on the left, showing two folders in the nested structure. The folder ArtNearTransit contains 74 features.
A: KML Folders Definition
KML Folders define the nested tree structure in the Google Earth Places pane (as shown above). This tree control helps organize data presentation to the user so they can turn on and off layers of interest. It also helps group features together so the user isn’t presented with a list of thousands of features. For example, instead of a hundred different zoning types, you might want to organize your zone features into zone categories such as commercial, residential, industrial, transportation and open space.
FME allows you to create KML folder structures in two ways:
1) The feature type fanout approach
2) Through the use of KML_IDs
These two approaches are intended for use in KML and not KMZ (which is a zipped KML file). In this example, we will be focusing on the feature type fanout approach. Using Vancouver’s Skytrain stations from a CSV, and Vancouver zones from a MapInfo file, we will create a KML that groups stations together by zones. This is a great way to organize features in the Google Earth legend, especially if you have lots of features that can be aggregated in a logical manner.
In a blank FME workspace, add Zones.tab as a MapInfo TAB (MAPINFO) format, keeping all default parameters.
Next, add the rapid_transit_stations.csv to your workspace, with a CSV Reader. Set the Attribute Definition to Manual and the Coordinate System to UTM84-10N. In the reader parameters, set x to Type x_coordinate, and y to y_coordinate in the Attributes section. This will tell FME to read these CSV points in as coordinate points.
Source data displayed in the Data Inspector. Skytrain stations are blue points, and polygons are zones.
We will use the PointOnAreaOverlayer transformer to merge the ZoneCategory Attributes onto the station points. Be sure to check the box for Merge Attributes in the Attribute Accumulation section of the transformer parameters.
You may have noticed that the zones don’t cover all of the train stations, so now we will clip our stations to the zone areas. Connect zones as the Clipper to the Clipper transformer, and the POINT output port of the PointOnAreaOverlayer as a Clippee.
Here we are clipping our train station points by our zone polygons since we want each station to have a zone attribute.
Attach an AttributeManager to the INSIDE output port. We will use this to rename STATION to ‘Station’, by editing it in the Output Attribute. Remove the attributes _overlaps and _clipped using the minus sign as we will no longer be needing these attributes.
So far, we have taken our train stations and clipped it to the scope of our demo. We have categories with which we want to aggregate our stations, called ZoneCategory. Next, we will use a TestFilter. Taking a quick glance at our CSV and zone data in the Data Inspector, we can see that our train stations only fall into three zones out of 7 possible ones: Commercial, Development, and Light Industrial. We will filter these out so that we can apply different styles to the output later.
Fill in the test conditions like the table below:
Test Condition | Output Port | |
If | ZoneCategory = Commercial | Commercial |
Else if | ZoneCategory = Comprehensive Development | Development |
Else | <All Other Conditions> | Light Industrial |
Connect an individual KMLStyler to each output port of the TestFilter. We will have 3 KML folders: Commercial, Comprehensive Development, and Light Industrial when we set our fanout to ZoneCategory. This step is simply for visualization so we can tell apart the points quickly. Set them to any icon or color that you desire in the Icon section. I have chosen to use the icon gx_rail, and the colors yellow, blue, and magenta, respectively.
Notice that each output of the TestFilter is connected to an individual KMLStyler to set them apart for quick visualization in Google Earth.
When writing out KML, select the feature type CSV. Accept the defaults and open up the writer parameters in your workspace. In the User Attributes tab, add two new attributes:
Name | Type | Width |
Station | kml_char | 40 |
ZoneCategory | kml_char | 40 |
To set the feature type fanout, switch to the Parameters tab in the feature type parameters. In the textbox for ‘Feature Type Name’: Type or select the attribute ZoneCategory. If the attribute isn’t visible in the dropdown menu, check that the ZoneCategory attribute that you have added to the User Attributes matches the spelling and capitalization of the ZoneCategory attribute from the source MapInfo file, Zones. For the attributes to auto-connect, it is important to pay attention to case-sensitivity.
Now that we have a set a feature type fanout on the attribute of ZoneCategory, we can expect that our train stations will be aggregated into folders by their zone categories.
Feature Type Fanout result viewed with Google Earth.
Success! Our train stations are aggregated by the Zone Categories: Commercial, Comprehensive Development, and Light Industrial. Because we have set KMLStylers, features are color coded by category for visualization purposes.
Q: What if I hadn’t known that these stations would only fall into exactly those 3 categories?
A: If we hadn’t checked the Data Inspector in advance to see that our stations only fell into three out of seven possible zone categories, we could have simply connected our writer feature type to the INSIDE output port of the Clipper. This would have given us all of our stations in as many categories (and thus, folders) as they fell into. That’s right! Creating folders can be as simple as that!
The output in Google Earth if we had attached the writer with the feature fanout right after the Clipper. Notice that because the points only fell into three categories, only 3 folders are written out, not seven.
The data used here originates from open data made available by the City of Vancouver, British Columbia. It contains information licensed under the Open Government License - Vancouver.
Hi @stewartharper, it's a useful article, thanks :)
I tried the second approach on KMZ. In my observation, looks like the kml_document attribute should be set to 'doc' always when writing a KMZ document. Is it an intentional difference between KML and KMZ? See also the attachment: kmz-folder-tree-test.fmw
© 2019 Safe Software Inc | Legal