Query by distance

4403
10
Jump to solution
09-30-2016 04:48 AM
MargaRaiz
New Contributor III

Hi!

I am trying to query by distance 2 layer (points and lines), I mean I have an initial point and I want to find the closest lines that are within 10 meters.

Afther doing some research in this forum this is my try:

First I query the point layer looking for the source point and I create a graphic symbol:

      var resultFeatures = results.features;
      symbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 3);
      var point= new esri.Graphic(resultFeatures[0].geometry, symbol);

I create a new query with this parameters:

 var query = new Query();
               
       queryTask = new QueryTask(lines_layer.url);               
       query.geometry = [point.geometry];
       query.distance = [10];
       query.units = esri.tasks.GeometryService.UNIT_METER;
       query.returnGeometry = true;
       query.outSpatialReference = new SpatialReference({ wkid: 102100 }); //ETRS89
       query.spatialRelationship = Query.SPATIAL_REL_CONTAINS;

I launch the query:

queryTask.execute(query).then(function (qResults) {
         //  Here goes the code for looping qResults of the lines elements

         //  that are close than 10 meters from the initial point.
});


Is this the correct approach to launch a query by distance (similar to the one that arcmap haves)?           

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Marga,

   Use this instead:

bufferParameters.geometries = [new Point(-200043.56228524158, 363980.960991833, new SpatialReference({ wkid: 102100 }))];

View solution in original post

10 Replies
RobertScheitlin__GISP
MVP Emeritus

Marga,


   The issue with your attempt is that the polyline would have to be fully within the 10m not just touching. You should use the default spatial relation of intersects instead.

MargaRaiz
New Contributor III

Hi Robert!

You min this right?

query.spatialRelationship = SPATIAL_REL_INTERSECTS

I have realiced that my first is wrong (the part to create the point), should be this:

symbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 3);

symbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, new Color([255, 0, 0]), 3);

So.. this is the way to find the lines that are 10 meters within the initial symbol-point?? Because I have been thinking about using the geometryService with bufferparams but i wasn't sure which was the correct methoth..

Thanks!

Marga

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Marga,

   Using distance property on the Query Class eliminates the need for using a geometry service buffer as long as you are using ArcGIS for Server 10.31 onward on the map service you are querying.

MargaRaiz
New Contributor III

Well thats the problem, I am using 10.2.1, so.. I will need to use the theed of using the geometry service buffer

0 Kudos
MargaRaiz
New Contributor III

Hi Robert,

I have been trying to query by distance using the geometry service with no succeed. This is my try:

I create the BufferParameters variable with this values:

// set the geometry service

 var gsvc = null;
 gsvc = new esri.tasks.GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

//create the point

                var geom = {"geometryType": "esriGeometryPoint",
                            "geometries": [{ "x": -200043.56228524158, "y": 363980.960991833 }]};

var bufferParameters = new BufferParameters();       

bufferParameters.bufferSpatialReference = new SpatialReference({ wkid: 102100 });
bufferParameters.unit = esri.tasks.GeometryService.UNIT_KILOMETER;
bufferParameters.distances = [10];
bufferParameters.outSpatialReference = new SpatialReference({ wkid: 102100 });      
bufferParameters.unionResults = true;
bufferParameters.geodesic = false;
bufferParameters.geometries = geom;

// Launch de buffer

gsvc.buffer(bufferParameters).then(function () {
                    alert('buffer launched'); });

I get this error:

{"error":{"code":400,"message":"Unable to perform buffer","details":["'geometries' must be specified"]}}

I do not understand the parameter geometries is not  in the url (while the other parameters are):

https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer/buffer?f=json&unit=9036&unionResults=true&geodesic=false&distances=10&outSR=102100&bufferSR=102100

This is what the firefox/firebug has in the moment before doing the call to Buffer function:

Any idea that what is happening?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Marga,

   Use this instead:

bufferParameters.geometries = [new Point(-200043.56228524158, 363980.960991833, new SpatialReference({ wkid: 102100 }))];

MargaRaiz
New Contributor III

Now the buffer is working and I am able to see the buffer (blue circle) in the map so if I am right, I will need to use a query in order to get the the lines that intersectswith the buffer polygons,right?

I use the query and the query task, but again.. problems and I think  that also becuase the geometry parameter..

svc.buffer(bufferParameters).then(function (evt) {

resultEvent = evt[0];                    
var buf_pol = new Polygon(evt[0],new SpatialReference({ wkid: 102100 }));  //I have try this "copying" the previous error solution just in case but doesn't work                
LineQuery= new QueryTask(linesLayer.url);                    
LineQuery= new Query();
LineQuery.Geometry = buf_pol;

// I have also try like [buf_pol] or  whith  the buffer [graphic.geometry] but no succeed
LineQuery.outFields = ["*"];
LineQuery.orderByFields = ["ID"];
LineQuery.returnGeometry = true;
LineQuery.outSpatialReference = { wkid: 102100 };
LineQuery.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;                                       
LineQuery.execute(LineQuery).then(function (results) {
   console.log ('QueryBuffer Results');  
                    });

                    debugger
                    //                    this.graphicsLayerBuffer.clear();
                    //                    this.graphicsLayerBuffer.add(graphic);
                });

[...]

This is the call:

http://10.81.6.16:6080/arcgis/rest/services/lin_Layer/FeatureServer/0/query?f=json&where=&returnGeom... 

(I dont see the Geometry parameter in this url whereas when I do others calls yes.

This is the error:

{"error":{"code":400,"message":"Unable to complete operation.","details":["Unable to perform query operation."]}}

I don't understand why I have again problems with geometry parameters I have follow the documentation examples..

0 Kudos
LoriGonzalez
New Contributor III

Try renaming the variable for your Query Task.

LineQueryTask= new QueryTask(linesLayer.url);                    
LineQuery= new Query();

and then update your execute task with the new variable name.

LineQueryTask.execute(LineQuery)

MargaRaiz
New Contributor III

Hello Lori!

Thanks for your idea, but it doesn't work. Anyway, I have been able to make it works with this code fot it helps to others:

svc.buffer(bufferParameters).then(function (evt) {

    var simplefillsymbol = this.config.bufferDefaults.simplefillsymbol;


   //create a graphic with the buffer polygon

    var graphic = new Graphic();

    graphic.symbol = new SimpleFillSymbol(simplefillsymbol);
    graphic.geometry = evt[0];
    map.graphics.add(graphic); //Add the buffer polygon to the map

   / /Create the query between the lines layer and the buffer graphic

    var queryTask = new QueryTask(linesLayer.url);
    var query = new Query();
    query.returnGeometry = true;
    query.outFields = ["*"];
    query.outSpatialReference = { "wkid": 102100};
    query.geometry = webMercatorUtils.webMercatorToGeographic(graphic.geometry);
    query.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
    queryTask.execute(query).then(function (featureSet) {

          //Loop throught the results

     });

});

It has started working when I have added this line of code:

query.geometry = webMercatorUtils.webMercatorToGeographic(graphic.geometry);

0 Kudos