c#調(diào)用arcgis地圖rest服務(wù)示例詳解(arcgis地圖輸出)
1、使用步驟
1)構(gòu)建請求網(wǎng)址
A、確定端點(diǎn):每個(gè)GIS服務(wù)都有一個(gè)端點(diǎn)。例如,ArcGIS Server上Demographics文件夾下名為ESRI_Census_USA的一個(gè)地圖服務(wù)sampleserver1.arcgisonline.com的端點(diǎn)為:http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer.
B、確定操作:不同地理信息系統(tǒng)服務(wù)支持不同的操作。不同的操作會(huì)返回不同的結(jié)果。地圖服務(wù)可以地圖輸出,點(diǎn)擊查看,查找和生成KML。輸出地圖可以生成地圖,同時(shí)可以點(diǎn)擊產(chǎn)看是否給出地圖服務(wù)圖層的屬性表。
C、確定參數(shù):不同的操作需要不同的參數(shù)。例如,如果請求地圖圖片,需要提供地圖范圍的四周角點(diǎn)坐標(biāo)參數(shù),也就是地圖覆蓋范圍。
D、確定輸出格式:REST API支持很多輸出格式,例如JSON,KMZ,圖片和HTML。確定輸出格式的重要參數(shù)是f。在URL請求的查詢字符串后面加上”f=<你的格式>”來確定輸出格式。例如:f=html返回的數(shù)據(jù)格式為html;f=json返回的數(shù)據(jù)格式為json;f=image返回的格式為image等等。
我們就以上面的4個(gè)步驟來構(gòu)建自己需要的URL。一般來說,格式如下:
http://{ArcGIS Server name}/ArcGIS/rest/services/{foldername}/{service name}/{service type}/{operation}?{{parameter1}={somevalues}&{parameter2}={some values}&…&{parameter}={some values}}
可以看到,整個(gè)URL請求分為兩個(gè)部分,第一部分是服務(wù)的端點(diǎn)和操作類型,也就是“?”前面的部分;第二部分是查詢字符串,即請求參數(shù),“?”后面的部分。
2)發(fā)送請求到ArcGIS Server
提交URL請求到ArcGIS Server Sending,可以不通過編程發(fā)送URL請求。例如,只需在網(wǎng)頁瀏覽器的地址欄輸入網(wǎng)址,如IE或Firefox。每種編程語言都用不同的提出請求方式。
3)接受服務(wù)器的響應(yīng)
接受ArcGISServer的響應(yīng),ArcGIS Server處理請求并返回響應(yīng)到客戶端。對于一個(gè)同步的工作,客戶端一直等待收到服務(wù)器的響應(yīng)。對于一部工作,服務(wù)器發(fā)送一份工作編號來定期跟蹤客戶端的工作狀態(tài)。
4)解析服務(wù)器響應(yīng)
ArcGIS Server REST Web服務(wù)的響應(yīng)可以是多種格式,例如JSON,KML,圖片和HTML??蛻舳丝膳袛囗憫?yīng)時(shí)成功還是失敗。如果失敗了,客戶端可以判斷錯(cuò)誤信息。如果響應(yīng)是成功的,客戶端可以解析響應(yīng)所需的信息,并恰當(dāng)?shù)乩眠@些信息。
2、編程使用
代碼以ArcGIS API for WPF為例,操作為addFeatures,這里只是add一個(gè)要素點(diǎn),參考ArcGIS官方文檔說明:http://sampleserver3.arcgisonline.com/ArcGIS/SDK/REST/index.html?fsadd.html
參考代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;
using System.Net;
using System.IO;
namespace ArcGISDemo
{
//自定義的Feature
class FeatureItem
{
public Geometry Geometry { set; get; }
public IDictionary<string, object> Attributes { set; get; }
};
class Program
{
static bool AddFeature(string layerUrl, FeatureItem featureItem)
{
string url = layerUrl+"/addFeatures";
string data = "f=json"; //以json格式返回結(jié)果
ESRI.ArcGIS.Client.Graphic g = new ESRI.ArcGIS.Client.Graphic()
{
//Graphic的Attributes在ArcGIS API for WPF 中是只讀的
//如果是可寫的,就可以直接使用Graphic的Attributes,而不需要拼接json
//Attributes = featureItem.Attributes,
Geometry = featureItem.Geometry
};
FeatureSet fs = new FeatureSet();
fs.Features.Add(g);
//使用FeatureSet自帶的ToJson函數(shù)轉(zhuǎn)換,可以幫助轉(zhuǎn)換Feature的Geometry對象
//ArcGIS的Geometry對象序列化為json字符串時(shí)和標(biāo)準(zhǔn)的json不太一樣
string json = fs.ToJson();
int begin = json.IndexOf("[");
int end = json.IndexOf("]", begin);
string featuresJson = json.Substring(begin, end - begin + 1);
string features = string.Format("&features={0}", featuresJson);
data += features;
//使用fastJson轉(zhuǎn)換Attributes
//fastJSON.JSON.Instance.Parameters.UseEscapedUnicode = false;
//string attr = fastJSON.JSON.Instance.ToJSON(featureItem.Attributes);
string attr = Newtonsoft.Json.JsonConvert.SerializeObject(featureItem.Attributes);
//int attrPos = data.IndexOf("attributes");
//將原來空的Attributes替換掉,以自己轉(zhuǎn)換的json字符串實(shí)際情況為準(zhǔn)
string para = data.Replace("\"attributes\":{}","\"attributes\":"+attr);
string res = PostData(url, para);
//處理返回的結(jié)果
if (res.Contains("error"))
return false;
Dictionary<string, List<Dictionary<string, object>>> resDic
= Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, List<Dictionary<string, object>>>>(res);
if (resDic.ContainsKey("addResults"))
{
List<Dictionary<string, object>> addRes = resDic["addResults"];
foreach (Dictionary<string, object> dic in addRes)
{
if (dic.ContainsKey("success"))
{
if (dic["success"].ToString().ToLower() == "true")
return true;
else return false;
}
}
}
return false;
}
static string PostData(string url, string data)
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] bs = Encoding.UTF8.GetBytes(data);
Stream reqStream = request.GetRequestStream();
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
string responseString = null;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
responseString = reader.ReadToEnd();
reader.Close();
}
return responseString;
}
static void Main(string[] args)
{
string url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/0";
MapPoint point = new MapPoint(105, 30);
FeatureItem fi = new FeatureItem();
fi.Geometry = point;
fi.Attributes = new Dictionary<string, object>();
fi.Attributes.Add("description", "測試點(diǎn)");
bool res = AddFeature(url, fi);
if (res)
{
Console.WriteLine("添加要素成功!");
}
else
{
Console.WriteLine("添加要素失??!");
}
Console.ReadKey();
}
}
}
相關(guān)文章
C#判斷一個(gè)字符串是否是數(shù)字或者含有某個(gè)數(shù)字的方法
這篇文章主要介紹了C#判斷一個(gè)字符串是否是數(shù)字或者含有某個(gè)數(shù)字的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06C#保存listbox中數(shù)據(jù)到文本文件的方法
這篇文章主要介紹了C#保存listbox中數(shù)據(jù)到文本文件的方法,涉及C#操作listbox數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-04-04C#中Activator.CreateInstance()方法用法分析
這篇文章主要介紹了C#中Activator.CreateInstance()方法用法,實(shí)例分析了C#中Activator.CreateInstance()方法的功能、定義及使用技巧,需要的朋友可以參考下2015-03-03