gpt4 book ai didi

java - 需要帮助将 JTS Geometry 对象数组转换为形状文件

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:22 25 4
gpt4 key购买 nike

我有一个 JTS Geometry 对象数组,需要将其放入形状文件中。我还有一些其他属性需要放入 DBase 文件中。我还需要索引空间对象,并根据需要创建投影文件。有没有办法使用 JTS/GeoTools 来做到这一点。我尝试了 ShapeFileWriter 但这似乎还不够(例如没有 dbf 支持)。

public Shape(String shpFileName, String shxFileName) throws FileNotFoundException {
RandomAccessFile shpFile = new RandomAccessFile(shpFileName, "rw");
this.shpChannel = shpFile.getChannel();
RandomAccessFile shxFile = new RandomAccessFile(shxFileName, "rw");
this.shxChannel = shxFile.getChannel();
}

public void createShapeFile(GeometryCollection geometries, ShapeType shapeType) throws IOException {
ShapefileWriter writer = new ShapefileWriter(this.shpChannel, this.shxChannel);
writer.write(geometries, shapeType);
writer.close();


}

最佳答案

您无法写出一组几何图形并获取 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;
}

https://github.com/ianturton/geotools-cookbook/tree/master/modules/output/src/main/java/org/ianturton/cookbook/output 有一个完整的工作示例(包括整理依赖关系的pom文件)

关于java - 需要帮助将 JTS Geometry 对象数组转换为形状文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26935862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com