基于Python實現GeoServer矢量文件批量發(fā)布
0. 前言
由于矢量圖層文件較多,手動發(fā)布費時費力,python支持的關于geoserver包(geoserver-restconfig)又由于年久失修,無法在較新的geoserver版本中正常使用。
查閱了很多資料,參考了下面這篇博客,我簡單寫了一個自動化發(fā)布矢量文件的代碼。
基本流程:獲取指定文件夾下所有的.shp文件,在通過模擬正常發(fā)布的流程逐個發(fā)布。
Python+Selenium實現在Geoserver批量發(fā)布Mongo矢量數據
1. 環(huán)境
1.1 基礎環(huán)境
首先你的電腦要有python環(huán)境、谷歌瀏覽器和geoserver2.19左右的版本
接著在命令行中通過如下指令,安裝Web自動化測試工具selenium
pip install selenium
1.2 谷歌瀏覽器驅動
此外,還需要谷歌瀏覽器的對應驅動。
首先需要查詢你的谷歌瀏覽器的版本,在谷歌瀏覽器的網址欄輸入chrome://version/,第一行就是版本號

在這個網址中找到對應版本號的驅動
這里和我的谷歌瀏覽器最匹配的驅動是

下載windows版本的驅動

解壓后將exe文件放置在main.py文件所在的目錄下。
2. 基本流程
2.1 初始化
運行代碼后,程序會自動開啟一個google瀏覽器窗口,接著進入geoserver。

2.2 登錄
自動輸入用戶名和密碼,并點擊登錄

2.3 新建數據源
進入新建數據源發(fā)布頁面
http://localhost:8080/geoserver/web/wicket/bookmarkable/org.geoserver.web.data.store.NewDataPage
選擇shapefile文件格式
2.4 保存數據存儲
選擇工作區(qū),數據源名稱,shapefile文件的位置,設置DBF字符集,點擊保存

2.5 發(fā)布圖層
首先點擊發(fā)布

接著設置源坐標系,目標坐標系,原始邊界和目標邊界

最后點擊保存完成發(fā)布
3. 完整代碼
main.py
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)
# 發(fā)布一個圖層服務
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)
# 選擇工作區(qū)
driver.find_element_by_xpath("http://fieldset/div[1]/div/select").send_keys(workplace)
# 輸入數據源名稱
driver.find_element_by_xpath("http://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("http://fieldset/div[2]/div/select").send_keys("GB2312")
# 點擊保存
driver.find_element_by_link_text("保存").click()
## ----------------發(fā)布圖層--------------
sleep(0.8)
# 點擊發(fā)布
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)
# 發(fā)布圖層
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" # 工作區(qū)名
# geoserver根網址
baseUrl = "http://localhost:8080/geoserver/"
# 發(fā)布文件所在文件夾的絕對路徑
absolutePath = "D:\\geoserver-2.19.1-bin\\data_dir\\test_res\\"
files = getFiles(absolutePath, ".shp")
# 啟動瀏覽器
driver = webdriver.Chrome()
login()
for file in files:
publish_a_layer(workplace, absolutePath, file)
以上就是基于Python實現GeoServer矢量文件批量發(fā)布的詳細內容,更多關于Python GeoServer矢量文件發(fā)布的資料請關注腳本之家其它相關文章!
相關文章
解決Python3錯誤:SyntaxError: unexpected EOF while
這篇文章主要介紹了解決Python3錯誤:SyntaxError: unexpected EOF while parsin問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Python編程使用matplotlib繪制動態(tài)圓錐曲線示例
這篇文章主要介紹了Python使用matplotlib繪制動態(tài)的圓錐曲線示例實現代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-10-10

