shp数据插入sde连接的PostgreSQL库(一)----基于 IntelliJ IDE的GeoTools快速搭建环境

前言

   早就听闻大名鼎鼎的GeoTools,因为自己不搞Java,所以之前没用过,

背景

   最近有个需求,一个白模系统,具体是数据是用SDE导入到postgresql中,然后用arcgis server发布了矢量,最后用 arcgis api  for js 4.x拉伸,得到有高度的白模。以前的数据都是通过sde导入的,现在的需求是要通过前端,用户自己去更新矢量数据。本系列只涉及读取shp数据并插入到SDE连接的PostgreSQL已有表中。

  正常来说客户的数据量不大,可以用前端来做的,前端也有库解析shp文件,然后利用FeatureLayer.applyEdits() 实现跟数据库的操作,但是我还是想尝试下GeoTools,是个学习的机会,另外就是觉得前端不适合处理数据,

环境

  Windows 10

   IntelliJ IDE Ultimate 2021.3

  PostgreSql 9.4

  PostGIS Bundle 2.2 for PostgreSQL ×64 9.4

  ArcGIS 10.4.1

  ArcGIS Server 10.4.1

  ArcGIS API for JavaScript 4.24

  geotools 27.x

 步骤

一.找到GeoTools官网

  会看到给出了官网列出几种环境的搭建方式,我们选择在IntelliJ IDE搭建环境:

 

               

  二.安装jdk

  jdk版本有很多,目前已经到Java20,但是感觉主流还是Java8,至少搭建GeoTools环境还是推荐用jdk1.8,尤其新手不要自找麻烦。下表展示了GeoTools与Java版本的对应关系(表格来源《GeoTools 地理信息系统开发》表2-1):

 

  

           

     另外注意设置环境变量

三.新建工程

  1.顺序依次为:新建工程,选择Maven,单击"Create from archetype",选择“org.apache.maven.archetypes:maven-archetype-quickstart

          

   2.单击Next,填写信息:

         

    3.保持默认,单击Finish:

          

    4.创建后的工程为(红可以看到色框在转圈像是下载包):

  

   5.完全结束后是这样子:

   6.运行下App文件,会打印处我们熟悉的“Hello World”:

 四.将Jar包添加到工程

  首先官方文档有个提示,启用离线模式。原文是“如果您按照本教程进行操作,则可能已经提供了预加载的 Maven 存储库。我们可以使用离线模式来确保 Maven 不会尝试下载任何依赖项”, 让设置里面把"Work offline”打个勾。但是我们根据官方教程操作,发现没有预加载库,所以这个选项务必不要打勾,否则依赖会下载失败!我们需要在线下载依赖。

 

 

 

 

  1.打开项目根目录下的 pom.xml 文件。您可以看到我们之前通过向导输入的一些信息。主要涉及到GeoTools的版本、依赖,存储库。不过为了加快速度,直接复制一份过去得了:

  

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6 
  7   <groupId>org.geotools</groupId>
  8   <artifactId>tutorial</artifactId>
  9   <version>1.0-SNAPSHOT</version>
 10 
 11   <name>tutorial</name>
 12   <!-- FIXME change it to the project's website -->
 13   <url>http://www.example.com</url>
 14 
 15   <properties>
 16     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 17     <maven.compiler.source>1.7</maven.compiler.source>
 18     <maven.compiler.target>1.7</maven.compiler.target>
 19     <geotools.version>28-SNAPSHOT</geotools.version>
 20     <maven.deploy.skip>true</maven.deploy.skip>
 21   </properties>
 22 
 23   <dependencies>
 24     <dependency>
 25       <groupId>junit</groupId>
 26       <artifactId>junit</artifactId>
 27       <version>4.11</version>
 28       <scope>test</scope>
 29     </dependency>
 30     <dependency>
 31       <groupId>org.geotools</groupId>
 32       <artifactId>gt-shapefile</artifactId>
 33       <version>${geotools.version}</version>
 34     </dependency>
 35     <dependency>
 36       <groupId>org.geotools</groupId>
 37       <artifactId>gt-swing</artifactId>
 38       <version>${geotools.version}</version>
 39     </dependency>
 40     <dependency>
 41       <groupId>org.geotools.jdbc</groupId>
 42       <artifactId>gt-jdbc-postgis</artifactId>
 43       <version>${geotools.version}</version>
 44     </dependency>
 45   </dependencies>
 46 
 47   <repositories>
 48     <repository>
 49       <id>osgeo</id>
 50       <name>OSGeo Release Repository</name>
 51       <url>https://repo.osgeo.org/repository/release/</url>
 52       <snapshots><enabled>false</enabled></snapshots>
 53       <releases><enabled>true</enabled></releases>
 54     </repository>
 55     <repository>
 56       <id>osgeo-snapshot</id>
 57       <name>OSGeo Snapshot Repository</name>
 58       <url>https://repo.osgeo.org/repository/snapshot/</url>
 59       <snapshots><enabled>true</enabled></snapshots>
 60       <releases><enabled>false</enabled></releases>
 61     </repository>
 62   </repositories>
 63 
 64 
 65   <build>
 66     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
 67       <plugins>
 68         <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
 69         <plugin>
 70           <artifactId>maven-clean-plugin</artifactId>
 71           <version>3.1.0</version>
 72         </plugin>
 73         <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
 74         <plugin>
 75           <artifactId>maven-resources-plugin</artifactId>
 76           <version>3.0.2</version>
 77         </plugin>
 78         <plugin>
 79           <artifactId>maven-compiler-plugin</artifactId>
 80           <version>3.8.0</version>
 81         </plugin>
 82         <plugin>
 83           <artifactId>maven-surefire-plugin</artifactId>
 84           <version>2.22.1</version>
 85         </plugin>
 86         <plugin>
 87           <artifactId>maven-jar-plugin</artifactId>
 88           <version>3.0.2</version>
 89         </plugin>
 90         <plugin>
 91           <artifactId>maven-install-plugin</artifactId>
 92           <version>2.5.2</version>
 93         </plugin>
 94         <plugin>
 95           <artifactId>maven-deploy-plugin</artifactId>
 96           <version>2.8.2</version>
 97         </plugin>
 98         <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
 99         <plugin>
