跳转到主要内容

GeoServer REST配置

项目描述

geoserver-restconfig

https://travis-ci.org/geosolutions-it/geoserver-restconfig.svg?branch=master

geoserver-restconfig 是一个用于通过GeoServer RESTConfig API操作GeoServer实例的Python库。

注意: geoserver-restconfig 是旧版 https://travis-ci.org/boundlessgeo/gsconfig 的分支。

该项目遵循 MIT许可

安装

pip install geoserver-restconfig

对于开发者

git clone git@github.com:geosolutions-it/geoserver-restconfig.git
cd geoserver-restconfig
python setup.py develop

获取帮助

http://geonode.org/geoserver-restconfig/ 有简短的说明手册。如果您有问题,请在GeoServer用户邮件列表中提问:http://geoserver.org/

请使用Github项目 http://github.com/geosolutions-it/geoserver-restconfig 报告任何错误(并且欢迎提交拉取请求,但请尽可能包括测试。)

示例图层创建代码

from geoserver.catalog import Catalog
cat = Catalog("http://localhost:8080/geoserver/rest")
topp = cat.get_workspace("topp")
shapefile_plus_sidecars = shapefile_and_friends("states")
# shapefile_and_friends should look on the filesystem to find a shapefile
# and related files based on the base path passed in
#
# shapefile_plus_sidecars == {
#    'shp': 'states.shp',
#    'shx': 'states.shx',
#    'prj': 'states.prj',
#    'dbf': 'states.dbf'
# }

# 'data' is required (there may be a 'schema' alternative later, for creating empty featuretypes)
# 'workspace' is optional (GeoServer's default workspace is used by... default)
# 'name' is required
ft = cat.create_featurestore(name, workspace=topp, data=shapefile_plus_sidecars)

运行测试

由于本模块的整个目的就是与GeoServer交互,因此测试套件主要由集成测试组成。这些测试必然依赖于GeoServer的运行副本,并假定该GeoServer实例将使用GeoServer包含的默认数据目录。这些数据也包含在GeoServer源代码库中的/data/release/。此外,预计将有一个PostgreSQL数据库位于postgres:password@localhost:5432/db。您可以使用psql命令行客户端通过以下命令测试连接到该数据库: $ psql -d db -Upostgres -h localhost -p 5432(您将被交互式提示输入密码。)

要覆盖假设的数据库连接参数,以下环境变量被支持

  • DATABASE

  • DBUSER

  • DBPASS

如果存在,psycopg将在运行测试之前用于验证数据库连接。

如果提供,以下环境变量将用于重置数据目录

GEOSERVER_HOME

从git仓库读取干净数据的git仓库的位置。如果只提供此选项,则将使用git clean重置数据。

GEOSERVER_DATA_DIR

GeoServer运行时数据目录的备用位置。如果提供,则使用rsync重置数据。

GS_VERSION

可选环境变量,允许目录测试案例自动从网络下载并启动纯GeoServer WAR。请确保HTTP端口8080上没有正在运行的服务。

以下是我运行geoserver-restconfig测试之前重置所使用的命令

$ cd ~/geoserver/src/web/app/
$ PGUSER=postgres dropdb db
$ PGUSER=postgres createdb db -T template_postgis
$ git clean -dxff -- ../../../data/release/
$ git checkout -f
$ MAVEN_OPTS="-XX:PermSize=128M -Xmx1024M" \
GEOSERVER_DATA_DIR=../../../data/release \
mvn jetty:run

在此阶段,GeoServer将前台运行,但实际开始监听http请求需要几秒钟。您可以使用CTRL-C停止它(但在运行测试之前不要这样做!)您可以使用以下命令运行geoserver-restconfig测试

$ python setup.py test

而不是在每次运行后重新启动GeoServer以重置数据,以下命令应允许重新运行测试

$ git clean -dxff -- ../../../data/release/
$ curl -XPOST --user admin:geoserver http://localhost:8080/geoserver/rest/reload

更多示例 - 更新为GeoServer 2.4+

使用geoserver-restconfig加载GeoServer 目录非常简单。以下示例允许您通过指定自定义凭据连接到GeoServer。

from geoserver.catalog import Catalog
cat = Catalog("http://localhost:8080/geoserver/rest/", "admin", "geoserver")

以下代码允许您筛选要返回的工作空间

cat.get_workspaces(names="geosolutions,topp")

您还可以指定工作空间为正确列表

cat.get_workspaces(names=["geosolutions", "topp"])

以下代码允许您筛选要返回的存储

cat.get_stores(names=["sf", "mosaic"], workspaces=["nurc", "topp", "sf"])

namesworkspaces可以是逗号分隔的字符串或列表。这对于get_workspacesget_storesget_resourcesget_layergroupsget_styles都适用。

以下代码允许您从Shapefile创建FeatureType

