在开始前,请设置好开发环境,我们将列出所需要的maven包依赖关系。
本章主要介绍如何在geotools查询空间地理数据。在之前的教程中我们一直在使用shapefiles。本章的重点是用于查询DataStores的Filter API,例如shapefiles和数据库以及WFS(Web Feature Server)服务。在接下来的实际操作中,我们会使用真正的空间数据库。
如果您在具有空间数据库(例如Oracle,DB2)或者地理空间中间件(例如ArcSDE)的企业中工作,那么您可以使用GeoTools连接到现有的基础架构。在这里,我们将使用PostGIS,这是一个支持SQL语言简单查询的PostgreSQL的空间拓展。我们将构建一个可以连接到PostGIS数据库和shapefile的应用程序。
我们尝试使用这些文章中的代码最初的想法是-让您有机会从源代码开始,今后如果您有任何问题,可以继续浏览这些想法。
查询实验室应用程序
- 请确保您的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>
- 创建 包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);
}
<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>
/*
* 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
接下来,我们创建应用程序用户界面,其中包含一个文本字段以输入查询和一个表,以显示查询所选功能的数据。
以下是创建控件的代码:
- 添加以下构造函数:
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();
- 接下来,我们将菜单项和操作添加到文件菜单以连接到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); } });
- 现在,我们来看看数据菜单项和操作:
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,它接收连接参数的映射。请注意,相同的代码可用于连接到由DataStoreFactorySpi(Service Provider Interface)参数指定的完全不同类型的数据存储。文件菜单操作使用ShapefileDataStoreFactory或PostgisNGDataStoreFactory的实例来调用此方法。