I have a featureCollection and I want to save it into PostGIS dataStore. So I'm going to create a database table by geotools. I want to name my table "ali". But I can't rename the schema!

DataStore dataStore = DataStoreFinder.getDataStore(params);
SimpleFeatureCollection collection=...;
SimpleFeatureType schema = collection.getSchema();
SimpleFeatureType schema2 = DataUtilities.createSubType(schema,null,null,"ali",null);
dataStore.createSchema(schema2); // but it creates with name "polygon"

How can I rename the schema to create a table with my desired name?


The problem is caused by null inputs of the createSubType function.
If you have a glance at the DataUtilities#createSubType you will see:

public static SimpleFeatureType createSubType(SimpleFeatureType featureType,
        String[] properties, CoordinateReferenceSystem override, String typeName, URI namespace)
        throws SchemaException {

    if ((properties == null) && (override == null)) {
        return featureType;

It shows that the featuretype will not change with null inputs.

DataUtilitis.java github

You can make your own function like this:

import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.opengis.feature.simple.SimpleFeatureType;

public class MyUtil {

  public static SimpleFeatureType createWithNewName(SimpleFeatureType featureType, String newTypeName) {
    SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
    tb.setCRS(featureType.getCoordinateReferenceSystem()); // not interested in warnings from this simple method
    return tb.buildFeatureType();

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.