I used ArcGIS JavaScript API 3.25 for developing. I want to select polygons within 1km radius. so I used a sample code that esri provides. But it works only for point features. I want to use that code for select polygons.
Can you help me to correct my coding?
var featureLayer = new FeatureLayer("http://203.189.68.219:6080/arcgis/rest/services/Sites/inginimitiya_Tank/MapServer/11"
/*
,{
mode: FeatureLayer.MODE_ONDEMAND,
infoTemplate: template,
outFields: ["Name", "OBJECTID"]
}
*/
);
var symbol = new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SOLID,
12,
new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SOLID,
new Color([247, 34, 101, 0.9]),
1
),
new Color([207, 34, 171, 0.5])
);
featureLayer.setSelectionSymbol(symbol);
// Make unselected features invisible
//var nullSymbol = new SimpleMarkerSymbol().setSize(0);
//featureLayer.setRenderer(new SimpleRenderer(nullSymbol));
map.addLayer(featureLayer);
var circleSymb = new SimpleFillSymbol(
SimpleFillSymbol.STYLE_NULL,
new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SHORTDASHDOTDOT,
new Color([105, 105, 105]),
2
), new Color([255, 255, 0, 0.25])
);
var circle;
// When the map is clicked create a buffer around the click point of the specified distance
map.on("click", function (evt) {
circle = new Circle({
center: evt.mapPoint,
geodesic: true,
radius: 1,
radiusUnit: "esriMiles"
});
map.graphics.clear();
var graphic = new Graphic(circle, circleSymb);
map.graphics.add(graphic);
var query = new Query();
query.geometry = circle.getExtent();
// Use a fast bounding box query. It will only go to the server if bounding box is outside of the visible map.
featureLayer.queryFeatures(query, selectInBuffer);
});
function selectInBuffer(response) {
var feature;
var features = response.features;
var inBuffer = [];
// Filter out features that are not actually in buffer, since we got all points in the buffer's bounding box
for (var i = 0; i < features.length; i++) {
feature = features[i];
if (circle.contains(feature.geometry)) {
inBuffer.push(feature.attributes[featureLayer.objectIdField]);
}
}
var query = new Query();
query.objectIds = inBuffer;
// Use an objectIds selection query (should not need to go to the server)
featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function (results) {
var totalPopulation = sumPopulation(results);
var r = "";
r = "<b>The total Census Block population within the buffer is <i>" + totalPopulation + "</i>.</b>";
dom.byId("messages").innerHTML = r;
});
}
1 Answer 1
You could replace most of the selectInBuffer
function, by performing a spatial operation on the query
. Notice, i also perform a featureLayer.queryIds
map.on("click", function(evt){
circle = new Circle({
center: evt.mapPoint,
...
var query = new Query();
query.geometry = circle;
query.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
featureLayer.queryIds(query, selectInBuffer);
});
Now you have the objectids of the geometries that intersect the 1 km circle :
function selectInBuffer(inBuffer) {
/*
var features = response.features;
var inBuffer = [];
// Filter out features that are not actually in buffer, since we got all points in the buffer's bounding box
for (var i = 0; i < features.length; i++) {
var feature = features[i];
if(circle.contains(feature.geometry)){
inBuffer.push(feature.attributes[featureLayer.objectIdField]);
}
}
*/
var query = new Query();
query.objectIds = inBuffer;
...
Oh, by the way, you specified 1 esriMiles, not kilometer. You can fix this, using the module "esri/units"
and add this line in the creation of the circle radiusUnit: units.KILOMETERS,