I have a base map on top of which I have added a feature layer . I pull thousands of json data from my db which has lat/long information , then I create points using lat/long and I plot those points on map. Many points may be drawn on the feature layer(polygon).
I have to find common points which all fall on the feature layer, for this I used below approach similar to this: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Intersect/00080000000p000000/
function plotandfind() {
var count=0;
require(["dojo/parser", "dojo/_base/connect", "dojo/on",
"esri/layers/ArcGISDynamicMapServiceLayer", "esri/layers/FeatureLayer",
"esri/geometry/Extent", "esri/symbols/SimpleFillSymbol",
"esri/symbols/SimpleLineSymbol", "dojo/_base/Color", "esri/graphic",
"dojox/collections/Dictionary", "esri/geometry/Point",
"esri/symbols/SimpleMarkerSymbol", "esri/SpatialReference",
"esri/geometry/webMercatorUtils", "dojo/dom", "esri/tasks/query",
"dojo/domReady!"
], function(
parser, connect, on, ArcGISDynamicMapServiceLayer, FeatureLayer, Extent,
SimpleFillSymbol, SimpleLineSymbol, Color, Graphic, Dictionary, Point,
SimpleMarkerSymbol, SpatialReference, webMercatorUtils, Query
) {
try{
dojo.xhrGet({
url:"q.json",
handleAs:"json",
load: function(data){
console.log("data "+data[0].Admin1);
dojo.forEach(data,function(locations){
var wgs = new SpatialReference({ "wkid": 4326});
var latlng = new Point(parseFloat(locations.Longitude), parseFloat(locations.Latitude),wgs);
var webMercator = webMercatorUtils.geographicToWebMercator(latlng);
var geometry = new esri.geometry.Point(webMercator.x,webMercator.y,map.spatialReference);
var symbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CROSS, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0]), 1), new dojo.Color([0,255,0,0.25]));
var attr = {"LOCATION_ID":data.Admin1};
var graphic = new esri.Graphic(geometry, symbol,attr);
map.graphics.add(graphic);
var queryTask = new esri.tasks.QueryTask("http://10.97.43.4:6080/arcgis/rest/services/POC/Sandy_Hurricane_Forecast/MapServer/0");
var query = new esri.tasks.Query();
query.geometry=geometry;
query.returnGeometry = true;
query.spatialRelationship = Query.SPATIAL_REL_CONTAINS;
queryTask.execute(query,function(featureSet){
var resultFeatures = featureSet.features;
//Loop through each feature returned
for (var i=0, il=resultFeatures.length; i<il; i++) {
count++;
}
});
});
},
error:console.log("error")
});
console.log(" added point");
}catch(e){
console.log(" error adding point"+e);
}
});
}
I will pass the point that I created as the query input to the feature layer and I will get to know whether it is resides in the polygon. Is this right way ?
The problem is what if I have millions of records, is this the right approach or is there a better way?
I tried your link, but I am not sure if I am doing the right thing. Just to reiterate, I have a polygon which I have to plot some points (only those points which fall in that polygon but I have millions of polygons). I have loaded all points as a feature layer now and did code as below. But I am facing an issue where I have loaded main feature to load points from server. I have loaded it as feature layer, but do I have to add feature layer? It will plot all points but I first need to get common points then add those points in the polygon.
// Layer url is the feature layer where points are loaded as polygon.
function plotPoints(layerUrl) {
// this layer contains points
var count=0;
require(["dojo/parser", "dojo/_base/connect", "dojo/on",
"esri/layers/ArcGISDynamicMapServiceLayer", "esri/layers/FeatureLayer",
"esri/geometry/Extent", "esri/symbols/SimpleFillSymbol",
"esri/symbols/SimpleLineSymbol", "dojo/_base/Color", "esri/graphic",
"dojox/collections/Dictionary", "esri/geometry/Point",
"esri/symbols/SimpleMarkerSymbol", "esri/SpatialReference",
"esri/geometry/webMercatorUtils", "dojo/dom", "esri/tasks/query", "dojo/domReady!"
], function(
parser, connect, on, ArcGISDynamicMapServiceLayer, FeatureLayer, Extent,
SimpleFillSymbol, SimpleLineSymbol, Color, Graphic, Dictionary, Point,
SimpleMarkerSymbol, SpatialReference, webMercatorUtils, Query
) {
try{
featureLayer = new esri.layers.FeatureLayer(layerUrl,{
mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,
outFields: ["*"],
opacity:.75
});
map.addLayer(featureLayer);
// once the featrue layer containing points i loaded i will find common points
featureLayer.on("update-end",function(){
loadPoints(featureLayer);
});
console.log(" added point");
}catch(e){
console.log(" error adding point"+e);
}
});
}
function getCommonPoints(featureLayer) {
try{
console.log(" feature layer");
// query task is the feature layer which has main polygon on top of which I have to plot some commonpoints
queryTask = new esri.tasks.QueryTask("http://10.97.43.4:6080/arcgis/rest/services/POC/CAT_Events/MapServer/0");
query = new esri.tasks.Query();
var feature;
var features = featureLayer.graphics;
console.log(" features "+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];
inBuffer.push(feature.attributes[featureLayer.objectIdField]);
}
query.objectIds = inBuffer;
query.returnGeometry = true;
query.outSpatialReference = {wkid:4326};
query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_CONTAINS;
queryTask.execute(query,function(featureSet){
var features = featureSet.features;
for (var i = 0; i < features.length; i++) {
feature = features[i];
attributes = feature.attributes;
console.log(" SOME FEATURES SELECTED ");
}
});
}catch(e){
console.log(" error occured "+e);
}
}