Java 需要帮助将JTS几何体对象数组转换为形状文件吗

Java 需要帮助将JTS几何体对象数组转换为形状文件吗,java,geotools,jts,Java,Geotools,Jts,我有一个JTS几何体对象数组,我需要将其放入一个形状文件中。我还有一些其他属性需要放在DBase文件中。我还需要索引空间对象,如果需要,还需要创建投影文件。是否有一种方法可以使用JTS/GeoTools实现这一点。我尝试了ShapeFileWriter,但这似乎还不够(例如,没有dbf支持) 您无法写出一组几何图形并获取DBF文件(因为没有属性可放入其中)。您需要创建FeatureCollection,然后将其传递给ShapeFileDatastore 您将需要以下内容: public bool

我有一个JTS几何体对象数组,我需要将其放入一个形状文件中。我还有一些其他属性需要放在DBase文件中。我还需要索引空间对象,如果需要,还需要创建投影文件。是否有一种方法可以使用JTS/GeoTools实现这一点。我尝试了ShapeFileWriter,但这似乎还不够(例如,没有dbf支持)


您无法写出一组几何图形并获取DBF文件(因为没有属性可放入其中)。您需要创建FeatureCollection,然后将其传递给ShapeFileDatastore

您将需要以下内容:

public boolean writeFeatures(
        FeatureCollection<SimpleFeatureType, SimpleFeature> features) {

    if (shpDataStore == null) {
        throw new IllegalStateException(
                "Datastore can not be null when writing");
    }
    SimpleFeatureType schema = features.getSchema();
    GeometryDescriptor geom = schema
            .getGeometryDescriptor();

    try {

        /*
         * Write the features to the shapefile
         */
        Transaction transaction = new DefaultTransaction(
                "create");

        String typeName = shpDataStore.getTypeNames()[0];
        SimpleFeatureSource featureSource = shpDataStore
                .getFeatureSource(typeName);

        /*
         * The Shapefile format has a couple limitations: - "the_geom" is always
         * first, and used for the geometry attribute name - "the_geom" must be of
         * type Point, MultiPoint, MuiltiLineString, MultiPolygon - Attribute
         * names are limited in length - Not all data types are supported (example
         * Timestamp represented as Date)
         *
         * Because of this we have to rename the geometry element and then rebuild
         * the features to make sure that it is the first attribute.
         */

        List<AttributeDescriptor> attributes = schema
                .getAttributeDescriptors();
        GeometryType geomType = null;
        List<AttributeDescriptor> attribs = new ArrayList<AttributeDescriptor>();
        for (AttributeDescriptor attrib : attributes) {
            AttributeType type = attrib.getType();
            if (type instanceof GeometryType) {
                geomType = (GeometryType) type;

            } else {
                attribs.add(attrib);
            }
        }

        GeometryTypeImpl gt = new GeometryTypeImpl(
                new NameImpl("the_geom"), geomType.getBinding(),
                geomType.getCoordinateReferenceSystem(),
                geomType.isIdentified(), geomType.isAbstract(),
                geomType.getRestrictions(), geomType.getSuper(),
                geomType.getDescription());

        GeometryDescriptor geomDesc = new GeometryDescriptorImpl(
                gt, new NameImpl("the_geom"),
                geom.getMinOccurs(), geom.getMaxOccurs(),
                geom.isNillable(), geom.getDefaultValue());

        attribs.add(0, geomDesc);

        SimpleFeatureType shpType = new SimpleFeatureTypeImpl(
                schema.getName(), attribs, geomDesc,
                schema.isAbstract(), schema.getRestrictions(),
                schema.getSuper(), schema.getDescription());


        shpDataStore.createSchema(shpType);

        if (featureSource instanceof SimpleFeatureStore) {
            SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

            List<SimpleFeature> feats = new ArrayList<SimpleFeature>();

            FeatureIterator<SimpleFeature> features2 = features
                    .features();
            while (features2.hasNext()) {
                SimpleFeature f = features2.next();
                SimpleFeature reType = SimpleFeatureBuilder
                        .build(shpType, f.getAttributes(), "");

                feats.add(reType);
            }
            features2.close();
            SimpleFeatureCollection collection = new ListFeatureCollection(
                    shpType, feats);

            featureStore.setTransaction(transaction);
            try {
                List<FeatureId> ids = featureStore
                        .addFeatures(collection);
                transaction.commit();
            } catch (Exception problem) {
                problem.printStackTrace();
                transaction.rollback();
            } finally {
                transaction.close();
            }
            shpDataStore.dispose();
            return true;
        } else {
            shpDataStore.dispose();
            System.err.println("ShapefileStore not writable");
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}
公共布尔写特征(
功能(收集功能){
如果(shpDataStore==null){
抛出新的非法状态异常(
“写入时数据存储不能为空”);
}
SimpleFeatureType schema=features.getSchema();
GeometryDescriptor geom=schema
.getGeometryDescriptor();
试一试{
/*
*将特征写入shapefile
*/
事务处理=新的DefaultTransaction(
“创建”);
字符串typeName=shpDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource=shpDataStore
.getFeatureSource(类型名称);
/*
*Shapefile格式有两个限制:-“theu geom”总是
*首先,用于几何体属性名称-“the_geom”必须为
*类型Point、MultiPoint、MuiltiLineString、MultiPolygon-属性
*名称的长度有限-并非所有数据类型都受支持(例如
*时间戳(表示为日期)
*
*因此,我们必须重命名几何体元素,然后重新生成
*这些功能可确保它是第一个属性。
*/

列表(包括整理依赖项的pom文件)

Shapefilewriter将完成所有工作-可能您可以向我们展示一些代码我在问题中添加了部分代码以供参考。@iant
public boolean writeFeatures(
        FeatureCollection<SimpleFeatureType, SimpleFeature> features) {

    if (shpDataStore == null) {
        throw new IllegalStateException(
                "Datastore can not be null when writing");
    }
    SimpleFeatureType schema = features.getSchema();
    GeometryDescriptor geom = schema
            .getGeometryDescriptor();

    try {

        /*
         * Write the features to the shapefile
         */
        Transaction transaction = new DefaultTransaction(
                "create");

        String typeName = shpDataStore.getTypeNames()[0];
        SimpleFeatureSource featureSource = shpDataStore
                .getFeatureSource(typeName);

        /*
         * The Shapefile format has a couple limitations: - "the_geom" is always
         * first, and used for the geometry attribute name - "the_geom" must be of
         * type Point, MultiPoint, MuiltiLineString, MultiPolygon - Attribute
         * names are limited in length - Not all data types are supported (example
         * Timestamp represented as Date)
         *
         * Because of this we have to rename the geometry element and then rebuild
         * the features to make sure that it is the first attribute.
         */

        List<AttributeDescriptor> attributes = schema
                .getAttributeDescriptors();
        GeometryType geomType = null;
        List<AttributeDescriptor> attribs = new ArrayList<AttributeDescriptor>();
        for (AttributeDescriptor attrib : attributes) {
            AttributeType type = attrib.getType();
            if (type instanceof GeometryType) {
                geomType = (GeometryType) type;

            } else {
                attribs.add(attrib);
            }
        }

        GeometryTypeImpl gt = new GeometryTypeImpl(
                new NameImpl("the_geom"), geomType.getBinding(),
                geomType.getCoordinateReferenceSystem(),
                geomType.isIdentified(), geomType.isAbstract(),
                geomType.getRestrictions(), geomType.getSuper(),
                geomType.getDescription());

        GeometryDescriptor geomDesc = new GeometryDescriptorImpl(
                gt, new NameImpl("the_geom"),
                geom.getMinOccurs(), geom.getMaxOccurs(),
                geom.isNillable(), geom.getDefaultValue());

        attribs.add(0, geomDesc);

        SimpleFeatureType shpType = new SimpleFeatureTypeImpl(
                schema.getName(), attribs, geomDesc,
                schema.isAbstract(), schema.getRestrictions(),
                schema.getSuper(), schema.getDescription());


        shpDataStore.createSchema(shpType);

        if (featureSource instanceof SimpleFeatureStore) {
            SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

            List<SimpleFeature> feats = new ArrayList<SimpleFeature>();

            FeatureIterator<SimpleFeature> features2 = features
                    .features();
            while (features2.hasNext()) {
                SimpleFeature f = features2.next();
                SimpleFeature reType = SimpleFeatureBuilder
                        .build(shpType, f.getAttributes(), "");

                feats.add(reType);
            }
            features2.close();
            SimpleFeatureCollection collection = new ListFeatureCollection(
                    shpType, feats);

            featureStore.setTransaction(transaction);
            try {
                List<FeatureId> ids = featureStore
                        .addFeatures(collection);
                transaction.commit();
            } catch (Exception problem) {
                problem.printStackTrace();
                transaction.rollback();
            } finally {
                transaction.close();
            }
            shpDataStore.dispose();
            return true;
        } else {
            shpDataStore.dispose();
            System.err.println("ShapefileStore not writable");
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}