How to Highlight KML Icons & Labels

Liz Sanderson
Liz Sanderson
  • Updated

FME Version

Introduction

In Google KML, it is possible to control the visibility of feature labels and icons using KML StyleMap elements. To create highlighted icons, it is required to define two styles, normal and highlight. With FME, you need to create a dummy feature and then set up two StyleMaps using the KMLStyler transformers. These transformers create <StyleMap> elements that have two key/value pairs that map each icon style to an icon state. An example of how to control the visibility of features’ labels is shown in the demo workspace. For more information about StyleMaps, see the Google Developers KML Reference
 

Step-by-step Instructions

1. Create Normal and Highlight StyleMaps
Open FME Workbench, then start a blank workspace. First, we need to create a dummy feature to create the StyleMaps. Add a Creator to the canvas then connect two KMLStylers to it.
KMLStylers.png

The first KMLStyler will be the normal style. This KMLStyler will create the StyleMap attribute which will be the normal style of the features when viewed in Google Earth. In the KMLStyler parameters, set Allow Unique Styles Per Feature to No, then expand the Icon section and set the Name to C2. Confirm that the Scale for both the Icon and Label are set to 0.5. Finally, to set the Style ID, change the Transformer Name to mystyle-normal, this will be referenced when we create the StyleMap attributes. 
KMLNormal.png

 
The second KMLStyler, will be the style of the features when one hovers over the icon with the cursor in Google Earth. In the KMLStyler_2 parameters, set Allow Unique Styles Per Feature to No, then expand the Icon section and set Icon Name to C2 with a Scale of 0.7. Next, expand the Label section and set the Scale to 1.0. Finally, to set the Style ID, change the Transformer Name to mystyle-highlight, this will be referenced when we create the StyleMap attributes. 
KMLStyler1.png
 
2. Write Out Style
Now let’s write out our KML Style to wrap up this section of the workspace. Add an OGC/Google KML Writer to the canvas and browse to a location to save the dataset. Name the file HighlightOnHover.kml, then change the Feature Type Definition to Manual and click OK. 
StyleWriter.png

In the Feature Type dialog, set the Feature Type Name to Style then click OK. Connect the Style writer feature type to both KMLStylers. 
StyleFTConnection.png
 

3. Add FoodVendors
Now we will create the point features to attach the StyleMaps to. Add an Esri Geodatabase (File Geodb Open API( reader to the canvas. Browse to the CommunityMapping.gdb dataset that is available to download from the Files section on this article. Click OK to add the reader. 
GdbReader.png

In the Select Feature Types dialog, select only FoodVendors then click OK. 
SelectFT.png

4. Reproject Data
Google Earth and the OGC KML format only accepts coordinate system in latitude and longitude. Add a CsmapReprojector to the canvas and connect it to the FoodVendors reader feature type. In the parameters, set the Destination Coordinate System to LL84, then click OK. 
Reprojector.png
 
5. Create KML StyleMap Attributes
Now we are going to use the AttributeCreator to create the StyleMap attributes, these attributes assign the created StyleMaps to the FoodVendor point features. Add an AttributeCreator to the canvas and connect it to the CsmapReprojector. In the parameters, create the following attributes:

Attribute Name Value
kml_target_style_normal mystyle-normal
kml_target_style_highlight mystyle-highlight


AttributeCreator.png


6. Set Values for FoodVendor Labels
Now we need to create the labels for each of the FoodVendors. Add a KMLPropertySetter to the canvas and connect it to the AttributeCreator. In the parameters, set the Name to VendorName, then set Include Attribute Table to No. 
KMLPropSet.png
 
7. Write Out FoodVendors
We will write out the FoodVendors to the same KML writer we used for Style. On the top menu bar go to Writers > Add Feature Type. 
AddFT.png
 
In the Feature Type dialog, set the Feature Type Name to FoodVendors, then click OK. Connect the FoodVendors writer feature type to the KMLPropertySetter.
FoodVendorsConnection.png

After connecting the FoodVendors writer feature type, if you reopen the parameters and switch to the User Attributes tab, you’ll notice that the attributes we created with the AttributeCreator are not there. This is because they are format attributes, switch to the Format Attributes tab to see these attributes. 
FormatAttributes.png
 
8. Run Workspace and View Output in Google Earth
Run the workspace, then view the output in Google Earth. Hover over an icon to see the style change. 
GoogleEarth.png

 

Data Attribution

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. 

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.