I have a script which reload data from dbf file and create a new table in postgres database using geotools plugin. My problem is when geotools reload fields name from the file it adds a new field fid to make it as a primary key(by default).

PS: my file fields does not contain a field name fid, My fields are(field1, field2)

I dont want to use the default primary key, i want to set my custom one from the my fields list. please find below my script which work perfectly except the primary key issue . Could you please help me.

    public static void importDBFFileToPostgreSQL(Path filepath, String tablename) {
    DataStore pgDatastore = postgisDataStore.getInstance();
    DbaseFileReader dbaseFileReader = null;
    try {
        try (FileChannel in = FileChannel.open(filepath)) {
            dbaseFileReader = new DbaseFileReader(in, true, Charset.forName("ISO-8859-1"));
        DbaseFileHeader header = dbaseFileReader.getHeader();
        List<AttributeDescriptor> attributes = buildAttributesFromDBFHeader(header);

        // create the feature type
        SimpleFeatureTypeBuilder simpleFeatureTypeBuilder = new SimpleFeatureTypeBuilder();

        SimpleFeatureType newtype = simpleFeatureTypeBuilder.buildFeatureType();
  • where do you set the new FID? That code just sets up the table. – Ian Turton Oct 2 '18 at 15:23
  • the fid column is set automatically when it creates the schema pgDatastore.createSchema(newtype); i think because that i didn't mention the primary key which is my problem – Anas Ben Mansour Oct 2 '18 at 15:26
  • If you look in the newType you will see the FID defined (it's mandatory in the SF spec) – Ian Turton Oct 2 '18 at 16:20

The GeoTools gt-jdbc module defines this behaviour and will always add a serial integer fid column as your primary key for any table it creates.

The best way to generate fids in the way you want is to use a "virtual table" (or view) like this:

VirtualTable vt = new VirtualTable(tableName + "_v",
    "select * from " + tableName);
List<String> prime = new ArrayList<>();

JDBCDataStore jdbcDataStore = (JDBCDataStore) dataStore;

Though you still end up with a fid like fid id=states_v.TN.47.

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.