1

We are trying to parse below KML file to retrieve simpleData and geometry.

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document><Folder><name>super_regi_icsc</name>
<Schema name="super_regi_icsc" id="super_regi_icsc">
    <SimpleField name="RecordID" type="int"></SimpleField>
    <SimpleField name="Centre_Name" type="string"></SimpleField>
    <SimpleField name="Address" type="string"></SimpleField>
    <SimpleField name="City" type="string"></SimpleField>
    <SimpleField name="State" type="string"></SimpleField>
    <SimpleField name="Format" type="string"></SimpleField>
    <SimpleField name="Scheme" type="string"></SimpleField>
    <SimpleField name="Sf" type="int"></SimpleField>
    <SimpleField name="Yr_Open_Renovate" type="string"></SimpleField>
    <SimpleField name="Owner_Leasing_Agent" type="string"></SimpleField>
    <SimpleField name="Notable_Tenants" type="string"></SimpleField>
    <SimpleField name="Geocode_Address" type="string"></SimpleField>
    <SimpleField name="Geo_Code_Address" type="string"></SimpleField>
    <SimpleField name="status" type="string"></SimpleField>
    <SimpleField name="Match_Type" type="string"></SimpleField>
    <SimpleField name="formatted_address" type="string"></SimpleField>
    <SimpleField name="Match_To" type="string"></SimpleField>
    <SimpleField name="lat" type="float"></SimpleField>
    <SimpleField name="lng" type="float"></SimpleField>
    <SimpleField name="AddCityState" type="string"></SimpleField>
</Schema>
  <Placemark>
    <ExtendedData><SchemaData schemaUrl="#super_regi_icsc">
        <SimpleData name="RecordID">100008</SimpleData>
        <SimpleData name="Centre_Name">West End Mkt</SimpleData>
        <SimpleData name="Address">1 Augusta St</SimpleData>
        <SimpleData name="City">Greenville</SimpleData>
        <SimpleData name="State">SC</SimpleData>
        <SimpleData name="Format">Malls</SimpleData>
        <SimpleData name="Scheme">Regional</SimpleData>
        <SimpleData name="Sf">45</SimpleData>
        <SimpleData name="Yr_Open_Renovate"></SimpleData>
        <SimpleData name="Owner_Leasing_Agent">Joseph Moon</SimpleData>
        <SimpleData name="Notable_Tenants">Kudzu, Emporium</SimpleData>
        <SimpleData name="Geocode_Address"></SimpleData>
        <SimpleData name="Geo_Code_Address">1 Augusta St, Greenville,  SC</SimpleData>
        <SimpleData name="status">OK</SimpleData>
        <SimpleData name="Match_Type">street_address</SimpleData>
        <SimpleData name="formatted_address">1 Augusta Street, Greenville, SC 29601, USA</SimpleData>
        <SimpleData name="Match_To">street_number</SimpleData>
        <SimpleData name="lat">34.844234900</SimpleData>
        <SimpleData name="lng">-82.404362500</SimpleData>
        <SimpleData name="AddCityState">1 Augusta St, Greenville, SC</SimpleData>
    </SchemaData></ExtendedData>
      <Point><coordinates>-82.4043625,34.8442349,0</coordinates></Point>
  </Placemark>
    <Placemark>
    <ExtendedData><SchemaData schemaUrl="#super_regi_icsc">
        <SimpleData name="RecordID">100009</SimpleData>
        <SimpleData name="Centre_Name">Bellis Fair Mall</SimpleData>
        <SimpleData name="Address">1 Bellis Fair Pkwy</SimpleData>
        <SimpleData name="City">Bellingham</SimpleData>
        <SimpleData name="State">WA</SimpleData>
        <SimpleData name="Format">Malls</SimpleData>
        <SimpleData name="Scheme">Regional</SimpleData>
        <SimpleData name="Sf">770</SimpleData>
        <SimpleData name="Yr_Open_Renovate"></SimpleData>
        <SimpleData name="Owner_Leasing_Agent">General Growth Properties</SimpleData>
        <SimpleData name="Notable_Tenants">Sears, Regal Cinemas Inc, Macys, Old Navy, Kohls</SimpleData>
        <SimpleData name="Geocode_Address"></SimpleData>
        <SimpleData name="Geo_Code_Address">1 Bellis Fair Pkwy, Bellingham,  WA</SimpleData>
        <SimpleData name="status">OK</SimpleData>
        <SimpleData name="Match_Type">street_address</SimpleData>
        <SimpleData name="formatted_address">1 Bellis Fair Parkway, Bellis Fair Mall, Bellingham, WA 98226, USA</SimpleData>
        <SimpleData name="Match_To">street_number</SimpleData>
        <SimpleData name="lat">48.785697000</SimpleData>
        <SimpleData name="lng">-122.490442000</SimpleData>
        <SimpleData name="AddCityState">1 Bellis Fair Pkwy, Bellingham, WA</SimpleData>
    </SchemaData></ExtendedData>
      <Point><coordinates>-122.490442,48.785697,0</coordinates></Point>
  </Placemark>
