Select to view content in your preferred language

Query / Buffer / Return results

3278
13
01-20-2011 08:41 AM
JayKappy
Frequent Contributor
In my app I have a search textblock that is returning a record from the search criteria...in my example its the unique "PID" (parcel identification).  I then push the results to a results window.

What I am looking to do now is run the search list above BUT also return the results from a couple other layers in my app.  The problem is that they are not ideitifyed by the same unique id and most of them are much larger polygons representing polling precints, school boundaries etc.

I assume that I should be able to do some sort of spatial query based on the found feature searched on and then spatially query to locate the polygon that it lies within.  Then take those fields values and return them to another results window....Does that make sense...

Just looking for some ideas on how to take this one on....THanks
0 Kudos
13 Replies
DominiqueBroux
Esri Frequent Contributor
Depending on your data model, there are different ways to get related items:
    1) if there is a relationship in your geodatabase between your parcels and your larger polygons, you can use a 'RelationShipQuery' (method ExecuteRelationshipQueryAsync)
    2) if the relationship is managed by fields in the database, you can use simple queries by setting the right where clause
    3) if the relationship is only spatially managed, you can use spatial query (by setting Geometry property of the query).

From my understanding, you are in case 3), so a spatial query looks good.
0 Kudos
JayKappy
Frequent Contributor
Yea I think that is the way...little confused on where to start....

This is what I have (code below)...I have a textbloxk in my app.  The user types in a PID value and clicks a button....The button fires the code below that:
does the query
finds the PID value
Zooms to it and highlights it
and populates two results panels with the info..

Now I have the selected feature found in the Query (A GRAPHIC)
Do I then use this graphics geometry to do another query to find which polygon its within say in the precincts feature(layer)

not sure where to start?

