Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GeoTools:将栅格保存到shp文件_Java_Geotools - Fatal编程技术网

Java GeoTools:将栅格保存到shp文件

Java GeoTools:将栅格保存到shp文件,java,geotools,Java,Geotools,我对GeoTools非常陌生。我想创建一个十六进制网格并将其保存到SHP文件中。但在此过程中出现了一些问题(保存的SHP文件是空的)。在调试模式中,我发现网格是正确创建的,并且包含一组有意义的多边形。将这些文件写入形状文件证明是困难的。我在GeoTools的网站上学习了教程,但这还不够。我怀疑类型定义不正确,但无法找到如何正确定义它 非常感谢您对如何将网格存储到SHP文件中的任何帮助 ReferencedEnvelope gridBounds = new ReferencedEnvelo

我对GeoTools非常陌生。我想创建一个十六进制网格并将其保存到SHP文件中。但在此过程中出现了一些问题(保存的SHP文件是空的)。在调试模式中,我发现网格是正确创建的,并且包含一组有意义的多边形。将这些文件写入形状文件证明是困难的。我在GeoTools的网站上学习了教程,但这还不够。我怀疑类型定义不正确,但无法找到如何正确定义它

非常感谢您对如何将网格存储到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");
    }
ReferencedEnvelope gridBounds=新的ReferencedEnvelope(xMin、xMax、yMin、yMax、DefaultGeographicCRS.WGS84);
//每个六角边的长度
双面透镜=0.5;
//顶点之间的最大距离
双顶点间距=sideLen/20;
SimpleFeatureSource grid=Grids.createHexagonalGrid(gridBounds、sideLen、vertexSpacing);
/*
*我们使用DataUtilities类来创建一个FeatureType,该FeatureType将描述我们数据库中的数据
*形状文件。
* 
*另请参见下面的createFeatureType方法,了解另一种更灵活的方法。
*/
最终SimpleFeatureType类型=createFeatureType();
/*
*获取输出文件名并创建新的shapefile
*/
File newFile=新文件(“D:/test/shape.shp”);
ShapefileDataStoreFactory dataStoreFactory=新的ShapefileDataStoreFactory();
Map params=新的HashMap();
参数put(“url”,newFile.toURI().tour());
参数put(“创建空间索引”,Boolean.TRUE);
ShapefileDataStore newDataStore=(ShapefileDataStore)dataStoreFactory.createNewDataStore(参数);
createSchema(类型);
/*
*如果使用createFeatureType方法(在
*类文件),而不是DataUtilities.createType
*/
newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
/*
*将特征写入shapefile
*/
事务处理=新的默认事务处理(“创建”);
字符串typeName=newDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource=newDataStore.getFeatureSource(typeName);
if(SimpleFeatureStore的功能源实例){
SimpleFeatureStore featureStore=(SimpleFeatureStore)featureSource;
featureStore.setTransaction(事务);
试一试{
featureStore.addFeatures(grid.getFeatures());
commit();
}捕获(异常问题){
问题:printStackTrace();
transaction.rollback();
}最后{
transaction.close();
}
}否则{
System.out.println(typeName+“不支持读/写访问”);
}

private静态SimpleFeatureType createFeatureType(){
SimpleFeatureTypeBuilder=新SimpleFeatureTypeBuilder();
建造商名称(“位置”);

builder.setCRS(DefaultGeographicCRS.WGS84);//我认为需要将几何体字段称为“the_geom”
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;
}