3

Using GeoTools v13.2 I need to add features to a GeoPackage from KML. So far I can get the features in a SimpleFeatureCollection which can be added to the GeoPackage:

public void addFeaturesToGeoPackage(InputStream featuresKML, GeoPackage geopackage)
{
    Parser parser = new Parser(new KMLConfiguration());

    SimpleFeature f = (SimpleFeature) parser.parse(featuresKML);
    Collection placemarks = (Collection) f.getAttribute("Feature");
    List<SimpleFeature> featureList = new ArrayList<SimpleFeature>(placemarks);

    //Get SimpleFeatureCollection
    SimpleFeatureCollection featureCollection = DataUtilities.collection(features);

    //Get the feature schema
    SimpleFeatureType schema = featureCollection.getSchema();

    //TODO: Create FeatureEntry from schema
    FeatureEntry featureEntry = createFeatureEntry(schema);

    //Create the geometry table
    geopackage.create(featureEntry, schema);

    //Add all the features
    geopackage.add(featureEntry, featureCollection);

}

I'm not sure how to create the FeatureEntry as what I'm currently using gives an arbitrary name to the "geometryColumnName" attribute which causes a java.lang.IllegalArgumentException:

Caused by: java.lang.IllegalArgumentException: Geometry column featureVectors does not exist in schema

This is what I was able to come up with based on the documentation and examples I could find through some Google searches but I would like to know the right way to accomplish this/get the FeatureEntry.

1

The answer to your question seems to be code like:

  private FeatureEntry createFeatureEntry(SimpleFeatureType schema, ReferencedEnvelope bbox) {
        FeatureEntry ret = new FeatureEntry();
        ret.setGeometryColumn(schema.getGeometryDescriptor().getLocalName());
        GeometryType type = schema.getGeometryDescriptor().getType();
        Geometries gType = Geometries.getForName(type.getName().getLocalPart());
        ret.setGeometryType(gType);
        ret.setBounds(bbox);
        return ret;
    }

But I have to confess that while it now works and writes out a GeoPackage when I try to read it into QGis I don't get a map because QGis reads the geometry as a string. I'm not sure if it's my fault or QGis's fault. I'll see if I can find time to investigate some more.

If you see the same issue you may want to ask on the Geotools-user list.

  • This fixed the original exception but now I'm getting the following exception when I call geopackage.add(): Caused by: java.io.IOException: Schema 'placemark' does not exist. – user1364811 Aug 12 '15 at 21:56
  • I looked at the source for that call and it looks like they change the table name to the schema's type name. Change this in createFeatureEntry() and it worked. Just need to test with QGIS to see if the geopackage is written correctly. – user1364811 Aug 13 '15 at 16:05

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.