geoserver的api接口_geoserver REST使用
1.部署一個簡單的測試環境
測試geoserver REST接口,我們可使用python來測試,很方便。需要下載包:
python,http://python.org/。我下載的是Python27版本。
cURL,幾個簡單的命令行工具,很容易使用命令交互, 地址為http://curl.haxx.se/download.html,下載curl-7.53.1-win64-mingw版本。
三個包現在后,配置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'。查詢結果:
tiger
5.查詢命名空間
6.創建命名空間
使用curl創建,指令如下:
curl -u admin:geoserver -XPOST -H 'Content-type: text/xml' -d 'newWorkspacehttp://geoserver.org' 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內容:
newWorkspacehttp://geoserver.org
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內容:
newWorkspacehttp://localhost:8082/geoserver
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內容如下:
50m-rivers-lake-centerlines
ne_10m_railroads
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()
輸出結果如下:
/p>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
GeoServer Configuration- ne_50m_populated_places
總結
以上是生活随笔為你收集整理的geoserver的api接口_geoserver REST使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 老电脑内存容量查找大揭秘!三招教你轻松搞
- 下一篇: CPU和内存:电脑世界的黄梅戏