geoserver上传数据自动发布服务python代码

本文介绍使用Python和Geoserver Rest API实现本地数据自动上传至PostGIS数据库,并自动发布地理信息服务的过程。涵盖代码实现细节及当前存在的局限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

geoserver上传数据自动发布服务python代码

目录

0.目的

1.python代码实现

2.缺点


0.目的

        像arcgis portal那样,从浏览器中选择本地数据上传,数据会自动上传到数据库中,然后再自动发布服务!

        geoserver能否也像portal那样呢?geoserver可以连接postgis的数据源,但是没有从本地上传数据到数据库的选项!参考geonode实现geoserver数据上传数据库,发布服务的代码。geonode-win10安装教程

1.python代码实现

        python geoserver rest api库包:geoserver-restconfig(这里采用)、geoserver-rest

# pip install geoserver-restconfig
# example: https://github.com/GeoNode/geoserver-restconfig/blob/master/examples/postgis_import.py

from geoserver.catalog import Catalog

cat = Catalog("http://localhost:8080/geoserver/rest", "admin", "geoserver")
dsname = "postgis"  # 数据源-数据存储名称
workspace = "postgis"  # 工作区

ds = cat.create_datastore(dsname, workspace=workspace)
# 缺失部分信息
# ds.connection_parameters.update(
#     host="localhost",
#     port="5432",
#     database="test_postgis",  # 需要提前建立
#     user="postgres",
#     passwd="123",
#     dbtype="postgis")

ds.connection_parameters.update({'Evictor run periodicity': 300,
    'Estimated extends': 'true',
    'schema': "public",
    'fetch size': 100000,
    'encode functions': 'false',
    'Expose primary keys': 'true',
    'validate connections': 'true',
    'Support on the fly geometry simplification': 'false',
    'Connection timeout': 10,
    'create database': 'false',
    'Batch insert size': 30,
    'preparedStatements': 'true',
    'min connections': 10,
    'max connections': 100,
    'Evictor tests per run': 3,
    'Max connection idle time': 300,
    'Loose bbox': 'true',
    'Test while idle': 'true',
    'host': "localhost",         # 重要参数
    'port': "5432",              # 重要参数
    'database': "test_postgis",  # 重要参数 数据库名称 需要提前建立database
    'user': "postgres",          # 重要参数
    'passwd': "123",             # 重要参数
    'dbtype': "postgis"})

cat.save(ds)
ds = cat.get_store(dsname, workspace=workspace)
components = dict((ext, f"D:\Program_Files\geoserver-2.20.4\data_dir\data\shapefiles\states.{ext}") for ext in ["shp", "prj", "shx", "dbf"])
cat.add_data_to_store(ds, "states", components)  # 第2个参数为 写入zip内的名字

2.缺点

        (1)geoserver无法自己从本地上传数据,自动存储到数据库,发布服务!只能通过geoserver rest api调用实现!!!

        (2)目前,仅探索了单个shp文件上传geoserver,保存数据库,发布服务的试验。对于多个图层-实现图层组 or 批量导入的操作,暂时还未探索。

        TODO 更多的探索!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值