0

I am not so seasoned at GeoTools libs, I am trying to transform a large (near 60K features) GML file. I want to change its CRS and features coordinates accordingly. The code I have is basically this:

public void parse(String crs, InputStream collection) throws Exception {
  Configuration config = new GMLConfiguration();
  StreamingParser parser = new StreamingParser(config, collection, SimpleFeature.class);
  SimpleFeature feature = (SimpleFeature) parser.parse();
  SimpleFeatureType featureType = feature.getFeatureType();
  SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
  builder.setName(featureType.getName());
  builder.setNamespaceURI(featureType.getName().getNamespaceURI());
  builder.setCRS(CRS.getAuthorityFactory(true).
    createCoordinateReferenceSystem("EPSG:4326"));
  builder.setDefaultGeometry(feature.getDefaultGeometryProperty().
    getDescriptor().getName().getLocalPart());
  Collection<Property> props = feature.getProperties();
  for (Property prop : props) {
    builder.add(prop.getName().getLocalPart(), prop.getType().getBinding());
  }
  SimpleFeatureType buildedFeatureType = builder.buildFeatureType();
  FeatureCollection<SimpleFeatureType, SimpleFeature> outCollection = FeatureCollections.newCollection();
  do {
    SimpleFeatureBuilder build = new SimpleFeatureBuilder(buildedFeatureType);
    for (AttributeType attributeType : buildedFeatureType.getTypes()) {
      Name attrName = attributeType.getName();
      Object value = feature.getProperty(attrName).getValue();
      build.add(value);
    }
    String id = feature.getIdentifier().getID();
    SimpleFeature sf = build.buildFeature(id);
    outCollection.add(sf);
  } while ((feature = (SimpleFeature) parser.parse()) != null);
}

it takes a lot of time to complete, about a minute. Is there a better solution to deal with these big feature collections? Oh, I am bound to Java 8 SE and GeoTools version 2.7.0, migrate to newer GeoTools is not an option.

UPDATE I am dealing with a GML not shapefile. Shapefile was transformed to GML in a previous step.

1

You seem to be reading a GML file not a ShapeFile that will never be fast, if you used a later version of geotools I would suggest using a ReprojectingFeatureCollection which would at least save you some boilerplate code generating the new schema but that's insignificant compared to the cost of reading 60K GML features.

On your desire to remain on GeoTools 2.7 and use Java 8 - this gives away your problem at some point in the distant past you (or someone) probably forked GeoTools and went off on their own. I can tell this because GeoTools didn't compile using Java 8 until version 11 or 12. You are now more than 10 versions behind the version of GeoTools that is being worked on. At this point you need to either move back to the main branches or fork your product and release it and try to get some developers to join you in maintaining it.

  • You are right. I am dealing with a GML. Shapefile was previously transformed to GML. – Francesco Sep 5 '17 at 15:50
  • don't do that unless you actively need a GML file - its a text format and takes a long time to parse. – Ian Turton Sep 5 '17 at 15:52

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.