Skip to main content

I have a kml file that is generated by FME that I use as a layer in Google Maps. The auto generated kml file has a lot of kml features/elements that are not required for the display, making the kml file unnecessary bulky.

 

Is there a way to remove such features? I have done this by hand and can confirm that the kml file still displays correctly in Google Maps.

 

I assume that the XML Updater should be used for this, but I have not been able to get the required results. I most probably did something wrong in my implementation with my limited understanding of how it is supposed to be used. Perhaps there is a way to do this with the kml transformers, which I was not able to find. Any inputs on how to resolve this would be much appreciated.

As an example, say you have the following (sample) kml file where you would like to remove elements/features.

<?xml version="1.0" encoding="UTF-8"?>

<!-- Generated by FME 2018.0.0.0 (Build 18284) -->

<kml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://www.opengis.net/kml/2.2">

<Document>

<name>lines</name>

<visibility>1</visibility>

<Schema name="abc" id="kml_schema_ft_abc">

<SimpleField type="xsd:string" name="Field1">

<displayName>Field1</displayName>

</SimpleField>

<SimpleField type="xsd:string" name="Field2">

<displayName>Field2</displayName>

</SimpleField>

</Schema>

<Schema name="def" id="kml_schema_ft_def">

<SimpleField type="xsd:string" name="Field1">

<displayName>Field1</displayName>

</SimpleField>

<SimpleField type="xsd:string" name="Field2">

<displayName>Field2</displayName>

</Schema>

<Folder id="kml_ft_def">

<name>def</name>

<Placemark id="kml_1">

<name>kml_1</name>

<snippet> </snippet>

<description><!;CDATAT<center><table><tr><th colspan='2' align='center'><em>Attributes</em></th></tr><tr bgcolor="#E3E3F3">

<th>Field1</th>

<td>Field1 result here</td>

</tr><tr bgcolor="">

<th>Field2</th>

<td>Field2 result here</td>

</tr><tr bgcolor="#E3E3F3">

</tr></table></center>]]></description>

<ExtendedData>

<SchemaData schemaUrl="#kml_schema_ft_def">

<SimpleData name="Field1">Field1 result here</SimpleData>

<SimpleData name="Field2">Field2 result here</SimpleData>

</SchemaData>

</ExtendedData>

<LineString>

<coordinates> <!-- set of coordinates here --></coordinates>

</LineString>

</Placemark>

<Placemark id="kml_2">

<name>kml_2</name>

<snippet> </snippet>

<description><!;CDATAT<center><table><tr><th colspan='2' align='center'><em>Attributes</em></th></tr><tr bgcolor="#E3E3F3">

<th>Field1</th>

<td>Field1 result here</td>

</tr><tr bgcolor="">

<th>Field2</th>

<td>Field2 result here</td>

</tr><tr bgcolor="#E3E3F3">

</tr></table></center>]]></description>

<ExtendedData>

<SchemaData schemaUrl="#kml_schema_ft_def">

<SimpleData name="Field1">Field1 result here</SimpleData>

<SimpleData name="Field2">Field2 result here</SimpleData>

</SchemaData>

</ExtendedData>

<LineString>

<coordinates> <!-- set of coordinates here --></coordinates>

</LineString>

</Placemark>

</Folder>

</Document>

</kml>

 

In this case, I would like to remove the Schema and ExtendedData features/attributes from this file. The output should thus look as follows.

<?xml version="1.0" encoding="UTF-8"?>

<!-- Generated by FME 2018.0.0.0 (Build 18284) -->

<kml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://www.opengis.net/kml/2.2">

<Document>

<name>lines</name>

<visibility>1</visibility>

<Folder id="kml_ft_def">

<name>def</name>

<Placemark id="kml_1">

<name>kml_1</name>

<snippet> </snippet>

<description><!tCDATAA<center><table><tr><th colspan='2' align='center'><em>Attributes</em></th></tr><tr bgcolor="#E3E3F3">

<th>Field1</th>

<td>Field1 result here</td>

</tr><tr bgcolor="">

<th>Field2</th>

<td>Field2 result here</td>

</tr><tr bgcolor="#E3E3F3">

</tr></table></center>]]></description>

<LineString>

