1、Why?
通过geoserver的web界面上传数据时,只能单个文件上传,当需要上传几百上千个文件时,操作以来就非常恶心了。
2、How?
geoserver提供了API可以通过代码实现批量上传,本文采用python来实现。
本文测试环境:
geoserver 2.13.2
python 3.7
首先,安装python第三方库gsconfg-py3(对应python3版本)
安装以后,python代码如下。本文实现了对SRTM的geotiff数据的批量上传。
-
from import Catalog
-
import os
-
-
fileheader = "file://E:/Data/SRTM_db/" #本地数据路径
-
filter =[".tif"]
-
readpath = "E://Data//SRTM_db"
-
geourl = "http://192.168.1.200:5433/geoserver/rest/" # the url of geoserver
-
geocat = Catalog(geourl) # create a Catalog object,默认用户名和密码
-
workspace = geocat.get_workspace("Global") # workspace name
-
-
def uploadGeotiff(filename):
-
store_name = filename
-
data_url = fileheader+filename
-
geostore = geocat.create_coveragestore_external_geotiff(store_name, data_url, workspace,True)
-
-
def listFiles(filepath):
-
for maindir,subdir,file_name_list in (filepath):
-
for filename in file_name_list:
-
apath = (maindir, filename)
-
ext = (apath)[1]
-
if ext in filter:
-
uploadGeotiff(filename) # 依次上传tif数据
-
-
listFiles(readpath)