100           <artifactId>maven-site-plugin</artifactId>
101           <version>3.7.1</version>
102         </plugin>
103         <plugin>
104           <artifactId>maven-project-info-reports-plugin</artifactId>
105           <version>3.0.0</version>
106         </plugin>
107       </plugins>
108     </pluginManagement>
109   </build>
110 </project>
pom.xml

  复制完毕后发现会有报错现象:

   2.右键项目,选择Maven,选择"Reload project":

   3.我们发现可以了:

   4.可以到C盘相关文件夹里面看到相关的文件都下载完成(其它两个27和30版本是之前踩坑留下的,为了写博客重新用了28版本):

   搭建环境部分到此结束,到官网后面有个“Quickstart Application”根据1~6步进行操作,会显示出地图的。

 

后续 

2023.10.11更新

  最近要操作GeoJson, 所以pom.xml需要引入依赖,

<dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-geojson</artifactId>
      <version>${geotools.version}</version>
</dependency>

  代码中需要添加

import org.geotools.geojson.geom.GeometryJSON;

   

 

  如果要查看历史版本的geotools文档,需要到官方文档右侧的blog页面右侧找到相应版本,然后在左侧找到userguide下载,解压后打开index.html即可,另外github仓库也有更新日志。

  关于版本选择,为何我用了27.x,因为目前Java主流还是Java8,或者说公司项目用的Java8:

 

参考资料

有些资料可能没参考,只是觉得不错,所以收藏一下

1.《GeoTools 地理信息系统开发》 王项 刘钧文 王新宁 孙运娟

2.  Getting started with geotools.org using IntelliJ IDEA 2020(油管视频)

3.Geotools简介以及quickstsrt加载shp文件并显示

4.geoTools18.4开发环境快速搭建,使用java可视化读取shapefile文件_idea配置geotools_GIS开发者的博客-CSDN博客(不使用Maven)

5.GeoTools, the Java GIS toolkitGeoTools, the Java GIS toolkit Files (geotools包离线下载)

6. Introduction to GeoTools (概要,及一些常用的用法)

7.IDEA运行时报错“类文件具有错误的版本 55.0, 应为 52.0”的解决方法

8.GeoTools应用:提取Shape文件属性列头信息(1)(本地导入依赖

posted @ 2023-05-07 18:40  Youse的二分口粮地  阅读(89)  评论(0编辑  收藏  举报