Last active
April 26, 2019 20:56
-
-
Save nommuna2/9b4b2751351df0422b4b24e46dc7d1ff to your computer and use it in GitHub Desktop.
(ArcGIS API for JavaScript) Two samples of how to get the points within a certain polygon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using geometry engine, you can use the contains method to loop through all the points and see if that point is within a polygon. | |
// This requires using a nested for loop, so the more points you have then the slower the program may be. | |
require(["esri/map", | |
"esri/layers/FeatureLayer", | |
"esri/tasks/query", | |
"esri/tasks/QueryTask", | |
"esri/geometry/geometryEngine", | |
"esri/graphic", | |
"esri/symbols/SimpleMarkerSymbol", | |
"esri/Color", | |
"esri/symbols/SimpleLineSymbol", | |
"esri/layers/GraphicsLayer", | |
"dojo/domReady!" | |
], function (Map, FeatureLayer, Query, QueryTask, geometryEngine, Graphic, SimpleMarkerSymbol, Color, SimpleLineSymbol, GraphicsLayer) { | |
//Initializing the map constructor | |
var map = new Map("map", { | |
center: [-118, 34.5], | |
zoom: 8, | |
basemap: "topo" | |
}); | |
var query = new Query(); | |
query.where = "1=1" | |
query.returnGeometry = true; | |
var arr = []; | |
// The query task is used to get the geometry in JSON form. | |
var queryPoints = new QueryTask(myURL); | |
var queryPolygons = new QueryTask(myURL); | |
queryPoints.execute(query, (points) => { | |
//Get the points in JSON form do another query on the second layer. | |
queryPolygons.execute(query, (polygons) => { | |
//Loop through each point and see if that point is inside a polygon | |
points.features.forEach((point) => { | |
//console.log(point) | |
polygons.features.forEach((polygon) => { | |
if(geometryEngine.contains(polygon.geometry, point.geometry)){ | |
arr.push({'point': point.attributes.CITY_FIPS,'polygon': polygon.geometry}); | |
} | |
}); | |
}); | |
}); | |
}); | |
console.log(arr); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This sample uses the server to find if any points are within a certain polygon. | |
// This option only uses one for loop to go through each polygon and then uses a spatial filter as input to the query for the points. | |
// For this option your points would need to be on the server either through a map service or a feature layer. | |
require(["esri/map", | |
"esri/layers/FeatureLayer", | |
"esri/tasks/query", | |
"esri/tasks/QueryTask", | |
"esri/geometry/geometryEngine", | |
"esri/graphic", | |
"esri/symbols/SimpleMarkerSymbol", | |
"esri/Color", | |
"esri/symbols/SimpleLineSymbol", | |
"esri/layers/GraphicsLayer", | |
"dojo/domReady!" | |
], function (Map, FeatureLayer, Query, QueryTask, geometryEngine, Graphic, SimpleMarkerSymbol, Color, SimpleLineSymbol, GraphicsLayer) { | |
//Initializing the map constructor | |
var map = new Map("map", { | |
center: [-118, 34.5], | |
zoom: 8, | |
basemap: "topo" | |
}); | |
var fl1 = new FeatureLayer("myURL"); | |
var fl2 = new FeatureLayer("myURL"); | |
var arr = []; | |
var query = new Query(); | |
query.where = "1=1" | |
query.returnGeometry = true; | |
var queryPoints = new QueryTask(fl1.url); | |
var queryPolygons = new QueryTask(fl2.url); | |
queryPolygons.execute(query, (polygons) => { | |
polygons.features.forEach((polygon) => { | |
query.geometry = polygon.geometry; | |
queryPoints.execute(query, (points) => { | |
arr.push({'polygon': polygon, 'points': points}); | |
}); | |
}); | |
}); | |
console.log(arr); | |
//map.addLayers([fl2,fl1]) | |
//map.addLayer(fl2) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment