david8515

Query Relationships Problem

Discussion created by david8515 on Aug 4, 2012
Latest reply on Aug 6, 2012 by JGravois-esristaff
Howdy folks,

Having trouble conducting a spatial query where a county is clicked and adjacent counties are also selected through the SPATIAL_REL_TOUCHES query. I basically copied the code found in the esri query adjacent polygon sample, but used different layers to query. I'm thinking the problem has to do with either the proxy page reference (line 62-63) or the feature geometry reference (line 90). Any ideas? Thanks in advance!

-David
Code below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--The viewport meta tag is used to improve the presentation and behavior of the samples
on iOS devices-->
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<title>Full Map Layout</title>
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.1/js/dojo/dijit/themes/claro/claro.css">
<style type="text/css">
html, body {
height: 100%; width: 100%; margin: 0; padding: 0;
}
body{
background-color:#777; overflow:hidden; font-family: "Trebuchet MS";
}
#map{
overflow:hidden;
padding:0;
}
</style>
<script type="text/javascript">
var djConfig = {
parseOnLoad: true
};
</script>
<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.1">
</script>
<script type="text/javascript">
dojo.require("dijit.dijit"); // optimize: load dijit layer
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("esri.map");
dojo.require("esri.tasks.query");
dojo.require("esri.tasks.geometry");

var map, queryMedIncmTract, queryTask3, tractIncome, symbol, currentPoly;


function init() {
map = new esri.Map("map", {
extent: new esri.geometry.Extent(-140, 20, -55, 55, new esri.SpatialReference({wkid:4326}))});
var streetMap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer");
dojo.connect(map, 'onLoad', function(map) {
dojo.connect(dijit.byId('map'), 'resize', resizeMap);
});
var referenceLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Median_Household_Income/MapServer");
map.addLayer(streetMap);
map.addLayer(referenceLayer);
//listen for when map is loaded and then add query functionality
dojo.connect(map, "onLoad", initFunctionality);
}

function initFunctionality(map) {
var queryTask = new esri.tasks.QueryTask("http://server.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Median_Household_Income/MapServer/3");
var queryTaskTouches = new esri.tasks.QueryTask("http://server.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Median_Household_Income/MapServer/3");

//identify proxy page to use if the toJson payload to the geometry service is greater than 2000 characters.
//If this null or not available the buffer operation will not work. Otherwise it will do a http post to the proxy.
esriConfig.defaults.io.proxyUrl = "/arcgisserver/apis/javascript/proxy/proxy.ashx";
esriConfig.defaults.io.alwaysUseProxy = false;

// Query
var query = new esri.tasks.Query();
query.returnGeometry = true;
query.outFields = ["MEDHINC_CY"];


var currentClick = null;
// +++++Listen for map onClick event+++++
dojo.connect(map, "onClick", function(evt) {
map.graphics.clear();
currentClick = query.geometry = evt.mapPoint;

query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_INTERSECTS;
queryTask.execute(query);
dojo.byId('messages').innerHTML = "<b>Executing Point Intersection Query...</b>";

});

var firstGraphic = null;
// +++++Listen for QueryTask onComplete event+++++
dojo.connect(queryTask, "onComplete", function(graphics) {
firstGraphic = graphics.features[0];
var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new dojo.Color([100,100,100]), 3), new dojo.Color([255,0,0,0.20]));
firstGraphic.setSymbol(symbol);
map.graphics.add(firstGraphic);
query.geometry = firstGraphic.geometry;
query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_TOUCHES;
queryTaskTouches.execute(query);
dojo.byId('messages').innerHTML = "<b>Executing Polygon Touches Query...</b>";

});



// +++++Listen for QueryTask executecomplete event+++++
dojo.connect(queryTaskTouches, "onComplete", function(fset) {
var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new dojo.Color([100,100,100]), 2), new dojo.Color([0,0,255,0.20]));

var resultFeatures = fset.features;
for (var i=0, il=resultFeatures.length; i<il; i++) {
var graphic = resultFeatures[i];
graphic.setSymbol(symbol);
map.graphics.add(graphic);
}
dojo.byId('messages').innerHTML = "";

});
}
function resizeMap() {
//resize the map when the browser resizes - view the 'Resizing and repositioning the map' section in
//the following help
var resizeTimer;
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
map.resize();
map.reposition();
}, 500);
}
//show map on load
dojo.addOnLoad(init);
</script>
</head>
<body class="claro">
<div dojotype="dijit.layout.BorderContainer" design="headline" gutters="false"
style="width: 100%; height: 100%; margin: 0;">
<div id="map" dojotype="dijit.layout.ContentPane" region="center">
<span id="messages"></span>

</div>
</div>
</body>

</html>

Outcomes