• Geoserver的RESTful接口使用


    概述

    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"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 删除工作区
      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"}
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 添加数据存储 - shp文件夹
      POST: http://localhost:8085/geoserver/rest/workspaces/lzugis/datastores
    {
      "dataStore": {
        "name": "china",
        "connectionParameters": {
          "entry": [
            {"@key":"url","$":"file:/D:/data/china"}
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 添加数据存储 - 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"}
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 删除数据存储
      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"
    			}
    		]
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 删除图层
      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

    
    <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">
      
      <NamedLayer>
        <Name>default_lineName>
        <UserStyle>
        
          <Title>Default LineTitle>
          <Abstract>A sample style that draws a lineAbstract>
          
          
          <FeatureTypeStyle>
            <Rule>
              <Name>rule1Name>
              <Title>Blue LineTitle>
              <Abstract>A solid blue line with a 1 pixel widthAbstract>
              <LineSymbolizer>
                <Stroke>
                  <CssParameter name="stroke">#0000FFCssParameter>
                Stroke>
              LineSymbolizer>
            Rule>
          FeatureTypeStyle>
        UserStyle>
      NamedLayer>
    StyledLayerDescriptor>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 删除样式
      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)
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    参考资料
  • 相关阅读:
    外包干了3个月,技术退步明显。。。。。
    桂院校园导航 | 云上高校导航 云开发项目 二次开发教程 1.3
    linux内核驱动——字符设备实现两个终端单向收发
    PHP:CentOS Linux环境下源码安装PHP
    力扣刷题day46|1143最长公共子序列、1035不相交的线、53最大子序和
    C++基础01
    数据库连接关闭工具类、Statement介绍、PreparedStatement介绍及区别
    MySQL到TiDB:Hive Metastore横向扩展之路
    Kubernetes (k8s) 管理工具 kubectl 命令详解
    方舟生存进化是什么游戏?好不好玩
  • 原文地址:https://blog.csdn.net/GISShiXiSheng/article/details/138005578