1.部署一个简单的测试环境

测试geoserver REST接口,我们可使用python来测试,很方便。需要下载包:

python,http://python.org/。我下载的是Python27版本。

cURL,几个简单的命令行工具,很容易使用命令交互, 地址为http://curl.haxx.se/download.html,下载curl-7.53.1-win64-mingw版本。

Requests,一个给phtyon提供REST支持的插件,https://github.com/kennethreitz/requests

三个包现在后,配置python的环境变量。打开Requests下载后的目录。在该目录下执行以下命令:

python setup.py install
~$ python
>>> import requests

2.使用curl获取工作区列表

解压curl下载包,进入目录“curl-7.53.1-win64-mingw\bin”,执行cmd命令。输入以下命令:

curl -u admin:geoserver -v -XGET -H 'Accept: text/xml' http://localhost:8082/geoserver/rest/workspaces -o E:\workspaces.xml

参数说明: -u表示验证的用户名和密码,-v表示输入版本, -X表示请求的方式,-H表示输入的请求头信息,-o打印输出文件。但实际不知道-o文件输出到哪里去了,没找到。 输出结果如下:

3.使用python获取工作区

打开用于输出的一个目录,在该目录下执行cmd指令。分别一步一步执行以下python指令:

python
import requests
myUrl = 'http://localhost:8082/geoserver/rest/workspaces'
headers = {'Accept': 'text/xml'}
resp = requests.get(myUrl,auth=('admin','geoserver'),headers=headers)
resp.status_code
file = open('workspaces_py.xml','w')
file.write(resp.text)
file.close()

打开workspaces_py.xml文件查看输出结果。额curl输出结果一样。

4.查看工作区下的数据存储

指令和3相似,myUrl有点区别:myUrl = 'http://localhost:8082/geoserver/rest/workspaces/tiger'。查询结果:

<workspace>
<name>tiger</name>
<dataStores>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8082/geoserver/rest/workspaces/tiger/datastores.xml" type="application/xml"/>
</dataStores>
<coverageStores>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8082/geoserver/rest/workspaces/tiger/coveragestores.xml" type="application/xml"/>
</coverageStores>
<wmsStores>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8082/geoserver/rest/workspaces/tiger/wmsstores.xml" type="application/xml"/>
</wmsStores>
</workspace>

5.查询命名空间

查询命名空间列表url:http://localhost:8082/geoserver/rest/namespaces

查询某个具体的命名空间url:http://localhost:8080/geoserver/rest/namespaces/tiger

6.创建命名空间

使用curl创建,指令如下:

curl -u admin:geoserver -XPOST -H 'Content-type: text/xml' -d  '<namespace><prefix>newWorkspace</prefix><uri>http://geoserver.org</uri></namespace>' http://localhost:8082/geoserver/rest/namespaces

使用python创建,指令如下:

>>> myUrl = 'http://localhost:8082/geoserver/rest/namespaces'
>>> file = open('requestBody.xml','r')
>>> payload = file.read()
>>> headers = {'Content-type': 'text/xml'}
>>> resp = requests.post(myUrl, auth=('admin','geoserver'),data=payload, headers=headers)
>>> resp.status_code

requestBody.xml内容:

<namespace><prefix>newWorkspace</prefix><uri>http://geoserver.org</uri></namespace>

7.修改命名空间

使用python指令如下:

>>> myUrl = 'http://localhost:8082/geoserver/rest/namespaces/newWorkspace'
>>> file = open('requestBody.xml','r')
>>> payload = file.read()
>>> headers = {'Content-type': 'text/xml'}
>>> resp = requests.put(myUrl,auth=('admin','geoserver'),data=payload, headers=headers)

requestBody.xml内容:

<namespace><prefix>newWorkspace</prefix><uri>http://localhost:8082/geoserver</uri></namespace>

8.删除命名空间

使用python指令如下:

