GeoTools: Saving grid to shp file

641 views Asked by At

I am very new to GeoTools. I would like to create a hex grid and save it to a SHP file. But something goes wrong along the way (the saved SHP file is empty). In the debug mode I found that the gird is correctly created and contains a bunch of polygons that make sense. Writing those to a shape file proves to be difficult. I followed the tutorial on GeoTools' website, but that does not quite do it yet. I suspect TYPE to be incorrectly defined, but could not find out how to define it correctly.

Any help of how to store the grid into a SHP file is highly appreciated.

    ReferencedEnvelope gridBounds = new ReferencedEnvelope(xMin, xMax, yMin, yMax, DefaultGeographicCRS.WGS84);

    // length of each hexagon edge
    double sideLen = 0.5;
    // max distance between vertices
    double vertexSpacing = sideLen / 20;

    SimpleFeatureSource grid = Grids.createHexagonalGrid(gridBounds, sideLen, vertexSpacing);

    /*
     * We use the DataUtilities class to create a FeatureType that will describe the data in our
     * shapefile.
     * 
     * See also the createFeatureType method below for another, more flexible approach.
     */
    final SimpleFeatureType TYPE = createFeatureType();

    /*
     * Get an output file name and create the new shapefile
     */
    File newFile = new File("D:/test/shape.shp");

    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 newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
    newDataStore.createSchema(TYPE);

    /*
     * You can comment out this line if you are using the createFeatureType method (at end of
     * class file) rather than DataUtilities.createType
     */
    newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);

    /*
     * Write the features to the shapefile
     */
    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(grid.getFeatures());
            transaction.commit();
        } catch (Exception problem) {
            problem.printStackTrace();
            transaction.rollback();
        } finally {
            transaction.close();
        }
    } else {
        System.out.println(typeName + " does not support read/write access");
    }

private static SimpleFeatureType createFeatureType() {

    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setName("Location");
    builder.setCRS(DefaultGeographicCRS.WGS84); // <- Coordinate reference system

    // add attributes in order
    builder.add("Polygon", Polygon.class);
    builder.length(15).add("Name", String.class); // <- 15 chars width for name field

    // build the type
    final SimpleFeatureType LOCATION = builder.buildFeatureType();

    return LOCATION;
}
0

There are 0 answers