GeoTools Query Spatial Data

Before starting, please set up the development environment, we will list the required maven package dependencies.

This chapter mainly introduces how to query spatial geographic data in geotools. We have been working with shapefiles in the previous tutorials. This chapter focuses on the Filter API for querying DataStores, such as shapefiles and databases, and WFS (Web Feature Server) services. In the next practical operation, we will use a real spatial database.

If you are working in an enterprise with a spatial database (eg Oracle, DB2) or geospatial middleware (eg ArcSDE), then you can use GeoTools to connect to your existing infrastructure. Here, we will use PostGIS, a spatial extension of PostgreSQL that supports simple queries in the SQL language. We will build an application that can connect to PostGIS databases and shapefiles.

The original idea of ​​our attempts to use the code in these articles was - to give you the opportunity to start with the source code and continue to browse the ideas later if you have any questions.

Query Lab Application

  1. Make sure you have the following in your pom.xml file:
    <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. Create package org.geotools.tutorial.filter  and class  QueryLab and copy-paste the following into the file:
    /*
     *    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);
        }

Application GUI

Next, we create the application user interface with a text field to enter the query and a table to display the data for the selected function of the query.

Here is the code to create the control:

  1. Add the following constructor:
    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. Next, we add menu items and actions to the file menu to connect to a shapefile or PostGIS database:
    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. Now, let's look at the data menu items and actions:
    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();
                }
            });
    }

Connect to DataStore

In Quick Launch, we use FileDataStoreFinder to connect to a specific file. This time, we'll use the more generic DataStoreFinder , which accepts a map of connection parameters. Note that the same code can be used to connect to a completely different type of data store specified by the DataStoreFactorySpi ( Service Provider Interface ) parameter. File menu actions use an instance of ShapefileDataStoreFactory or PostgisNGDataStoreFactory to call this method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325807706&siteId=291194637