Python实现GeoServer矢量文件的批量发布
我是本际云服务器推荐网的小编小本本,今天给大家详细介绍一下Python实现GeoServer矢量文件的批量发布。

操作步骤
由于矢量图层文件较多,手动发布费时费力,Python支持的关于GeoServer包(GeoServer-restconfig)又由于年久失修,无法在较新的GeoServer版本中正常使用。因此,我参考了资料,写了一个自动化发布矢量文件的代码。具体操作步骤如下:
- 安装Python环境、谷歌浏览器和GeoServer2.19左右的版本。
- 在命令行中通过指令,安装Web自动化测试工具Selenium。
- 下载谷歌浏览器的对应驱动,并将exe文件放置在main.py文件所在的目录下。
- 运行代码后,程序会自动开启一个Google浏览器窗口,接着进入GeoServer。
- 自动输入用户名和密码,并点击登录。
- 进入新建数据源发布页面,选择Shapefile文件格式。
- 选择工作区,数据源名称,Shapefile文件的位置,设置DBF字符集,点击保存。
- 依次发布每一个图层,设置源坐标系、目标坐标系、原始边界和目标边界,最后点击保存完成发布。
代码示例
以下是主要代码示例,实现获取指定文件夹下所有的.shp文件,并通过模拟正常发布的流程逐个发布图层:
from time import sleep
from selenium import webdriver
import os
#登录
def login():
driver.get(baseUrl)
driver.find_element_by_id("username").send_keys(username)#填入用户名
driver.find_element_by_id("password").send_keys(password)#填入密码
driver.find_element_by_css_selector(".positive").click()
sleep(0.8)
#发布一个图层服务
def publish_a_layer(workplace,path,file,defined_srs="EPSG:3857"):
##------------存储数据----------------
#进入数据存储
driver.get(baseUrl+"web/wicket/bookmarkable/org.geoserver.web.data.store.NewDataPage")
#选择shapefile格式
driver.find_element_by_link_text("Shapefile").click()
sleep(0.8)
#选择工作区
driver.find_element_by_xpath("//fieldset/div[1]/div/select").send_keys(workplace)
#输入数据源名称
driver.find_element_by_xpath("//fieldset/div[2]/div/input").send_keys(file)
#清空原有的连接参数
driver.find_element_by_css_selector(".longtext").clear()
#输入Shapefile文件的位置
driver.find_element_by_css_selector(".longtext").send_keys("file:"+path+file+".shp")
#选择DBF的字符集
driver.find_element_by_xpath("//fieldset/div[2]/div/select").send_keys("GB2312")
#点击保存
driver.find_element_by_link_text("保存").click()
##----------------发布图层--------------
sleep(0.8)
#点击发布
driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div[2]/div/div[2]/div/table/tbody/tr/td[3]/span/a").click()
sleep(0.8)
#输入图层命名
driver.find_element_by_css_selector("input#name").clear()
driver.find_element_by_css_selector("input#name").send_keys(file)
#输入图层标题
driver.find_element_by_css_selector("input#title").clear()
driver.find_element_by_css_selector("input#title").send_keys(file)
#输入定义SRS
driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div[2]/form/div[2]/div[2]/div[1]/div/ul/div/li[1]/fieldset/ul/li[2]/span/input").clear()
driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div[2]/form/div[2]/div[2]/div[1]/div/ul/div/li[1]/fieldset/ul/li[2]/span/input").send_keys(defined_srs)
#设置边界
driver.find_element_by_link_text("从数据中计算").click()
driver.find_element_by_link_text("Compute from native bounds").click()
driver.find_element_by_id("srsHandling").send_keys("Reproject native to declared")
driver.find_element_by_link_text("从数据中计算").click()
driver.find_element_by_link_text("Compute from native bounds").click()
sleep(0.8)
#发布图层
driver.find_element_by_link_text("保存").click()
sleep(1)
#查找dir目录中文件后缀为suffix的文件
def getFiles(dir,suffix):
res=[]
for root,directory,files in os.walk(dir):#当前根,根下目录,目录下的文件
for filename in files:
name,suf=os.path.splitext(filename)#文件名,文件后缀
if suf==suffix:
res.append(name)#把一串字符串组合成路径
return res
#配置参数
username="admin"#用户名
password="geoserver"#密码
workplace="test"#工作区名
#geoserver根网址
baseUrl="http://localhost:8080/geoserver/"
#发布文件所在文件夹的绝对路径
absolutePath="D:geoserver-2.19.1-bindata_dirtest_res"
files=getFiles(absolutePath,".shp")
#启动浏览器
driver=webdriver.Chrome()
login()
for file in files:
publish_a_layer(workplace,absolutePath,file)
结语
这篇文章通过Python和Selenium实现了自动化发布矢量文件的代码,省去了手动发布费时费力的过程,希望能对有需要的读者提供帮助。
原创文章,作者:小编小本本,如若转载,请注明出处:https://www.benjiyun.com/yunzhujiyunwei/vps-yunwei/7126.html
