geoserver REST使用


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文件輸出到哪里去了,沒找到。 輸出結果如下:

image 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服務界面查看文件路徑:

image

        對應的物理路徑在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”。

image

    如果我們想增加更多的圖層配置,例如顯示名稱“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格式的圖片文件。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM