I am trying to query and return data from a spatial relationship.
I have a county layer with many counties
I have another Layer that has larger polygons that are made up of multiple counties.
I want to use the larger polygon to select the smaller polygons that would be inside the geometry of the larger polygon.
I have tried all the possible spatial relationships
Possible Values: "intersects" | "contains" | "crosses" | "disjoint" | "envelope-intersects" | "index-intersects" | "overlaps" | "touches" | "within" | "relation"
I think my issue is that the boundaries of the two Feature Layers coincide. Is there any other way to query these? Can I search by centroid of the polygon layer (create a centroid on the fly?)
function queryCountiesthatIntersect() {
var query = countyLayer.createQuery();
query.geometry = resultsLayer.graphics.items[0].geometry;
query.spatialRelationship = "index-intersects";
query.outFields = "*";
query.returnGeometry = true;
return countyLayer.queryFeatures(query);
}
Solved! Go to Solution.
Jay,
There is redundancy in your code.
function displayResults2(results) {
var testPoly = resultsLayer2.graphics.items[0].geometry;
var finalFeats = [];
results.features.map(function (graphic) {
if (testPoly.contains(graphic.geometry.centroid)) {
var cGra = graphic.clone();
cGra.symbol = {
type: "simple-fill",
style: "solid",
color: [240, 248, 255, 0.9],
size: "10px", // pixels
outline: {
// autocasts as new SimpleLineSymbol()
color: [191, 42, 18, 0.9],
width: 1 // points
}
};
finalFeats.push(cGra);
resultsLayer3.addMany(finalFeats);
}
});
...
Jay,
I would try overlaps or contains.
Should Return 11
Returns 3: Contains only gives me the ones that are completely contained within the Large Polygon....it does not give me the ones that are touching the boundary of the Large Polygon....those lines coincide and are not selected with Contains.
Returns 19: If I use Overlaps I get this....I get the ones that lie outside selected because the lines coincide.
Can I grab the centroid for Query?
Can I somehow tell the query to look at the centroids of each little county and use those centroids to find the ones that are "contained"????
var parcelCentroid = feature.geometry.getExtent().getCenter();
Describes query operations that can be performed on features in the layer.
The maximum number of records that will be returned for a given query. |
Indicates if the geometry centroid associated with each polygon feature can be returned. This operation is only supported in ArcGIS Online hosted feature services. |
Jay,
the best I know of is that you can get the polygon centroid like this
var countyCentroid = feature.geometry.cetroid;
So you would do your query like this:
function queryCountiesthatIntersect_step1() {
var query = countyLayer.createQuery();
query.geometry = resultsLayer.graphics.items[0].geometry;
query.spatialRelationship = "intersects";
query.outFields = "*";
query.returnGeometry = true;
return countyLayer.queryFeatures(query);
}
then find if the resulting counties centroid is inside the polygon.
queryCountiesthatIntersect_step1.then(function(results){
var testPoly = resultsLayer.graphics.items[0].geometry;
results.features.map(function (graphic) {
if(testPoly.contains(graphic.geometry.centroid)){
//Now you know if the centroid of the county is in your polygon
// and you can add that graphic to the map or whatever...
}
});
});
Having a hard time following this..
I have:
CountyLayer var countyLayerWebMercator = new FeatureLayer({
this is the full county layer with hundreds of polygons
resultsLayer2 this is a graphic layer that houses the large polygon for the query.
this is the poly that I want to identify all the counties from the County Layer above
resultsLayer3 this is the graphics layer where I want to write all the counties that are within the resultsLayer2
all the polygons that have their centroid in resultsLayer2
Am I missing something?
// SNIP CODE ABOVE BUT HERE IS TEH CALL
queryCountiesthatIntersect().then(displayResults2).else(function (err) {
console.log(err);
});
}
function queryCountiesthatIntersect() {
var query = countyLayerWebMercator.createQuery();
query.geometry = resultsLayer2.graphics.items[0].geometry;
query.spatialRelationship = "intersects";
query.outFields = "*";
query.returnGeometry = true;
return countyLayerWebMercator.queryFeatures(query);
}
function displayResults2(results) {
queryCountiesthatIntersect.then(function (results) {
var testPoly = resultsLayer2.graphics.items[0].geometry;
results.features.map(function (graphic) {
if (testPoly.contains(graphic.geometry.centroid)) {
//Now you know if the centroid of the county is in your polygon
// and you can add that graphic to the map or whatever...
// alert(testPoly.length);
var features3 = graphic.features.map(function (graphic) {
graphic.symbol = {
type: "simple-fill",
style: "solid",
color: [240, 248, 255, 0.9],
size: "10px", // pixels
outline: {
// autocasts as new SimpleLineSymbol()
color: [191, 42, 18, 0.9],
width: 1 // points
}
};
return graphic;
});
var numCounties = features3.length;
alert(numCounties);
resultsLayer3.addMany(features3);
}
});
});
}
Jay,
Unless I am misreading the code you ported you have an infinite loop going on.
queryCountiesthatIntersect().then(displayResults2)....
then in displayResults2 you have
function displayResults2(results) {
queryCountiesthatIntersect.then(function (results) {
Which is a function calling itself each time it finishes...
this is what I am trying to do now....just saw that loop
Although this still returns the features that are outside the area of interest....does not seem to be using this
if (testPoly.contains(graphic.geometry.centroid)) {
Think it might be this. Where I am calling the "results" again
var features3 = results.features.map(function (graphic) {
function displayResults2(results) {
var testPoly = resultsLayer2.graphics.items[0].geometry;
results.features.map(function (graphic) {
//queryCountiesthatIntersect.then(function (results) {
if (testPoly.contains(graphic.geometry.centroid)) {
var features3 = results.features.map(function (graphic) {
graphic.symbol = {
type: "simple-fill",
style: "solid",
color: [240, 248, 255, 0.9],
size: "10px", // pixels
outline: {
// autocasts as new SimpleLineSymbol()
color: [191, 42, 18, 0.9],
width: 1 // points
}
};
return graphic;
});
var numCounties = features3.length;
alert(numCounties);
resultsLayer3.addMany(features3);
}
});
Jay,
There is redundancy in your code.
function displayResults2(results) {
var testPoly = resultsLayer2.graphics.items[0].geometry;
var finalFeats = [];
results.features.map(function (graphic) {
if (testPoly.contains(graphic.geometry.centroid)) {
var cGra = graphic.clone();
cGra.symbol = {
type: "simple-fill",
style: "solid",
color: [240, 248, 255, 0.9],
size: "10px", // pixels
outline: {
// autocasts as new SimpleLineSymbol()
color: [191, 42, 18, 0.9],
width: 1 // points
}
};
finalFeats.push(cGra);
resultsLayer3.addMany(finalFeats);
}
});
...