Select to view content in your preferred language

Projecting multiple points inside for loop

746
2
Jump to solution
10-05-2017 05:56 AM
SivaramKrishnan2
New Contributor III

Hello,

Here i'm facing some issue in projecting points inside a for loop

please refer the code below,

var jsonFS = new Object();
jsonFS.geometryType = "esriGeometryPoint";
var features = [];
var taskQuery = [];
var pts = [];
gsvc = new esri.tasks.GeometryService("https://<your Domain>/webgis/rest/services/Utilities/Geometry/GeometryServer/");
for (i = 0; i < 100 i++)
{
var attributeObj = new Object();
attributeObj.objectid = i;
attributeObj.latitude = response.EntityData.lat;
attributeObj.longitude = response.EntityData.lon;
attributeObj.pop = response.EntityData.pop;

pt = new Point(response.EntityData.lon,response.EntityData.lat,new SpatialReference(4326));
var outSR = new SpatialReference(this.map.spatialReference);
var prjParams = new esri.tasks.ProjectParameters();
prjParams.geometries = [pt];
prjParams.outSR = outSR;
gsvc.project(prjParams,function(projectedPoints) {
ptnew = projectedPoints[0];
});

this.feature = new Object();
this.feature.attributes = attributeObj;
this.feature.geometry = ptnew;
this.features.push(feature);
}

here when the execution reaches "gsvc.project" , the execution was skipped and remaining codes are executed.

after completing the entire for loop the "gsvc.project" is executing asynchronously for the loop cont(100).

Can anyone help me to execute this projection inside a for loop in a sequential manner.

Thanks & Regards

Siva

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Siva,

  Like Thomas is saying you use one geometry call like this:

var jsonFS = new Object();
jsonFS.geometryType = "esriGeometryPoint";
var features = [];
var taskQuery = [];
var pts = [];
gsvc = new esri.tasks.GeometryService("https://<your Domain>/webgis/rest/services/Utilities/Geometry/GeometryServer/");
for(i = 0; i < 100 i++) {
  var attributeObj = new Object();
  attributeObj.objectid = i;
  attributeObj.latitude = response.EntityData[i].lat;
  attributeObj.longitude = response.EntityData[i].lon;
  attributeObj.pop = response.EntityData[i].pop;
  pt = new Point(response.EntityData[i].lon, response.EntityData[i].lat, new SpatialReference(4326));
  pts.push(pt);
  this.feature = new esri.Graphic(pt,null,attributeObj);
  //this.feature.attributes = attributeObj;
  //this.feature.geometry = pt;
  this.features.push(feature);
}

//var outSR = new SpatialReference(this.map.spatialReference);
var prjParams = new esri.tasks.ProjectParameters();
prjParams.geometries = pts;
prjParams.outSR = this.map.spatialReference;
gsvc.project(prjParams, function(projectedPoints) {
  for(g = 0; g < projectedPoints.length; g++) {
    var gra = this.features[g];
    gra.setGeometry(projectedPoints[g]);
  }
});

I have some concerns about scope in your code.

View solution in original post

0 Kudos
2 Replies
ThomasSolow
Regular Contributor

As long as you're projecting from spatial reference A to spatial reference B for all 100 geometries, you only need to make one request to GeometryService.project.  So in your case, you should get an array of geometries from every feature you want to project and use that array as prjParams.geometries.

In general, when you want to do many requests at once, you should use Promise.all, like this:

var features = [...] // array of graphics
var promiseArray = []; // empty array

for (let i = 0; i < features.length; i++){
  var promise = doThingAsync(feature);
  promiseArray.push(promise);
}

Promise.all(promiseArray).then(function(results){
  // this code will be executed when every promise resolves
  ...

});

// function does something async, like an http request, and returns a promise
function doThingAsync(feature){
  return new Promise(...);
}
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

But you probably don't have to do this here.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Siva,

  Like Thomas is saying you use one geometry call like this:

var jsonFS = new Object();
jsonFS.geometryType = "esriGeometryPoint";
var features = [];
var taskQuery = [];
var pts = [];
gsvc = new esri.tasks.GeometryService("https://<your Domain>/webgis/rest/services/Utilities/Geometry/GeometryServer/");
for(i = 0; i < 100 i++) {
  var attributeObj = new Object();
  attributeObj.objectid = i;
  attributeObj.latitude = response.EntityData[i].lat;
  attributeObj.longitude = response.EntityData[i].lon;
  attributeObj.pop = response.EntityData[i].pop;
  pt = new Point(response.EntityData[i].lon, response.EntityData[i].lat, new SpatialReference(4326));
  pts.push(pt);
  this.feature = new esri.Graphic(pt,null,attributeObj);
  //this.feature.attributes = attributeObj;
  //this.feature.geometry = pt;
  this.features.push(feature);
}

//var outSR = new SpatialReference(this.map.spatialReference);
var prjParams = new esri.tasks.ProjectParameters();
prjParams.geometries = pts;
prjParams.outSR = this.map.spatialReference;
gsvc.project(prjParams, function(projectedPoints) {
  for(g = 0; g < projectedPoints.length; g++) {
    var gra = this.features[g];
    gra.setGeometry(projectedPoints[g]);
  }
});

I have some concerns about scope in your code.

0 Kudos