Online terrain slope analysis using ArcGIS API for Silverlight

Source: Internet
Author: User

The hard-pressed postgraduate course has finally ended today. Maybe from now on, I will no longer sit in a university class and officially listen to the teacher's lecture, the next time, you have to start looking for a job ..... I have been busy some time ago, taking classes, exams, papers, and contacting my internship... the internship has not been completed yet.

Some days ago, a netizen gave an online slope analysis example. The approximate function is to select any terrain area and then implement the terrain slope analysis and visual display on the web side, as shown below:

 

The basic idea of implementation is roughly divided into the following three parts:

1. Create a slope solution model and a slope Statistical Analysis Model In ArcMap.

2. Release the model as the result map service and call it on the client.

3. Add a hierarchical view for terrain rendering based on the results.

 

1. Establish slope analysis and statistical analysis models

Open ArcMap and use modelbuilder to create the following model:

 

Note the following two points:

1. slopedata is intermediate data, which is the data created by the model but not the output of the model. If it is not set as a model parameter, the intermediate data created by the task will be automatically deleted by ArcGIS Server.

Before Version 10.1, you must explicitly set the intermediate data variables in the model builder. We strongly recommend that you use the % scratchworkspace % convention in the output path to write all the output to the scratchworkspace environment. This Convention is not required in 10.1.

2. the model has two output model parameters: slopedata, indicating the grid data of the slope, and zonaltable, indicating the statistical results of the grid data of the slope (such as the maximum, minimum, and average values of the slope ).

After the model is created, you need to publish the model as the result map service. For details about the release of the result map service in section 10.1, refer to the previous blog post.

 

2. The Silverlight client calls the result map service to obtain the slope analysis results and statistical results (maximum slope, minimum, and average values ).

The web call process is the same as the process of calling the GP service, which is roughly as follows:

1. Declare the geographical processing variable:

 Geoprocessor _geoprocessor;

2. instantiate the geographical processing service variable and register the corresponding event:

 _geoprocessor = new Geoprocessor("http://localhost:6080/arcgis/rest/services/MyGPService/SlopeAnalysis/GPServer/SlopeTool");            _geoprocessor.JobCompleted += new EventHandler<JobInfoEventArgs>(_geoprocessor_JobCompleted);            _geoprocessor.GetResultDataCompleted += new EventHandler<GPParameterEventArgs>(_geoprocessor_GetResultDataCompleted);            _geoprocessor.GetResultImageLayerCompleted += new EventHandler<GetResultImageLayerEventArgs>(_geoprocessor_GetResultImageLayerCompleted);            _geoprocessor.Failed += new EventHandler<TaskFailedEventArgs>(_geoprocessor_Failed);

3. Enter parameters related to the GP service to request the GP service.

We can see from the model created above:

The input model parameters include:

Polygon: indicates the selected area, that is, the terrain area for performing slope analysis.

Output Measurement Unit: shows how the slope analysis result is represented. There are two options: Degree: The slope is represented by the angle, the value range is between 0 and 90 degrees, and percent_rise: indicates the percentage of the elevation, the default value is degree.

Z factor: the ratio of a Z unit to an X or Y unit.

Enter GP service parameters in the background and call the service code:

// E. geometry is the polygon featureset = new featureset (E. geometry); List <gpparameter> parameter = new list <gpparameter> (); parameter. add (New gpfeaturerecordsetlayer ("polygon", featureset); parameter. add (New gpstring ("output measurement unit", "degree"); // X, Y indicates latitude and longitude, the elevation value is expressed by meters. // the formula for converting latitude and longitude to meters: Degree = meter/(2 * Math. pI * 6378137.0) * 360; parameter. add (New gpdouble ("Z _ factor", 8.98315e-6); _ geoprocessor. submitjobasync (parameter );

After requesting the GP service, the next step is to obtain the GP service result. here we need to obtain two results: first, the slope analysis result (the grid data in the format of gpresultimagelayer ), first, the slope statistical result (table, corresponding data format: gprecordset)
Here we first obtain the slope analysis result, that is, the grid data.

Raster data can be obtained through the getresultimagelayercompleted event of the geographic processing service. However, we need to request the completed event of the geographical processing service. The completed event indicates that the geographic processing task has been completed, then you can obtain the results of the geographic processing service. Sample Code:

At the beginning, we have registered the jobcompleted, getresultdatacompleted, and getresultimagelayercompleted events of the geographic processing service, and then complete the corresponding code in the corresponding part,

Jobcompleted is the prerequisite for obtaining the results. All requests to obtain the geographical processing results must wait for the completion of the geographical processing service (and jobcompleted). At the same time, the two results cannot be requested at the same time, the code for obtaining raster data is as follows:

