Is there a way to create a Definition Query works with Query Filter synchronously in ArcGIS PRO SDK?

192
1
Jump to solution
12-28-2020 11:47 AM
OguzSariyildiz
Occasional Contributor

I have created a table view which users can filter features by making selection via multiple combo boxes  

OguzSariyildiz_0-1609183901379.png

 

 

 

public void UpdateSelection()
{
    QueuedTask.Run(() =>
    {
        if (_tableContent != null)
        {
            QueryFilter qfFType = new QueryFilter { WhereClause = 
             $@"FEATURE_CLASS = '{Feature}'" };
            QueryFilter qfState = new QueryFilter { WhereClause = 
            $@"STATE_ALPHA = '{State}'" };
            QueryFilter qfCounty = new QueryFilter { WhereClause = 
            $@"COUNTY_NAME = '{County}'" };
            QueryFilter qfQuad = new QueryFilter { WhereClause = 
            $@"MAP_NAME = '{Quad}'" };

            Dictionary<QueryFilter, string> FieldFilterDic = new 
                Dictionary<QueryFilter, string>
                    {
                        { qfFType, Feature},
                        { qfState, State },
                        { qfCounty, County},
                        { qfQuad, Quad}
                    };

            if (Feature == "All")
                FieldFilterDic.Remove(qfFType);
            if (State == "All")
                FieldFilterDic.Remove(qfState);
            if (County == "All")
                FieldFilterDic.Remove(qfCounty);
            if (Quad == "All")
                FieldFilterDic.Remove(qfQuad);

            if (FieldFilterDic.Count > 0)
            {
                var firstFilter = FieldFilterDic.Keys.First();
                var firststring = FieldFilterDic[firstFilter];
                GNISFLayer.Select(firstFilter, 
                    SelectionCombinationMethod.New);
                FieldFilterDic.Remove(firstFilter);

                foreach (KeyValuePair<QueryFilter, string> kvp in FieldFilterDic)
                {
                    GNISFLayer.Select(kvp.Key, 
                         SelectionCombinationMethod.And);
                }
            }
            else
                GNISFLayer.ClearSelection();
        }
    });
}

 

 

 

The method above works fine for selecting features based on multiple criteria taken from combo boxes. 

I would also like to remove features from the display as user filters out the features. 

I think I can adapt the syncing myself as far as if I can use multiple criteria on the Definition Query. For instance,  something like

Definition Query = GNISFLayer.SetDefinitionQuery($@"FEATURE_CLASS = '{Feature}' && $@"STATE_ALPHA = '{State}'"");

0 Kudos
1 Solution

Accepted Solutions
OguzSariyildiz
Occasional Contributor

I was able to use multiple criteria when setting definition by using AND instead of &&.

 

//Make sure to insert spaces before and after " AND "
string dq = $@"FEATURE_CLASS = '{Feature}'" + " AND " + $@"STATE_ALPHA = '{State}'"
GNISFLayer.SetDefinitionQuery(dq)

 

Then, I came up with this solution:

 

public Task<Dictionary<QueryFilter, string[]>> UpdateSelection()
        {
            return QueuedTask.Run(() =>
            {
                if (_tableContent != null)
                {
                    string GNISFType = $@"FEATURE_CLASS = '{Feature}'";
                    string GNISState = $@"STATE_ALPHA = '{State}'";
                    string GNISCounty = $@"COUNTY_NAME = '{County}'";
                    string GNISQuad = $@"MAP_NAME = '{Quad}'";

                    string[] GNISFTypeArr = { GNISFType, Feature };
                    string[] GNISStateArr = { GNISState, State };
                    string[] GNISCountyArr = { GNISCounty, County };
                    string[] GNISQuadArr = { GNISQuad, Quad };

                    QueryFilter qfFType = new QueryFilter { WhereClause = GNISFType };
                    QueryFilter qfState = new QueryFilter { WhereClause = GNISState };
                    QueryFilter qfCounty = new QueryFilter { WhereClause = GNISCounty };
                    QueryFilter qfQuad = new QueryFilter { WhereClause = GNISQuad };

                    FieldFilterDic = new Dictionary<QueryFilter, string[]>
                    {
                        { qfFType, GNISFTypeArr},
                        { qfState, GNISStateArr},
                        { qfCounty, GNISCountyArr},
                        { qfQuad, GNISQuadArr}
                    };

                    if (Feature == "All")
                        FieldFilterDic.Remove(qfFType);
                    if (State == "All")
                        FieldFilterDic.Remove(qfState);
                    if (County == "All")
                        FieldFilterDic.Remove(qfCounty);
                    if (Quad == "All")
                        FieldFilterDic.Remove(qfQuad);

                    if (FieldFilterDic.Count > 0)
                    {
                        foreach (KeyValuePair<QueryFilter, string[]> kvp in FieldFilterDic)
                        {
                            if ( kvp.Key == FieldFilterDic.Keys.First())
                                GNISFLayer.Select(kvp.Key, SelectionCombinationMethod.New);
                            else
                                GNISFLayer.Select(kvp.Key, SelectionCombinationMethod.And);
                        }    
                    }
                    else
                        GNISFLayer.ClearSelection();
                }
                return FieldFilterDic;
            });
        }

        private void DisplaySelBoolChanged()
        {
            if (DisplaySelBool)
            {
                QueuedTask.Run(() =>
                {
                    if (SelectionDict.Count > 0)
                    {
                        GNISFLayer.RemoveAllDefinitionFilters();
                        string entireExp = null;
                        

                        if (SelectionDict.Count > 0)
                        {
                            foreach (KeyValuePair<QueryFilter, string[]> kvp in FieldFilterDic)
                            {
                                if (kvp.Key == SelectionDict.Keys.Last())
                                    entireExp += kvp.Value[0];
                                else
                                    entireExp += kvp.Value[0] + " AND ";
                            }
                        }

                        MessageBox.Show(entireExp);
                        GNISFLayer.SetDefinitionQuery(entireExp);
                    }
                    else
                    {
                        GNISFLayer.RemoveAllDefinitionFilters();
                        DisplaySelBool = false;
                    }
                });
            }
            //else
            //    GNISFLayer.RemoveAllDefinitionFilters();
        }

 

 

