锦通 发表于 2023-5-8 03:20:04

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官网

  会看到给出了官网列出几种环境的搭建方式,我们选择在IntelliJ IDE搭建环境:
 
               https://img2023.cnblogs.com/blog/760774/202305/760774-20230507154636276-1400061383.png
  二.安装jdk

  jdk版本有很多,目前已经到Java20,但是感觉主流还是Java8,至少搭建GeoTools环境还是推荐用jdk1.8,尤其新手不要自找麻烦。下表展示了GeoTools与Java版本的对应关系(表格来源《GeoTools 地理信息系统开发》表2-1):
 
  
           https://img2023.cnblogs.com/blog/760774/202305/760774-20230507181127115-719326605.png
     另外注意设置环境变量
三.新建工程

  1.顺序依次为:新建工程,选择Maven,单击"Create from archetype",选择“org.apache.maven.archetypes:maven-archetype-quickstart”
          https://img2023.cnblogs.com/blog/760774/202305/760774-20230507183936025-1446283427.png
   2.单击Next,填写信息:
         https://img2023.cnblogs.com/blog/760774/202305/760774-20230507185155623-1909766272.png
    3.保持默认,单击Finish:
          https://img2023.cnblogs.com/blog/760774/202305/760774-20230507185352954-1107091238.png
    4.创建后的工程为(红可以看到色框在转圈像是下载包):
  
https://img2023.cnblogs.com/blog/760774/202305/760774-20230507185714111-825582822.png
   5.完全结束后是这样子:
https://img2023.cnblogs.com/blog/760774/202305/760774-20230507190020897-418002849.png
   6.运行下App文件,会打印处我们熟悉的“Hello World”:
https://img2023.cnblogs.com/blog/760774/202305/760774-20230507190230084-2029059554.png
 四.将Jar包添加到工程

  首先官方文档有个提示,启用离线模式。原文是“如果您按照本教程进行操作,则可能已经提供了预加载的 Maven 存储库。我们可以使用离线模式来确保 Maven 不会尝试下载任何依赖项”, 让设置里面把"Work offline”打个勾。但是我们根据官方教程操作,发现没有预加载库,所以这个选项务必不要打勾,否则依赖会下载失败!我们需要在线下载依赖。
 
 
https://img2023.cnblogs.com/blog/760774/202305/760774-20230507195056472-624446321.png
 
 
  1.打开项目根目录下的 pom.xml 文件。您可以看到我们之前通过向导输入的一些信息。主要涉及到GeoTools的版本、依赖,存储库。不过为了加快速度,直接复制一份过去得了:
  
https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gifhttps://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif1 <?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   
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>
67       <plugins>
68         
69         <plugin>
70         <artifactId>maven-clean-plugin</artifactId>
71         <version>3.1.0</version>
72         </plugin>
73         
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         
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  复制完毕后发现会有报错现象:
https://img2023.cnblogs.com/blog/760774/202305/760774-20230507192938493-2050931250.png
   2.右键项目,选择Maven,选择"Reload project":
https://img2023.cnblogs.com/blog/760774/202305/760774-20230507193259566-815217134.png
   3.我们发现可以了:
https://img2023.cnblogs.com/blog/760774/202305/760774-20230507195221562-878307599.png
   4.可以到C盘相关文件夹里面看到相关的文件都下载完成(其它两个27和30版本是之前踩坑留下的,为了写博客重新用了28版本):
https://img2023.cnblogs.com/blog/760774/202305/760774-20230507195816560-179742466.png
   搭建环境部分到此结束,如果想测试环境是否可以运行,到官网后面有个“Quickstart Application”根据1~6步进行操作,会显示出地图的。
https://img2023.cnblogs.com/blog/760774/202305/760774-20230507200319046-1375403527.png
 
 
参考资料

有些资料可能没参考,只是觉得不错,所以收藏一下
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.https://a.fsdn.com/allura/p/geotools/icon?1682537611GeoTools, the Java GIS toolkit Files (geotools包离线下载)
6. Introduction to GeoTools(概要,及一些常用的用法)
7.IDEA运行时报错“类文件具有错误的版本 55.0, 应为 52.0”的解决方法
8.GeoTools应用:提取Shape文件属性列头信息(1)(本地导入依赖)

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: shp数据插入sde连接的PostgreSQL库(一)----基于 IntelliJ IDE的GeoTools快