GeoTools介绍、环境安装、读取shp文件并显示

GeoTools是一个开放源代码(LGPL)Java代码库,它提供了符合标准的方法来处理地理空间数据,例如实现地理信息系统(GIS)。GeoTools库实现了开放地理空间联盟(OGC)规范。

  • Geotools主要提供各种GIS算法,实现各种数据格式的读写和显示。
  • 在显示方面要差一些,只是用Swing实现了地图的简单查看和操作。
  • 用户可以根据Geotools提供的算法自己实现地图的可视化。OpenJump和udig就是基于Geotools的。
  • 目前的大部分开源软件,如udig,geoserver等,对空间数据的处理都是由geotools来做支撑。
  • web服务,命令行工具和桌面程序都可以由geotools来实现。
  • 是构建在OGC标准之上的,是OGC思想的一种实现。而OGC是国际标准,所以geotools将来必定会成为开源空间数据处理的主要工具,
  • Geotools用到的两个较重要的开源GIS工具包是JTS和GeoAPI。前者主要是实现各种GIS拓扑算法,也是基于GeoAPI的。
  • Geotools现在还只是基于2D图形的,缺乏对 3D空间数据算法和显示的支持。

Geotools支持的数据格式

  1. arcsdearcgridgeotiffgrassrastergtopo30image(JPEGTIFFGIFPNG),imageio-ext-gdalimagemoasaicimagepyramidJP2K,matlab

  2. 支持的数据库“jdbc-ng”:db2h2mysqloraclepostgisspatialite,sqlserver

  3. 支持的矢量格式和数据访问:app-schemaarcsdecsvdxfedigeoexcel,geojson,orgpropertyshapefilewfs

  4. XML绑定。基于xml的Java数据结构和绑定提供了如下格式xsd-core (xml simple types),fes,filtergml2gml3kmlowssldwcswfswmswpsvpf。对于额外的geometrysldfilter的编码和解析可以通过domsax程序。

     

    支持大部分的OGC标准

  5. OGC中的sld/SE和渲染引擎;

  6. OGC一般要素模型包括简单要素支持;

  7. OGC中栅格信息的网格影像表达;

  8. OGC中WFS,WMS和额外的WPS;

  9. ISO 19107 geometry规范;

Geotools依赖的开源项目

  1. JTS:JTS是加拿大的 Vivid Solutions 做的一套开放源码的 Java API。它提供了一套空间数据操作的核心算法,为在兼容OGC标准的空间对象模型中进行基础的几何操作提供2D空间谓词API。
  2. GeoAPI:GeoAPI为OpenGIS规范提供一组Java接口。

环境搭建

在本文最后的github项目中有安装介绍和遇到的坑。

第一个demo

其实就是官网的入门案例。查看本文下面的 github中的源码也可以。

package com.tutorial.quickstart;
 
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2019, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
 
 
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;
 
import java.io.File;
import java.io.IOException;
 
/**
* Prompts the user for a shapefile and displays the contents on the screen in a map frame.
*
* <p>This is the GeoTools Quickstart application used in documentationa and tutorials. *
*/
public class Quickstart {
 
/**
* GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its
* contents on the screen in a map frame
*/
public static void main(String[] args) {
// display a data store file chooser dialog for shapefiles
File file = JFileDataStoreChooser.showOpenFile("shp", null);
if (file == null) {
return;
}
 
// "data/CHN_adm_shp/CHN_adm0.shp"
// String path = "data/ne_50m_admin_0_countries/ne_50m_admin_0_countries.shp";
 
// File file = new File(path);
 
// FileDataStoreFinder
// 可以使我们轻松处理文件。另一种处理方法是使用连接参数映射。这种技术使我们对使用shapefile的方式有了更多的控制,
// 还使我们可以连接到数据库和Web功能服务器。
FileDataStore store = null;
try {
store = FileDataStoreFinder.getDataStore(file);
} catch (IOException e) {
e.printStackTrace();
}
SimpleFeatureSource featureSource = null;
try {
featureSource = store.getFeatureSource();
} catch (IOException e) {
e.printStackTrace();
}
 
// Create a map content and add our shapefile to it
MapContent map = new MapContent();
map.setTitle("Quickstart");
 
 
 
Style style = SLD.createSimpleStyle(featureSource.getSchema());
 
 
Layer layer = new FeatureLayer(featureSource, style);
map.addLayer(layer);
 
// Now display the map
JMapFrame.showMap(map);
}
 
 
}

关注geotools-book查看源码和介绍。

本文参考

原文链接GIS之家小专栏

对本专栏感兴趣的话,可以关注一波

猜你喜欢

转载自www.cnblogs.com/giserhome/p/11640737.html