I am doing a viewer with geotools and I am trying to read a kml file. I am using PullParser class for parsing and also I am using KML and KMLConfiguration classes from org.geotools.kml.v22 package. If I use a kml file with just one geometry, the code works, but when I try with a file with more than one point, it read only the first coordinates, even though it capture all the names of the points. I don't know how to make possible to read more than one point of the same file Do you know how can I solve this?
Here is the code:
void readKML(File file){
fis = new FileInputStream(file);
PullParser parser = new PullParser(new KMLConfiguration(), fis, KML.Placemark);
int index = 0;
DefaultGeographicCRS crs = DefaultGeographicCRS.WGS84;
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
typeBuilder.setName(file.getName());
typeBuilder.setCRS(crs);
typeBuilder.add("name", String.class);
typeBuilder.add("the_geom", Geometry.class);
SimpleFeatureType type = typeBuilder.buildFeatureType();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
DefaultFeatureCollection newCollection = new DefaultFeatureCollection();
StyleMap.GeomType geom = null;
SimpleFeature f = null;
while ((f = ((SimpleFeature) parser.parse())) != null) {
String geometryDesc = f.getDefaultGeometry().toString();
//This function is for making the style
geom = getGeometryType(geometryDesc);
Geometry geometry = (Geometry) f.getDefaultGeometry();
Object nameAttribute = null;
nameAttribute = f.getAttribute("name");
builder.addAll(new Object[]{nameAttribute, geometry});
SimpleFeature feature = builder.buildFeature(String.valueOf(index++));
newCollection.add(feature);
}
}
I based on this reference: How to parse KML data using geotools? and also on this code: http://direct.massapi.com/source/github/17/47/1747678994/plugins/org.locationtech.udig.catalog.kml/src/main/java/org/locationtech/udig/catalog/kml/core/KmlUtils.java.html#85