跳转到主要内容

GeoServer REST配置

项目描述

gsconfig

https://travis-ci.org/boundlessgeo/gsconfig.svg?branch=master

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

本项目遵循MIT许可证进行分发。

安装

pip install gsconfig

针对开发者

git clone git@github.com:boundlessgeo/gsconfig.git
cd gsconfig
python setup.py develop

获取帮助

有关简要手册,请访问http://boundlessgeo.github.io/gsconfig/。如果您有任何问题,请通过GeoServer用户邮件列表提问:http://geoserver.org/comm/

如有任何错误报告,请使用http://github.com/boundlessgeo/gsconfig上的GitHub项目。欢迎提交pull request,但请尽可能包括测试。

示例层创建代码

from geoserver.catalog import Catalog
cat = Catalog("http://localhost:8080/geoserver/")
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/中。此外,预计将有一个位于postgres:password@localhost:5432/db的PostgreSQL数据库。您可以使用psql命令行客户端测试连接到该数据库,运行以下命令:$ psql -d db -Upostgres -h localhost -p 5432(您将被提示交互式输入密码。)

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

  • DATABASE

  • DBUSER

  • DBPASS

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

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

GEOSERVER_HOME

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

GEOSERVER_DATA_DIR

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

GS_VERSION

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

以下是我运行gsconfig测试前重置的命令

$ 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停止它(但不要这样做,直到您运行了测试!)您可以使用以下命令运行gsconfig测试

$ python setup.py test

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

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

更多示例 - 更新为GeoServer 2.4+

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

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

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

geosolutions = cat.get_workspace("geosolutions")
import geoserver.util
shapefile_plus_sidecars = geoserver.util.shapefile_and_friends("C:/work/gsconfig/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虚拟层。以下代码允许创建一个新的由自定义sql定义的SQL视图my_jdbc_vt_test

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)

此示例展示了如何轻松更新属性。相同的做法可以用于每个目录资源。

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()

有一些功能可以管理ImageMosaic覆盖。可以创建新的ImageMosaic,向其中添加颗粒,还可以读取覆盖的元数据,修改拼贴的维度,最后查询拼贴的颗粒并列出它们的属性。

gsconfig方法映射了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.harvest_externalgranule("file://D:/Work/apache-tomcat-6.0.16/instances/data/data/MetOc/NOAAWW3/20131001/WIND/NOAAWW3_NCOMultiGrid__WIND_000_20131001T000000.tif", store)

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

cat.harvest_uploadgranule("NOAAWW3_NCOMultiGrid__WIND_000_20131002T000000.zip", store)

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

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

以下方法允许您加载和更新ImageMosaic的覆盖元数据。当然,您需要为ImageMosaic的每个覆盖都这样做。

coverage = cat.get_resource_by_url("http://localhost:8180/geoserver/rest/workspaces/natocmre/coveragestores/NOAAWW3_NCOMultiGrid_WIND_test/coverages/NOAAWW3_NCOMultiGrid_WIND_test.xml")
coverage.supported_formats = ['GEOTIFF']
cat.save(coverage)

默认情况下,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'")

项目详情


下载文件

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

源分布

gsconfig-1.0.10.tar.gz (28.7 kB 查看哈希值)

上传于

支持者