文章目录
- 简要说明
- maven依赖
- 样例代码
简要说明
利用开源工具geoserver-manager,对postgresql的空间表进行地图服务发布
maven依赖
<dependency><groupId>com.github.dov-vlaanderen</groupId><artifactId>geoserver-manager</artifactId><version>1.8</version></dependency>
样例代码
封装的入参实体
@Data
public class ReqDTO {/*** 数据源信息,主要包含数据库地址、端口、库名、用户名称、密码,就不详细列出来了*/private DataSourceInfo dataSourceInfo;/*** geoserver站点地址*/private String serverUrl;/*** 站点用户名称*/private String userName;/*** 站点密码*/private String passWord;/*** 工作区名称*/private String workspaceName;/*** 表名*/private String tableName;/*** 样式名称*/private String styleName;/*** 样式文件,若有样式文件,会注册样式*/private MultipartFile styleFile;}
辅助方法
/*** 上传的样式文件保存*/private File storePgStyleFile(ReqDTO reqDTO) throws IOException {MultipartFile multipartFile = reqDTO.getStyleFile();if (Objects.isNull(multipartFile) || multipartFile.getSize() == 0) {return null;}String styleName = null;if (!StringUtils.isEmpty(reqDTO.getStyleName())) {styleName = reqDTO.getStyleName();} else {String originalFilename = multipartFile.getOriginalFilename();styleName = originalFilename.substring(0, originalFilename.indexOf("."));}String styleFileName = styleName + ".sld";//spatialDataStoragePath 自定义的存储路径String styleFilePath = spatialDataStoragePath + "style" + File.separator + System.currentTimeMillis() + File.separator + styleFileName;File styleFile = new File(styleFilePath);File parentFile = styleFile.getParentFile();if (!parentFile.exists()) {parentFile.mkdirs();}multipartFile.transferTo(styleFile);reqDTO.setStyleName(styleName);return styleFile;}/*** 获取geoserver管理器** @param serverUrl* @return*/public GeoServerRESTManager getManager(String serverUrl, String userName, String passWord) {try {GeoServerRESTManager manager = new GeoServerRESTManager(new URL(serverUrl), userName, passWord);System.out.println("连接geoserver服务器成功!");return manager;} catch (MalformedURLException e) {System.out.println("连接geoserver服务器失败");}return null;}/*** 判断工作空间是否存在 不存在则创建工作空间** @param manager* @param wokeSpaceName*/private boolean judgeWorkspace(GeoServerRESTManager manager, String wokeSpaceName) {boolean wsFlag = false;GeoServerRESTPublisher publisher = manager.getPublisher();GeoServerRESTReader reader = manager.getReader();wsFlag = reader.existsWorkspace(wokeSpaceName);//判断工作空间是否存在if (!wsFlag) {System.out.println("工作空间不存在,创建新的工作空间---" + wokeSpaceName);//创建一个新的存储空间wsFlag = publisher.createWorkspace(wokeSpaceName);System.out.println("创建工作空间是否成功" + wsFlag);return wsFlag;} else {System.out.println("工作空间已存在");}return wsFlag;}/*** 判断或注册样式** @param* @return*/private boolean judgeStyleOrCreate(GeoServerRESTManager manager, GeoServerRESTReader reader, String styleName, File styleFile, Boolean localFlag) {if (StringUtils.isEmpty(styleName) && Objects.isNull(styleFile)) {return false;}if (!StringUtils.isEmpty(styleName) && Objects.isNull(styleFile)) {boolean styleValidateFlag = styleValidate(reader, styleName);}if (Objects.nonNull(styleFile)) {if (StringUtils.isEmpty(styleName)) {String styleFileName = styleFile.getName();styleName = styleFileName.substring(0, styleFileName.indexOf("."));}System.out.println("发布样式");publishSldFile(manager, styleFile, styleName);if (!localFlag) {//不是本地文件,即为上传的FileUtil.del(styleFile);}return true;}return false;}/*** sld文件发布**/private void publishSldFile(GeoServerRESTManager manager, File styleFile, String styleName) {GeoServerRESTStyleManager styleManager = manager.getStyleManager();boolean result = false;//判断style是否已经存在,不存在则创建boolean existsStyle = styleManager.existsStyle(styleName);if (!existsStyle) {//不基于工作空间发布 true成功false失败result = styleManager.publishStyle(styleFile, styleName);} else {System.out.println("该样式已经存在");}return;}
主体发布代码
private void publishSde(ReqDTO reqDTO) {//发布的结果boolean result = false;File styleFile = null;//判断文件是否存在if (Objects.nonNull(reqDTO.getStyleFile())) {styleFile = storePgStyleFile(sdePublishReqDTO);}//获取管理器GeoServerRESTManager manager = getManager(reqDTO.getServerUrl(), reqDTO.getUserName(), reqDTO.getPassWord());//工作空间是否存在 不存在则创建工作空间boolean wsFlag = judgeWorkspace(manager, sdePublishReqDTO.getWorkspaceName());GeoServerRESTPublisher publisher = manager.getPublisher();GeoServerRESTReader reader = manager.getReader();//存储区名称String dsName = reqDTO.getDataSourceInfo().getDataBaseName() + "_" + reqDTO.getTableName;boolean existsDatastoreFlag = reader.existsDatastore(reqDTO.getWorkspaceName(), dsName);if(!existsDatastoreFlag) {System.out.println("存储区已经存在");}//判断是否要注册样式boolean styleFlag = judgeStyleOrCreate(manager, reader, reqDTO.getStyleName(), styleFile, Boolean.FALSE);//判断图层是否已经存在,不存在则创建并发布RESTLayer layer = reader.getLayer(sdePublishReqDTO.getWorkspaceName(), sdePublishReqDTO.getTableName());if (Objects.isNull(layer)) {GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();pds.setTitle(sdePublishReqDTO.getTableName());pds.setName(sdePublishReqDTO.getTableName());//pds.setSRS("EPSG:4326");不用明显定义,表若有坐标系,会自动带上GSLayerEncoder layerEncoder = new GSLayerEncoder();if (styleFlag) {layerEncoder.setDefaultStyle(sdePublishReqDTO.getStyleName());}result = publisher.publishDBLayer(reqDTO.getWorkspaceName(), dsName, pds, layerEncoder);}