文章目录
- 简要说明
- maven依赖
- 样例代码
简要说明
geoserver本身是不支持GDB的发布,不过可以利用gdal,将GDB的图层转为shpfile,就可以进行发布。
maven依赖
<!--geoserver需要的jar--><dependency><groupId>com.github.dov-vlaanderen</groupId><artifactId>geoserver-manager</artifactId><version>1.8</version></dependency><!--gdal包--><!--需要安装完gdal后,本地install gdal包才能使用 --><!--gdal安装可参考 https://blog.csdn.net/qq_41613913/article/details/135743562 --><dependency><groupId>org.gdal</groupId><artifactId>gdal</artifactId><version>3.7.3</version></dependency>
样例代码
封装的入参实体
@Data
public class ReqDTO {/*** 站点地址*/private String serverUrl;/*** 站点用户名称*/private String userName;/*** 站点密码*/private String passWord;/*** 工作区名称*/private String workspaceName;/***文件路径*/private String dataPath;/***gdb的图层名称* 怎么获取图层名称,可以参考 https://blog.csdn.net/qq_41613913/article/details/135746653*/private String layerName;/***样式名称*/private String styleName;/***样式文件路径,若有样式文件,会注册样式*/private String styleFilePath;
}
辅助方法
/*** gdb的单图层转shpfile*/private void gdbSingleLayer2Shpfile(String gdbDirPath, String layerName, String tempPath) {// 注册所有的驱动gdal.AllRegister();// 为了支持中文路径,请添加下面这句代码gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");// 为了使属性表字段支持中文,请添加下面这句gdal.SetConfigOption("SHAPE_ENCODING", "CP936");//打开gdb数据DataSource gdbDataSource = ogr.Open(gdbDirPath,0);Layer sourceLayer = gdbDataSource.GetLayerByName(layerName);//打开shp 引擎Driver shpDriver = ogr.GetDriverByName("ESRI Shapefile");//指定shp存储路径File tempPathFile = new File(tempPath);File parentFile = tempPathFile .getParentFile();if (!parentFile.exists()) {parentFile.mkdirs();}DataSource shpDataSource = shpDriver.CreateDataSource(targetPath);shpDataSource.CopyLayer(sourceLayer,layerName);//释放资源shpDataSource.delete();shpDriver.delete();gdbDataSource.delete();gdal.GDALDestroyDriverManager();}
/*** 获取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;}
/*** 工作空间是否存在 不存在则创建工作空间**/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("工作空间不存在,创建工作空间");//创建一个新的存储空间wsFlag = publisher.createWorkspace(wokeSpaceName);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;}/*** 本地shpfile 数据存储区判断和创建*/private boolean dataValidateOrCreate(GeoServerRESTManager manager,GeoServerRESTReader reader, String workspaceName, File shpFile) {String shpFileName = shpFile.getName();String dsName = shpFileName.substring(0, shpFileName.indexOf("."));boolean existsDatastore = reader.existsDatastore(workspaceName, dsName);if (!existsDatastore) {//创建shape文件存储try {//shp文件所在的位置String urlDataStorePath = shpFile.getPath();// 数据存储需要的文件String shpFilePath = String.format("file://%s", urlDataStorePath);URL urlShapeFile = new URL(shpFilePath);// 创建数据集GSShapefileDatastoreEncoder datastoreEncoder = new GSShapefileDatastoreEncoder(dsName, urlShapeFile);existsDatastore = manager.getStoreManager().create(workspaceName, datastoreEncoder);} catch (Exception e) {System.out.println("创建shp存储区异常");}}return existsDatastore;}
主体发布代码
private void publishGdbSinglelayerLocal(ReqDTO reqDTO, String tempPath) {//gdb图层转shpfilegdbSingleLayer2Shpfile(reqDTO.getDataPath, reqDTO.getLayerName, tempPath)//发布的结果boolean result = false;//获取管理器GeoServerRESTManager manager = getManager(reqDTO.getServerUrl(), reqDTO.getUserName(), reqDTO.getPassWord());System.out.println("工作空间是否存在");//工作空间是否存在 不存在则创建工作空间boolean wsFlag = judgeWorkspace(manager, reqDTO.getWorkspaceName());GeoServerRESTPublisher publisher = manager.getPublisher();GeoServerRESTReader reader = manager.getReader();//判断是否要注册样式File styleFile = null;if (!StringUtil.isEmpty(reqDTO.getStyleFilePath())) {styleFile = new File(reqDTO.getStyleFilePath());if(!styleFile.exists()) {System.out.println("样式文件不存在");return;}}boolean styleFlag = judgeStyleOrCreate(manager, reader, reqDTO.getStyleName(), styleFile , Boolean.TRUE);//若有样式,重新设置值if (styleFlag) {String styleName = localFilePublishReqDTO.getStyleName();if (StringUtil.isEmpty(styleName) && Objects.nonNull(styleFile)) {String styleFileName = styleFile.getName();styleName = styleFileName.substring(0, styleFileName.indexOf("."));reqDTO.setStyleName(styleName);}}//判断存储区是否存在,没有就创建boolean dsFlag = dataValidateOrCreate(manager, reader,reqDTO.getWorkspaceName(), shpFile);if(!dsFlag){System.out.println("数据存储区创建失败");return;}//判断图层是否已经存在,不存在则创建并发布String layerName = reqDTO.getLayerName;RESTLayer layer = reader.getLayer(reqDTO.getWorkspaceName(), layerName);if (Objects.isNull(layer)) {GSFeatureTypeEncoder gsFeatureTypeEncoder = new GSFeatureTypeEncoder();gsFeatureTypeEncoder.setTitle(layerName);gsFeatureTypeEncoder.setName(layerName);GSLayerEncoder gsLayerEncoder = new GSLayerEncoder();if (styleFlag) {gsLayerEncoder.setDefaultStyle(reqDTO.getStyleName());}String shpFileName = shpFile.getName();String dsName = shpFileName.substring(0, shpFileName.indexOf("."));result = publisher.publishDBLayer(reqDTO.getWorkspaceName(), dsName, gsFeatureTypeEncoder, gsLayerEncoder);} else {System.out.println("shp图层已经发布过了");}}