概述
利用geoserver-manager将postgis数据发布到geoserver并修改geoserver默认的style
整体效果如下:
过程
添加依赖
<dependency>
<groupId>it.geosolutions</groupId>
<artifactId>geoserver-manager</artifactId>
<version>1.8-SNAPSHOT</version>
</dependency>
读取配置信息
@Value("${geoserver.url}")
private String geoServerUrl;
@Value("${geoserver.username}")
private String geoServerUsername;
@Value("${geoserver.password}")
private String geoServerPassword;
@Value("${postgis.host}")
private String postgisHost;
@Value("${postgis.port}")
private int postgisPort;
@Value("${postgis.username}")
private String postgisUsername;
@Value("${postgis.password}")
private String postgisPassword;
@Value("${postgis.database}")
private String postgisDatabase;
发布postgis中的矢量图数据为wms服务
/**
* 发布postGis中的矢量图数据为wms服务
* @param workspace 待创建和发布图层的工作区名称workspace
* @param storeName 待创建和发布图层的数据存储名称store
* @param tableName 表名
* @return
*/
@Override
public ShpDataVO publishPostGisShpDataToWMS(String workspace, String storeName, String tableName) {
boolean createWSFlag = false;
boolean createStoreFlag = false;
boolean createLayerFlag = false;
workspace = "t"+workspace;//geoserver中工作区不能以数字开头,所以在租户id前面追加一个"t"
storeName = "t"+storeName;
try {
//判断工作区(workspace)是否存在,不存在则创建
URL u = new URL(geoServerUrl);
GeoServerRESTManager manager = new GeoServerRESTManager(u, geoServerUsername, geoServerPassword);
GeoServerRESTPublisher publisher = manager.getPublisher() ;
List<String> workspaces = manager.getReader().getWorkspaceNames();
if(!workspaces.contains(workspace)){
createWSFlag = publisher.createWorkspace(workspace);
log.info("create ws : " + createWSFlag);
}else {
createWSFlag = true;
log.info("workspace已经存在了,ws :" + workspace);
}
//判断数据存储(datastore)是否已经存在,不存在则创建
RESTDataStore restStore = manager.getReader().getDatastore(workspace, storeName);
if(restStore == null){
GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder(storeName);
store.setHost(postgisHost);//设置url
store.setPort(postgisPort);//设置端口
store.setUser(postgisUsername);// 数据库的用户名
store.setPassword(postgisPassword);// 数据库的密码
store.setDatabase(postgisDatabase);// 那个数据库;
store.setSchema("public"); //当前先默认使用public这个schema
store.setConnectionTimeout(20);// 超时设置
//store.setName(schema);
store.setMaxConnections(20); // 最大连接数
store.setMinConnections(1); // 最小连接数
store.setExposePrimaryKeys(true);
createStoreFlag = manager.getStoreManager().create(workspace, store);
log.info("create store : " + createStoreFlag);
} else {
createStoreFlag = true;
log.info("数据存储已经存在了,store:" + storeName);
}
//判断图层是否已经存在,不存在则创建并发布
RESTLayer layer = manager.getReader().getLayer(workspace, tableName);
if(layer == null){
GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
pds.setTitle(tableName);
pds.setName(tableName);
pds.setSRS("EPSG:4326");
GSLayerEncoder layerEncoder = new GSLayerEncoder();
layerEncoder.setDefaultStyle("iray"); // 设置style
createLayerFlag = manager.getPublisher().publishDBLayer(workspace, storeName, pds, layerEncoder);
log.info("publish : " + createLayerFlag);
}else {
createLayerFlag = true;
log.info("表已经发布过了,table:" + tableName);
}
}catch (Exception e){
log.warn(e.getMessage());
}
if (createWSFlag && createStoreFlag && createLayerFlag){
String serviceUrl = geoServerUrl+"/"+workspace+"/wms";
return new ShpDataVO(serviceUrl,workspace,tableName);
}else {
return null;
}
}
新增geoserver style
通过uDig来配置生成SLD样式文件,主要代码片段