>>> myUrl = 'http://localhost:8082/geoserver/rest/workspaces/newWorkspace'
>>> headers = {'Accept': 'text/xml'}
>>> resp = requests.delete(myUrl, auth=('admin','geoserver'),headers=headers)
>>> resp.status_code

9.获取数据存储列表

使用python指令如下:

>>> myUrl = 'http://localhost:8082/geoserver/rest/workspaces/tiger/datastores'
>>> headers = {'Accept': 'text/xml'}
>>> resp = requests.get(myUrl,auth=('admin','geoserver'),headers=headers)
>>> file = open('tiger_datastores.xml','w')
>>> file.write(resp.text)
>>> file.close()

输出的tiger_datastores.xml内容如下:

<dataStores>
<dataStore>
<name>50m-rivers-lake-centerlines</name>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8082/geoserver/rest/workspaces/tiger/datastores/50m-rivers-lake-centerlines.xml" type="application/xml"/>
</dataStore>
<dataStore>
<name>ne_10m_railroads</name>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8082/geoserver/rest/workspaces/tiger/datastores/ne_10m_railroads.xml" type="application/xml"/>
</dataStore>
</dataStores>

10.获取某个存储的具体数据

使用python指令如下:

>>> myUrl = 'http://localhost:8082/geoserver/rest/workspaces/tiger/datastores/ne_50m_populated_places'
>>> headers = {'Accept': 'text/html'}
>>> resp = requests.get(myUrl,auth=('admin','geoserver'),headers=headers)
>>> file = open('tiger_ne_50m_populated_places_datastores.xml','w')
>>> file.write(resp.text)
>>> file.close()

输出结果如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>GeoServer Configuration</title>
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/>
</head>
<body> Data Store "ne_50m_populated_places"
<ul>
<li><a href="http://localhost:8082/geoserver/rest/workspaces/tiger/datastores/ne_50m_populated_places/featuretypes/ne_50m_populated_places.html">ne_50m_populated_places</a></li>
</ul>
</body>
</html>

11.添加数据存储

使用python指令如下:

>>> myUrl = 'http://localhost:8082/geoserver/rest/workspaces/tiger/datastores'
>>> file = open('tigerCounties.xml','r')
>>> payload = file.read()
>>> headers = {'Content-type': 'text/xml','Accept': 'text/xml'}
>>> resp = requests.post(myUrl, auth=('admin','geoserver'),data=payload, headers=headers)
>>> resp.status_code

传递的tigerConties.xml内容如下:

<dataStore>
<name>tl_2011_47_concity_REST</name>
<description>tiger counties created from REST</description>
<!-- 上传数据存储的类型 -->
<type>Shapefile</type>
<!-- 数据存储是否可用-->
<enabled>true</enabled>
<connectionParameters>
<!-- 使用内存映射的缓冲区 -->
<entry key="memory mapped buffer">false</entry>
<!-- 如果缺少空间索引或者空间索引过时,重新建立空间索引 -->
<entry key="create spatial index">true</entry>
<!-- DBF的字符集 -->
<entry key="charset">ISO-8859-1</entry>
<!-- 数据文件类型 -->
<entry key="filetype">shapefile</entry>
<!-- 如果缺少空间索引或者空间索引过时,重新建立空间索引 -->
<entry key="cache and reuse memory maps">true</entry>
<!-- 上传数据文件的路径 -->
<entry key="url">E:\ebook\GeoServer_Beginners+Guide\output\tl_2011_47_concity\tl_2011_47_concity.shp</entry>
<entry key="namespace">http://www.census.gov</entry>
</connectionParameters>
<__default>false</__default>
</dataStore>

12.修改数据存储

和新增数据存储相似,配置文件tigerCounties.xml修改后。上传该文件,提交使用“PUT”方式。

resp = requests.put(myUrl, auth=('admin','geoserver'),data=payload, headers=headers)

13.删除数据存储

