[GIS] Exporting WFS to shapefile

geoservergeotoolsshapefilewfs

I've installed GeoServer and uploaded a shapefile in it and i want to export this added layer to shapefile using GeoTools library.

I've writtren some code which generates a shapefile(shp,dbf,shx,prj) that contains features without geomertic shape. in the other word it just exports attributes.

pls help me with this problem. where should i revise my code.

here is my code

try {
        String getCapabilities = "http://localhost:8060/geoserver/ows?service=wfs&version=1.0.0&request=GetCapabilities";
        Map connectionParameters = new HashMap();
        connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities);
        DataStore data = DataStoreFinder.getDataStore(connectionParameters); 

        String[] typeNames = data.getTypeNames();

        for (int i = 0; i < typeNames.length; i++) {

            if (!typeNames[i].contains("navahi"))
                continue;
            System.out.println(typeNames[i]);
            SimpleFeatureSource wfsFeatureSource = data.getFeatureSource(typeNames[i]);

            if (wfsFeatureSource instanceof SimpleFeatureStore) {
                SimpleFeatureType TYPE = wfsFeatureSource.getSchema();




                File newFile = new File(System.getProperty("user.dir") + typeNames[i].replace(":", "_") + ".shp");

                System.err.println(newFile.getAbsolutePath());

                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);

                ShapefileDataStore shpDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
                shpDataStore.createSchema(TYPE);
                String shpTypeName = shpDataStore.getTypeNames()[0];
                SimpleFeatureSource shpFeatureSource = shpDataStore.getFeatureSource(shpTypeName);


                SimpleFeatureStore shpFeatureStore = (SimpleFeatureStore) shpFeatureSource;

                SimpleFeatureCollection wfsFeatureCollection = wfsFeatureSource.getFeatures();

                Transaction transaction = new DefaultTransaction("create");
                shpFeatureStore.setTransaction(transaction);

                try {
                    shpFeatureStore.addFeatures(wfsFeatureCollection);
                    transaction.commit();
                } catch (Exception problem) {
                    problem.printStackTrace();
                    transaction.rollback();

                } finally {
                    transaction.close();
                }

            }

        }
    } catch (Exception e) {
        // TODO: handle exception }
    }

Best Answer

The most likely reason to not see a geometry column in a Shapefile is that your column name is not the_geom. This is required by the ShapeFile standard - in an ideal world GeoTools would be smart enough to fix this for you behind the scenes, but no one has contributed that code yet!

So you will need to inspect the schema of your features from the WFS and modify the names of any columns that don't fit the ShapeFile standard, i.e. start with an digit, longer than 10 characters etc.

Related Question