tostGIS中的表格到没有特定列的形状文件

发布于 2025-01-20 23:59:31 字数 2664 浏览 4 评论 0原文

我想将 postgreSQL (Postgis) 表导出到 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();
        }

I Would like to export a postgreSQL (Postgis) table to a Shape file, but without a cetrain column. I don't want to delete the column in the db first. How do I exclude this certain column?
This is the export function:

    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();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2025-01-27 23:59:31

您需要为 shapefile 生成新架构,然后将要素重新键入该架构。 DataUtilities 提供了有用的方法,createSubType 生成仅限于较短属性列表的新架构,并且 reType 将过滤器更改为与新架构匹配的过滤器。

You need to generate a new schema for your shapefile and then retype your features to that schema. DataUtilities provides useful methods for this, createSubType to generate a new schema limited to a shorter list of attributes, and reType to change a filter into one that matches the new schema.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文