Getting annotations out of the feature model

I am going to explain how to retrieve (and print) annotations of all spatial features in a feature collection. In this example we assume we are parsing a KML file, the nature of the annotations usually depends on the source data model. Whereas annotations like a name or a description are used across different encoding standards, more specific annotations like the Key-Value-Properties can only be found in certain standards like KML. This tutorial presumes understanding of the Tutorials Parsing KML into the Feature Model and Getting coordinates out of KML files.

What is an annotation in a KML file?

The spatial features in a KML file are representey by Placemarks. Each Placemark provides a lot of information, like the name or the geometry. This information is provided by KML-Tags and these tags are called annotations. So, this Tutorial helps you to read the Name and some Key-Value-Pairs out of a Placemark using annotations. An example of a annotated KML file can be found in the Tutorial Get current water levels, which describes a concrete scenarion (following the first scenario).

Printing out specific annotations

The following sample code expects an iteration over a FeatureCollection (e.g. see Getting coordinates out of KML files) and prints the value of  untyped name/value-pairs, which we call Key-Value-Property in sapience.

        public void printKeys() throws IOException {
                InputStream is = this.getClass().getResourceAsStream(file);
                
                Streams parser = new KMLStream();
                FeatureCollection collection = parser.read(is);
                
                for(Feature f : collection.listFeaturesRecursively()) {
                        List<KeyValueProperty> extendedMetadataList = f.listAnnotations(KeyValueProperty.class);
                        
                        for(KeyValueProperty kvp : extendedMetadataList) {

                                // check key of property
                                if(kvp.getKey().equalsIgnoreCase("river")) {

                                        // get value of property
                                        System.out.println(kvp.getValue());
                                }
                                
                        }
                        
                }
                

        }

In the next step, you could for example work with PegelOnline.

Written by:  Henry