The c call arcgis map rest service example details the of arcgis map output

  • 2020-05-27 07:01:34
  • OfStack

1. Use steps

1) build the request url

A, determine the endpoint: each GIS service has one endpoint. , for example, ArcGIS Server Demographics folder on a map service called ESRI_Census_USA sampleserver1. arcgisonline. com endpoint for: http: / / sampleserver1 arcgisonline. com ArcGIS/rest services/Demographics/ESRI_Census_USA/MapServer.

B, confirm operations: different gis services support different operations. Different operations will return different results. Map service can map output, click view, find and generate KML. The output map can generate a map, and you can click on the output to see if the map service layer's property sheet is given.

C, determine parameters: different operations require different parameters. For example, if you request a map image, you need to provide the 4-week corner coordinate parameter of the map scope, which is the map coverage.

D, determine the output format: REST API supports many output formats, such as JSON, KMZ, pictures, and HTML. The important parameter to determine the output format is f. Add "f=" to the query string requested by URL < Your format > "To determine the output format. For example: f=html returns data in html format; f=json returns data in the format json; f=image returns image and so on.

We built the URL we needed in the four steps above. 1 generally, the format is as follows:

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

As you can see, the entire URL request is divided into two parts. Part 1 is the endpoint of the service and the type of operation, i.e. "?" The front part; Part 2 is the query string, the request parameter, "?" The back part.

2) send the request to ArcGIS Server

Submit the URL request to ArcGIS Server Sending and you can send the URL request without programming. For example, simply enter a web address in the address bar of your web browser, such as IE or Firefox. Each programming language makes requests in a different way.

3) accept the response from the server

Accept the response from ArcGISServer, and ArcGIS Server processes the request and returns the response to the client. For a synchronous job, client 1 waits to receive a response from the server. For a job, the server sends a job number to periodically track the client's work status.

4) parse the server response

The response of the ArcGIS Server REST Web service can be in a variety of formats, such as JSON, KML, image, and HTML. The client can judge whether the response is successful or unsuccessful. If it fails, the client can determine the error message. If the response is successful, the client can parse the information needed for the response and use it appropriately.


2. Programming and use

Code to ArcGIS API for WPF as an example, the operation for addFeatures, here only add1 key elements, the reference ArcGIS official documentation: http: / / sampleserver3 arcgisonline. com/ArcGIS/SDK/REST/index html? fsadd. html

Reference code:


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
{
// The custom of 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"; // In order to json Format return result 
ESRI.ArcGIS.Client.Graphic g = new ESRI.ArcGIS.Client.Graphic()
{
//Graphic the Attributes in ArcGIS API for WPF  Is read-only 
// If it is writable, it can be used directly Graphic the Attributes Without splicing json
//Attributes = featureItem.Attributes, 
Geometry = featureItem.Geometry
};
FeatureSet fs = new FeatureSet();
fs.Features.Add(g);
// use FeatureSet built-in ToJson Function conversion, which helps with conversion Feature the Geometry object 
//ArcGIS the Geometry Object serialization to json String time and standard json Not too 1 sample 
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;
// use fastJson conversion 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");
// Will be originally empty Attributes Replace with self - converting json The actual condition of the string shall prevail 
string para = data.Replace("\"attributes\":{}","\"attributes\":"+attr);
string res = PostData(url, para);
// Process the returned results 
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", " Test point ");
bool res = AddFeature(url, fi);
if (res)
{
Console.WriteLine(" Add element success! ");
}
else
{
Console.WriteLine(" Add element failed! ");
}
Console.ReadKey();
}
}
}


Related articles: