java代码实现tiff上传geoserver发布服务 并设置坐标系style样式

参考大佬博文:https://blog.youkuaiyun.com/qq_42402783/article/details/102821601

自己进行了改动,增加了设置坐标系及style样式。

 

环境介绍:

      需要的jar包有:commons-codec-1.15.jar commons-io-2.2.jar commons-logging-1.2.jar geoserver-manager-1.7.0-pdok2.jar jdom-1.1.3.jar org.apache.commons.httpclient-3.1.0.jar slf4j-api-1.7.9.jar

 

环境下载:https://download.youkuaiyun.com/download/zl15145428/18581025

 

示例代码:

		// GeoServer的连接配置
		String url = "http://localhost:18081/geoserver";
		String username = "admin";
		String passwd = "geoserver";

		String workSpace = "zhangsan"; // 待创建和发布图层的工作区名称workspace
		try {
			// 判断工作区(workspace)是否存在,不存在则创建
			URL u = new URL(url);
			GeoServerRESTManager manager = new GeoServerRESTManager(u, username, passwd);
			GeoServerRESTPublisher publisher = manager.getPublisher();
			List<String> workspaces = manager.getReader().getWorkspaceNames();
			if (!workspaces.contains(workSpace)) {
				boolean createws = publisher.createWorkspace(workSpace);
				System.out.println("create ws : " + createws);
			} else {
				System.out.println("workspace已经存在了,workSpace :" + workSpace);
			}

			// 判断数据存储(datastore)是否已经存在,不存在则创建

			String fileName = "E:\\xxxx.tiff";
			String layerName = "xxxx";
			String store_name = "xxxx.tiff"; // 待创建和发布图层的数据存储名称store
			
			RESTDataStore restStore = manager.getReader().getDatastore(workSpace, store_name);
			if (restStore == null) {
				GSGeoTIFFDatastoreEncoder gsGeoTIFFDatastoreEncoder = new GSGeoTIFFDatastoreEncoder(store_name);
				gsGeoTIFFDatastoreEncoder.setWorkspaceName(workSpace);
				gsGeoTIFFDatastoreEncoder.setUrl(new URL("file:" + fileName));
				boolean createStore = manager.getStoreManager().create(workSpace, gsGeoTIFFDatastoreEncoder);
				System.out.println("create store (TIFF文件创建状态) : " + createStore);
				
				GeoServerRESTStyleManager styleManager = manager.getStyleManager();
				if (!styleManager.existsStyle("sst2")) {
					// 向geoserver写入style
					boolean publishStyleInWorkspace = styleManager.publishStyleInWorkspace(workSpace, "abc", "sst2");
					System.out.println("publishStyleInWorkspace : "+publishStyleInWorkspace);
				}		
				boolean publish = publisher.publishGeoTIFF(workSpace,store_name, layerName, new File(fileName), "EPSG:4326",
                        GSResourceEncoder.ProjectionPolicy.FORCE_DECLARED, "sst1", null);
				System.out.println("publish (TIFF文件发布状态) : " + publish);
			} else {
				System.out.println("数据存储已经存在了,store:" + store_name);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	

设置坐标系是:“EPSG::4326”

设置syle是:sst1,我这里已经上传到geoserver了,如果style没有上传,就需要“abc”处编写自己的style样式上传即可。

好了文章介绍结束,有疑问可以留言,欢迎点赞关注,路转粉不迷路。

评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值