1

I'm trying to create & populate Shapefile in Java using GeoTools.

Basically following this link (quite similar to the official documentation & other tutorials specifically for the point I'm asking below).

For below code I'm getting first warning that FeatureCollections is deprecated and error that add() cannot be resolved.

FeatureCollection<SimpleFeatureType, SimpleFeature> collection = FeatureCollections.newCollection();
...
collection.add(feature);

How can I fix that or any other way to create Shapefile using Java?

4

The prefered solution is to use the gt-csv datastore to read in the features which will return you a feature collection directly, that you can then write out to a shapefile.

If you really need to do your own parsing the simplest solution is to put the features into a Java collection (e.g. list) and then call DataUtilities.collection(list) on it.

Or you could use DefaultFeatureCollection

SimpleFeatureType featureType = DataUtilities.createType(tipoShape,
      "the_geom:" + tipoShape + ":srid=4326," + "number:Integer");

DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal", featureType);

But make sure to call you geometry column the_geom or the Shapefile writer will ignore it.

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.