c#调用arcgis地图rest服务示例详解(arcgis地图输出)


1、使用步骤

1)构建请求网址

A、确定端点:每个GIS服务都有一个端点。例如,ArcGIS Server上Demographics文件夹下名为ESRI_Census_USA的一个地图服务sampleserver1.arcgisonline.com的端点为:http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer.

B、确定操作:不同地理信息系统服务支持不同的操作。不同的操作会返回不同的结果。地图服务可以地图输出,点击查看,查找和生成KML。输出地图可以生成地图,同时可以点击产看是否给出地图服务图层的属性表。

C、确定参数:不同的操作需要不同的参数。例如,如果请求地图图片,需要提供地图范围的四周角点坐标参数,也就是地图覆盖范围。

D、确定输出格式:REST API支持很多输出格式,例如JSON,KMZ,图片和HTML。确定输出格式的重要参数是f。在URL请求的查询字符串后面加上”f=<你的格式>”来确定输出格式。例如:f=html返回的数据格式为html;f=json返回的数据格式为json;f=image返回的格式为image等等。

我们就以上面的4个步骤来构建自己需要的URL。一般来说,格式如下:

http://{ArcGIS Server name}/ArcGIS/rest/services/{foldername}/{service name}/{service type}/{operation}?{{parameter1}={somevalues}&{parameter2}={some values}&…&{parameter}={some values}}

可以看到,整个URL请求分为两个部分,第一部分是服务的端点和操作类型,也就是“?”前面的部分;第二部分是查询字符串,即请求参数,“?”后面的部分。

2)发送请求到ArcGIS Server

提交URL请求到ArcGIS Server Sending,可以不通过编程发送URL请求。例如,只需在网页浏览器的地址栏输入网址,如IE或Firefox。每种编程语言都用不同的提出请求方式。

3)接受服务器的响应

接受ArcGISServer的响应,ArcGIS Server处理请求并返回响应到客户端。对于一个同步的工作,客户端一直等待收到服务器的响应。对于一部工作,服务器发送一份工作编号来定期跟踪客户端的工作状态。

4)解析服务器响应

ArcGIS Server REST Web服务的响应可以是多种格式,例如JSON,KML,图片和HTML。客户端可判断响应时成功还是失败。如果失败了,客户端可以判断错误信息。如果响应是成功的,客户端可以解析响应所需的信息,并恰当地利用这些信息。


2、编程使用

代码以ArcGIS API for WPF为例,操作为addFeatures,这里只是add一个要素点,参考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格式返回结果

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函数转换,可以帮助转换Feature的Geometry对象
//ArcGIS的Geometry对象序列化为json字符串时和标准的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转换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替换掉,以自己转换的json字符串实际情况为准
string para = data.Replace("\"attributes\":{}","\"attributes\":"+attr);

string res = PostData(url, para);

//处理返回的结果
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", "测试点");
bool res = AddFeature(url, fi);
if (res)
{
Console.WriteLine("添加要素成功!");
}
else
{
Console.WriteLine("添加要素失败!");
}
Console.ReadKey();
}
}
}


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3