使用python指令如下:

>>> myUrl = 'http://localhost:8082/geoserver/rest/workspaces/tiger/datastores/tl_2011_47_concity_REST'
>>> headers = {'Accept': 'text/xml'}
>>> resp = requests.delete(myUrl, auth=('admin','geoserver'),headers=headers)

14.使用put方式增加shapefile文件

这种方式是直接把shapfile文件上传到geoserver服务器上,所有必须是*.zip包。使用python命令如下:

>>> myUrl = 'http://localhost:8082/geoserver/rest/workspaces/tiger/datastores/Natural+Earth+Countries/file.shp'
>>> file = open('110m-admin-0-countries.zip','rb')
>>> payload = file.read()
>>> headers = {'Content-type': 'application/zip'}
>>> resp = requests.put(myUrl, auth=('admin','geoserver'),data=payload, headers=headers)

“Natural+Earth+Countries”表示数据存储名字为“Natural Earth Countries”;url中的file.shpe是指上传到服务器上存储的shape文件名字;“110m-admin-0-countries.zip”即是需要上传的shape文件压缩包。上传过后,可以在geoserver服务界面查看文件路径:

对应的物理路径在geoserver安装目录下的data目录下。

15.把PostGIS中的一个表添加到一个图层

之前在工作空间cite有创建过一个PostGIS的数据存储,叫做myPostGIS。假如我们已经在PostgreSQL数据库中增加了一个表“ne_110m_admin”,现在我想把表映射到数据存储myPostGIS下的一个图层。

使用python指令如下:

>>> myUrl = 'http://localhost:8082/geoserver/rest/workspaces/cite/datastores/myPostGIS/featuretypes'
>>> payload = '<featureType><name>ne_110m_admin</name></featureType>'
>>> headers = {'Content-type': 'text/xml'}
>>> resp = requests.post(myUrl, auth=('admin','geoserver'),data=payload, headers=headers)

以上命令执行后,可在图层列表总看到一个新的图层“ne_110m_admin”。

如果我们想增加更多的图层配置,例如显示名称“name”、唯一标示名称“nativeName”、keywords包含表字段。如下:

<featureType>
<name>World boundaries</name>
<nativeName>ne_110m_admin</nativeName>
<title>World boundaries</title>
<abstract>World administrative boundaries at small scale</abstract>
<keywords>
<string>Political</string>
<string>World</string>
</keywords>
<featureType>

如果我们想创建一个新表,可以在配置信息中配置表的字段、srs等。例如:

<featureType>
<name>rivers</name>
<nativeName>rivers</nativeName>
<title>World River</title>
<srs>EPSG:4326</srs>
<attributes>
<attribute>
<name>geom</name>
<binding>com.vividsolutions.jts.geom.Polyline</binding>
</attribute>
<attribute>
<name>name</name>
<binding>java.lang.String</binding>
<length>30</length>
</attribute>
<attribute>
<name>country_code</name>
<binding>java.lang.String</binding>
<length>8</length>
</attribute>
</attributes>
</featureType>

16.查询样式

使用python指令如下:

>>> myUrl = 'http://localhost:8082/geoserver/rest/styles/PopulatedPlacesLabeled'
>>> headers = {'Accept: application/vnd.ogc.sld+xml'}
>>> resp = requests.get(myUrl, auth=('admin','geoserver'),headers=headers)
>>> file = open('PopulatedPlacesBlueLabeled.xml','w')
>>> file.write(resp.text)
>>> file.close()

17.新增样式

使用python指令如下:

>>> myUrl = 'http://localhost:8082/geoserver/rest/styles'
>>> file = open('PopulatedPlacesBlueLabeled.xml','r')
>>> payload = file.read()
>>> headers = {'Content-type': 'application/vnd.ogc.sld+xml'}
>>> resp = requests.post(myUrl, auth=('admin','geoserver'),data=payload, headers=headers)

