GeoServer-Manager应用:java编码实现发布矢量数据或栅格数据至GeoServer
简介与下载
GeoServer-Manager是使用Java编写的面向GeoServer的客户端库,通过GeoServer的REST管理接口与GeoServer交互。提供了访问GeoServer并管理(增删改查)其中工作区、数据存储、图层、图层组、样式等的接口函数。
其为github上的开源项目,最近一次更新在五年前。
项目源码: https://github.com/geosolutions-it/geoserver-manager.
jar库:http://maven.geo-solutions.it/it/geosolutions/geoserver-manager/1.7.0/geoserver-manager-1.7.0.jar
依赖
在maven项目的pom.xml中配置依赖。
依赖库除了geoserver-manager以外,还需要geoserver-manager用到的一些外部依赖。
<repositories>
<repository>
<id>GeoSolutions</id>
<url>http://maven.geo-solutions.it/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>it.geosolutions</groupId>
<artifactId>geoserver-manager</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
编码发布矢量数据
此处以shp格式为例。
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
import it.geosolutions.geoserver.rest.encoder.datastore.GSShapefileDatastoreEncoder;
import java.io.File;
import java.net.URL;
public class shpTest {
public static void main(String[] args) throws Exception {
//GeoServer连接配置
String url = "http://localhost:8080/geoserver";
String userName = "admin";
String passWord = "geoserver";
String workSpace = "test"; //创建的工作区名称
String storeName = "shpTest"; //创建的数据存储名称
//与GeoServer连接,进行管理
GeoServerRESTManager manager = new GeoServerRESTManager(new URL(url), userName, passWord);
GeoServerRESTPublisher publisher = manager.getPublisher();
//创建工作区
boolean createWorkSpace = publisher.createWorkspace(workSpace);
System.out.println("create ws " + createWorkSpace);
//待发布shp数据路径与矢量数据包路径
String filePath = "F:\\GeoServerDatabase\\Community\\Community.shp";
String zipPath = "F:\\GeoServerDatabase\\Community\\Community.zip";
//创建shp数据存储
GSShapefileDatastoreEncoder gsShapefileDatastoreEncoder= new GSShapefileDatastoreEncoder(storeName,new URL("file:" + filePath));
boolean createStore = manager.getStoreManager().create(workSpace, gsShapefileDatastoreEncoder);
System.out.println("create shp store " + createStore);
//发布shp图层(如果没有目标数据存储则会先自动创建该命名数据存储再发布)
boolean publish = manager.getPublisher().publishShp(workSpace, storeName, "Community",new File(zipPath));
System.out.println("publish shp " + publish);
//读取目标矢量数据存储
//RESTDataStore restDataStore = manager.getReader().getDatastore(workSpace, storeName);
}
}
编码发布栅格数据
此处以GeoTiff格式为例。
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore;
import it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder;
import static it.geosolutions.geoserver.rest.encoder.GSResourceEncoder.ProjectionPolicy.REPROJECT_TO_DECLARED;
import java.io.File;
import java.net.URL;
public class tiffTest {
public static void main(String[] args) throws Exception {
//GeoServer连接配置
String url = "http://localhost:8080/geoserver";
String userName = "admin";
String passWord = "geoserver";
String workSpace = "test"; //创建的工作区名称
String storeName = "tiffTest"; //创建的数据存储名称
//与GeoServer连接,进行管理
GeoServerRESTManager manager = new GeoServerRESTManager(new URL(url), userName, passWord);
GeoServerRESTPublisher publisher = manager.getPublisher();
//创建工作区
boolean createWorkSpace = publisher.createWorkspace(workSpace);
System.out.println("create ws " + createWorkSpace);
//初始化和构造栅格数据储存
String srs = "EPSG";//SRS空间参考系统 crs坐标参考系统
GSCoverageEncoder gsCoverageEncoder = new GSCoverageEncoder();
gsCoverageEncoder.setName("test");
gsCoverageEncoder.setTitle("test");
gsCoverageEncoder.setSRS(srs);
gsCoverageEncoder.setNativeFormat("GeoTIFF");
gsCoverageEncoder.addSupportedFormats("GEOTIFF");
gsCoverageEncoder.addKeyword("geoTiff");
gsCoverageEncoder.addKeyword("WCS");
gsCoverageEncoder.setNativeCRS(srs);
gsCoverageEncoder.setRequestSRS(srs);
gsCoverageEncoder.setResponseSRS(srs);
gsCoverageEncoder.setProjectionPolicy(REPROJECT_TO_DECLARED);
gsCoverageEncoder.setLatLonBoundingBox(-180, -90, 180, 90, "EPSG:4326");
//创建栅格数据存储
boolean createStore = manager.getPublisher().createCoverage(workSpace,storeName,gsCoverageEncoder);
System.out.println("Coverage store " + createStore);
//待发布GeoTIFF数据路径
String filePath = "F:\\GeoServerDatabase\\test.tif";
//发布GeoTIFF(如果没有目标数据存储则会先自动创建该命名栅格数据存储再发布)
boolean publish = manager.getPublisher().publishGeoTIFF(workSpace, storeName,new File(filePath));
System.out.println("publish Coverage " + publish);
//读取目标栅格数据存储
//RESTCoverageStore restCoverageStore = manager.getReader().getCoverageStore(workSpace, storeName);
}
}