</Folder></Document></kml>

Below is the java Code used to parse KML file

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.geometry.Geometry;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.feature.simple.SimpleFeatureImpl;
import org.geotools.kml.KML;
import org.geotools.kml.KMLConfiguration;
//import org.geotools.kml.v22.KML;
//import org.geotools.kml.v22.KMLConfiguration;
import org.geotools.xml.Parser;
import org.geotools.xml.PullParser;
import org.geotools.xml.StreamingParser;
    public class App {
        static String path = "C:/Work/Work/Useful files/sample.kml";
        public static void main(String[] args) {
            File file = new File(path);
            FileInputStream fis = null;

            try {
                fis = new FileInputStream(file);
                InputStream iputstream = new FileInputStream(file);
                new App().kmlparser2(iputstream);
                //validateKMLType(file);
                //ParseKML();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fis != null)
                        fis.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

        public void kmlparser2(InputStream inputStream) throws Exception {
            Parser parser = new Parser(new KMLConfiguration()); 
            SimpleFeature f = (SimpleFeature) parser.parse(inputStream); 

            System.out.println(f.getClass().getName()); 
            System.out.println(f.getAttributes()); 

            Collection placemarks = (Collection) f.getAttribute("Feature"); 

            System.out.println(placemarks.getClass().getName()); 
            System.out.println(placemarks); 
            System.out.println(placemarks.size()); 

            SimpleFeature sf = (SimpleFeature)placemarks.iterator().next(); 
            printAttrs(sf.getAttributes(), ""); 
        }

        public static void printAttrs(List<Object> attrs, String tab) { 
            if (attrs == null) return; 
            tab += "  "; 

            for (Object o : attrs) { 
                if (o instanceof SimpleFeature) { 
                    SimpleFeature sf = (SimpleFeature)o; 
                    System.out.println(tab + sf.getName() + " [");
                    System.out.println("geometry : "+sf.getDefaultGeometry());
                    Set<Entry<Object, Object>> attrSet = sf.getUserData().entrySet();
                    System.out.println(attrSet);

                    printAttrs(sf.getAttributes(), tab); 
                    System.out.println(tab + " ]"); 
                } else if (o instanceof  List) { 
                    List list = (List)o; 
                    System.out.println(tab + " ["); 
                    printAttrs(list, tab); 
                    System.out.println(tab + " ]"); 
                } else { 
                    System.out.println(tab + (o==null?"":o.getClass().getSimpleName()));
                } 
            } 
        } 
    }

The attrSet from getUserData().entrySet() is null. No luck, even when v22.kml import is used. Can anyone guide me?

0

When I use the KML2.2 configuration, the extended data elements show as regular properties for me.

package spike;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.stream.XMLStreamException;

import org.geotools.data.DataUtilities;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.kml.v22.*;
import org.geotools.xml.PullParser;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.xml.sax.SAXException;

public class KMLReader {

  public static void main(String[] args) throws IOException, XMLStreamException, SAXException {
    FileInputStream reader = new FileInputStream(args[0]);
    PullParser parser = new PullParser(new KMLConfiguration(), reader, SimpleFeature.class);

    ArrayList<SimpleFeature> features = new ArrayList<>();
    SimpleFeature simpleFeature = (SimpleFeature) parser.parse();
    while (simpleFeature != null) {
      // System.out.println(simpleFeature);
      features.add(simpleFeature);
      simpleFeature = (SimpleFeature) parser.parse();
    }
    SimpleFeatureCollection fc = DataUtilities.collection(features);
    int count = 0;
    try (SimpleFeatureIterator itr = fc.features()) {
      while (itr.hasNext()) {
        System.out.println("feature " + (count++));
        SimpleFeature feature = itr.next();
        Collection<Property> attribs = feature.getProperties();
        for (Property p : attribs) {
          if (p.getValue() != null)
            System.out.println(p.getName() + ": \"" + p.getValue()+"\"");
        }
        System.out.println("Extended Data");
        Map<Object, Object> userData = feature.getUserData();
        for (Entry<Object, Object> e : userData.entrySet()) {
          System.out.println(e.getKey() + ":" + e.getValue());
        }

      }
    }
  }

}

Gives me the following output:

feature 0
visibility: "true"
open: "true"
Feature: "[]"
Extended Data
Folder:[]
feature 1
name: "super_regi_icsc"
visibility: "true"
open: "true"
name: "super_regi_icsc"
Feature: "[]"
Extended Data
Folder:[Folder (name=super_regi_icsc)]
feature 2
visibility: "true"
open: "true"
RecordID: "100009"
Centre_Name: "Bellis Fair Mall"
Address: "1 Bellis Fair Pkwy"
City: "Bellingham"
State: "WA"
Format: "Malls"
Scheme: "Regional"
Sf: "770"
Yr_Open_Renovate: ""
Owner_Leasing_Agent: "General Growth Properties"
Notable_Tenants: "Sears, Regal Cinemas Inc, Macys, Old Navy, Kohls"
Geocode_Address: ""
Geo_Code_Address: "1 Bellis Fair Pkwy, Bellingham,  WA"
status: "OK"
Match_Type: "street_address"
formatted_address: "1 Bellis Fair Parkway, Bellis Fair Mall, Bellingham, WA 98226, USA"
Match_To: "street_number"
lat: "48.785698"
lng: "-122.49044"
AddCityState: "1 Bellis Fair Pkwy, Bellingham, WA"
Geometry: "POINT (-122.490442 48.785697)"
Extended Data
Folder:[Folder (name=super_regi_icsc)]
UntypedExtendedData:{}
feature 3
visibility: "true"
open: "true"
RecordID: "100008"
Centre_Name: "West End Mkt"
Address: "1 Augusta St"
City: "Greenville"
State: "SC"
Format: "Malls"
Scheme: "Regional"
Sf: "45"
Yr_Open_Renovate: ""
Owner_Leasing_Agent: "Joseph Moon"
Notable_Tenants: "Kudzu, Emporium"
Geocode_Address: ""
Geo_Code_Address: "1 Augusta St, Greenville,  SC"
status: "OK"
Match_Type: "street_address"
formatted_address: "1 Augusta Street, Greenville, SC 29601, USA"
Match_To: "street_number"
lat: "34.844234"
lng: "-82.404366"
AddCityState: "1 Augusta St, Greenville, SC"
Geometry: "POINT (-82.4043625 34.8442349)"
Extended Data
Folder:[Folder (name=super_regi_icsc)]
UntypedExtendedData:{}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.