将 Postgis 中的 table 导出到没有特定列的形状文件
Export table in Postgis to a shape file without a specific column
我想将 postgreSQL (Postgis) table 导出到 Shape 文件,但没有 cetrain 列。我不想先删除数据库中的列。我如何排除这个特定的列?
这是导出函数:
private void exportShapeFile(String name, String path) {
try {
DataStore pgDatastore = Snippets.createPostgisDataStore();
SimpleFeatureCollection sfc = Snippets.getSimpleFeatureCollection(pgDatastore, name);
final SimpleFeatureType TYPE = Snippets.getPostgisSimpleFeatureType(pgDatastore, name);
String filename = path + "\" + name + ".shp";
File newFile = new File(filename);
CoordinateReferenceSystem sourceCRS = Snippets.getCRS(name);
Object[] a = sourceCRS.getIdentifiers().toArray();
String crsOrig = a[0].toString();
String wkt = null;
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);
File directory = new File(txtFieldDir.getText());
ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
newDataStore.createSchema(TYPE);
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(sfc);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
pgDatastore.dispose();
newDataStore.dispose();
}
} else {
Snippets.appendToPane(txtLog,"ERROR:" + typeName + " does not support read/write access.\n", MainDialog.colRed);
}
} catch (MalformedURLException e) {
Snippets.appendToPane(txtLog,e.toString() + "\n", MainDialog.colRed);
e.printStackTrace();
} catch (IOException e) {
Snippets.appendToPane(txtLog,e.toString() + "\n", MainDialog.colRed);
e.printStackTrace();
}
您需要为您的 shapefile 生成一个新模式,然后将您的要素重新键入该模式。 DataUtilities
provides useful methods for this, createSubType
to generate a new schema limited to a shorter list of attributes, and reType
将过滤器更改为与新架构匹配的过滤器。
我想将 postgreSQL (Postgis) table 导出到 Shape 文件,但没有 cetrain 列。我不想先删除数据库中的列。我如何排除这个特定的列? 这是导出函数:
private void exportShapeFile(String name, String path) {
try {
DataStore pgDatastore = Snippets.createPostgisDataStore();
SimpleFeatureCollection sfc = Snippets.getSimpleFeatureCollection(pgDatastore, name);
final SimpleFeatureType TYPE = Snippets.getPostgisSimpleFeatureType(pgDatastore, name);
String filename = path + "\" + name + ".shp";
File newFile = new File(filename);
CoordinateReferenceSystem sourceCRS = Snippets.getCRS(name);
Object[] a = sourceCRS.getIdentifiers().toArray();
String crsOrig = a[0].toString();
String wkt = null;
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);
File directory = new File(txtFieldDir.getText());
ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
newDataStore.createSchema(TYPE);
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(sfc);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
pgDatastore.dispose();
newDataStore.dispose();
}
} else {
Snippets.appendToPane(txtLog,"ERROR:" + typeName + " does not support read/write access.\n", MainDialog.colRed);
}
} catch (MalformedURLException e) {
Snippets.appendToPane(txtLog,e.toString() + "\n", MainDialog.colRed);
e.printStackTrace();
} catch (IOException e) {
Snippets.appendToPane(txtLog,e.toString() + "\n", MainDialog.colRed);
e.printStackTrace();
}
您需要为您的 shapefile 生成一个新模式,然后将您的要素重新键入该模式。 DataUtilities
provides useful methods for this, createSubType
to generate a new schema limited to a shorter list of attributes, and reType
将过滤器更改为与新架构匹配的过滤器。