geoserver批量上传数据

时间:2024-12-16 18:11:43

1、Why?

通过geoserver的web界面上传数据时,只能单个文件上传,当需要上传几百上千个文件时,操作以来就非常恶心了。

2、How?

geoserver提供了API可以通过代码实现批量上传,本文采用python来实现。

本文测试环境:

geoserver 2.13.2

python 3.7

首先,安装python第三方库gsconfg-py3(对应python3版本)

安装以后,python代码如下。本文实现了对SRTM的geotiff数据的批量上传。

  1. from import Catalog
  2. import os
  3. fileheader = "file://E:/Data/SRTM_db/" #本地数据路径
  4. filter =[".tif"]
  5. readpath = "E://Data//SRTM_db"
  6. geourl = "http://192.168.1.200:5433/geoserver/rest/" # the url of geoserver
  7. geocat = Catalog(geourl) # create a Catalog object,默认用户名和密码
  8. workspace = geocat.get_workspace("Global") # workspace name
  9. def uploadGeotiff(filename):
  10. store_name = filename
  11. data_url = fileheader+filename
  12. geostore = geocat.create_coveragestore_external_geotiff(store_name, data_url, workspace,True)
  13. def listFiles(filepath):
  14. for maindir,subdir,file_name_list in (filepath):
  15. for filename in file_name_list:
  16. apath = (maindir, filename)
  17. ext = (apath)[1]
  18. if ext in filter:
  19. uploadGeotiff(filename) # 依次上传tif数据
  20. listFiles(readpath)