4

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 }
    }
8
  • 1
    Do you have a question?
    – til_b
    Aug 24, 2016 at 19:00
  • yeah sure, if i had not why on earth i post this question Aug 24, 2016 at 19:09
  • 1
    And what is the question? Please edit your post to include a question.
    – til_b
    Aug 24, 2016 at 19:11
  • What happens with your code, do you get any outputs or errors?
    – nmtoken
    Aug 24, 2016 at 19:19
  • 2
    All right, much more understandable so. If you want shapefiles out from WFS, why don't you simply make GetFeature with shape-zip outputformat docs.geoserver.org/latest/en/user/services/wfs/…?
    – user30184
    Aug 24, 2016 at 21:02

2 Answers 2

6

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.

1
  • yes your point was completely right. When i was importing my layer to postgresql i didnt change the geometry column name and the default name was geom so i did it again with the_geom and then i published it in geoserver and when i'd run my code that worked fine. Aug 25, 2016 at 17:45
4

You can request a Shapefile via the WFS request to GeoServer.

http://docs.geoserver.org/latest/en/user/services/wfs/outputformats.html

States, WFS returns features and feature information in a number of formats. The syntax for specifying an output format is:

outputFormat=format

For ShapeFile

outputFormat=shape-zip

Just add that param to the request URL

1
  • thanks for answering my question although I need my application to be in full connection with geoserver and have list of all layers and their features and attributes and i need to mange all them in memory so Im not able to send a request for all of these stuffs but in some situations i could do make request Aug 25, 2016 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.