<coordinates> <!-- set of coordinates here --></coordinates>

</LineString>

</Placemark>

<Placemark id="kml_2">

<name>kml_2</name>

<snippet> </snippet>

<description><!tCDATAA<center><table><tr><th colspan='2' align='center'><em>Attributes</em></th></tr><tr bgcolor="#E3E3F3">

<th>Field1</th>

<td>Field1 result here</td>

</tr><tr bgcolor="">

<th>Field2</th>

<td>Field2 result here</td>

</tr><tr bgcolor="#E3E3F3">

</tr></table></center>]]></description>

<LineString>

<coordinates> <!-- set of coordinates here --></coordinates>

</LineString>

</Placemark>

</Folder>

</Document>

</kml>

 

According to what I understand, the XMLUpdater should link to the Document feature of the KML file and write out another KML Document file.

 

The XMLUpdater has been setup as follows to remove the Schema feature/attribute.

 

I have also tried setting the XML Path to /*:Shema, but this did not work either. The resulting KML file's structure is completely changed to something like this.

 

<?xml version="1.0" encoding="UTF-8"?>

 

<kml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://www.opengis.net/kml/2.2">

<Document id="kml_id_small_sample_data-original_1.1">

<name>kml_id_small_sample_data-original_1.1</name>

</Document></kml>

 

Any reason what I am doing wrong? I am probably overcomplicating the problem and should have been able to remove unnecessary kml elements/features inside the workflow where the KML file was generated, but I don't know how. :)

 

Any help is much appreciated.

In the XMLUpdater add 2 operations for Delete content, one for Schema and another for ExtendedData.

You can read more here https://www.w3schools.com/xml/xpath_syntax.asp

Bonus: if you write the kml whith FME you can use a FeatureWriter instead of the normal writer, and use the Dataset Attribute in the XML File field in XMLUpdater.


In the XMLUpdater add 2 operations for Delete content, one for Schema and another for ExtendedData.

You can read more here https://www.w3schools.com/xml/xpath_syntax.asp

Bonus: if you write the kml whith FME you can use a FeatureWriter instead of the normal writer, and use the Dataset Attribute in the XML File field in XMLUpdater.

Thank you for your comment and apologies for the late reply. I tried your steps, but this did not work. I did however find a solution which I will post below my initial question.


I found the following workaround to remove KML tags with the XMLUpdater transformer. Firstly, it does not seem that the XMLUpdater correctly parses a KML file that enters it (and hence provides the output as seen in my original question). A workaround, is to first read the KML file with a FeatureReader, but (very important) setting the Reader Format of the KML file to "Text File", not KML, and then setting the "Feature Types to Read" to "Document".

This then feeds into the Documents input of the XMLUpdater.

In the XMLUpdater, set the XML Input as XML File and point to the file location. In the Update section, set the Update Type to "Delete" and reference the path of the KML features.

I have noticed that the XMLUpdater incorrectly changes some of the KML tags in the process (ie. "<" becomes "&lt" and so on). I fixed this by doing some string replacements after the XMLUpdater, which completed the steps I needed to have a working KML file with reduced tags.

Perhaps I am missing the obvious on how to manipulate KML files generated with FME, but this solution worked for me. Hopefully this may help someone else with a similar issue.


I found the following workaround to remove KML tags with the XMLUpdater transformer. Firstly, it does not seem that the XMLUpdater correctly parses a KML file that enters it (and hence provides the output as seen in my original question). A workaround, is to first read the KML file with a FeatureReader, but (very important) setting the Reader Format of the KML file to "Text File", not KML, and then setting the "Feature Types to Read" to "Document".

This then feeds into the Documents input of the XMLUpdater.

In the XMLUpdater, set the XML Input as XML File and point to the file location. In the Update section, set the Update Type to "Delete" and reference the path of the KML features.

I have noticed that the XMLUpdater incorrectly changes some of the KML tags in the process (ie. "<" becomes "&lt" and so on). I fixed this by doing some string replacements after the XMLUpdater, which completed the steps I needed to have a working KML file with reduced tags.

Perhaps I am missing the obvious on how to manipulate KML files generated with FME, but this solution worked for me. Hopefully this may help someone else with a similar issue.


Reply