Intersect on selected polygons

720
3
Jump to solution
01-31-2018 01:56 PM
AJRS
by
New Contributor

I am trying to select multiple polygons with a Query and then pass that into a second spatial query to see what features from subsequent layer they intersect.  The output is then a table with the initial polygons and the attributes from the intersected polygon 'joined' to each feature.  I am not very proficient with JavaScript and so I found a similar thread and solution posted by Andrew Farrar that was pretty much exactly what I was trying to accomplish (Thank you!).  What I have subsequently realized is that he used a FindTask to select the initial feature and then passed that into a spatialquery.  My problem is once I got that to work I realized I couldn't select more than one feature with a FindTask and now am trying to substitute in a QueryTask for the FindTask and things aren't working.  I am guessing it is because FindTask and QueryTask return differently but I can't figure out how to now pass my results to call the spatialquery.  No matter what I try I can't seem to get the attributes to write out even though I can see in the array that I have 5 objects.

Any help would be VERY much appreciated!!!

// Called on each button click
function doFind() {

var claims = ["P 48582", "P 48581", "P 48580", "P 48579", "P 48578"];

var queryTask = new QueryTask("url");

var query = new Query();
query.returnGeometry = true;
query.outFields = [
"GRANT_NUMBER", "CLAIM_NAME", "CLAIM_LABEL", "TENURE_STATUS", "OWNER_NAME"
];
query.where = "GRANT_NUMBER in ('" + claims.join("','") + "')";
alert(query.where);
queryTask.execute(query)
.then(doGeoQuery)
.otherwise(rejectedPromise);

}

// Called if the Promise is Resolved
function doGeoQuery(results) {

//Empty the HTML table from previous results
resultsTable.innerHTML = "";
// Setup HTML table for new results
// Set up row for descriptive headers to display results
var topRow = resultsTable.insertRow(0);
var cell1 = topRow.insertCell(0);
var cell2 = topRow.insertCell(1);
var cell3 = topRow.insertCell(2);
var cell4 = topRow.insertCell(3);
var cell5 = topRow.insertCell(4);
var cell6 = topRow.insertCell(5);

cell1.innerHTML = "<b>Grant Number</b>";
cell2.innerHTML = "<b>Claim Name</b>";
cell3.innerHTML = "<b>Claim Label</b>";
cell4.innerHTML = "<b>Owner</b>";
cell5.innerHTML = "<b>Status</b>";
cell6.innerHTML = "<b>First Nation Traditional Territory</b>";

// "response.results" is the data object containing fields and geometry from the found feature
// empty the array from previous results
resultsarray = [];
alert(resultsarray);
alert(results.features.length);
//var results = response.results;
// If no results are returned from the FindTask "find", notify the user
if (results.features.length === 0) {
resultsTable.innerHTML = "<i>No results found</i>";
return;
}

// array.forEach(results, function(feature) {
// Performs Spatial Query and writes results for each result returned 
arrayUtils.forEach(featureSet.features, function(feature, i) {
// Get each value of the desired attributes from the FindTask "find"
var geo = feature.geometry;
var grNum = feature.attributes.GRANT_NUMBER;
var clNam = feature.attributes.CLAIM_NAME;
var clLab = feature.attributes.CLAIM_LABEL;
var owner = feature.attributes.OWNER_NAME;
var tnStat = feature.attributes.TENURE_STATUS;

//Setup Spatial Query
var spatialquery = new Query();
spatialquery.outFields = ["FNTT"];
spatialquery.geometry = geo;
spatialquery.spatialRelationship = "intersects";

// execute QueryTask for Spatial Query
qt.execute(spatialquery).then(function(spatialresults) {
// grab attribute from spatial query
var FNTT = spatialresults.features[0].attributes.FNTT;
//push results to array
resultsarray.push(grNum);
resultsarray.push(clNam);
resultsarray.push(clLab);
resultsarray.push(owner);
resultsarray.push(tnStat);
resultsarray.push(FNTT);

// If no results are returned from the task, notify the user
if (resultsarray.length === 0) {
resultsTable.innerHTML = "<i>No results found</i>";
return;
}

// Write html table underneath header row
var i = 1;
// Add each resulting value to the table as a new row, and create cells
var row = resultsTable.insertRow(i);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);

// Assign values to cells
cell1.innerHTML = grNum;
cell2.innerHTML = clNam;
cell3.innerHTML = clLab;
cell4.innerHTML = owner;
cell5.innerHTML = tnStat;
cell6.innerHTML = FNTT;

//Increase array counter
i++;
})
});
}

// Assign results table to the html table by ID
var resultsTable = dom.byId("tblResults");

// Executes each time the promise is rejected.
function rejectedPromise(err) {
console.error("Promise didn't resolve: ", err.message);
}

// methods to execute the function
// Run doFind() when button is clicked
on(dom.byId("btnFind"), "click", doFind);

// "Clicks" btnFind when "Enter" is pressed on keyboard, thereby running doFind()
document.getElementById('grantNumber').addEventListener('keypress', function(event) {
if (event.keyCode == 13) {
document.getElementById('btnFind').click();
}
});
});

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

AJ,

   The FindTask returns a object names "results" the QueryTask Return a object named "featureSet" which has a property called features that is an array of graphics.

View solution in original post

0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus

AJ,

   The FindTask returns a object names "results" the QueryTask Return a object named "featureSet" which has a property called features that is an array of graphics.

0 Kudos
AJRS
by
New Contributor

Thanks for your response.  I am assuming that means I'll have arrays of arrays which is definitely out of my area of expertise.  I definitely need to take a JavaScript course!

Thanks again!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

AJ,

   No the name of the returned object is just different based on the task that you use (that was my main point) a FeatureSet is just an array of Graphics.

0 Kudos