Select to view content in your preferred language

How to use Query() to pull intersecting features through a widget

2995
10
Jump to solution
02-17-2017 11:57 AM
StahlyEngineering
New Contributor III

How does one correctly use a query and QueryTask to retrieve features in a predefined layer that intersect a selected feature?

var queryTask = new QueryTask("http://services6.arcgis.com/3aTanasiPG0rbYg4/arcgis/rest/services/PlatBook_Test/FeatureServer/1");
var query = new Query();
query.returnGeometry = true;
query.outFields = ["SECDIVID"];
query.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
// execute query
queryTask.execute(query);

current piece of code spits out an error of faulty query arguments.

Once the query works the next task will be to output a specific field from the output into an array.

Any help is appreciated, I'm very new to writing scripts and have no idea what I'm doing.

Cheers!

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Stahly,

   Well I see a major issue. You are trying to set the queries geometry to a string (line 34)...

define([
    'dojo/_base/declare',
    'jimu/BaseFeatureAction',
    'jimu/WidgetManager',
    'esri/tasks/QueryTask',
    'esri/tasks/query',
    "esri/SpatialReference"
], function(declare, BaseFeatureAction, WidgetManager, QueryTask, Query, SpatialReference) {
    var clazz = declare(BaseFeatureAction, {
        //Icon format:50x50px main icon. _default/_hover 16x16px popup icons
        iconFormat: 'png',
        isFeatureSupported: function(featureSet) {
            //test feature compatibility for the action
            return featureSet.displayFieldName == 'OwnerName';
        },
        onExecute: function(featureSet) {
            WidgetManager.getInstance().triggerWidgetOpen(this.widgetId)
                .then(function(myWidget) {
                    //pointless text output here
                    var fName = [];
                    fName[0] = featureSet.spatialReference.wkt;
                    fName[1] = featureSet.displayFieldName;
                    console.log('log: step 0 ' + fName[0]);

                    // spatial query: select all PLSS_2ndDivision INTERSECTING the input feature
                    console.log('log: step 0.1 ');
                    var queryTask = new QueryTask("http://services6.arcgis.com/3aTanasiPG0rbYg4/arcgis/rest/services/PlatBook_Test/FeatureServer/1");
                    var Q = new Query();
                    Q.returnGeometry = true;
                    Q.outSpatialReference = new SpatialReference({
                        wkid: 102100
                    });
                    Q.outFields = ["SECDIVID"];
                    Q.geometry = 'polygon';
                    Q.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
                    console.log('log: step 0.2 ' + Q.geometry);
                    console.log('log: step 0.2 ' + Q.outSpatialReference.isWebMercator());
                    console.log('log: step 0.2 ' + Q.spatialRelationship);
                    console.log('log: step 0.2 ' + queryTask.url);
                    queryTask.execute(Q);

                    myWidget.SelectParcels(fName);
                });

        }
    });
    return clazz;
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I think you need to be setting it to a selected feature geometry right? Something like:

Q.geometry = featureSet.features[0].geometry;

Beyond that I have no idea what this line is doing:

myWidget.SelectParcels(fName);

Or Where is your QueryTask result handler function?

View solution in original post

10 Replies
RobertScheitlin__GISP
MVP Emeritus

Stahly,

  You need to set the 

query.geometry = x // where x is the geometry of the selected feature.

0 Kudos
StahlyEngineering
New Contributor III

Did that, now I get the following: TypeError: Cannot read property 'wkid' of undefined

console.log('log: step 0.1 ');
var queryTask = new QueryTask("http://services6.arcgis.com/3aTanasiPG0rbYg4/arcgis/rest/services/PlatBook_Test/FeatureServer/1");
var Q = new Query();
Q.returnGeometry = true;
//Q.outSpatialReference = (102100);
//Q.outFields = ["SECDIVID"];
Q.geometry = 'polygon';
Q.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
console.log('log: step 0.2 '+typeof(queryTask));
queryTask.execute(Q);

Thanks a bunch!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Stahly, 


  The querys outspatialreference is expecting a SpatialReference object not just a number in parentheses. So make sure you have the SpatialReference require in your require list and use code like this

Q.outSpatialReference = new SpatialReference(wkid:102100);


Don't forget to mark this question as answered.

0 Kudos
StahlyEngineering
New Contributor III

Still no go. console logs return return the correct parameters for the query object (geometry, spatial reference wkid number, spatialrelationship). I seem to be getting this type of error when an object isn't actually created, but I'm not sure which object that is.   

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Stahly,


   I can't say I understand what your saying. Did you add the require for SpatialReference to your require list? Maybe you should post more of your code.

0 Kudos
StahlyEngineering
New Contributor III

console.log('log: step 0.1 ');
var queryTask = new QueryTask("http://services6.arcgis.com/3aTanasiPG0rbYg4/arcgis/rest/services/PlatBook_Test/FeatureServer/1");
var Q = new Query();
Q.returnGeometry = true;
Q.outSpatialReference = new spatialReference(102100);
Q.outFields = ["SECDIVID"];
Q.geometry = 'polygon';
Q.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
console.log('log: step 0.2 '+Q.geometry);
console.log('log: step 0.2 '+Q.outSpatialReference.isWebMercator());
console.log('log: step 0.2 '+Q.spatialRelationship);
console.log('log: step 0.2 '+queryTask.url);
queryTask.execute(Q);

log: step 0.2 polygon
ShowVertexFeatureAction.js?wab_dv=2.3:37 log: step 0.2 true
ShowVertexFeatureAction.js?wab_dv=2.3:38 log: step 0.2 esriSpatialRelIntersects
ShowVertexFeatureAction.js?wab_dv=2.3:39 log: step 0.2 http://services6.arcgis.com/3aTanasiPG0rbYg4/arcgis/rest/services/PlatBook_Test/FeatureServer/1
init.js:113 TypeError: Cannot read property 'wkid' of undefined

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

SpatialReference object need to have a capital S in your code. JS is a case sensitive language.

0 Kudos
StahlyEngineering
New Contributor III

Did that, still getting the same error. Here's my entire code. Maybe I'm missing references? 

define([
'dojo/_base/declare',
'jimu/BaseFeatureAction',
'jimu/WidgetManager',
'esri/tasks/QueryTask',
'esri/tasks/query',
"esri/SpatialReference"
], function(declare, BaseFeatureAction, WidgetManager, QueryTask, Query, SpatialReference){
var clazz = declare(BaseFeatureAction, {
//Icon format:50x50px main icon. _default/_hover 16x16px popup icons
iconFormat: 'png',

isFeatureSupported: function(featureSet){
//test feature compatibility for the action
return featureSet.displayFieldName == 'OwnerName';
},

onExecute: function(featureSet){
WidgetManager.getInstance().triggerWidgetOpen(this.widgetId)
.then(function(myWidget) {
//pointless text output here
var fName = [];
fName[0] = featureSet.spatialReference.wkt;
fName[1] = featureSet.displayFieldName;
console.log('log: step 0 '+fName[0]);

// spatial query: select all PLSS_2ndDivision INTERSECTING the input feature
console.log('log: step 0.1 ');
var queryTask = new QueryTask("http://services6.arcgis.com/3aTanasiPG0rbYg4/arcgis/rest/services/PlatBook_Test/FeatureServer/1");
var Q = new Query();
Q.returnGeometry = true;
Q.outSpatialReference = new SpatialReference({wkid:102100});
Q.outFields = ["SECDIVID"];
Q.geometry = 'polygon';
Q.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
console.log('log: step 0.2 '+Q.geometry);
console.log('log: step 0.2 '+Q.outSpatialReference.isWebMercator());
console.log('log: step 0.2 '+Q.spatialRelationship);
console.log('log: step 0.2 '+queryTask.url);
queryTask.execute(Q);



myWidget.SelectParcels(fName);
});

}
});
return clazz;
});

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Stahly,

   Well I see a major issue. You are trying to set the queries geometry to a string (line 34)...

define([
    'dojo/_base/declare',
    'jimu/BaseFeatureAction',
    'jimu/WidgetManager',
    'esri/tasks/QueryTask',
    'esri/tasks/query',
    "esri/SpatialReference"
], function(declare, BaseFeatureAction, WidgetManager, QueryTask, Query, SpatialReference) {
    var clazz = declare(BaseFeatureAction, {
        //Icon format:50x50px main icon. _default/_hover 16x16px popup icons
        iconFormat: 'png',
        isFeatureSupported: function(featureSet) {
            //test feature compatibility for the action
            return featureSet.displayFieldName == 'OwnerName';
        },
        onExecute: function(featureSet) {
            WidgetManager.getInstance().triggerWidgetOpen(this.widgetId)
                .then(function(myWidget) {
                    //pointless text output here
                    var fName = [];
                    fName[0] = featureSet.spatialReference.wkt;
                    fName[1] = featureSet.displayFieldName;
                    console.log('log: step 0 ' + fName[0]);

                    // spatial query: select all PLSS_2ndDivision INTERSECTING the input feature
                    console.log('log: step 0.1 ');
                    var queryTask = new QueryTask("http://services6.arcgis.com/3aTanasiPG0rbYg4/arcgis/rest/services/PlatBook_Test/FeatureServer/1");
                    var Q = new Query();
                    Q.returnGeometry = true;
                    Q.outSpatialReference = new SpatialReference({
                        wkid: 102100
                    });
                    Q.outFields = ["SECDIVID"];
                    Q.geometry = 'polygon';
                    Q.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
                    console.log('log: step 0.2 ' + Q.geometry);
                    console.log('log: step 0.2 ' + Q.outSpatialReference.isWebMercator());
                    console.log('log: step 0.2 ' + Q.spatialRelationship);
                    console.log('log: step 0.2 ' + queryTask.url);
                    queryTask.execute(Q);

                    myWidget.SelectParcels(fName);
                });

        }
    });
    return clazz;
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I think you need to be setting it to a selected feature geometry right? Something like:

Q.geometry = featureSet.features[0].geometry;

Beyond that I have no idea what this line is doing:

myWidget.SelectParcels(fName);

Or Where is your QueryTask result handler function?