0

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

1
  • can you add a link to your KML file?
    – Ian Turton
    Commented Feb 29, 2016 at 9:00

1 Answer 1

1

I've just tried the following with the KML Samples file Google provide and all works fine:

private ArrayList<SimpleFeature> features;

  public ParseKML(String path) throws XMLStreamException, IOException, SAXException {
    source = new File(path);
    InputStream fis = new FileInputStream(source);
    PullParser parser = new PullParser(new KMLConfiguration(), fis, KML.Placemark);
    SimpleFeature f = null;
    features = new ArrayList<SimpleFeature>();
    while ((f = (SimpleFeature) parser.parse()) != null) {

      features.add(f);
    }
  }

  private SimpleFeatureCollection getFeatures() {
    return DataUtilities.collection(features);
  }
4
  • Hi, Thanks for you help @iant. I actually, try your and my code with the file you provides and it works fine. But it doesn't work with this file I created on Google earth sample_file. I also tried with others files I also create with Google Earth and the results are the same.
    – Giecod
    Commented Feb 29, 2016 at 20:31
  • your sample file gives me POINT (6.203382129855367 -75.56383925335378) punto3 POINT (6.203382129855367 -75.56383925335378) punto2 POINT (6.203382129855367 -75.56383925335378) punto1
    – Ian Turton
    Commented Mar 1, 2016 at 8:52
  • That's true. It is the same coordinates all the time, but the points are not the same. If I open the fine on Google Earth or using sublime or notepad. The coordinates are different for each point. Then, I don't know how to fix it. I also try creating a different file of points and it doesn't work.
    – Giecod
    Commented Mar 1, 2016 at 12:39
  • Oops I hadn't noticed that. I'll have another look.
    – Ian Turton
    Commented Mar 1, 2016 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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