geosolutions = cat.get_workspace("geosolutions")
import geoserver.util
shapefile_plus_sidecars = geoserver.util.shapefile_and_friends("C:/work/geoserver-restconfig/test/data/states")
# shapefile_and_friends should look on the filesystem to find a shapefile
# and related files based on the base path passed in
#
# shapefile_plus_sidecars == {
#    'shp': 'states.shp',
#    'shx': 'states.shx',
#    'prj': 'states.prj',
#    'dbf': 'states.dbf'
# }
# 'data' is required (there may be a 'schema' alternative later, for creating empty featuretypes)
# 'workspace' is optional (GeoServer's default workspace is used by... default)
# 'name' is required
ft = cat.create_featurestore("test", shapefile_plus_sidecars, geosolutions)

还可以创建JDBC虚拟层。以下代码允许创建一个新的名为my_jdbc_vt_test的SQL视图,该视图由自定义sql定义。

from geoserver.catalog import Catalog
from geoserver.support import JDBCVirtualTable, JDBCVirtualTableGeometry, JDBCVirtualTableParam

cat = Catalog('http://localhost:8080/geoserver/rest/', 'admin', '****')
store = cat.get_store('postgis-geoserver')
geom = JDBCVirtualTableGeometry('newgeom','LineString','4326')
ft_name = 'my_jdbc_vt_test'
epsg_code = 'EPSG:4326'
sql = 'select ST_MakeLine(wkb_geometry ORDER BY waypoint) As newgeom, assetid, runtime from waypoints group by assetid,runtime'
keyColumn = None
parameters = None

jdbc_vt = JDBCVirtualTable(ft_name, sql, 'false', geom, keyColumn, parameters)
ft = cat.publish_featuretype(ft_name, store, epsg_code, jdbc_virtual_table=jdbc_vt)

下一个示例显示如何创建一个PostGIS JNDI数据存储(连接参数来自另一个示例。设置可能因您的需求而异)

cat = Catalog('http://localhost:8080/geoserver/rest/', 'admin', '****')
datastore_name = 'sample_jndi_store'

dstore = cat.get_store(name = datastore_name, workspace=metadata[WS])
# Let's check that the store doesn't already exist
if ds_store is None:
    ws = 'my_workspace'
    dstore = cat.create_datastore(workspace=ws, name = datastore_name)
    connection_parameters= {
        'type': 'PostGIS (JNDI)',
        'schema': 'my_schema',
        'Estimated extends': 'true',
        'fetch size': '1000',
        'encode functions': 'true',
        'Expose primary keys': 'false',
        'Support on the fly geometry simplification': 'true',
        'Batch insert size': '1',
        'preparedStatements': 'false',
        'Support on the fly geometry simplification, preserving topology': 'true',
        'jndiReferenceName': 'java:comp/env/jdbc/geodb',
        'dbtype': 'postgis',
        'namespace': 'my_workspace',
        'Loose bbox': 'true'
    }
    dstore.connection_parameters.update(connection_parameters)
    cat.save(dstore)
    assert dstore.enabled
    return dstore

此示例显示如何轻松更新属性。可以使用相同的方法处理每个目录资源

ne_shaded = cat.get_layer("ne_shaded")
ne_shaded.enabled=True
cat.save(ne_shaded)
cat.reload()

从目录中删除一个存储需要先清除所有相关联的图层。这可以通过以下方式实现:

st = cat.get_store("ne_shaded")
cat.delete(ne_shaded)
cat.reload()
cat.delete(st)
cat.reload()

或者,您也可以一次性删除一个存储及其所有底层图层,如下所示:

store = cat.get_store("ne_shaded")
cat.delete(store, purge=True, recurse=True)

有一些功能允许管理ImageMosaic覆盖。可以创建新的ImageMosaic,向其中添加颗粒,并读取覆盖的元数据,修改镶嵌尺寸,最后查询镶嵌颗粒并列出它们的属性。

geoserver-restconfig方法映射了ImageMosaic的REST API

为了创建一个新的ImageMosaic图层,您可以准备一个包含镶嵌配置属性文件的zip文件。有关镶嵌配置的详细信息,请参阅GeoTools ImageMosaic插件指南。该软件包包含一个已经配置好的包含两个颗粒的zip文件。在创建镶嵌之前,您需要更新或删除datastore.properties文件,否则您将收到异常。

from geoserver.catalog import Catalog
cat = Catalog("http://localhost:8180/geoserver/rest")
cat.create_imagemosaic("NOAAWW3_NCOMultiGrid_WIND_test", "NOAAWW3_NCOMultiGrid_WIND_test.zip")

默认情况下,cat.create_imagemosaic尝试配置图层。如果您只想创建存储,可以指定以下参数:

cat.create_imagemosaic("NOAAWW3_NCOMultiGrid_WIND_test", "NOAAWW3_NCOMultiGrid_WIND_test.zip", "none")