Private void _ geoprocessor_jobcompleted (Object sender, jobinfoeventargs e) {If (E. jobinfo. jobstatus = esrijobstatus. esrijobsucceeded) {jobid = E. jobinfo. jobid; httpwebrequest. registerprefix ("http: //", system. net. browser. webrequestcreator. clienthttp); _ geoprocessor. getresultimagelayerasync (E. jobinfo. jobid, "slopedata"); // note that you cannot request the GP service result at the same time. If you add the following code here, an error occurs. // _ geoprocessor. getresultdataasync (jobid, "zonaltable");} else {MessageBox. show ("failed to request GP service" + E. jobinfo. messages. tostring ());}}

The next step is to obtain the Raster Data of the slope, display it on the map, and send a request to obtain the statistical table at the same time:

Private void _ geoprocessor_getresultimagelayercompleted (Object sender, getresultimagelayereventargs e) {// The returned result is actually an image gpresultimagelayer imagelayer = E. gpresultimagelayer; // defines the layer ID imagelayer. id = "interpolationlayer"; // sets the transparency of imagelayer. opacity = 0.7; imagelayer. displayname = "slope layer"; // clear the original result if (map1.layers ["interpolationlayer"]! = NULL) {map1.layers. remove (map1.layers ["interpolation"]);} // Add the current result to the layer map1.layers. add (imagelayer); // obtain the slope statistics table data _ geoprocessor. getresultdataasync (jobid, "zonaltable ");}

Obtain the data in the slope statistics table. The format transmitted on the Web Client is gprecordset. You can directly traverse the table. Here, because a slope analysis is executed, there is only one record in the table. The sample code is as follows:

Private void _ geoprocessor_getresultdatacompleted (Object sender, gpparametereventargs e) {gprecordset GPR = E. parameter as gprecordset; If (GPR. featureset! = NULL) {double slope_maxvalue = convert. todouble (GPR. featureset. features [0]. attributes ["Max"]); double slope_minvalue = convert. todouble (GPR. featureset. features [0]. attributes ["min"]); double slope_meanvalue = convert. todouble (GPR. featureset. features [0]. attributes ["mean"]); meanslopevalue. TEXT = slope_meanvalue.tostring ("0.000"); List <solidcolorbrush> rendercolors = new list <solidcolorbrush> (); // The default value is 10. The color ranges from green to red rendercolors = createcolors. createcolorlist (10); // interval value double stepvalue = (slope_maxvalue-slope_minvalue)/rendercolors. count; // construct a list of colors and descriptions of Different Levels <rendermodel> rendermodels = new list <rendermodel> (); For (INT I = 0; I <rendercolors. count; I ++) {rendermodels. add (New rendermodel () {endvalue = (I + 1) * stepvalue, startvalue = I * stepvalue, rendercolor = rendercolors [I]});} // bind to rendercolorlistbox In The ListBox where the rendering level is displayed. itemssource = rendermodels; rendercolorborder. visibility = visibility. visible ;}}

The above Code contains the color level display legend for Raster Data Rendering, in which a method is used: createcolorlist, which is used to construct different gradient colors from green to yellow to red.

       public static List<SolidColorBrush> CreateColorList(int _classCount)        {            List<SolidColorBrush> ColorList=new List<SolidColorBrush> ();            double step=255/_classCount;            for (int i = 0; i <= _classCount/2; i++)            {                ColorList.Add(new SolidColorBrush(Color.FromArgb(255, (byte)(i*step*2), 255, 0)));            }            for (int i = _classCount / 2 + 1; i <= _classCount; i++)            {                ColorList.Add(new SolidColorBrush(Color.FromArgb(255, 255,(byte)((_classCount-i)*step*2) , 0)));            }            return ColorList;        }

And rendermodel. The code for this class is as follows:

  public class RenderModel    {        public SolidColorBrush RenderColor { get; set; }        public double StartValue { get; set; }        public double EndValue { get; set; }        public string Description        {            get { return string.Format("{0}-{1}", StartValue.ToString("#0.00"), EndValue.ToString("#0.00")); }        }    }

This completes the slope analysis and color level legend.

A few more!

Initial interface:

 

Select the terrain area for slope analysis:

Ongoing slope analysis:

 

The slope analysis result shows:

 

PS: It took me one night to write this log. I failed to upload the image and refresh it after the failure. I did not save what I wrote before, but I did not save it, the Code has not been successful for a long time. Therefore, if you need code, please leave your mailbox. I will send the code to your mailbox immediately.

Finally, thank you for reading my article. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.