first method (UpdateSelection()) use the dictionary and return it to the main thread and the same dictionary is used for setting up/impementing the Definition Filter in the DisplaySelBoolChanged() method

 private async void FeatureTypeSelectionChanged()
        {
            var resultDic = await UpdateSelection();
            SelectionDict = resultDic;
            DisplaySelBoolChanged();
        }

View solution in original post

0 Kudos
1 Reply
OguzSariyildiz
Occasional Contributor

I was able to use multiple criteria when setting definition by using AND instead of &&.

 

//Make sure to insert spaces before and after " AND "
string dq = $@"FEATURE_CLASS = '{Feature}'" + " AND " + $@"STATE_ALPHA = '{State}'"
GNISFLayer.SetDefinitionQuery(dq)

 

Then, I came up with this solution:

 

public Task<Dictionary<QueryFilter, string[]>> UpdateSelection()
        {
            return QueuedTask.Run(() =>
            {
                if (_tableContent != null)
                {
                    string GNISFType = $@"FEATURE_CLASS = '{Feature}'";
                    string GNISState = $@"STATE_ALPHA = '{State}'";
                    string GNISCounty = $@"COUNTY_NAME = '{County}'";
                    string GNISQuad = $@"MAP_NAME = '{Quad}'";

                    string[] GNISFTypeArr = { GNISFType, Feature };
                    string[] GNISStateArr = { GNISState, State };
                    string[] GNISCountyArr = { GNISCounty, County };
                    string[] GNISQuadArr = { GNISQuad, Quad };

                    QueryFilter qfFType = new QueryFilter { WhereClause = GNISFType };
                    QueryFilter qfState = new QueryFilter { WhereClause = GNISState };
                    QueryFilter qfCounty = new QueryFilter { WhereClause = GNISCounty };
                    QueryFilter qfQuad = new QueryFilter { WhereClause = GNISQuad };

                    FieldFilterDic = new Dictionary<QueryFilter, string[]>
                    {
                        { qfFType, GNISFTypeArr},
                        { qfState, GNISStateArr},
                        { qfCounty, GNISCountyArr},
                        { qfQuad, GNISQuadArr}
                    };

                    if (Feature == "All")
                        FieldFilterDic.Remove(qfFType);
                    if (State == "All")
                        FieldFilterDic.Remove(qfState);
                    if (County == "All")
                        FieldFilterDic.Remove(qfCounty);
                    if (Quad == "All")
                        FieldFilterDic.Remove(qfQuad);

                    if (FieldFilterDic.Count > 0)
                    {
                        foreach (KeyValuePair<QueryFilter, string[]> kvp in FieldFilterDic)
                        {
                            if ( kvp.Key == FieldFilterDic.Keys.First())
                                GNISFLayer.Select(kvp.Key, SelectionCombinationMethod.New);
                            else
                                GNISFLayer.Select(kvp.Key, SelectionCombinationMethod.And);
                        }    
                    }
                    else
                        GNISFLayer.ClearSelection();
                }
                return FieldFilterDic;
            });
        }

        private void DisplaySelBoolChanged()
        {
            if (DisplaySelBool)
            {
                QueuedTask.Run(() =>
                {
                    if (SelectionDict.Count > 0)
                    {
                        GNISFLayer.RemoveAllDefinitionFilters();
                        string entireExp = null;
                        

                        if (SelectionDict.Count > 0)
                        {
                            foreach (KeyValuePair<QueryFilter, string[]> kvp in FieldFilterDic)
                            {
                                if (kvp.Key == SelectionDict.Keys.Last())
                                    entireExp += kvp.Value[0];
                                else
                                    entireExp += kvp.Value[0] + " AND ";
                            }
                        }

                        MessageBox.Show(entireExp);
                        GNISFLayer.SetDefinitionQuery(entireExp);
                    }
                    else
                    {
                        GNISFLayer.RemoveAllDefinitionFilters();
                        DisplaySelBool = false;
                    }
                });
            }
            //else
            //    GNISFLayer.RemoveAllDefinitionFilters();
        }

 

 

first method (UpdateSelection()) use the dictionary and return it to the main thread and the same dictionary is used for setting up/impementing the Definition Filter in the DisplaySelBoolChanged() method

 private async void FeatureTypeSelectionChanged()
        {
            var resultDic = await UpdateSelection();
            SelectionDict = resultDic;
            DisplaySelBoolChanged();
        }

View solution in original post

0 Kudos