Geoserver的RESTful接口使用

server/2024/10/11 0:45:02/

概述

GeoServer提供了一个RESTful接口,客户端可以通过该接口获取有关实例的信息并进行配置更改。REST接口使用简单的HTTP调用,通过客户端就可以配置GeoServer,而无需使用Web管理接口。

Geoserver中的关系

工作区、数据源、图层、图层组以及样式之间的关系如下图所示。

image.png

可得知:

  • 工作区用于组织、管理和发布地图数据和地图服务。通过合理地使用工作区,可以更好地管理不同类型的地图数据和业务逻辑,提高地图服务的可用性和可维护性。

  • 数据源是地图数据在地理信息系统中的来源,可以是文件、数据库、网络等。数据源可以包含多个图层,每个图层可以代表一个地理要素或空间数据集。数据源必须属于某个工作区。

  • 图层是将数据源发布成地图服务,因此图层也必须属于某个工作区。

  • 图层组是将多个图层或图层组组合在一起,形成一个逻辑上的图层。由于图层或图层组属于不同的工作区,所以图层组可不属于工作区。

  • 样式是图层的样式定义,用于控制图层的显示效果,如颜色、透明度、线型、填充样式等,同一个样式可以被不同的图层引用,所以样式也可不属于工作区。

RESTful接口使用

1. 工作区

  • 获取工作区列表
    GET: http://localhost:8085/geoserver/rest/workspaces.json

  • 获取工作区信息
    GET: http://localhost:8085/geoserver/rest/workspaces/lzugis.json

  • 添加工作区
    POST: http://localhost:8085/geoserver/rest/workspaces?default=true

{"workspace": {"name": "lzugis-workspace"}
}
  • 删除工作区
    DELETE: http://localhost:8085/geoserver/rest/workspaces/lzugis-workspace?recurse=true

2. 数据存储

  • 获取工作区下所有数据存储
    GET: http://localhost:8085/geoserver/rest/workspaces/lzugis/datastores.json

  • 获取存储信息
    GET: http://localhost:8085/geoserver/rest/workspaces/lzugis/datastores/lzugis.json

  • 添加数据存储 - shp文件
    POST: http://localhost:8085/geoserver/rest/workspaces/lzugis/datastores

{"dataStore": {"name": "nyc","connectionParameters": {"entry": [{"@key":"url","$":"file:/D:/data/china/capital.shp"}]}}
}
  • 添加数据存储 - shp文件夹
    POST: http://localhost:8085/geoserver/rest/workspaces/lzugis/datastores
{"dataStore": {"name": "china","connectionParameters": {"entry": [{"@key":"url","$":"file:/D:/data/china"}]}}
}
  • 添加数据存储 - PG数据库
    POST: http://localhost:8085/geoserver/rest/workspaces/lzugis/datastores
{"dataStore": {"name": "postgres","connectionParameters": {"entry": [{"@key":"host","$":"localhost"},{"@key":"port","$":"5432"},{"@key":"database","$":"lzugis"},{"@key":"user","$":"postgres"},{"@key":"passwd","$":"passwd"},{"@key":"dbtype","$":"postgis"}]}}
}
  • 删除数据存储
    DELETE: http://localhost:8085/geoserver/rest/workspaces/lzugis/datastores/nyc?recurse=true

3. 图层

  • 获取所有图层
    GET: http://localhost:8085/geoserver/rest/layers

  • 获取工作区下的所有图层
    GET: http://localhost:8085/geoserver/rest/workspaces/lzugis/layers

  • 获取图层信息
    GET: http://localhost:8085/geoserver/rest/workspaces/lzugis/layers/layer_capital

  • 获取图层要素信息
    GET: http://localhost:8085/geoserver/rest/workspaces/lzugis/datastores/postgres/featuretypes/layer_capital.json

  • 图层
    POST: http://localhost:8085/geoserver/rest/workspaces/lzugis/datastores/postgres/featuretypes

