gpt4 book ai didi

java - Geotools MultiPolygon 到数据库并返回

转载 作者:行者123 更新时间:2023-11-29 02:55:45 24 4
gpt4 key购买 nike

我正在读取一组包含大量 multypoligons 的数据。我正在使用 Geotools,我想将此列表存储在 mysql 数据库表中。

我不知道如何以有效的方式存储形状并能够重新创建多多边形对象。

如果我得到多面体的坐标,那么我会得到一个数组,其中包含该几何体的所有顶点的值(在几何体是复合体的情况下,该数组包含组件的所有顶点,顺序为其中组件出现在几何中),但我不知道如何使用这些坐标重新创建新的多面体。

请在下面找到我得到的结果。

private List<Shape> parseFile2ShapeList(File file) {

List<Shape> shapes = new ArrayList<Shape>();
FileDataStore myData = null;
SimpleFeatureIterator sfit = null;
try {
// Extract all features
myData = FileDataStoreFinder.getDataStore( file );
SimpleFeatureSource source = myData.getFeatureSource();
SimpleFeatureCollection sfc = source.getFeatures();
sfit = sfc.features();

// Read the features and store in a list only the ones with Venue_ID
while (sfit.hasNext()) {
SimpleFeature feature = sfit.next();
String id = (String) feature.getAttribute("ID");
MultiPolygon mulPoly = (MultiPolygon) feature.getAttribute("the_geom");
Shape shape = new Shape(id, mulPoly);
shapes.add(shape);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
sfit.close();
myData.dispose();
}
return shapes;
}

private boolean insertShapes(List<Shape> shapes) {
// Insert the shapes in the DB
boolean inserted = false;
try (Database db = new Database()) {
// Store in the DB all the shapes
for (Shape shape : shapes) {
db.getShapesDao().insertShape(shape); // What shall I store in the DB if the shape is a multipolygon? What if it is any Geometry?
}
inserted = true;
} catch (SQLException e) {
e.printStackTrace();
inserted = false;
} catch (IOException e) {
e.printStackTrace();
inserted = false;
}
return inserted;
}

private Shape selectShape(String shape_id) {
Shape shape = null;
try (Database db = new Database()) {
// Retrieve the shape
shape = db.getShapesDao().getShapeById(shape_id); // How do I recreate a multipolygon (or any other Geometry inserted in the DB?)
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return shape;
}

public void main() {

// display a data store file chooser dialog for shapefiles
File file = JFileDataStoreChooser.showOpenFile("shp", null);
if (file == null) {
return;
}

// Read the file and load in memory the venues
List<Shape> shapes = parseFile2ShapeList(file);
System.out.println("Shapes parsed: " + retrieved.size());
for (Shape shape : shapes) {
System.out.println("\t ID: " + shape.getId() );
}

// Insert in database
boolean inserted = insertVenues(venues);
System.out.println("Insertion successful? " + inserted);

// Retrieve from database
List<Shape> retrievedShapes = new ArrayList<Shape>();
for (Shape shape : shapes) {
Shape retrieved = selectShape(shape.getId());
retrievedShapes.add(retrieved);
}
System.out.println("Shapes retrieved: " + retrieved.size());
for (Shape shape : retrievedShapes) {
System.out.println("\t ID: " + shape.getId() );
}
}

目前我知道如何从多边形中取回多边形(因为我只是存储坐标并使用它们创建多边形),但我不知道如何存储和检索多边形。通常,最佳解决方案适用于任何几何体:

parse geometry obj -> store geometry to DB (with associated ID) -> (some time later...) -> retrieve geometry information (by ID) -> construct new geometry obj

新对象将是原始对象的副本,不再可用。

PS:序列化一个 Java 对象并将其存储在数据库中是我不想做的事情,因为我希望在数据库中有一些人类可读的东西。 :)

----编辑

我正在根据收到的答案添加更多信息(谢谢@user2731872!)。

我想在其中存储内容的数据库表具有以下架构:

mysql> describe shape_table;
+-----------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+----------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| shape_id | varchar(32) | NO | | NULL | |
| shape | text | NO | | NULL | |
+-----------------+----------------------+------+-----+---------+----------------+

此表还有其他列,但目前它们不相关。无论如何,给定输入文件,我想将每个几何图形(shape 列)存储在不同的行中。

在我的程序中,给定一个 shape_id,我想检索有关形状的相关信息(shape 列),然后构建几何。

最佳答案

您的工作水平太低,因此给自己造成了压力。 GeoTools 被设计(对于大多数用户/用途)与 DataStores 一起工作,DataStores 抽象出细节和为您处理几何和属性的功能。因此,您的问题分为两部分 - 第 1 部分读取 shapefile,第 2 部分将特征写入数据库。您已经成功完成了第一步,第二步有点棘手但相当容易。

获取到数据库的连接(我已经安装了 PostGIS,但 MySql 应该以相同的方式工作):

params.put("user", "geotools");
params.put("passwd", "geotools");
params.put("port", "5432");
params.put("host", "127.0.0.1");
params.put("database", "geotools");
params.put("dbtype", "postgis");
dataStore = DataStoreFinder.getDataStore(params);

然后将特征发送到数据源:

SimpleFeatureSource featureSource = dataStore
.getFeatureSource(schema.getName().getLocalPart());
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
featureStore.setTransaction(transaction);
try {
List < FeatureId > ids = featureStore.addFeatures(features);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
dataStore.dispose();
return true;
} else {
dataStore.dispose();
System.err.println("Database not writable");
return false;
}

在需要时创建新表的句柄有些困惑,您可以在 full code 中看到,它会在需要时创建一个新表或附加到现有表。

关于java - Geotools MultiPolygon 到数据库并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30782841/

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