18.所有REST接口查询地址

http://docs.geoserver.org/stable/en/user/rest/api/styles.html#rest-api-styles-post-put

19.查询WFS服务的能力

使用ptyhon指令如下:

myUrl = 'http://localhost:8082/geoserver/wfs?service=wfs&version=1.0.0&request=GetCapabilities'
headers = {'Accept': 'text/xml'}
resp = requests.get(myUrl,auth=('admin','geoserver'),headers=headers)
resp.status_code
file = open('getCapabilities.xml','w')
file.write(resp.text)
file.close()

查看getCapabilities.xml文件,内容比较多,包含了所有的图层信息FeatureType。FeatureType部分内容如下:

<FeatureType>
<Name>NaturalEarth:10m_railroads</Name>
<Title>10m_railroads</Title>
<Abstract/>
<Keywords>10m_railroads, features</Keywords>
<SRS>EPSG:4326</SRS>
<LatLongBoundingBox minx="-150.08159339101002"
miny="8.329046942181577" maxx="-59.94810950429127"
maxy="64.93097565311908"/>
</FeatureType>

20.查询featureType的完整描述

使用python指令如下:

myUrl = "http://localhost:8082/geoserver/wfs?
service=wfs&version=1.0.0&request=DescribeFeatureType&TypeName=tiger:tiger_roads"
headers = {'Accept': 'text/xml'}
resp = requests.get(myUrl,auth=('admin','geoserver'),headers=headers)
resp.status_code
file = open('tiger_roads.xml','w')
file.write(resp.text)
file.close()

查看getFeature.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns:gml="http://www.opengis.net/gml" 

xmlns:tiger="http://www.census.gov" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 

elementFormDefault="qualified" targetNamespace="http://www.census.gov"> <xsd:import 

namespace="http://www.opengis.net/gml" 

schemaLocation="http://localhost:8082/geoserver/schemas/gml/2.1.2/feature.xsd"/> <xsd:complexType 

name="tiger_roadsType"> <xsd:complexContent> <xsd:extension base="gml:AbstractFeatureType"> 

<xsd:sequence> <xsd:element maxOccurs="1" minOccurs="0" name="the_geom" nillable="true" 

type="gml:MultiLineStringPropertyType"/> <xsd:element maxOccurs="1" minOccurs="0" name="CFCC" 

nillable="true" type="xsd:string"/> <xsd:element maxOccurs="1" minOccurs="0" name="NAME" 

nillable="true" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> 

</xsd:complexType> <xsd:element name="tiger_roads" substitutionGroup="gml:_Feature" 

type="tiger:tiger_roadsType"/></xsd:schema>

21.查看featureType的数据

使用python指令如下:

myUrl = "http://localhost:8082/geoserver/wfs?
service=wfs&version=1.1.0&request=GetFeature&TypeName=tiger:tiger_roads&maxFeatures=1"
headers = {'Accept': 'text/xml'}
resp = requests.get(myUrl,auth=('admin','geoserver'),headers=headers)
resp.status_code
file = open('getFeature.xml','w')
file.write(resp.text)
file.close()

maxFeaturres参数指定参数feature的条数,这里只查询了一条。有时候我们还需要一些额外的空间属性过滤条件。例如可以使用bbox参数设置查询边界。

"http://localhost:8082/geoserver/wfs?service=wfs&version=1.0.0&request=GetFeature&TypeName=tiger_roads&bbox=-116.68,36.29,-111.36,39.90"

返回数据格式如下:

<wfs:FeatureCollection