{"name": "layer_capital","nativeName": "layer_capital","namespace": {"name": "lzugis","href": "http://localhost:8085/geoserver/rest/namespaces/lzugis.json"},"title": "layer_capital","keywords": {"string": ["features","layer_capital"]},"nativeCRS": "GEOGCS[\"WGS 84\", \r\n  DATUM[\"World Geodetic System 1984\", \r\n    SPHEROID[\"WGS 84\", 6378137.0, 298.257223563, AUTHORITY[\"EPSG\",\"7030\"]], \r\n    AUTHORITY[\"EPSG\",\"6326\"]], \r\n  PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]], \r\n  UNIT[\"degree\", 0.017453292519943295], \r\n  AXIS[\"Geodetic longitude\", EAST], \r\n  AXIS[\"Geodetic latitude\", NORTH], \r\n  AUTHORITY[\"EPSG\",\"4326\"]]","srs": "EPSG:4326","nativeBoundingBox": {"minx": 87.5760726928711,"maxx": 126.56706237792969,"miny": 19.970149993896484,"maxy": 45.6938591003418,"crs": "EPSG:4326"},"latLonBoundingBox": {"minx": 87.5760726928711,"maxx": 126.56706237792969,"miny": 19.970149993896484,"maxy": 45.6938591003418,"crs": "EPSG:4326"},"projectionPolicy": "FORCE_DECLARED","enabled": true,"store": {"@class": "dataStore","name": "postgres","href": "http://localhost:8085/geoserver/rest/workspaces/lzugis/datastores/postgres.json"},"serviceConfiguration": false,"simpleConversionEnabled": false,"internationalTitle": "","internationalAbstract": "","maxFeatures": 0,"numDecimals": 0,"padWithZeros": false,"forcedDecimal": false,"overridingServiceSRS": false,"skipNumberMatched": false,"circularArcPresent": false,"attributes": {"attribute": [{"name": "name","minOccurs": 0,"maxOccurs": 1,"nillable": true,"binding": "java.lang.String"},{"name": "geom","minOccurs": 0,"maxOccurs": 1,"nillable": true,"binding": "org.locationtech.jts.geom.Point"},{"name": "lon","minOccurs": 0,"maxOccurs": 1,"nillable": true,"binding": "java.lang.Double"},{"name": "lat","minOccurs": 0,"maxOccurs": 1,"nillable": true,"binding": "java.lang.Float"}]}
}
  • 删除图层
    DELETE: http://localhost:8085/geoserver/rest/workspaces/lzugis/datastores/postgres/featuretypes/capital?recurse=true

4. 样式

  • 获取所有样式
    GET: http://localhost:8085/geoserver/rest/styles

  • 获取工作区下的所有图层
    GET: http://localhost:8085/geoserver/rest/workspaces/lzugis/styles

  • 添加样式
    POST: (不指定工作区)http://localhost:8085/geoserver/rest/styles?name=test
    POST: (指定工作区)http://localhost:8085/geoserver/rest/workspaces/lzugis/styles?name=test
    设置头信息content-typeapplication/vnd.ogc.sld+xml

<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><!-- a Named Layer is the basic building block of an SLD document --><NamedLayer><Name>default_line</Name><UserStyle><!-- Styles can have names, titles and abstracts --><Title>Default Line</Title><Abstract>A sample style that draws a line</Abstract><!-- FeatureTypeStyles describe how to render different features --><!-- A FeatureTypeStyle for rendering lines --><FeatureTypeStyle><Rule><Name>rule1</Name><Title>Blue Line</Title><Abstract>A solid blue line with a 1 pixel width</Abstract><LineSymbolizer><Stroke><CssParameter name="stroke">#0000FF</CssParameter></Stroke></LineSymbolizer></Rule></FeatureTypeStyle></UserStyle></NamedLayer>
</StyledLayerDescriptor>
  • 删除样式
    DELETE: http://localhost:8085/geoserver/rest/styles/test?recurse=true
