当前位置:   article > 正文

Java实现GeoServer通过rest发布shp至WMS服务_publishshp传参

publishshp传参

项目中遇到需要GeoTools发布shp至GeoServer,特此记录。

 

一、添加依赖

  1. <dependency>
  2. <groupId>org.geotools</groupId>
  3. <artifactId>gt-shapefile</artifactId>
  4. <version>${geotools.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.geotools</groupId>
  8. <artifactId>gt-api</artifactId>
  9. <version>${geotools.version}</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.geotools</groupId>
  13. <artifactId>gt-wms</artifactId>
  14. <version>${geotools.version}</version>
  15. </dependency>

因为刚学习geoserver,具体需要哪些依赖我也记不清了。geotools的jar拉取不下来,需要在maven中添加geo的仓库。

  1. <repositories>
  2. <repository>
  3. <id>osgeo</id>
  4. <name>OSGeo Release Repository</name>
  5. <url>https://repo.osgeo.org/repository/release/</url>
  6. <snapshots>
  7. <enabled>false</enabled>
  8. </snapshots>
  9. <releases>
  10. <enabled>true</enabled>
  11. </releases>
  12. </repository>
  13. <repository>
  14. <id>osgeo-snapshot</id>
  15. <name>OSGeo Snapshot Repository</name>
  16. <url>https://repo.osgeo.org/repository/snapshot/</url>
  17. <snapshots>
  18. <enabled>true</enabled>
  19. </snapshots>
  20. <releases>
  21. <enabled>false</enabled>
  22. </releases>
  23. </repository>
  24. </repositories>

此处还需要用geoserver-manager的依赖

  1. <dependency>
  2. <groupId>it.geosolutions</groupId>
  3. <artifactId>geoserver-manager</artifactId>
  4. <version>1.7.0</version>
  5. </dependency>

我在网上找了各种依赖拉取,都是失败。最后找的源码,自己打包引入的项目。

geoserver-manager 地址: https://github.com/geosolutions-it/geoserver-manager

二、Java代码

前一篇文章讲了GeoServer的web管理端发布WMS服务,Java发布服务与管理端流程一致。

  1. public class PublishWms {
  2. public static void main(String[] args) {
  3. System.out.println("====================开始发布服务");
  4. publishShape();
  5. System.out.println("====================发布服务完成");
  6. }
  7. }

1. geoserver创建连接信息

  1. // geoServer信息
  2. String url = "http://localhost:8080/geoserver";
  3. String user = "admin";
  4. String password = "geoserver";
  5. // 连接geoServer
  6. GeoServerRESTManager geoServerRESTManager = null;
  7. try {
  8. geoServerRESTManager = new GeoServerRESTManager(new URL(url), user, password);
  9. } catch (Exception e) {
  10. System.out.println("远程连接GeoServer失败...");
  11. e.printStackTrace();
  12. }

2. manager中重要的几个类对象

geoserver-manager中几个重要的类对象

  1. GeoServerRESTManager该对象是一个最大的管理者可以获取以下两个对象,创建数据存储
  2. GeoServerRESTPublisher,发布对象,用来发布各种数据和创建工作空间(主要用来创建对象)
  3. GeoServerRESTReader,获取数据存储、图层、样式、图层组等(主要用来获取信息)
  1. // shp读写和发布
  2. assert geoServerRESTManager != null;
  3. GeoServerRESTReader restReader = geoServerRESTManager.getReader();
  4. GeoServerRESTPublisher restPublisher = geoServerRESTManager.getPublisher();

3. 创建工作区

  1. // 工作区
  2. String workSpace = "spacetest2";
  3. // 存在相应的工作区
  4. if (!restReader.existsWorkspace(workSpace)) {
  5. restPublisher.createWorkspace(workSpace);
  6. }

4. 添加style样式

  1. // style样式
  2. String styleName = "styletest2";
  3. String styleSld;
  4. // 判断是否已经发布了style
  5. if (!restReader.existsStyle(workSpace, styleName)) {
  6. String styleFilePath = "Z:\\GIStone\\SuperMap\\Server\\webapps\\iserver\\WEB-INF\\config\\region.sld";
  7. File styleFile = new File(styleFilePath);
  8. restPublisher.publishStyleInWorkspace(workSpace, styleFile, styleName);
  9. }
  10. styleSld = restReader.getSLD(workSpace, styleName);

style样式引入的sld文件。SLD是风格化图层描述器(Styled Layer Descriptor)的简称。SLD描述了如何在WMS规范的基础上进行扩展使之支持用户对要素数据进行自定义的符号化显示。SLD是一种基于XML语言的OGC标准。这表示SLD文件会被GeoServer创建并且能够被任何一种支持WMS的服务器软件所支持。我们不想限制大家渲染地图的方式,因此我们使用OGC标准规定的SLD作为GeoServer的渲染系统的核心。

5. 创建数据存储

  1. // 数据存储
  2. String dataSetName = "datasettest2";
  3. if (!restReader.existsDatastore(workSpace, dataSetName)) {
  4. //创建shape文件存储
  5. try {
  6. //shp文件所在的位置
  7. String urlDataStorePath = file.getPath();
  8. // 数据存储需要的文件
  9. String shpFilePath = String.format("file://%s", urlDataStorePath);
  10. URL urlShapeFile = new URL(shpFilePath);
  11. // 创建数据集
  12. GSShapefileDatastoreEncoder datastoreEncoder = new GSShapefileDatastoreEncoder(dataSetName, urlShapeFile);
  13. datastoreEncoder.setCharset(Charset.forName("GBK"));
  14. geoServerRESTManager.getStoreManager().create(workSpace, datastoreEncoder);
  15. } catch (MalformedURLException e) {
  16. e.printStackTrace();
  17. }
  18. }
String shpFilePath = String.format("file://%s", urlDataStorePath);

路径参照web端添加数据源时,需要shp文件的位置参数。我在这里踩了很久的坑................................................

6. 添加图层layer发布

  1. String layerName = "ne_50m_admin_0_tiny_countries_scale_rank";
  2. if (!restReader.existsLayer(workSpace, layerName)) {
  3. try {
  4. GSFeatureTypeEncoder gsFeatureTypeEncoder = new GSFeatureTypeEncoder();
  5. gsFeatureTypeEncoder.setTitle(layerName);
  6. gsFeatureTypeEncoder.setName(layerName);
  7. gsFeatureTypeEncoder.setSRS(GeoServerRESTPublisher.DEFAULT_CRS);
  8. GSLayerEncoder gsLayerEncoder = new GSLayerEncoder();
  9. gsLayerEncoder.addStyle(styleSld);
  10. boolean layer = restPublisher.publishDBLayer(workSpace, dataSetName, gsFeatureTypeEncoder, gsLayerEncoder);
  11. System.out.println("publish layer : " + layer);
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. }
  15. }

至此,发布完成。

三、web管理查看

四、完整代码

  1. private static void publishShape() {
  2. File file = new File("Z:\\GIStone\\Projects\\tutorial\\document\\data\\50m_cultural\\ne_50m_admin_0_tiny_countries_scale_rank.shp");
  3. // File zipFile = new File("Z:\\GIStone\\Projects\\tutorial\\document\\data\\50m_cultural\\ne_50m_admin_0_tiny_countries_scale_rank.zip");
  4. // geoServer信息
  5. String url = "http://localhost:8080/geoserver";
  6. String user = "admin";
  7. String password = "geoserver";
  8. // 连接geoServer
  9. GeoServerRESTManager geoServerRESTManager = null;
  10. try {
  11. geoServerRESTManager = new GeoServerRESTManager(new URL(url), user, password);
  12. } catch (Exception e) {
  13. System.out.println("远程连接GeoServer失败...");
  14. e.printStackTrace();
  15. }
  16. // shp读写和发布
  17. assert geoServerRESTManager != null;
  18. GeoServerRESTReader restReader = geoServerRESTManager.getReader();
  19. GeoServerRESTPublisher restPublisher = geoServerRESTManager.getPublisher();
  20. // 工作区
  21. String workSpace = "spacetest2";
  22. // 存在相应的工作区
  23. if (!restReader.existsWorkspace(workSpace)) {
  24. restPublisher.createWorkspace(workSpace);
  25. }
  26. // style样式
  27. String styleName = "styletest2";
  28. String styleSld;
  29. // 判断是否已经发布了style
  30. if (!restReader.existsStyle(workSpace, styleName)) {
  31. String styleFilePath = "Z:\\GIStone\\SuperMap\\Server\\webapps\\iserver\\WEB-INF\\config\\region.sld";
  32. File styleFile = new File(styleFilePath);
  33. restPublisher.publishStyleInWorkspace(workSpace, styleFile, styleName);
  34. }
  35. styleSld = restReader.getSLD(workSpace, styleName);
  36. // 数据存储
  37. String dataSetName = "datasettest2";
  38. if (!restReader.existsDatastore(workSpace, dataSetName)) {
  39. //创建shape文件存储
  40. try {
  41. //shp文件所在的位置
  42. String urlDataStorePath = file.getPath();
  43. // 数据存储需要的文件
  44. String shpFilePath = String.format("file://%s", urlDataStorePath);
  45. URL urlShapeFile = new URL(shpFilePath);
  46. // 创建数据集
  47. GSShapefileDatastoreEncoder datastoreEncoder = new GSShapefileDatastoreEncoder(dataSetName, urlShapeFile);
  48. datastoreEncoder.setCharset(Charset.forName("GBK"));
  49. geoServerRESTManager.getStoreManager().create(workSpace, datastoreEncoder);
  50. } catch (MalformedURLException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. // 图层layer
  55. String layerName = "ne_50m_admin_0_tiny_countries_scale_rank";
  56. if (!restReader.existsLayer(workSpace, layerName)) {
  57. try {
  58. GSFeatureTypeEncoder gsFeatureTypeEncoder = new GSFeatureTypeEncoder();
  59. gsFeatureTypeEncoder.setTitle(layerName);
  60. gsFeatureTypeEncoder.setName(layerName);
  61. gsFeatureTypeEncoder.setSRS(GeoServerRESTPublisher.DEFAULT_CRS);
  62. GSLayerEncoder gsLayerEncoder = new GSLayerEncoder();
  63. gsLayerEncoder.addStyle(styleSld);
  64. boolean layer = restPublisher.publishDBLayer(workSpace, dataSetName, gsFeatureTypeEncoder, gsLayerEncoder);
  65. System.out.println("publish layer : " + layer);
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. // 发布shape
  71. /*try {
  72. boolean shape = restPublisher.publishShp(workSpace, dataSetName, layerName, zipFile, GeoServerRESTPublisher.DEFAULT_CRS);
  73. System.out.println("publish shape : " + shape);
  74. } catch (FileNotFoundException e) {
  75. e.printStackTrace();
  76. }*/
  77. /*// 创建发布类
  78. GeoServerRESTPublisher geoServerRESTPublisher = new GeoServerRESTPublisher(url, user, password);
  79. try {
  80. boolean flag = geoServerRESTPublisher.publishShp(workSpace, dataSetName,
  81. new NameValuePair[]{new NameValuePair("charset", "GBK")},
  82. dataSetName,
  83. GeoServerRESTPublisher.UploadMethod.FILE,
  84. file.toURI(),
  85. GeoServerRESTPublisher.DEFAULT_CRS,
  86. styleName);
  87. System.out.println("publish shp : " + flag);
  88. } catch (FileNotFoundException e) {
  89. e.printStackTrace();
  90. }*/
  91. }

五、踩过的坑

1. 在发布shapefile时候创建数据源前面要加“file://”。

2. layer图层的名称一定要与shp文件的名称一样。

3. 如果需要用到压缩文件,压缩文件只能为zip格式,不能是rar格式否则会报错,而且压缩文件的路径是全路径。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/457473
推荐阅读
相关标签
  

闽ICP备14008679号