GeoTools查询空间数据

在开始前,请设置好开发环境,我们将列出所需要的maven包依赖关系。

本章主要介绍如何在geotools查询空间地理数据。在之前的教程中我们一直在使用shapefiles。本章的重点是用于查询DataStores的Filter API,例如shapefiles和数据库以及WFS(Web Feature Server)服务。在接下来的实际操作中,我们会使用真正的空间数据库。

如果您在具有空间数据库(例如Oracle,DB2)或者地理空间中间件(例如ArcSDE)的企业中工作,那么您可以使用GeoTools连接到现有的基础架构。在这里,我们将使用PostGIS,这是一个支持SQL语言简单查询的PostgreSQL的空间拓展。我们将构建一个可以连接到PostGIS数据库和shapefile的应用程序。

我们尝试使用这些文章中的代码最初的想法是-让您有机会从源代码开始,今后如果您有任何问题,可以继续浏览这些想法。

查询实验室应用程序

  1. 请确保您的pom.xml文件中包含以下内容:
    <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <geotools.version>18-SNAPSHOT</geotools.version>
        </properties>
        <dependencies>
            <!-- Provides map projections -->
            <dependency>
                <groupId>org.geotools</groupId>
                <artifactId>gt-epsg-hsql</artifactId>
                <version>${geotools.version}</version>
            </dependency>
            <!-- Provides support for PostGIS. Note the different groupId -->
            <dependency>
                <groupId>org.geotools.jdbc</groupId>
                <artifactId>gt-jdbc-postgis</artifactId>
                <version>${geotools.version}</version>
            </dependency>
            <!-- Provides support for shapefiles -->
            <dependency>
                <groupId>org.geotools</groupId>
                <artifactId>gt-shapefile</artifactId>
                <version>${geotools.version}</version>
            </dependency>
            <!-- Provides GUI components -->
            <dependency>
                <groupId>org.geotools</groupId>
                <artifactId>gt-swing</artifactId>
                <version>${geotools.version}</version>
            </dependency>
        </dependencies>

  2. 创建 包org.geotools.tutorial.filter 和类 QueryLab,并将以下内容复制粘贴到文件中:
    /*
     *    GeoTools - The Open Source Java GIS Toolkit
     *    http://geotools.org
     *
     *    (C) 2006-2008, Open Source Geospatial Foundation (OSGeo)
     *
     *    This file is hereby placed into the Public Domain. This means anyone is
     *    free to do whatever they wish with this file. Use it well and enjoy!
     */
    
    package org.geotools.tutorial.filter;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.util.Map;
    import javax.swing.ComboBoxModel;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.table.DefaultTableModel;
    import org.geotools.data.DataStore;
    import org.geotools.data.DataStoreFactorySpi;
    import org.geotools.data.DataStoreFinder;
    import org.geotools.data.Query;
    import org.geotools.data.postgis.PostgisNGDataStoreFactory;
    import org.geotools.data.shapefile.ShapefileDataStoreFactory;
    import org.geotools.data.simple.SimpleFeatureCollection;
    import org.geotools.data.simple.SimpleFeatureIterator;
    import org.geotools.data.simple.SimpleFeatureSource;
    import org.geotools.filter.text.cql2.CQL;
    import org.geotools.swing.action.SafeAction;
    import org.geotools.swing.data.JDataStoreWizard;
    import org.geotools.swing.table.FeatureCollectionTableModel;
    import org.geotools.swing.wizard.JWizard;
    import org.opengis.feature.simple.SimpleFeature;
    import org.opengis.feature.type.FeatureType;
    import org.opengis.filter.Filter;
    import com.vividsolutions.jts.geom.Coordinate;
    import com.vividsolutions.jts.geom.Geometry;
    import com.vividsolutions.jts.geom.Point;
    
    /**
     * The Query Lab is an excuse to try out Filters and Expressions on your own data with a table to
     * show the results.
     * <p>
     * Remember when programming that you have other options then the CQL parser, you can directly make
     * a Filter using CommonFactoryFinder.getFilterFactory2().
     */
    @SuppressWarnings("serial")
    public class QueryLab extends JFrame {
        private DataStore dataStore;
        private JComboBox<String> featureTypeCBox;
        private JTable table;
        private JTextField text;
    
        public static void main(String[] args) throws Exception {
            JFrame frame = new QueryLab();
            frame.setVisible(true);
        }

应用程序GUI

接下来,我们创建应用程序用户界面,其中包含一个文本字段以输入查询和一个表,以显示查询所选功能的数据。

以下是创建控件的代码:

  1. 添加以下构造函数:
     public QueryLab() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getContentPane().setLayout(new BorderLayout());
    
            text = new JTextField(80);
            text.setText("include"); // include selects everything!
            getContentPane().add(text, BorderLayout.NORTH);
    
            table = new JTable();
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            table.setModel(new DefaultTableModel(5, 5));
            table.setPreferredScrollableViewportSize(new Dimension(500, 200));
    
            JScrollPane scrollPane = new JScrollPane(table);
            getContentPane().add(scrollPane, BorderLayout.CENTER);
    
            JMenuBar menubar = new JMenuBar();
            setJMenuBar(menubar);
    
            JMenu fileMenu = new JMenu("File");
            menubar.add(fileMenu);
    
            featureTypeCBox = new JComboBox<>();
            menubar.add(featureTypeCBox);
    
            JMenu dataMenu = new JMenu("Data");
            menubar.add(dataMenu);
            pack();

  2. 接下来,我们将菜单项和操作添加到文件菜单以连接到shapefile或PostGIS数据库:
    fileMenu.add(new SafeAction("Open shapefile...") {
                public void action(ActionEvent e) throws Throwable {
                    connect(new ShapefileDataStoreFactory());
                }
            });
            fileMenu.add(new SafeAction("Connect to PostGIS database...") {
                public void action(ActionEvent e) throws Throwable {
                    connect(new PostgisNGDataStoreFactory());
                }
            });
            fileMenu.add(new SafeAction("Connect to DataStore...") {
                public void action(ActionEvent e) throws Throwable {
                    connect(null);
                }
            });
            fileMenu.addSeparator();
            fileMenu.add(new SafeAction("Exit") {
                public void action(ActionEvent e) throws Throwable {
                    System.exit(0);
                }
            });

  3. 现在,我们来看看数据菜单项和操作:
    dataMenu.add(new SafeAction("Get features") {
                public void action(ActionEvent e) throws Throwable {
                    filterFeatures();
                }
            });
            dataMenu.add(new SafeAction("Count") {
                public void action(ActionEvent e) throws Throwable {
                    countFeatures();
                }
            });
            dataMenu.add(new SafeAction("Geometry") {
                public void action(ActionEvent e) throws Throwable {
                    queryFeatures();
                }
            });
    }

连接到DataStore

在快速启动中,我们使用FileDataStoreFinder连接到特定的文件。这一次,我们将使用更通用的DataStoreFinder,它接收连接参数的映射。请注意,相同的代码可用于连接到由DataStoreFactorySpiService Provider Interface)参数指定的完全不同类型的数据存储。文件菜单操作使用ShapefileDataStoreFactoryPostgisNGDataStoreFactory的实例来调用此方法。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Geotools删除数据,你可以使用PostGIS作为后端数据库,并且根据你提供的参考中的代码来删除表。具体步骤如下: 1. 确保你已经安装了PostGIS,并在数据库中创建了相应的表。 2. 在你的Java项目中,导入Geotools的相关库。 3. 使用Geotools连接到PostGIS数据库,并选择你要删除数据的表。 4. 使用Geotools提供的方法来执行删除操作,例如使用`DropGeometryTable()`方法来删除表。 请注意,这里只提供了删除表的代码示例,如果你需要删除表中的特定数据行,请使用适当的SQL语句来执行删除操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [GeoTools处理空间数据 -- 教程](https://blog.csdn.net/xgb2018/article/details/125779748)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [geotools 使用 部分代码总结](https://blog.csdn.net/woshioosm/article/details/7438564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值