Yesterday, a QQ friend asked me a question about cropping. I felt that I was not doing anything for me. Then I made an example of cropping, however, we have encountered many problems in this example. I would like to share with you here.
1. Implementation of the cropping Function
The cropping function here is very simple. You only need a clip tool.
However, you must note that the parameters of the cropping tool are as follows:
The cropping tool has two input parameters:
A. input element: this refers to what you use to crop the target element, that is, what you use to crop the target element.
B. Cropping element: this refers to the cropped element, that is, the target element, that is, who you want to crop.
For example, I now have a map of China, and I want to crop the part of a map of China containing any shape I input.
As shown in:
Map of China as the basemap (cropping element)
Input polygon (input element) to crop the map of China
Enter the map of China included in the Variable Line
The preceding section mainly describes the cropping tool. If it is reversed, the result cannot be obtained.
2. GP Service Model
The above describes the notes and functions of the cropping tool. The following is a model of the GP service, which is relatively simple. You only need to pay attention to the input elements and cropping elements. It is provided above and will not be explained here.
3. Publish the GP Service
Right-click the toolbox and publish it to ArcGIS Server.
Enter the GP service name and click Next to complete the release.
The GP service that implements the cropping function has been released. It is recommended that you verify the cropping function In ArcMap.
Note:
As mentioned above, the cropping tool must be careful not to reverse the input and cropping elements. In fact, when the parameters of the cropping tool are reversed, we will release the GP service to implement the cropping, and no error will be reported, in addition, the verification results In ArcMap will be the same as those in the absence of inversion. That is to say, it doesn't matter if the parameters of the cropping tool are reversed only for ArcMap, that is, it does not affect the final result, but when we call on the Web Side (this article is only for the Silverlight client), we will not get the correct result, however, no error is reported during the whole process when the Silverlight client calls the GP service.
For details about the release and precautions of the GP service, refer to the previous blog.
2. Silverlight client code
(1) declare a draw object and draw a polygon as the cropping element.
Draw myDraw = null;
(2) declare a geographical processing service variable for requesting the GP Service
Geoprocessor _geoprocessor = null;
(3) declare a graphicslayer to store the drawn polygon and the final cropping result.
GraphicsLayer graphicLayer = null;
(4) instantiate the above parameters
myDraw= new Draw(map1); myDraw.DrawComplete += new EventHandler<DrawEventArgs>(myDraw_DrawComplete); myDraw.DrawMode = DrawMode.Polygon; myDraw.IsEnabled = false; DrawButton.Click += new RoutedEventHandler(DrawButton_Click); ClipButton.Click += new RoutedEventHandler(ClipButton_Click); ClearButton.Click += new RoutedEventHandler(ClearButton_Click);
graphicLayer = map1.Layers["ClipGraphic"] as GraphicsLayer; _geoprocessor = new Geoprocessor("http://qzj-pc/arcgis/rest/services/ClipService/GPServer/ClipTool"); _geoprocessor.JobCompleted += new EventHandler<JobInfoEventArgs>(_geoprocessor_JobCompleted); _geoprocessor.Failed += new EventHandler<TaskFailedEventArgs>(_geoprocessor_Failed);
The preceding statements instantiate the draw object and geoprocessor, and register the corresponding button event.
(5). Click the draw Polygon Button to start plotting the cropped element polygon. Therefore, add the following code to the Click Event function of drawbutton:
Private void drawbutton_click (Object sender, routedeventargs e) {// set draw to true and start drawing mydraw. isenabled = true ;}
(6). After the painting is completed, obtain the drawn polygon in the drawcomplete event function and add it to the layer. The sample code is as follows:
Private void mydraw_drawcomplete (Object sender, draweventargs e) {graphiclayer. graphics. clear (); mydraw. isenabled = false; // The polygon is obtained because drawmode is set to Polygon. If drawmode is set to a rectangle, an error occurs. // The rectangle is not polygon, because only polygon, polyline, and mappoint can be specified as graphic geometry polygon clippolygon = E. geometry as polygon; graphic = new graphic () {geometry = clippolygon, symbol = layoutroot. resources ["fillsymbol"] As simplefillsymbol,}; graphiclayer. graphics. add (graphic );}
(7) after the painting is complete, the next step is to start the cropping and clip operation in the Click Event function of clipbutton.
Obtain the graphic in the graphiclayer. Before that, you need to make a judgment to confirm that graphicslayer is not empty.
Then, these graphic forms a featureset and uses this featureset as the input parameter of the GP service,
Finally, request the GP service. The sample code is as follows:
Private void clipbutton_click (Object sender, routedeventargs e) {If (graphiclayer. Graphics. Count = 0) {MessageBox. Show ("the cropping element cannot be blank! "); Return;} featureset = new featureset (); // traverse graphic and add it to featureset foreach (Graphic g in graphiclayer. graphics) {featureset. features. add (g) ;}// declare the GP service parameter list <gpparameter> gpparameter = new list <gpparameter> (); // assign gpparameter to the GP service parameter name named clippolygon. add (New gpfeaturerecordsetlayer ("clippolygon", featureset); // request GP service _ geoprocessor. submitjobasync (gpparameter );}
(8). register the getresultdatacompleted event in the jobcompleted event of the GP service and request the result of the GP service. The sample code is as follows:
Void _ geoprocessor_jobcompleted (Object sender, jobinfoeventargs e) {// If the GP service is successfully executed, obtain the result if (E. jobinfo. jobstatus = esrijobstatus. esrijobsucceeded) {_ geoprocessor. getresultdatacompleted + = new eventhandler <gpparametereventargs> (_ geoprocessor_getresultdatacompleted); _ geoprocessor. getresultdataasync (E. jobinfo. jobid, "clipresult");} else if (E. jobinfo. jobstatus = esrijobstatus. esrijobfailed) {message Box. Show ("GP service execution failed! ");}}
(9) Finally, in the getresultdatacompleted event function, obtain the execution result of the GP service and map it to reality. Sample Code:
Private void _ geoprocessor_getresultdatacompleted (Object sender, gpparametereventargs e) {graphiclayer. graphics. clear (); If (E. parameter is gpfeaturerecordsetlayer) {gpfeaturerecordsetlayer featuresetlayer = E. parameter as gpfeaturerecordsetlayer; If (featuresetlayer. featureset. features. count = 0) {MessageBox. show ("the cropping result is blank! ");} Else {for (INT I = 0; I <featuresetlayer. featureset. features. count; I ++) {featuresetlayer. featureset. features [I]. symbol = layoutroot. resources ["resultfillsymbol"] as ESRI. arcGIS. client. symbols. fillsymbol; graphiclayer. graphics. add (featuresetlayer. featureset. features [I]) ;}}}
Last:
Cropping result:
(All Rights Reserved. For details, refer to the source)