GeoTools:将网格保存到shp文件

时间:2013-12-02 20:06:51

标签: java geotools

我是GeoTools的新手。我想创建一个十六进制网格并将其保存到SHP文件。但是在此过程中出现了问题(保存的SHP文件为空)。在调试模式中,我发现正确创建了网格并包含一堆有意义的多边形。将它们写入形状文件证明是困难的。我按照GeoTools网站上的教程进行了操作,但是还没有完成。我怀疑TYPE定义不正确,但无法找到如何正确定义它。

非常感谢任何有关如何将网格存储到SHP文件的帮助。

    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 个答案:

没有答案