>
<gml:boundedBy>
<gml:null>unknown</gml:null>
</gml:boundedBy>
<gml:featureMember>
<NaturalEarth:10m_railroads fid="10m_railroads.481">
<NaturalEarth:the_geom>
<gml:MultiLineString srsName="http://www.opengis.
net/gml/srs/epsg.xml#4326">
<gml:lineStringMember>
<gml:LineString>
<gml:coordinates xmlns:gml="http://
www.opengis.net/gml" decimal="." cs="," ts=" ">-
116.86064613,34.86170075 -116.85924232,34.86536286
...
-112.16722572,40.70233796
-112.15178382,40.70752595</gml:coordinates>
</gml:LineString>
</gml:lineStringMember>
</gml:MultiLineString>
</NaturalEarth:the_geom>
<NaturalEarth:ScaleRank>8</NaturalEarth:ScaleRank>
<NaturalEarth:FeatureCla>Railroad</
NaturalEarth:FeatureCla>
<NaturalEarth:SOV_A3>USA</NaturalEarth:SOV_A3>
<NaturalEarth:UIDENT>49706</NaturalEarth:UIDENT>
</NaturalEarth:10m_railroads>
</gml:featureMember>
</wfs:FeatureCollection>

22.查询WCS服务能力

使用ptyhon指令如下:

myUrl = "http://localhost:8082/geoserver/wcs?service=wcs&version=1.0.0&request=GetCapabilities"
headers = {'Accept': 'text/xml'}
resp = requests.get(myUrl,auth=('admin','geoserver'),headers=headers)
file = open('getCapabilities.xml','w')
file.write(resp.text)
file.close()

返回结果包含了每个coverage影像的描述信息。nurc:Img_Sample影像的描述信息如下:

<wcs:CoverageOfferingBrief>
<wcs:description>A very rough imagery of North America</
wcs:description>
<wcs:name>nurc:Img_Sample</wcs:name>
<wcs:label>North America sample imagery</wcs:label>
<wcs:lonLatEnvelope srsName="urn:ogc:def:crs:OGC:1.3:CRS84">
<gml:pos>-130.85168 20.7052</gml:pos>
<gml:pos>-62.0054 54.1141</gml:pos>
</wcs:lonLatEnvelope>
<wcs:keywords>
<wcs:keyword>WCS</wcs:keyword>
<wcs:keyword>worldImageSample</wcs:keyword>
<wcs:keyword>worldImageSample_Coverage</wcs:keyword>
</wcs:keywords>
</wcs:CoverageOfferingBrief>

23.查询WCS某个图层的详细描述

使用python指令如下:

myUrl ="http://localhost:8082/geoserver/wcs?
service=wcs&version=1.0.0&request=DescribeCoverage&Coverage=nurc:Img_Sample"
headers = {'Accept': 'image/tiff'}
resp = requests.get(myUrl,auth=('admin','geoserver'),headers=headers)
file = open('describeCoverage.xml','w')
file.write(resp.text)
file.close()

返回结果包含了该图层的详细描述。

24.查看WCS图层的数据

查询影响图层数据,必须传递bbox、width以及height参数。使用python指如下:

myUrl = "http://localhost:8082/geoserver/wcs?
service=wcs&version=1.0.0&request=GetCoverage&coverage=nurc:Img_Sample&crs=EPSG:4326&bbox=-
130.85168,20.7052,-62.0054,54.1141&width=982&height=597&format=geotiff&bands=1"
headers = {'Accept': 'text/xml'}
resp = requests.get(myUrl,auth=('admin','geoserver'),headers=headers)
file = open('coverage.tiff','w')
file.write(resp.text)
file.close()

查询结果返回一个tiff格式的图片文件。