说明

由于上面的操作都需要登录Geoserver后方可,所以在进行接口调用时需要设置登录信息,如在Postman中可进行如下设置。
image.png

如在前端代码调用,需要设置头信息Authorization,设置方式如下:

const auth = window.btoa(`admin:geoserver`);
const url = 'http://localhost:8085/geoserver/rest/layers'
fetch(url, { method: 'get', headers: { 'Authorization': `Basic ${auth}` 
}).then(res => res.json()).then(res => {console.log(res)
})
参考资料
  • 官方文档

http://www.ppmy.cn/server/13569.html

相关文章

【Python】Python函数的黑魔法:递归,嵌套函数与装饰器

欢迎来到CILMY23的博客 本篇主题为&#xff1a; Python函数的黑魔法&#xff1a;递归&#xff0c;嵌套函数与装饰器 个人主页&#xff1a;CILMY23-CSDN博客 系列专栏&#xff1a;Python | C | C语言 | 数据结构与算法 感谢观看&#xff0c;支持的可以给个一键三连&#xff…

linux权限维持(二)

3.SSH 后门 3.1 SSH 软连接后门 软连接后门的原理是利用了 PAM 配置文件的作用&#xff0c;将 sshd 文件软连接名称设置为 su &#xff0c;这样应用在启动过 程中他会去PAM 配置文件夹中寻找是否存在对应名称的配置信息 (su) &#xff0c;然而 su 在 pam_rootok 只检测 uid…

Python基础:【习题系列】判断题(一)

在Python中&#xff0c;变量名可以以数字开头。 答案&#xff1a;错 Python中的列表和元组都可以通过索引访问。 答案&#xff1a;对 Python中的字符串是可变数据类型。 答案&#xff1a;错 在Python中使用运算符可以检查两个变量是否具有相同的身份。 答案&#xff1a;错 …

【k8s】(四)kubernetes1.29.4离线部署之-组件安装

&#xff08;一&#xff09;kubernetes1.29.4离线部署之-安装文件准备 &#xff08;二&#xff09;kubernetes1.29.4离线部署之-镜像文件准备 &#xff08;三&#xff09;kubernetes1.29.4离线部署之-环境初始化 &#xff08;四&#xff09;kubernetes1.29.4离线部署之-组件安装…

Spring - 3 ( 12000 字 Spring 入门级教程 )

一&#xff1a;Spring Web MVC入门 1.1 响应 在我们前⾯的代码例子中&#xff0c;都已经设置了响应数据, Http 响应结果可以是数据, 也可以是静态页面&#xff0c;也可以针对响应设置状态码, Header 信息等. 1.2 返回静态页面 创建前端页面 index.html(注意路径) html代码 …

ubuntu22.04 安装并使用 DirBuster

DirBuster是一个用于对Web应用程序进行目录和文件名枚举的工具&#xff0c;通常来说它是一个独立的Java程序&#xff0c;所以你应该看到一个.jar文件&#xff0c;而不是PHP文件。 要下载DirBuster&#xff0c;你需要访问OWASP的官方资源或者使用搜索引擎找到合适的下载链接。一…

Ubuntu 22.04上text-generation-webui service文件编写思路

自从拥抱了ai&#xff0c;coding工作简单不少。 由于不会翻墙&#xff0c;主要靠文心一言活了。最近用的时候会遇到服务有点忙的情况&#xff0c;于是就想把家中电脑的text-generation-webui给跑起来&#xff0c;在办公室使用。经过一番折腾&#xff08;百度&#xff0c;bing了…

达梦(DM)数据库管理表

达梦DM数据库管理表 管理表管理表的准则表的存储空间上限表的存储位置 创建表创建普通表指定表的聚集索引查询建表更改表删除表清空表查看表定义查看表空间使用 表是数据库中数据存储的基本单元&#xff0c;是对用户数据进行读和操纵的逻辑实体&#xff0c;表由列和行组成&…