THanks for your thoughts and help

    Private Sub ExecutePIDButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' QUERY NUMBER 1
        'Dim queryTask As New QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" & "Demographics/ESRI_Census_USA/MapServer/5")
        Dim queryTask As New QueryTask("http://gis.org/arcgis/rest/services/MG_Test_WGS84/MapServer/8")
        AddHandler queryTask.ExecuteCompleted, AddressOf QueryTaskFindPID_ExecuteCompletedSearch
        AddHandler queryTask.Failed, AddressOf QueryTaskFindPID_FailedSearch


        '' QUERY NUMBER 2
        Dim queryTask2 As New QueryTask("http://gis.org/arcgis/rest/services/MG_Test_WGS84/MapServer/2")
        AddHandler queryTask2.ExecuteCompleted, AddressOf QueryTask_ExecuteCompletedSearch
        AddHandler queryTask2.Failed, AddressOf QueryTaskFindPID_FailedSearch


        Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
        query.OutFields.Add("*")
        query.Text = FindPID.Text
        query.ReturnGeometry = True
        query.OutSpatialReference = MyMap.SpatialReference
        queryTask.ExecuteAsync(query)

    End Sub

    Private Sub QueryTaskFindPID_ExecuteCompletedSearch(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)

        Dim featureSet As FeatureSet = args.FeatureSet

        ' If an item has been selected            
        Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayerSearch"), GraphicsLayer)
        graphicsLayer.ClearGraphics()

        ''===============================================================================
        '' QUERY AND UPDATE THE FIRST RESULT LOCATION
        ''===============================================================================
        If featureSet IsNot Nothing AndAlso featureSet.Features.Count > 0 Then
            ' Show selected feature attributes in DataGrid
            Dim selectedFeature As Graphic = featureSet.Features(0)

            IdentifyDetailsDataGrid5.DataContext = selectedFeature.Attributes

            ' Hightlight selected feature
            selectedFeature.Symbol = TryCast(LayoutRoot.Resources("ResultsFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
            graphicsLayer.Graphics.Add(selectedFeature)

            ' Zoom to selected feature (define expand percentage)
            Dim selectedFeatureExtent As ESRI.ArcGIS.Client.Geometry.Envelope = selectedFeature.Geometry.Extent
            Dim expandPercentage As Double = 230
            Dim widthExpand As Double = selectedFeatureExtent.Width * (expandPercentage / 15)
            Dim heightExpand As Double = selectedFeatureExtent.Height * (expandPercentage / 15)
            Dim displayExtent As New ESRI.ArcGIS.Client.Geometry.Envelope(selectedFeatureExtent.XMin - (widthExpand / 2), selectedFeatureExtent.YMin - (heightExpand / 2), selectedFeatureExtent.XMax + (widthExpand / 2), selectedFeatureExtent.YMax + (heightExpand / 2))

            MyMap.ZoomTo(displayExtent)

            ' Update the Parcels results panel pane
            If IdentifyResultsPanel5.Visibility = Visibility.Collapsed Then
                IdentifyResultsPanel5.Visibility = Visibility.Visible
                IdentifyGrid5.Height = Double.NaN
                IdentifyGrid5.UpdateLayout()
            End If

        Else
            IdentifyResultsPanel5.DataContext = Nothing
            IdentifyResultsPanel5.Visibility = Visibility.Collapsed
            IdentifyGrid5.Height = Double.NaN
            IdentifyGrid5.UpdateLayout()
        End If

    End Sub
0 Kudos
JayKappy
Frequent Contributor
scratch that idea.....I cant add another queryTask because the Search criterial is the PID value....I need to somehow grab the selected features geometry and do a query on that....

I guess thats where I am stuck
0 Kudos
JenniferNery
Esri Regular Contributor
If you will be using FeatureLayer or GraphicsLayer, you can use Editor.Select http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#EditToolsAutoSave or EditorWidget http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#ToolkitEditorWidget. Either solutions will make a selection across different layers of same or different geometries and you can access them through SelectedGraphics property http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.GraphicsLa...(available in either FeatureLayer or GraphicsLayer).
0 Kudos
JayKappy
Frequent Contributor
If you will be using FeatureLayer or GraphicsLayer, you can use Editor.Select http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#EditToolsAutoSave or EditorWidget http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#ToolkitEditorWidget. Either solutions will make a selection across different layers of same or different geometries and you can access them through SelectedGraphics property http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.GraphicsLa...(available in either FeatureLayer or GraphicsLayer).



Thanks for your thoughts...but I do not see how those two examples shed light on taking a returned feature from a query and selecting another feature that either contains that foudn feature, etc....and then return the attributes to a results table

I guess what I am looking for is how to pass the Geomoetry of the query result into either a spatial query or Buffer to find more results to place in other results windows....
Say for the spatial query I can find out what Precinct that the parcel lies within and return that data
Say for the Buffer query I can find the curches within 5 miles and report them in a results window

If yoru examples are the route I shoudl take then cool...I just dont see any code examples that help me along with feeding the results from one query (the Parcel location and geometry) into the second Spatial or Buffer query.....

THANKS EVERYONE FOR YOUR PATIENCE AND HELP....it is very appreciated.
0 Kudos
JayKappy
Frequent Contributor
Could I take my original Query, add teh red code below to call another sub to handle the reseults?

I tried to handle the Geometry with this: querySpatial.Geometry = args.Geometry
Dopesnt like because of my "ByVal e As RoutedEventArgs" in the parameters...

I figure put the code here because I am running the query to get the Parcel from the "query.Text = FindPID.Text"

Then pass this geometry to another Sub routine to do the spatial query...

Am I way off base here?????

The spatial query example from ESRI shows the query beign feed from a user selection DrawEventArgs....I think all I need to do is replace this somehow with my reseult from the query locating the Parcel adn its geometry....
I already have he feature I want to feed into the spatial query or Buffer Query.....I think am close but cant figure this out.

    Private Sub ExecutePIDButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' QUERY NUMBER 1
        'Dim queryTask As New QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" & "Demographics/ESRI_Census_USA/MapServer/5")
        Dim queryTask As New QueryTask("http://gis.org/arcgis/rest/services/MG_Test_WGS84/MapServer/8")
        AddHandler queryTask.ExecuteCompleted, AddressOf QueryTaskFindPID_ExecuteCompletedSearch
        AddHandler queryTask.Failed, AddressOf QueryTaskFindPID_FailedSearch

        Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
        query.OutFields.Add("*")
        query.Text = FindPID.Text
        query.ReturnGeometry = True
        query.OutSpatialReference = MyMap.SpatialReference
        queryTask.ExecuteAsync(query)

 
        '' SPATIAL QUERY
        Dim queryTaskSpatial As New QueryTask("http://gis.org/arcgis/rest/services/MG_Test_WGS84/MapServer/5")
        AddHandler queryTaskSpatial.ExecuteCompleted, AddressOf QueryTask_ExecuteCompletedSearch
        AddHandler queryTaskSpatial.Failed, AddressOf QueryTask_FailedSearch

       ' Bind data grid to query results
        Dim resultFeaturesBinding As New Binding("LastResult.Features")
        resultFeaturesBinding.Source = queryTaskSpatial
        QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding)

        Dim querySpatial As New ESRI.ArcGIS.Client.Tasks.Query()
        querySpatial.OutFields.Add("*")
        querySpatial.Text = QueryComboBox.SelectedItem.ToString()
        querySpatial.ReturnGeometry = True
        querySpatial.Geometry = args.Geometry
        querySpatial.OutSpatialReference = MyMap.SpatialReference
        queryTaskSpatial.ExecuteAsync(querySpatial)

    End Sub
0 Kudos
dotMorten_esri
Esri Notable Contributor
You would have to put the red code in a different method that doesn't execute until the first querytask returns. You should use the ExecuteCompleted event on the querytask to trigger executing the red code. You won't have access to args.Geometry at the point where you placed the code, but that will be available in the ExecuteCompleted method hit by the first query.
0 Kudos
JayKappy
Frequent Contributor
Thanks Morten....really appreciate it....just looking for soem guidance....I think I will get it sooner or later....HAVE A GREAT WEEKEND...

So something along this lines??....I am sorry fro the stupid questions....I am really lost here no kidding....
I got a simple buffer working but again that was from the example and a mouse click, NOT using teh result of another query...I am trying to get the spatial filter and Buffer working from the result of the Parcel Search...

For now the Spatial.....I think this is what you are refering to.....

1. Push the result of the first query to another Method
2. Once in that method do some sort of spatial query, binding to a grid of some sort...WHAT I HAVE i think is completly wrong but

Just cant see where I would specify witin a specific feature or layer....

I dont care about highlighting the other results....just getting them into a results panel of some sort will do

THANKS AGAIN

    Private Sub ExecutePIDButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' QUERY NUMBER 1
        'Dim queryTask As New QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" & "Demographics/ESRI_Census_USA/MapServer/5")
        Dim queryTask As New QueryTask("http://gis.logis.org/arcgis/rest/services/MG_Test_WGS84/MapServer/8")
        AddHandler queryTask.ExecuteCompleted, AddressOf QueryTaskFindPID_ExecuteCompletedSearch
        AddHandler queryTask.Failed, AddressOf QueryTaskFindPID_FailedSearch


        '' QUERY NUMBER 2
        Dim queryTask2 As New QueryTask("http://gis.logis.org/arcgis/rest/services/MG_Test_WGS84/MapServer/2")
        AddHandler queryTask2.ExecuteCompleted, AddressOf QueryTask_ExecuteCompletedSearch
        AddHandler queryTask2.Failed, AddressOf QueryTaskFindPID_FailedSearch

        '' PUSH TO OTEHR METHOD TO DO SPATIAL QUERY
        Dim queryTaskSpatialQuery As New QueryTask("http://gis.logis.org/arcgis/rest/services/MG_Test_WGS84/MapServer/2")
        AddHandler queryTaskSpatialQuery.ExecuteCompleted, AddressOf QueryTask_ExecuteCompletedSpatialQuery
        AddHandler queryTaskSpatialQuery.Failed, AddressOf QueryTaskFindPID_FailedSpatialQuery

        Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
        query.OutFields.Add("*")
        query.Text = FindPID.Text
        query.ReturnGeometry = True
        query.OutSpatialReference = MyMap.SpatialReference
        queryTask.ExecuteAsync(query)

    End Sub


    Private Sub QueryTask_ExecuteCompletedSpatialQuery(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)

        '' SPATIAL QUERY
        Dim queryTaskSpatial As New QueryTask("http://gis.logis.org/arcgis/rest/services/MG_Test_WGS84/MapServer/5")
        AddHandler queryTaskSpatial.ExecuteCompleted, AddressOf QueryTask_ExecuteCompletedFINALBUFFER
        AddHandler queryTaskSpatial.Failed, AddressOf QueryTask_FailedSearch

        ' Bind data grid to query results
        Dim resultFeaturesBinding As New Binding("LastResult.Features")
        resultFeaturesBinding.Source = queryTaskSpatial
        QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding)

        Dim querySpatial As New ESRI.ArcGIS.Client.Tasks.Query()
        querySpatial.OutFields.Add("*")
        querySpatial.ReturnGeometry = True
        querySpatial.Geometry = args.Geometry
        querySpatial.OutSpatialReference = MyMap.SpatialReference
        queryTaskSpatial.ExecuteAsync(querySpatial)

    End Sub
    Private Sub QueryTaskFindPID_FailedSpatialQuery(ByVal sender As Object, ByVal args As TaskFailedEventArgs)
        MessageBox.Show("Query failed: " & args.Error.Message)
    End Sub


    Private Sub QueryTask_ExecuteCompletedFINALBUFFER(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
        Dim featureSet As FeatureSet = args.FeatureSet

        If featureSet Is Nothing OrElse featureSet.Features.Count < 1 Then
            MessageBox.Show("No features retured from query")
            Return
        End If

        Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MySelectionGraphicsLayer"), GraphicsLayer)

        If featureSet IsNot Nothing AndAlso featureSet.Features.Count > 0 Then
            For Each feature As Graphic In featureSet.Features
                feature.Symbol = TryCast(LayoutRoot.Resources("ResultsFillSymbol"), FillSymbol)
                graphicsLayer.Graphics.Insert(0, feature)
            Next feature
            ResultsDisplay.Visibility = Visibility.Visible
        End If

    End Sub

    Private Sub QueryTask_Failed(ByVal sender As Object, ByVal args As TaskFailedEventArgs)
        MessageBox.Show("Query failed: " & args.Error.ToString())
    End Sub
0 Kudos
JenniferNery
Esri Regular Contributor
While this sample does not solve your problem, it might serve as a guide http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#QueryRelatedRecords.

Notice in this sample that there are two Async operations ExecuteAsync() and ExecuteRelationshipQueryAsync(). They are called from the same QueryTask but their parameters and Completed event handler are different. Subscription to events are also done once, not every time completed event fires.

Unlike the sample above, your Async calls are related in that, you need to perform a Spatial Query within the Attribute Query completed event. Your code is somewhat correct except for few pointers I mentioned above. Also to get the geometry for your Spatial Query, you might have to get the first geometry this way:
For Each graphic As Graphic In args.FeatureSet.Features
 Dim querySpatial As New ESRI.ArcGIS.Client.Tasks.Query()
        querySpatial.OutFields.Add("*")
        querySpatial.ReturnGeometry = True
        querySpatial.Geometry = graphic.Geometry
        querySpatial.OutSpatialReference = MyMap.SpatialReference
        queryTaskSpatial.ExecuteAsync(querySpatial)
 Exit For
Next
0 Kudos