基于python实现geoserver半自动发布tif影像的功能

最近做一个任务,是要通过代码直接实现对geoserver服务的发布。

geoserver拥有rest接口,通过这个接口可以实现服务的自动发布。

第一阶段:

从网上找到了CURL对rest接口的调用,就想怎么样可以调用curl。(不明白什么是CURL的可以了解一下)

从geoserver官网的API中我们能发现CURL的操作

创建工作空间(修改ACE为你自己想命名的名称)
 curl -v -u admin:geoserver -XPOST -H "Content-type: text/xml" -d "<workspace><name>ACE</name></workspace>" http://localhost:8080/geoserver/rest/workspaces
curl -u admin:geoserver -XPUT -H "Content-type:image/tiff" --data-binary @D:/apache-tomcat-7.0.88/webapps/geoserver/data/test.tif http://localhost:8080/geoserver/rest/workspaces/ACE/coveragestores/test.tif/file.geotiff

注意第二段代码有路径,工作空间,文件名需要更改。

通过上述两段代码已经可以实现geoserver发布tif影像了,但是离自动还有着很远的距离。

如果可以通过一个简单的程序,点击后,不仅可以自动发布,还可以弹出发布后的页面是不是更好呢?

虽然基本上所有的语言都支持调用CDM控制台(DOS指令)。

前期试了C/C++,但是对于处理文件名&路径等,字节转换会比较麻烦,就决定用python(python特别友好。)

因此,博主选用了python。

python所需的库

import os
import webbrowser
import tkinter.filedialog

os用来执行DOS指令,webbrowser用来打开浏览器,tkinter.filedialog用来打开窗口选择路径。

首先通过 tkinter.filedialog 打开弹窗,便于自己选择文件。

# 文件路径
fpname = tkinter.filedialog.askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser((default_dir))))

文件选中了,同样上述操作也可以得到文件路径,但是并没有单独的文件名。

接下来就是如何得到文件名的信息了。

#文件名称
fname = os.path.split(fpname)[-1]

接着通过python调用DOS指令

os.system('curl -v -u admin:geoserver -XPOST -H "Content-type: text/xml" -d "<workspace><name>'+namespace+'</name></workspace>" http://localhost:8080/geoserver/rest/workspaces')

os.system('curl -u admin:geoserver -XPUT -H "Content-type:image/tiff" --data-binary @'+fpname+' http://localhost:8080/geoserver/rest/workspaces/'+namespace+'/coveragestores/'+fname+'/file.geotiff')

最后通过webbrowser打开发布后的页面。(URL属性可以修改)

url = 'http://localhost:8080/geoserver/'+namespace+'/wms?service=WMS&version=1.1.0&request=GetMap&layers='+namespace+':'+fname+'&styles=&bbox=395085.0,5292585.0,630615.0,5531415.0&width=757&height=768&srs=EPSG:32653&format=application/openlayers'

这样就完成了一个半自动发布的程序,下载地址。(要是没有积分可以留邮箱...。)

https://download.csdn.net/download/yuelizhe4774/10588759

猜你喜欢

转载自blog.csdn.net/yuelizhe4774/article/details/81483757