GeoServer自动发布地图服务
?
1 NetCDF氣象文件自動(dòng)發(fā)布案例
GeoServer是一個(gè)地理服務(wù)器,提供了管理頁面進(jìn)行服務(wù)發(fā)布,樣式,切片,圖層預(yù)覽等一系列操作,但是手動(dòng)進(jìn)行頁面配置有時(shí)并不滿足業(yè)務(wù)需求,所以GeoServer同時(shí)提供了豐富的rest接口可供用戶自己組織業(yè)務(wù)邏輯進(jìn)行自動(dòng)化管理。
本文以氣象文件的NetCDF自動(dòng)化發(fā)布的需求,闡述如何以rest接口實(shí)現(xiàn)用戶多樣性需求。氣象文件特殊性在于幾乎每隔一段時(shí)間就會(huì)更新,甚至逐小時(shí)或半小時(shí)的更新頻率,用戶如果手動(dòng)發(fā)布了氣象文件的若干圖層作為專題服務(wù),一旦獲取到最新的氣象文件,用戶希望立馬可以看到新的數(shù)據(jù)源上的專題圖,而人工即時(shí)更新現(xiàn)有的圖層服務(wù)幾乎是不現(xiàn)實(shí)的,類似這種定時(shí)或者即時(shí)響應(yīng)的需求應(yīng)該交由自動(dòng)化完成,本文實(shí)現(xiàn)NetCDF氣象文件自動(dòng)發(fā)布便是為了解決此類需求。
?
1.1 NetCDF插件安裝
選擇對(duì)應(yīng)版本的下載地址:http://geoserver.org/release/2.11.0/
下載插件,解壓,將jar文件全部復(fù)制到geoserver中的webapps\geoserver\WEB-INF\lib目錄中,重啟geoserver即可。
1.2 rest示例
發(fā)布nc文件數(shù)據(jù)存儲(chǔ)
將E:\xxx.nc該文件發(fā)布成柵格數(shù)據(jù)存儲(chǔ),發(fā)布到cite工作區(qū),數(shù)據(jù)存儲(chǔ)名稱為netcdfstore。
curl -v -u admin:geoserver -XPOST -H "Content-type: text/xml" -d"<coverageStore><name>netcdfstore</name><type>NetCDF</type><enabled>true</enabled>
<workspace><name>cite</name></workspace><__default>false</__default>
<url>file://E://xxx.nc</url></coverageStore>"
http://localhost:8090/geoserver/rest/workspaces/cite/coveragestores/netcdfstore
?注意路徑格式是:file://E://xxx.nc,而不是file://E:\xxx.nc或file://E:\\xxx.nc,這應(yīng)該是該插件的一個(gè)bug
修改nc文件數(shù)據(jù)存儲(chǔ)
將netcdfstore的數(shù)據(jù)存儲(chǔ)位置由E:\xxx.nc指向D:\xxv.nc。
curl -v -u admin:geoserver -XPUT -H "Content-type: text/xml" -d"<coverageStore><name>netcdfstore</name><type>NetCDF</type><enabled>true</enabled>
<workspace><name>cite</name></workspace><__default>false</__default>
<url>file://D://xxc.nc</url></coverageStore>"
http://localhost:8090/geoserver/rest/workspaces/cite/coveragestores/netcdfstore
發(fā)布柵格圖層
將netcdfstore數(shù)據(jù)存儲(chǔ)中的RH2圖層發(fā)布
curl -v -u admin:geoserver -XPOST -H "Content-type: text/xml" -d "<coverage><nativeCoverageName>RH2</nativeCoverageName><name>RH2</name></coverage>" http://localhost:8090/geoserver/rest/workspaces/cite/coveragestores/netcdfstore/coverages?綁定圖層樣式
將發(fā)布的RH2樣式綁定已經(jīng)發(fā)布的一個(gè)名稱叫RH2Style的樣式。
curl -v -u admin:geoserver -XPUT -H "Content-type: text/xml" -d "<layer> <defaultStyle><name>RH2Style</name></defaultStyle></layer>" http://localhost:8090/geoserver/rest/layers/RH21.3 自動(dòng)化發(fā)布
Node.js
var child_process = require('child_process'); var async = require('async'); //構(gòu)造一個(gè)netcdf管理類 function NetCDFManager(options){this.ip=options.ip;this.port=options.port;this._geoserverurl=`http://${this.ip}:${this.port}/geoserver/rest`;this.user=options.user;//geoserver的用戶名密碼this.password=options.password;this.layerlist=options.layerlist;this.ws=(options.ws!==undefined)?options.ws:'netcdf';//工作區(qū)間,默認(rèn)是netcdf工作區(qū)間this.storename=(options.storename!==undefined)?options.storename:'netcdfstore';//netcdf數(shù)據(jù)存儲(chǔ)名稱,默認(rèn)是netcdfstore } //根據(jù)名稱獲取柵格數(shù)據(jù)存儲(chǔ) NetCDFManager.prototype.getCoverageStorebyName=function(cb){let storename=this.storename;let url=this._geoserverurl+`/workspaces/${this.ws}/coveragestores/${storename}.json`;var cmd=`curl -v -u ${this.user}:${this.password} -XGET ${url}`;child_process.exec(cmd, function(err,stdout,stderr) {if(stdout.indexOf('No such')>-1){cb(false);return;}if(JSON.parse(stdout).coverageStore.name===storename)cb(true);elsecb(false);}); } //發(fā)布一個(gè)柵格數(shù)據(jù)存儲(chǔ) NetCDFManager.prototype.publishCoverageStore = function(netcdffile,cb){netcdffile=netcdffile.replace(/\\/g,'//');var xml=`<coverageStore><name>${this.storename}</name><type>NetCDF</type><enabled>true</enabled><workspace><name>${this.ws}</name></workspace><__default>false</__default><url>file://${netcdffile}</url></coverageStore>`;var cmd=`curl -v -u ${this.user}:${this.password} -XPOST -H "Content-type: text/xml" -d "${xml}" ${this._geoserverurl}/workspaces/${this.ws}/coveragestores`;child_process.exec(cmd, function(err,stdout,stderr) {if(stdout=='')cb(true);elsecb(false);}); } //修改已發(fā)布的數(shù)據(jù)存儲(chǔ) NetCDFManager.prototype.updateCoverageStore = function(netcdffile,cb){netcdffile=netcdffile.replace(/\\/g,'//');var xml=`<coverageStore><name>${this.storename}</name><type>NetCDF</type><enabled>true</enabled><workspace><name>${this.ws}</name></workspace><__default>false</__default><url>file://${netcdffile}</url></coverageStore>`;var cmd=`curl -v -u ${this.user}:${this.password} -XPUT -H "Content-type: text/xml" -d "${xml}" ${this._geoserverurl}/workspaces/${this.ws}/coveragestores/${this.storename}`;child_process.exec(cmd, function(err,stdout,stderr) {if(stdout=='')cb(true);elsecb(false);});} //發(fā)布一個(gè)圖層 NetCDFManager.prototype.publishCoverage = function(coverage_name,cb){let xml=`<coverage><nativeCoverageName>${coverage_name}</nativeCoverageName><name>${coverage_name}</name></coverage>`;let url=`${this._geoserverurl}/workspaces/${this.ws}/coveragestores/${this.storename}/coverages`;var cmd=`curl -v -u ${this.user}:${this.password} -XPOST -H "Content-type: text/xml" -d "${xml}" ${url}`;child_process.exec(cmd, function(err,stdout, stderr) {if(stdout=='')cb(true);elsecb(false);}); } //給發(fā)布的圖層賦予樣式 NetCDFManager.prototype.setLayerStyle = function(layername,stylename,cb){let xml=`<layer><defaultStyle><name>${stylename}</name></defaultStyle></layer>`;let url=`${this._geoserverurl}/layers/${layername}`;var cmd=`curl -v -u ${this.user}:${this.password} -XPUT -H "Content-type: text/xml" -d "${xml}" ${url}`;child_process.exec(cmd, function(err,stdout, stderr) {if(stdout=='')cb(true);elsecb(false);}); }/* 偽邏輯代碼1 根據(jù)數(shù)據(jù)存儲(chǔ)名稱,判定是否有該數(shù)據(jù)存儲(chǔ)。沒有,publishCoverageStore一個(gè),接步驟2.有,updateCoverageStore即可,end! 2 publishCoverageStore發(fā)布數(shù)據(jù)存儲(chǔ)后,將規(guī)定要發(fā)布的圖層逐一發(fā)布publishCoverage,逐一賦予樣式setLayerStyle 注意都是異步的,需要后臺(tái)代碼轉(zhuǎn)同步,js中的async庫負(fù)責(zé)處理異步陷阱,其他語言自行百度。*/var netCDFManager=new NetCDFManager({ip:'localhost',port:'8090',user:'admin',password:'geoserver',ws:'netcdf',storename:'netcdfstore',layerlist:['RH2','SKT','TP','V10','VIS'] }); function publish(ncfile) {async.waterfall([//查詢是否已經(jīng)存在命名為netcdfstore的數(shù)據(jù)存儲(chǔ)function (done) {netCDFManager.getCoverageStorebyName(function (info) {done(null, info);});},function (info, done) {//已存在數(shù)據(jù)存儲(chǔ),直接替換其數(shù)據(jù)源為新的nc文件if (info) {console.log('指定的數(shù)據(jù)存儲(chǔ)已存在,直接進(jìn)行更新操作');netCDFManager.updateCoverageStore(ncfile, function (info) {if (info) {console.log('數(shù)據(jù)存儲(chǔ)已經(jīng)更新成功!');done(null, info);} else {console.log('數(shù)據(jù)存儲(chǔ)已經(jīng)更新失敗!');done(info, null);}});}//不存在數(shù)據(jù)存儲(chǔ),新發(fā)布else {console.log('指定的數(shù)據(jù)存儲(chǔ)不存在,發(fā)布數(shù)據(jù)存儲(chǔ)');publishNC(ncfile, done);}}], function (error, result) {if (error)console.log('自動(dòng)發(fā)布存在錯(cuò)誤!');elseconsole.log('自動(dòng)發(fā)布完成!');}) }function publishNC(ncfile,cb){async.waterfall([function (done) {netCDFManager.publishCoverageStore(ncfile,function(info){if(info){console.log('數(shù)據(jù)存儲(chǔ)已經(jīng)發(fā)布成功!');done(null, info);}else{console.log('數(shù)據(jù)存儲(chǔ)已經(jīng)發(fā)布失敗!');done(info, null);}});}, function (resule,done) {//發(fā)布圖層 publishLayers(netCDFManager.layerlist,done);},function (result,done) {//發(fā)布樣式 publishStyles(netCDFManager.layerlist,done);}],function (error, result) {if(error){console.log('自動(dòng)發(fā)布存在錯(cuò)誤!');cb(error,null);}else{console.log('自動(dòng)發(fā)布完成!');cb(null,result);}}) } //自動(dòng)發(fā)布一些列圖層 function publishLayers(layerlist,cb){let asyncs={};for(let i=0;i<layerlist.length;i++){asyncs[i]=function(done){let layername=layerlist[i];netCDFManager.publishCoverage(layername,function(info){if(info){console.log(`${layername}發(fā)布成功!`);done(null, info);}else{console.log(`${layername}發(fā)布失敗!`);done(info, null);}});}}async.parallel(asyncs, function (error, result) {if(error)cb(error,null);elsecb(null,result);}) }//修改指定圖層為指定樣式 function publishStyles(stylelist,cb){let asyncs={};for(let i=0;i<stylelist.length;i++){asyncs[i]=function(done){let layername=stylelist[i];netCDFManager.setLayerStyle(layername,layername,function(info){if(info){console.log(`${layername}樣式發(fā)布成功!`);done(null, info);}else{console.log(`${layername}樣式發(fā)布失敗!`);done(info, null);}});}}async.parallel(asyncs, function (error, result) {if(error)cb(error,null);elsecb(null,result);}) }publish('D:\\G_2017070419.nc');執(zhí)行node app.js后
?
?
perfect!
?
2 實(shí)現(xiàn)批量發(fā)布地圖服務(wù)
上文《GeoServer發(fā)布地圖服務(wù) 》介紹了如何利用GeoServer發(fā)布WCS服務(wù),那么如果我有很多數(shù)據(jù)需要進(jìn)行發(fā)布,這樣利用GeoServer提供的UI界面進(jìn)行操作顯然很不顯示。那能不能利用GeoServer提供的API進(jìn)行操作呢?GeoServer提供了REST API方便我們利用代碼進(jìn)行操作。用戶手冊(cè)中提供了如下語言或方法進(jìn)行操作:cURL,PHP,Python,Java和Ruby。
可惜的是除了cURL有詳細(xì)的文檔之外,其它語言參考文檔很少。不得不說開源軟件就是沒有很好的技術(shù)支持,畢竟是開源免費(fèi)的,也不可能有很好的技術(shù)支持,免費(fèi)開源給你用就是最大的奉獻(xiàn)了。哈哈,支持開源!
Java篇
我先使用了Java語言的geoserver manager。在Eclipse新建一個(gè)Maven工程,添加相應(yīng)的依賴包,下面是一個(gè)讀出數(shù)據(jù)的例子:
public static boolean read() {String restUrl = "http://localhost/geoserver";String username = "admin";String password = "geoserver";GeoServerRESTReader reader;try {reader = new GeoServerRESTReader(restUrl, username, password);} catch (MalformedURLException e) {e.printStackTrace();return false;}String workspace = "whu.images";String store = "00N006E";String name = "00N006E";RESTCoverage coverage = reader.getCoverage(workspace, store, name);System.out.println(coverage.getAbstract());return true;}但是我在寫入柵格數(shù)據(jù)的時(shí)候出現(xiàn)了一些問題,如下是數(shù)據(jù)存儲(chǔ)的類繼承關(guān)系:
?
?我們可以看到Coverage Store沒有實(shí)現(xiàn)類,GSAbstractCoveragestoreEncoder是一個(gè)抽象類,而且是被標(biāo)注@Deprecated的,所以我不知道怎么新建Coverage Store,本來想自己寫一個(gè)實(shí)現(xiàn)類,最終還是放棄了。
Python篇
后來才用的Python解決了問題,但是也不是一帆風(fēng)順的。
首先安裝gsconfig包,如果不知道如何安裝,參考Python模塊常用的幾種安裝方式。
安裝完以后,代碼如下:
如下,采用默認(rèn)的用戶名,密碼,默認(rèn)的工作空間,所以函數(shù)的參數(shù)很少,如果你要自定義這些,詳細(xì)查看函數(shù)的說明。
但是上面使用create_coveragestore有一個(gè)問題,即會(huì)將你的文件默認(rèn)拷貝到你的Data Directory中,如果你數(shù)據(jù)很多,這樣你就會(huì)有兩份數(shù)據(jù)了,極大的浪費(fèi)了磁盤空間。
后來發(fā)現(xiàn)Catalog類有提供一個(gè)create_coveragestore2的方法,可以創(chuàng)建一個(gè)UnSavedCoveragestore,數(shù)據(jù)不會(huì)上傳。
from geoserver.catalog import Cataloggeourl = "http://localhost/geoserver/rest" # the url of geoserver geocat = Catalog(geourl) # create a Catalog object store_name = "00N010E" data_url = "fiel:E:/RSImageService/data/images/00N010E.tif" geostore = geocat.create_coveragestore2(store_name) geostore.url = data_url geocat.save(geostore)但是程序一運(yùn)行就回返回一個(gè)服務(wù)器內(nèi)部錯(cuò)誤505,Error code (505) from geoserver:: data store must be part of a workspace.
最后自己寫了一個(gè)方法用于發(fā)布GeoTIFF影像(從GitHub上看到的一段代碼,運(yùn)行有點(diǎn)問題,然后自己修改了下)。給Catalog類添加一個(gè)create_coveragestore3方法,用戶發(fā)布柵格數(shù)據(jù),同時(shí)不復(fù)制數(shù)據(jù)。這需要修改gsconfig源代碼,然后重新編譯下。
create_coveragestore3方法如下:
def create_coveragestore3(self, name, data_url, workspace=None, overwrite=False):if not overwrite:try:store = self.get_store(name, workspace)msg = "There is already a store named " + nameif workspace:msg += " in " + str(workspace)raise ConflictingDataError(msg)except FailedRequestError:# we don't really expect that every layer name will be takenpassif workspace is None:workspace = self.get_default_workspace()headers = {"Content-type": "text/plain","Accept": "application/xml"}ext = "geotiff"cs_url = url(self.service_url,["workspaces", workspace.name, "coveragestores", name, "external." + ext],{ "configure" : "first", "coverageName" : name})headers, response = self.http.request(cs_url, "PUT", data_url, headers)self._cache.clear()if headers.status != 201:raise UploadError(response)最后的客戶端調(diào)用代碼:
from geoserver.catalog import Cataloggeourl = "http://localhost/geoserver/rest" # the url of geoserver geocat = Catalog(geourl) # create a Catalog object store_name = "00N010E" data_url = "file:E:/RSImageService/data/images/00N010E.tif" geocat.create_coveragestore3(store_name, data_url)如果你要發(fā)布很多數(shù)據(jù),遍歷文件夾調(diào)用create_coveragestore3即可。
3. 利用java后臺(tái)進(jìn)行g(shù)eoserver查詢
使用后臺(tái)的原因
?
由于項(xiàng)目要求,之前的函數(shù)必須要拆開封裝,但對(duì)于jsonp來說,回調(diào)函數(shù)一旦分開,就會(huì)有異步的問題(jsonp永遠(yuǎn)都是異步的,除非你將處理都放到回調(diào)中去)。所以考慮從前臺(tái)傳參到后臺(tái)方法去處理,后臺(tái)再通過url來進(jìn)行寫入。后臺(tái)的主要實(shí)現(xiàn)方式
/** * geoserver查詢 * @param url 基地址 * @param layer 圖層名 * @param key 鍵 * @param value 值 * @return */ public String Geo2server(String url,String layer,String key,String value){ StringBuilder json = new StringBuilder(); MsgBox box = null; try { url += "?service=WFS&version=1.1.0&request=GetFeature&typeName=" + layer + "&outputFormat=application%2Fjson&filter=<Filter><PropertyIsEqualTo>" + "<PropertyName>"+ key +"</PropertyName>" + "<Literal>"+ value +"</Literal>" + "</PropertyIsEqualTo></Filter>"; URL newUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) newUrl.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8")); String inputLine = null; while ( (inputLine = in.readLine()) != null) { json.append(inputLine); } in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return json.toString(); }注意事項(xiàng)
由于沒有做數(shù)據(jù)的分析,所以有可能返回錯(cuò)誤的數(shù)據(jù),但概率很小,只要你地址沒寫出,沒數(shù)據(jù)的時(shí)候也能返回null,也能成功。?
?
?
?
?
?
?
?
參考文章
遙想公瑾當(dāng)年,GeoServer實(shí)現(xiàn)NetCDF氣象文件自動(dòng)發(fā)布
TheOneGIS, GeoServer:代碼實(shí)現(xiàn)批量發(fā)布地圖服務(wù)
WilsonOnIsland, 利用java后臺(tái)進(jìn)行g(shù)eoserver查詢
轉(zhuǎn)載于:https://www.cnblogs.com/arxive/p/8416427.html
總結(jié)
以上是生活随笔為你收集整理的GeoServer自动发布地图服务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海上牧云记苏语凝结局 毒酒穿肠过死在穆如
- 下一篇: 深入理解ES6 pdf