shapefilegeotools

Export table in Postgis to a shape file without a specific column


I Would like to export a postgreSQL (Postgis) table to a Shape file, but without a cetrain column. I don't want to delete the column in the db first. How do I exclude this certain column? This is the export function:

    private void exportShapeFile(String name, String path) {
        try {
            DataStore pgDatastore = Snippets.createPostgisDataStore();
            SimpleFeatureCollection sfc = Snippets.getSimpleFeatureCollection(pgDatastore, name);
            final SimpleFeatureType TYPE = Snippets.getPostgisSimpleFeatureType(pgDatastore, name);
            String filename = path + "\\" + name + ".shp";
            File newFile = new File(filename);
            CoordinateReferenceSystem sourceCRS = Snippets.getCRS(name);
            Object[] a = sourceCRS.getIdentifiers().toArray();
            String crsOrig = a[0].toString();
            String wkt = null;
            ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

            Map<String, Serializable> params = new HashMap<String, Serializable>();
            params.put("url", newFile.toURI().toURL());
            params.put("create spatial index", Boolean.TRUE);
            File directory =  new File(txtFieldDir.getText());
            ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
            newDataStore.createSchema(TYPE);

            Transaction transaction = new DefaultTransaction("create");

            String typeName = newDataStore.getTypeNames()[0];
            SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);

            if (featureSource instanceof SimpleFeatureStore) {
                SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

                featureStore.setTransaction(transaction);
                try {
                    featureStore.addFeatures(sfc);
                    transaction.commit();

                } catch (Exception problem) {
                    problem.printStackTrace();
                    transaction.rollback();

                } finally {
                    transaction.close();
                    pgDatastore.dispose();
                    newDataStore.dispose();
                }
                
            } else {
                Snippets.appendToPane(txtLog,"ERROR:" + typeName + " does not support read/write access.\n", MainDialog.colRed);
            }
        } catch (MalformedURLException e) {
            Snippets.appendToPane(txtLog,e.toString() + "\n", MainDialog.colRed);
            e.printStackTrace();
        } catch (IOException e) {
            Snippets.appendToPane(txtLog,e.toString() + "\n", MainDialog.colRed);
            e.printStackTrace();
        }

Solution

  • You need to generate a new schema for your shapefile and then retype your features to that schema. DataUtilities provides useful methods for this, createSubType to generate a new schema limited to a shorter list of attributes, and reType to change a filter into one that matches the new schema.