geoserver REST使用的更多相关文章

  1. GeoServer中WMS、WFS的请求规范

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 1.1WMS简介 Web地图服务(WMS)利用具有地理空间位置 ...

  2. [原]导入OpenStreetMap海图数据,并在GeoServer上发布

    转载请注明作者think8848和出处(http://think8848.cnblogs.com) 上回我们说到如何<在GeoServer中为OpenStreetMap数据设置OSM样式> ...

  3. [原]在GeoServer中为OpenStreetMap数据设置OSM样式

    转载请注明作者think8848和出处(http://think8848.cnblogs.com) 在前面几篇文章中,我们讲到了部署Postgresql,部署PostGis,部署GeoServer以及 ...

  4. Geoserver+Tomcat+GeoWebCache搭建地图服务

    依赖TomcatGeoserverGeoWebCache环境部署JDKTomcat服务器Geoserver配置GeoWebCache配置环境启动使用使用geowebcache进行切片 依赖 Tomca ...

  5. ElasticSearch+ElasticGeo+Geoserver发布ES地理数据

    依赖GeoserverElasticSearchElasticGeo部署部署ElasticGeo使用创建ES数据源并发布发布 依赖 Geoserver 环境搭建参考: ElasticSearch 环境 ...

  6. 简析Geoserver中获取图层列表以及各图层描述信息的三种方法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 实际项目中需要获取到Geoserver中的图层组织以及各图层 ...

  7. (十九)WebGIS中I查询的原理及设计(包含AGS、GeoServer、Supermap)

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 我们在使用arcmap时,经常会用到被称为I查询的工具.具体 ...

  8. 简析GeoServer服务的内部文件组织以及GeoServer自动化服务发布工具的开发思路

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.前言 通过GeoServer发布的服务,在GeoServer内部有 ...

  9. 简析将shp导入Oracle并利用geoserver将导入的数据发布

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.环境准备 1.1 软件准备 首先要安装有支持空间数据的Oracle ...

  10. GeoServer中利用SLD配图之矢量图层配图

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1 背景 我们在ArcMap中可以直接通过symbol功能对图层进行定 ...

随机推荐

  1. Instant Django 1.5 Application Development Starter

    Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic d ...

  2. PTS无法同步

    最近在使用PTS的时候,一直重现PTS无法同步的情况,一直显示No block source available,在查了中英各种帖子之后,终于解决了这个问题,下面是解决的办法. 在windows下运行 ...

  3. u-boot2011.09 启动流程记录

    start arch/arm/cpu/armv7/start.S 36 .globl _start 37 _start: b reset 136 reset: 137 bl save_boot_par ...

  4. cgi与fastcgi区别_转

    转自:https://www.cnblogs.com/wanghetao/p/3934350.html 当我们在谈到cgi的时候,我们在讨论什么 最早的Web服务器简单地响应浏览器发来的HTTP请求, ...

  5. 利用OpenLayers创建wkt字符串

    var polygon = OpenLayers.Geometry.Polygon.createRegularPolygon(new OpenLayers.Geometry.Point(6, 49), ...

  6. [转]C# RichTextBox不用滚动条

    转自:http://blog.csdn.net/happy09li/article/details/7444912 第一种思路: richTextBox1.ScrollBars = RichTextB ...

  7. 升级 Elasticsearch 集群数量实战记录

    搜索引擎 升级 Elasticsearch 集群数量实战记录 现在线上有一个elasticsearch集群搜索服务有三台elasticsearch实例(es1.es2.es3),打算将其升级为5台(增 ...

  8. PCL点云分割(2)

    关于点云的分割算是我想做的机械臂抓取中十分重要的俄一部分,所以首先学习如果使用点云库处理我用kinect获取的点云的数据,本例程也是我自己慢慢修改程序并结合官方API 的解说实现的,其中有很多细节如果 ...

  9. phpc.sinaapp.com 加密的解密方法

    很简单,用类似phpjm的解密方式,替换掉_inc.php中最后一个return中的eval为print就出来了. http://www.cnblogs.com/lonelyxmas/p/458285 ...

  10. 关于Unity中的小案例之运动的小船以及摄像机跟随技术(专题五)

    实例步骤 1.创建Unity项目和文件目录,保存场景 场景搭建 2.导入美术做好的资源包(第68) a: 导入地形资源包terrain.unitypackage,把里面的Map/Prefabs/Ter ...