赞
踩
项目中遇到需要GeoTools发布shp至GeoServer,特此记录。
- <dependency>
- <groupId>org.geotools</groupId>
- <artifactId>gt-shapefile</artifactId>
- <version>${geotools.version}</version>
- </dependency>
- <dependency>
- <groupId>org.geotools</groupId>
- <artifactId>gt-api</artifactId>
- <version>${geotools.version}</version>
- </dependency>
- <dependency>
- <groupId>org.geotools</groupId>
- <artifactId>gt-wms</artifactId>
- <version>${geotools.version}</version>
- </dependency>
因为刚学习geoserver,具体需要哪些依赖我也记不清了。geotools的jar拉取不下来,需要在maven中添加geo的仓库。
- <repositories>
- <repository>
- <id>osgeo</id>
- <name>OSGeo Release Repository</name>
- <url>https://repo.osgeo.org/repository/release/</url>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- <releases>
- <enabled>true</enabled>
- </releases>
- </repository>
- <repository>
- <id>osgeo-snapshot</id>
- <name>OSGeo Snapshot Repository</name>
- <url>https://repo.osgeo.org/repository/snapshot/</url>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- <releases>
- <enabled>false</enabled>
- </releases>
- </repository>
- </repositories>
此处还需要用geoserver-manager的依赖
- <dependency>
- <groupId>it.geosolutions</groupId>
- <artifactId>geoserver-manager</artifactId>
- <version>1.7.0</version>
- </dependency>
我在网上找了各种依赖拉取,都是失败。最后找的源码,自己打包引入的项目。
geoserver-manager 地址: https://github.com/geosolutions-it/geoserver-manager
前一篇文章讲了GeoServer的web管理端发布WMS服务,Java发布服务与管理端流程一致。
- public class PublishWms {
-
- public static void main(String[] args) {
- System.out.println("====================开始发布服务");
- publishShape();
- System.out.println("====================发布服务完成");
- }
-
- }
1. geoserver创建连接信息
- // geoServer信息
- String url = "http://localhost:8080/geoserver";
- String user = "admin";
- String password = "geoserver";
-
- // 连接geoServer
- GeoServerRESTManager geoServerRESTManager = null;
- try {
- geoServerRESTManager = new GeoServerRESTManager(new URL(url), user, password);
- } catch (Exception e) {
- System.out.println("远程连接GeoServer失败...");
- e.printStackTrace();
- }
2. manager中重要的几个类对象
geoserver-manager中几个重要的类对象
- // shp读写和发布
- assert geoServerRESTManager != null;
- GeoServerRESTReader restReader = geoServerRESTManager.getReader();
- GeoServerRESTPublisher restPublisher = geoServerRESTManager.getPublisher();
3. 创建工作区
- // 工作区
- String workSpace = "spacetest2";
- // 存在相应的工作区
- if (!restReader.existsWorkspace(workSpace)) {
- restPublisher.createWorkspace(workSpace);
- }
4. 添加style样式
- // style样式
- String styleName = "styletest2";
- String styleSld;
- // 判断是否已经发布了style
- if (!restReader.existsStyle(workSpace, styleName)) {
- String styleFilePath = "Z:\\GIStone\\SuperMap\\Server\\webapps\\iserver\\WEB-INF\\config\\region.sld";
- File styleFile = new File(styleFilePath);
- restPublisher.publishStyleInWorkspace(workSpace, styleFile, styleName);
- }
- styleSld = restReader.getSLD(workSpace, styleName);
style样式引入的sld文件。SLD是风格化图层描述器(Styled Layer Descriptor)的简称。SLD描述了如何在WMS规范的基础上进行扩展使之支持用户对要素数据进行自定义的符号化显示。SLD是一种基于XML语言的OGC标准。这表示SLD文件会被GeoServer创建并且能够被任何一种支持WMS的服务器软件所支持。我们不想限制大家渲染地图的方式,因此我们使用OGC标准规定的SLD作为GeoServer的渲染系统的核心。
5. 创建数据存储
- // 数据存储
- String dataSetName = "datasettest2";
- if (!restReader.existsDatastore(workSpace, dataSetName)) {
- //创建shape文件存储
- try {
- //shp文件所在的位置
- String urlDataStorePath = file.getPath();
- // 数据存储需要的文件
- String shpFilePath = String.format("file://%s", urlDataStorePath);
- URL urlShapeFile = new URL(shpFilePath);
- // 创建数据集
- GSShapefileDatastoreEncoder datastoreEncoder = new GSShapefileDatastoreEncoder(dataSetName, urlShapeFile);
- datastoreEncoder.setCharset(Charset.forName("GBK"));
- geoServerRESTManager.getStoreManager().create(workSpace, datastoreEncoder);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
String shpFilePath = String.format("file://%s", urlDataStorePath);
路径参照web端添加数据源时,需要shp文件的位置参数。我在这里踩了很久的坑................................................
6. 添加图层layer发布
- String layerName = "ne_50m_admin_0_tiny_countries_scale_rank";
- if (!restReader.existsLayer(workSpace, layerName)) {
- try {
- GSFeatureTypeEncoder gsFeatureTypeEncoder = new GSFeatureTypeEncoder();
- gsFeatureTypeEncoder.setTitle(layerName);
- gsFeatureTypeEncoder.setName(layerName);
- gsFeatureTypeEncoder.setSRS(GeoServerRESTPublisher.DEFAULT_CRS);
-
- GSLayerEncoder gsLayerEncoder = new GSLayerEncoder();
- gsLayerEncoder.addStyle(styleSld);
-
- boolean layer = restPublisher.publishDBLayer(workSpace, dataSetName, gsFeatureTypeEncoder, gsLayerEncoder);
- System.out.println("publish layer : " + layer);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
至此,发布完成。
- private static void publishShape() {
- File file = new File("Z:\\GIStone\\Projects\\tutorial\\document\\data\\50m_cultural\\ne_50m_admin_0_tiny_countries_scale_rank.shp");
- // File zipFile = new File("Z:\\GIStone\\Projects\\tutorial\\document\\data\\50m_cultural\\ne_50m_admin_0_tiny_countries_scale_rank.zip");
-
- // geoServer信息
- String url = "http://localhost:8080/geoserver";
- String user = "admin";
- String password = "geoserver";
-
- // 连接geoServer
- GeoServerRESTManager geoServerRESTManager = null;
- try {
- geoServerRESTManager = new GeoServerRESTManager(new URL(url), user, password);
- } catch (Exception e) {
- System.out.println("远程连接GeoServer失败...");
- e.printStackTrace();
- }
-
- // shp读写和发布
- assert geoServerRESTManager != null;
- GeoServerRESTReader restReader = geoServerRESTManager.getReader();
- GeoServerRESTPublisher restPublisher = geoServerRESTManager.getPublisher();
-
- // 工作区
- String workSpace = "spacetest2";
- // 存在相应的工作区
- if (!restReader.existsWorkspace(workSpace)) {
- restPublisher.createWorkspace(workSpace);
- }
-
- // style样式
- String styleName = "styletest2";
- String styleSld;
- // 判断是否已经发布了style
- if (!restReader.existsStyle(workSpace, styleName)) {
- String styleFilePath = "Z:\\GIStone\\SuperMap\\Server\\webapps\\iserver\\WEB-INF\\config\\region.sld";
- File styleFile = new File(styleFilePath);
- restPublisher.publishStyleInWorkspace(workSpace, styleFile, styleName);
- }
- styleSld = restReader.getSLD(workSpace, styleName);
-
- // 数据存储
- String dataSetName = "datasettest2";
- if (!restReader.existsDatastore(workSpace, dataSetName)) {
- //创建shape文件存储
- try {
- //shp文件所在的位置
- String urlDataStorePath = file.getPath();
- // 数据存储需要的文件
- String shpFilePath = String.format("file://%s", urlDataStorePath);
- URL urlShapeFile = new URL(shpFilePath);
- // 创建数据集
- GSShapefileDatastoreEncoder datastoreEncoder = new GSShapefileDatastoreEncoder(dataSetName, urlShapeFile);
- datastoreEncoder.setCharset(Charset.forName("GBK"));
- geoServerRESTManager.getStoreManager().create(workSpace, datastoreEncoder);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
-
- // 图层layer
- String layerName = "ne_50m_admin_0_tiny_countries_scale_rank";
- if (!restReader.existsLayer(workSpace, layerName)) {
- try {
- GSFeatureTypeEncoder gsFeatureTypeEncoder = new GSFeatureTypeEncoder();
- gsFeatureTypeEncoder.setTitle(layerName);
- gsFeatureTypeEncoder.setName(layerName);
- gsFeatureTypeEncoder.setSRS(GeoServerRESTPublisher.DEFAULT_CRS);
-
- GSLayerEncoder gsLayerEncoder = new GSLayerEncoder();
- gsLayerEncoder.addStyle(styleSld);
-
- boolean layer = restPublisher.publishDBLayer(workSpace, dataSetName, gsFeatureTypeEncoder, gsLayerEncoder);
- System.out.println("publish layer : " + layer);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- // 发布shape
- /*try {
- boolean shape = restPublisher.publishShp(workSpace, dataSetName, layerName, zipFile, GeoServerRESTPublisher.DEFAULT_CRS);
- System.out.println("publish shape : " + shape);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }*/
-
- /*// 创建发布类
- GeoServerRESTPublisher geoServerRESTPublisher = new GeoServerRESTPublisher(url, user, password);
- try {
- boolean flag = geoServerRESTPublisher.publishShp(workSpace, dataSetName,
- new NameValuePair[]{new NameValuePair("charset", "GBK")},
- dataSetName,
- GeoServerRESTPublisher.UploadMethod.FILE,
- file.toURI(),
- GeoServerRESTPublisher.DEFAULT_CRS,
- styleName);
- System.out.println("publish shp : " + flag);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }*/
- }
1. 在发布shapefile时候创建数据源前面要加“file://”。
2. layer图层的名称一定要与shp文件的名称一样。
3. 如果需要用到压缩文件,压缩文件只能为zip格式,不能是rar格式否则会报错,而且压缩文件的路径是全路径。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。