datagaea:
geoserver:
endpoint: http://10.1.3.30:8080/geoserver
username: admin
password: geoserver
workspacename: datagaea
配置GeoServer config
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConfigurationProperties(prefix = "spring.datagaea.geoserver")
public class GeoServerProperties {
private String endpoint;
private String username;
private String password;
private String workspacename;
}
GeoServerInfo GeoServerRESTManager bean 装载
package com.tudou.potato.datagaea.ogc.config;
import com.tudou.potato.datagaea.ogc.GeoServerInfo;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import java.net.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GeoServerConfig {
@Autowired
private GeoServerProperties geoServerProperties;
@Bean(name = "geoServerInfoFactory")
public GeoServerInfo geoServerInfoFactory() {
return new GeoServerInfo(geoServerProperties.getEndpoint(), geoServerProperties.getUsername(),
geoServerProperties.getPassword());
}
@Bean(name = "geoServerRESTManagerFactory")
public GeoServerRESTManager geoServerRESTManagerFactory() {
try {
return new GeoServerRESTManager(new URL(geoServerProperties.getEndpoint()),
geoServerProperties.getUsername(), geoServerProperties.getPassword());
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
package com.tudou.potato.datagaea.ogc.repo.impl;
import com.tudou.potato.datagaea.ogc.DataSourceInfo;
import com.tudou.potato.datagaea.ogc.GeoServerInfo;
import com.tudou.potato.datagaea.ogc.GeoServerUtil;
import com.tudou.potato.datagaea.ogc.PublishLayerInfo;
import com.tudou.potato.datagaea.ogc.entity.GeoServerUrlInfo;
import com.tudou.potato.datagaea.ogc.entity.OgcLayerGeoInfo;
import com.tudou.potato.datagaea.ogc.entity.OgcLayerMetadata;
import com.tudou.potato.datagaea.ogc.repo.GeoServerService;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.decoder.*;
import it.geosolutions.geoserver.rest.decoder.RESTLayer.Type;
import it.geosolutions.geoserver.rest.encoder.datastore.GSShapefileDatastoreEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import it.geosolutions.geoserver.rest.encoder.feature.GSAttributeEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class GeoServiceServiceImpl implements GeoServerService {
@Autowired
private GeoServerRESTManager geoServerRESTManager;
@Override
public int publish(GeoServerInfo geoServerInfo, PublishLayerInfo publishLayerInfo,
DataSourceInfo dataSourceInfo) {
//创建工作空间
Boolean result = GeoServerUtil.checkWorkSpaceIsExist(geoServerInfo, publishLayerInfo);
if (!result) {
//不存在则创建工作空间
result = GeoServerUtil.createWorkSpace(geoServerInfo, publishLayerInfo);
}
if (!result) {
//创建工作空间失败,则退出
System.out.println("创建工作空间是失败");
return 0;
}
//创建数据源,此处采用先删除在创建的方式,如果没有数据源,则创建;有数据源则先删除后创建
result = GeoServerUtil.checkDataStoreIsExist(geoServerInfo, publishLayerInfo);
if (!result) {
//不存在则创建数据源
result = GeoServerUtil.createDataStore(geoServerInfo, publishLayerInfo, dataSourceInfo);
}
if (!result) {
//创建数据源失败,则退出
System.out.println("创建数据存储失败");
return 0;
}
//发布图层
result = GeoServerUtil.checkLayerIsExist(geoServerInfo, publishLayerInfo);
if (!result) {
//不存在则发布图层
result = GeoServerUtil.publishLayer(geoServerInfo, publishLayerInfo, dataSourceInfo);
}
if (!result) {
//创建图层失败,则退出
System.out.println("发布图层失败");
return 0;
}
return 1;
}
@Override
public int offline(GeoServerInfo geoServerInfo, PublishLayerInfo publishLayerInfo) {
//判断工作空间
Boolean isWSExist = GeoServerUtil.checkWorkSpaceIsExist(geoServerInfo, publishLayerInfo);
if (!isWSExist) {
System.out.println("工作模型不存在,无法删除图层");
return 0;
}
//判断图层是否存在
isWSExist = GeoServerUtil.checkLayerIsExist(geoServerInfo, publishLayerInfo);
if (!isWSExist) {
System.out.println("图层不存在,无法删除图层");
return 0;
}
isWSExist = GeoServerUtil.deleteLayer(geoServerInfo, publishLayerInfo);
if (!isWSExist) {
System.out.println("图层删除失败");
return 0;
}
isWSExist = GeoServerUtil.deleteDataStore(geoServerInfo,publishLayerInfo);
if (!isWSExist) {
System.out.println("数据源删除失败");
return 0;
}
System.out.println("图层删除成功");
return 1;
}
@Override
public int checkLayerExist(GeoServerInfo geoServerInfo, PublishLayerInfo publishLayerInfo,
DataSourceInfo dataSourceInfo) {
//判断工作空间
Boolean isWSExist = GeoServerUtil.checkWorkSpaceIsExist(geoServerInfo, publishLayerInfo);
if (!isWSExist) {
System.out.println("工作模型不存在");
return 0;
}
//判断图层是否存在
isWSExist = GeoServerUtil.checkLayerIsExist(geoServerInfo, publishLayerInfo);
if (!isWSExist) {
System.out.println("图层不存在");
return 0;
}
System.out.println("图层存在");
return 1;
}
@Override
public String getLayerUrl(GeoServerInfo geoServerInfo, PublishLayerInfo publishLayerInfo,
String serviceType,GeoServerUrlInfo urlInfo) {
String styleName = geoServerRESTManager.getReader().getLayer(publishLayerInfo.getWorkSpace(),publishLayerInfo.getStoreName()).getDefaultStyle();
publishLayerInfo.setStyleName(styleName);
RESTLayer restLayer = geoServerRESTManager.getReader().getLayer(publishLayerInfo.getWorkSpace(),publishLayerInfo.getLayerName());
RESTFeatureType featureType = geoServerRESTManager.getReader().getFeatureType(restLayer);
RESTBoundingBox boundingbox = featureType.getNativeBoundingBox();
String nativeCrs = boundingbox.getCRS();
double nativeMinX = boundingbox.getMinX();
double nativeMaxX = boundingbox.getMaxX();
double nativeMinY = boundingbox.getMinY();
double nativeMaxY = boundingbox.getMaxY();
//判断工作空间
Boolean isWSExist = GeoServerUtil.checkWorkSpaceIsExist(geoServerInfo, publishLayerInfo);
if (!isWSExist) {
System.out.println("工作模型不存在");
return null;
}
//判断图层是否存在
isWSExist = GeoServerUtil.checkLayerIsExist(geoServerInfo, publishLayerInfo);
if (!isWSExist) {
System.out.println("图层不存在");
return null;
}
//根据路径信息拼装URL
urlInfo.setUrl(geoServerInfo.getUrl());
urlInfo.setWorkspaceName(publishLayerInfo.getWorkSpace());
urlInfo.setLayerName(publishLayerInfo.getLayerName());
urlInfo.setHeight(786);
urlInfo.setWidth(643);
urlInfo.setMinX(nativeMinX);
urlInfo.setMaxX(nativeMaxX);
urlInfo.setMinY(nativeMinY);
urlInfo.setMaxY(nativeMaxY);
urlInfo.setEpsgCode(nativeCrs);
if((serviceType != null) && serviceType.equals("WMTS")){
return urlInfo.toWMTSUrl();
}
return urlInfo.toWMSUrl();
}
@Override
public List<String> selectStyleInfo() {
List<String> datas = new ArrayList<>();
RESTStyleList stylelist = geoServerRESTManager.getReader().getStyles();
stylelist.forEach(item -> {
datas.add(item.getName());
});
return datas;
}
@Override
public String getStyleInfo(String styleName) {
return geoServerRESTManager.getStyleManager().getSLD(styleName);
}
@Override
public boolean addStyle(String workspace, String styleName, String SLDInfo) {
return geoServerRESTManager.getStyleManager()
.publishStyleInWorkspace(workspace, SLDInfo, styleName);
}
@Override
public boolean deleteStyle(String workspace, String styleName) {
return geoServerRESTManager.getStyleManager().removeStyle(styleName, true);
}
}