xupla

Calculate the length of pipes within a polygon

Discussion created by xupla on Apr 15, 2014
Latest reply on Jun 13, 2014 by Muditagarwal100
I am calculating the total length of poly-lines (pipes) within a polygon. I am querying the layer and I am getting the result. The problem is that when I'm comparing the results with ArcMap I have some difference in the length. My code is:

featureLayer.on("click", function (evt) {
 map.graphics.clear();
 var highlightGraphic = new Graphic(evt.graphic.geometry, highlightSymbol);
 map.graphics.add(highlightGraphic);

 var query = new Query();            
 var queryTask = new QueryTask("feature layer with polylines");
 query.geometry = evt.graphic.geometry;
 query.returnGeometry = true;
 query.outSpatialReference = map.spatialReference;
 query.outFields = ["*"];

 queryTask.execute(query, function (results) {
  var lengthParams = new LengthsParameters();

  var pipesList = [];
  for (var i = 0; i < results.features.length; i++) {
   pipesList.push(results.features[i].geometry);
  }
  lengthParams.polylines = pipesList;
  lengthParams.lengthUnit = GeometryService.UNIT_KILOMETER;
  geometryService.lengths(lengthParams);
});

dojo.connect(geometryService, "onLengthsComplete", outputDistance);

function outputDistance(result) {
    var total = 0;
    for (var i = 0; i < result.lengths.length; i++) {
        total += result.lengths[i];
    }
    alert("Count: " + result.lengths.length + " Distance: "  + total.toFixed(2) + "km");
}



Am I doing something wrong with my code? Thanks

Outcomes