为了从目录中检索ImageMosaic覆盖存储,您可以这样做:

store = cat.get_store("NOAAWW3_NCOMultiGrid_WIND_test")

可以在运行时向镶嵌添加更多颗粒。以下方法可以添加已经在机器本地路径上存在的颗粒。

cat.add_granule("file://D:/Work/apache-tomcat-6.0.16/instances/data/data/MetOc/NOAAWW3/20131001/WIND/NOAAWW3_NCOMultiGrid__WIND_000_20131001T000000.tif", store.name, store.workspace.name)

以下方法允许通过POST将颗粒远程发送到ImageMosaic。颗粒将被上传并存储在ImageMosaic索引文件夹中。

cat.add_granule("NOAAWW3_NCOMultiGrid__WIND_000_20131002T000000.zip", store.name, store.workspace.name)

要删除ImageMosaic存储,您可以遵循标准方法,首先删除图层。注意:在此期间,您需要手动清理数据目录中的镶嵌颗粒,如果您使用了DB数据存储,您还必须删除镶嵌表。

layer = cat.get_layer("NOAAWW3_NCOMultiGrid_WIND_test")
cat.delete(layer)
cat.reload()
cat.delete(store)
cat.reload()

默认情况下,ImageMosaic图层没有配置覆盖尺寸。可以使用覆盖元数据来更新和管理覆盖尺寸。注意:请注意,presentation参数只接受以下值之一:{'LIST', 'DISCRETE_INTERVAL', 'CONTINUOUS_INTERVAL'}

from geoserver.support import DimensionInfo
timeInfo = DimensionInfo("time", "true", "LIST", None, "ISO8601", None)
coverage.metadata = ({'dirName':'NOAAWW3_NCOMultiGrid_WIND_test_NOAAWW3_NCOMultiGrid_WIND_test', 'time': timeInfo})
cat.save(coverage)

一旦配置了ImageMosaic,就可以读取覆盖及其颗粒模式和信息。

from geoserver.catalog import Catalog
cat = Catalog("http://localhost:8180/geoserver/rest")
store = cat.get_store("NOAAWW3_NCOMultiGrid_WIND_test")
coverages = cat.mosaic_coverages(store)
schema = cat.mosaic_coverage_schema(coverages['coverages']['coverage'][0]['name'], store)
granules = cat.list_granules(coverages['coverages']['coverage'][0]['name'], store)

可以通过以下方式轻松读取颗粒的详细信息:

granules['crs']['properties']['name']
granules['features']
granules['features'][0]['properties']['time']
granules['features'][0]['properties']['location']
granules['features'][0]['properties']['run']

当镶嵌增长并开始拥有大量颗粒时,您可能需要通过覆盖模式属性上的CQL过滤器对颗粒查询进行筛选。

granules = cat.list_granules(coverages['coverages']['coverage'][0]['name'], store, "time >= '2013-10-01T03:00:00.000Z'")
granules = cat.list_granules(coverages['coverages']['coverage'][0]['name'], store, "time >= '2013-10-01T03:00:00.000Z' AND run = 0")
granules = cat.list_granules(coverages['coverages']['coverage'][0]['name'], store, "location LIKE '%20131002T000000.tif'")

创建图层组

可以通过向目录提供一系列图层及其相关样式来设置图层组。在下一个示例中,创建了一个包含3个图层及其相关样式的图层组。

workspace = 'my_workspace'
layers_in_group = ['my_workspace:layer_1', 'my_workspace:layer_2', 'my_workspace:layer_3']
styles = ['my_workspace:style_1', 'my_workspace:style_2', 'my_workspace:style_3']
layergroup_name = 'test_layergroup'

layergroup = cat.create_layergroup(layergroup_name, layers_in_group, styles, workspace)
# Note that if no bounds are provided, GeoServer will automatically compute the layergroup bounding box
cat.save(layergroup)

嵌套图层组

图层组内部可以包含图层组。在下一个示例中,将创建一个包含layer_4简单图层和先前创建的test_layegroup的额外图层组。这次,需要指定图层属性。

workspace = 'my_workspace'

layers = []
layers.append({'name':'my_workspace:layer_4', 'attributes':{'type':'layer'}})
layers.append({'name':'my_workspace:test_layergroup', 'attributes':{'type':'layerGroup'}})

# Not specifying the style for the nested layergroup
styles = []
styles.append('my_workspace:style_4')
styles.append(None)
layergroup_name = 'outer_layergroup'

outer_layergroup = cat.create_layergroup(layergroup_name, layers, styles, workspace)
cat.save(outer_layergroup)

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分布

此版本没有可用的源代码分布文件。请参阅生成分布存档的教程。

构建分布

geoserver_restconfig-2.0.12-py3-none-any.whl (40.8 kB 查看散列值)

上传时间 Python 3

由以下支持