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?