Overview:
The existence of a GP service enables the use of spatial analysis provided by ArcGIS on the web side, and the capabilities of these analyses are the same as in the desktop. Therefore, it is a focus of ArcGIS for JS, but also a difficult point. Therefore, this article describes how to publish and call the GP service in code to implement buffer analysis calculations.
Brief introduction:
Frame Introduction Reference article: http://www.cnblogs.com/HPhone/archive/2012/11/05/2755833.html
Service Release reference article: http://www.cnblogs.com/HPhone/archive/2012/11/18/2775492.html
Model parameters:
Parameters:Parameter:input Data type:gpfeaturerecordsetlayer Display Name input description:input buffer direction:es Rigpparameterdirectioninput Default value:geometry type:esrigeometrypoint hasz:false hasm:false Spatial reference:432 6 (4326) Fields:fid (type:esrifieldtypeoid, Alias:fid) name (type:esrifieldtypestring, Alias:name, length:100) ID (type:esrifieldtypedouble, alias:id) Features:None.Parameter type:esrigpparametertyperequired category:parameter : Output Data type:gpfeaturerecordsetlayer Display Name output description:ouput feature Direction:esrigpparameterdirec Tionoutput Default value:geometry type:esrigeometrypolygon hasz:false hasm:false Spatial reference:4326 (4326) fields : FID (type:esrifieldtypeoid, Alias:fid) name (type:esrifieldtypestring, Alias:name, length:100) ID (type:esrif Ieldtypedouble, Alias:id) buff_dist (type:esrifieldtypedouble, Alias:buff_dist) shape_length (Type:esrifieldtypedo Uble, Alias:shape_length) Shape_area (type:esrifieldtypedouble, Alias:shape_area) Features:None.Parameter type:esrigpparametertyperequired Category:Parameter:Distance__value_or_field_ Data type:gplinearunit Display Name Distance description:distance Direct Ion:esrigpparameterdirectioninput Default value:50.0 (esrikilometers) Parameter type:esrigpparametertyperequired Cat Egory:
Description
There are three parameters in the model: 1, input, 2, output, 3, buffer distance unit or field.
Code implementation:
1. Add drawing tools and define events
toolbar = new Draw (map); Dojo.connect (toolbar, ' Ondrawend ', drawend); $ ("#point"). On ("click", Function () { map.graphics.clear (); Toolbar.activate (Esri.toolbars.Draw.POINT); }); $ ("#polyline"). On ("click", Function () { map.graphics.clear (); Toolbar.activate (Esri.toolbars.Draw.POLYLINE); }); $ ("#polygon"). On ("click", Function () { map.graphics.clear (); Toolbar.activate (Esri.toolbars.Draw.POLYGON); });
2. After the drawing is finished, submit the calculation
/** * Draw End * @param geometry */function Drawend (geometry) {$.messager. Prompt (' Hint message ', ' Enter buffer range: ', function (dist) {if (dist) {$.messager.progress ({ Text: "In the calculation, please later ..."}); Toolbar.deactivate (); var symbol = NULL; if (geometry.type=== "point") {symbol = new Esri.symbol.SimpleMarkerSymbol (esri.symbol.SimpleMarkerSy Mbol. Style_square, New Esri.symbol.SimpleLineSymbol (Esri.symbol.SimpleLineSymbol.STYLE_SOLID , New ESRI. Color ([255,0,0]), 1), New ESRI. Color ([0,255,0,0.25])); } else if (geometry.type=== "Polyline") {symbol=new Esri.symbol.SimpleLineSymbol (E Sri.symbol.SimpleLineSymbol.STYLE_DASHDOT, New Dojo. Color ([255, 0, 0]), 2); } else{symbol = new Esri.symbol.SimpleFillSymbol (esri.symbol.Si Mplefillsymbol.style_solid, New Esri.symbol.SimpleLineSymbol (Esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, New dojo. Color ([255, 0, 0]), 2), New dojo. Color ([255, 255, 0, 0.25])); } var graphic = new Esri. Graphic (geometry, symbol); Map.graphics.add (graphic); Tojob (graphic,dist); } }); } function Tojob (graphic,distance) {//First step constructs gp var gpurl = ' http://localhost: 6080/arcgis/rest/services/buffer/gpserver/buffer '; GP = new Esri.tasks.Geoprocessor (Gpurl); The second step, the construction parameters//We pass above, to understand that gpfeaturerecordsetlayer corresponds to featureset var features = []; Features.push (graphic); var featureset = new Esri.tasks.FeatureSet (); Featureset.features = features; Construction SlowPunch length, here the unit is can be changed, I use the degree, simple some var Dis = new Esri.tasks.LinearUnit (); Dis.distance = distance; Dis.units = Esri. Units.kilometers; var parms = {input:featureset, distance__value_or_field_: Dis}; Here the function is asynchronous, using the function is Submitjob, the synchronous use is execute. After the success, call Jobresult, it is recommended to look at this parameter. Gp.submitjob (parms, Jobresult); }
3, the calculation of the successful drawing of the results
/** * Calculation Complete * @param result * * function Jobresult (result) {var jobId = R Esult.jobid; var status = Result.jobstatus; if (status = = = Esri.tasks.JobInfo.STATUS_SUCCEEDED) {//successful, the results are taken out, of course, this is the parameter name. In the model, to take out the intermediate results, you need to set the model parameter Gp.getresultdata (jobId, "output", addresults); }} function Addresults (results) {$.messager.progress (' close '); var features = Results.value.features; if (features.length>0) {for (var i = 0, length = features.length; I! = length; ++i) { var feature = Features[i]; var polysymbolred = new Esri.symbol.SimpleFillSymbol (); Polysymbolred.setoutline (New Esri.symbol.SimpleLineSymbol (Esri.symbol.SimpleLineSymbol.STYLE_SOLID, New Dojo). Color ([0, 0, 0, 0.5]), 1)); Polysymbolred.setcolor (New dojo. Color ([255, 0, 0, 0.5])); Feature.setsymbol (polysymbolred); Map.graphics.add (feature); } $.messager.alert ("Prompt", "Calculation succeeded!") "); } else{$.messager.alert ("hint", "Calculation failed!") "); } }
post-Implementation effects:
Input distance
Point calculation succeeded
Line Buffer
Face buffer
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Arcgis for JS GP implements buffer calculation