# Filtering shape file on several columns

**URL:** <https://community.thinkgeo.com/t/filtering-shape-file-on-several-columns/3778>\
**Category:** WPF\
**Created:** [February 16, 2015, 6:16am UTC](https://community.thinkgeo.com/t/filtering-shape-file-on-several-columns/3778 "2015-02-16T06:16:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter3](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/p/958977/32.png) [@Peter3](https://community.thinkgeo.com/u/Peter3)\
**Post date:** [February 16, 2015, 6:16am UTC](https://community.thinkgeo.com/t/filtering-shape-file-on-several-columns/3778/1 "2015-02-16T06:16:02Z")

</div>

Hi,  
  
  
  
I&nbsp;need&nbsp;to&nbsp;filter&nbsp;a&nbsp;shape&nbsp;file&nbsp;on&nbsp;a&nbsp;predicate&nbsp;as&nbsp;in&nbsp;an&nbsp;sql&nbsp;where&nbsp;clause.  
  
  
  
e.g&nbsp;filter&nbsp;=&nbsp;"[COL1]&nbsp;IN&nbsp;(1,2)&nbsp;AND&nbsp;[COL2]=3"  
  
  
  
Looking&nbsp;through&nbsp;the&nbsp;ShapeFileFeatureSource&nbsp;we&nbsp;have&nbsp;a&nbsp;  
  
GetFeaturesByColumnValue(…)&nbsp;and&nbsp;an&nbsp;ApplyFilters(…)&nbsp;(didn’t&nbsp;find&nbsp;much&nbsp;spec.&nbsp;on&nbsp;that&nbsp;one&nbsp;in&nbsp;the&nbsp;documentation).  
  
  
  
None&nbsp;of&nbsp;those&nbsp;take&nbsp;a&nbsp;simple&nbsp;where&nbsp;clause&nbsp;as&nbsp;input.  
  
  
  
Do&nbsp;you&nbsp;have&nbsp;a&nbsp;solution&nbsp;for&nbsp;me?  
  
  
  
Thanks,  
  
Peter

---

<div class="post-metadata">

**Author:** ![Ethan](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/ethan/32/19964_2.png) [@Ethan](https://community.thinkgeo.com/u/Ethan)\
**Post date:** [February 17, 2015, 2:26am UTC](https://community.thinkgeo.com/t/filtering-shape-file-on-several-columns/3778/2 "2015-02-17T02:26:45Z")

</div>

Hi&nbsp;Peter,&nbsp;  
&nbsp;&nbsp;  
&nbsp;GetFeaturesByColumnValue&nbsp;only&nbsp;support&nbsp;pass&nbsp;one&nbsp;column&nbsp;in.&nbsp;  
&nbsp;&nbsp;  
&nbsp;ApplyFilters&nbsp;looks&nbsp;don’t&nbsp;support&nbsp;the&nbsp;grammar&nbsp;like&nbsp;you&nbsp;mentioned.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Have&nbsp;you&nbsp;tried&nbsp;this&nbsp;API&nbsp;layer.QueryTools.ExecuteQuery?&nbsp;  
&nbsp;&nbsp;  
&nbsp;Regards,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Don

---

<div class="post-metadata">

**Author:** ![Peter3](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/p/958977/32.png) [@Peter3](https://community.thinkgeo.com/u/Peter3)\
**Post date:** [February 20, 2015, 2:08am UTC](https://community.thinkgeo.com/t/filtering-shape-file-on-several-columns/3778/3 "2015-02-20T02:08:44Z")

</div>

Hi&nbsp;Don,  
  
  
  
QueryTools.ExecuteQuery&nbsp;doesn’t&nbsp;seem&nbsp;to&nbsp;do&nbsp;it&nbsp;(actually&nbsp;it&nbsp;didn’t&nbsp;return&nbsp;anything&nbsp;where&nbsp;a&nbsp;featureSource.ExecuteScalar&nbsp;worked&nbsp;on&nbsp;a&nbsp;similar&nbsp;sql&nbsp;where&nbsp;‘SELECT&nbsp;\*’&nbsp;was&nbsp;replaced&nbsp;with&nbsp;‘SELECT&nbsp;COUNT(\*)’).  
  
  
  
My&nbsp;conclusion:&nbsp;QueryTools&nbsp;doesn’t&nbsp;do&nbsp;the&nbsp;job.  
  
  
  
I’m&nbsp;looking&nbsp;for&nbsp;a&nbsp;signature&nbsp;a&nbsp;la&nbsp;FeatureSource.GetFeaturesByFilter(filter).  
  
  
  
It&nbsp;would&nbsp;also&nbsp;be&nbsp;nice&nbsp;to&nbsp;be&nbsp;able&nbsp;to&nbsp;mix&nbsp;spatial&nbsp;predicates&nbsp;and&nbsp;column&nbsp;predicates&nbsp;in&nbsp;the&nbsp;same&nbsp;query.  
  
  
  
thanks,  
  
Peter

---

<div class="post-metadata">

**Author:** ![Howard](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/h/90ced4/32.png) [@Howard](https://community.thinkgeo.com/u/Howard)\
**Post date:** [February 20, 2015, 3:51am UTC](https://community.thinkgeo.com/t/filtering-shape-file-on-several-columns/3778/4 "2015-02-20T03:51:27Z")

</div>

Hi&nbsp;Peter,  
  
  
  
I&nbsp;agree&nbsp;that&nbsp;QueryTools.ExecuteQuery&nbsp;doesn’t&nbsp;do&nbsp;your&nbsp;job.&nbsp;Here&nbsp;is&nbsp;a&nbsp;clue&nbsp;for&nbsp;you&nbsp;to&nbsp;filter&nbsp;the&nbsp;features&nbsp;by&nbsp;the&nbsp;T-SQL&nbsp;where&nbsp;clause.&nbsp;Please&nbsp;feel&nbsp;free&nbsp;to&nbsp;make&nbsp;changes&nbsp;on&nbsp;my&nbsp;extension.

> `static class QueryToolExtension`  
> `{`  
> `    ``public static Collection<``Feature``> GetFeaturesByFilter(this FeatureLayer featureLayer, string whereClause, IEnumerable<``string``> returningColumnNames)`  
> `    ``{`  
> `        ``string sql = string.Format(CultureInfo.InvariantCulture, "SELECT RECID FROM {0} WHERE {1}", featureLayer.Name, whereClause);`  
> `        ``DataTable dataTable = featureLayer.QueryTools.ExecuteQuery(sql);`  
> `        ``string[] ids = dataTable.Rows.Cast<``DataRow``>().Select(r => r[0].ToString()).ToArray();`  
> `        ``return featureLayer.QueryTools.GetFeaturesByIds(ids, returningColumnNames);`  
> `    ``}`  
> `}`

  
  
With&nbsp;this&nbsp;extension,&nbsp;we&nbsp;could&nbsp;use&nbsp;it&nbsp;like&nbsp;following&nbsp;code&nbsp;(The&nbsp;data&nbsp;is&nbsp;using&nbsp;"USSTATES.shp"&nbsp;in&nbsp;our&nbsp;How&nbsp;Do&nbsp;I&nbsp;Samples).&nbsp;There&nbsp;are&nbsp;three&nbsp;things&nbsp;before&nbsp;using&nbsp;it.  
  

  
&nbsp;&nbsp;&nbsp;&nbsp;1. Set&nbsp;the&nbsp;name&nbsp;of&nbsp;the&nbsp;FeatureLayer&nbsp;in&nbsp;case&nbsp;we&nbsp;could&nbsp;find&nbsp;the&nbsp;table&nbsp;name.
  
&nbsp;&nbsp;&nbsp;&nbsp;2. Open&nbsp;the&nbsp;layer.
  
&nbsp;&nbsp;&nbsp;&nbsp;3. Make&nbsp;sure&nbsp;there&nbsp;is&nbsp;a&nbsp;RECID&nbsp;column&nbsp;in&nbsp;the&nbsp;DBF;&nbsp;actually,&nbsp;most&nbsp;of&nbsp;the&nbsp;DBF&nbsp;files&nbsp;contain&nbsp;this&nbsp;column.&nbsp;If&nbsp;not,&nbsp;we&nbsp;might&nbsp;need&nbsp;to&nbsp;find&nbsp;another&nbsp;column&nbsp;name&nbsp;to&nbsp;indicate&nbsp;the&nbsp;row\_number.
  

  
  
  

> `ShapeFileFeatureLayer statesLayer = (ShapeFileFeatureLayer)wpfMap1.FindFeatureLayer("StatesLayer");`  
> `statesLayer.Name = "USStates";`  
> &nbsp;  
> `statesLayer.Open();`  
> `Collection<``Feature``> features = statesLayer.GetFeaturesByFilter("PERIMETER > 30 Order by PERIMETER", new[] { "STATE_ID", "STATE_NAME" });`  
> &nbsp;  
> `Console.WriteLine(features);`

  
  
  
  
Thanks&nbsp;and&nbsp;feel&nbsp;free&nbsp;to&nbsp;let&nbsp;us&nbsp;know&nbsp;if&nbsp;you&nbsp;have&nbsp;more&nbsp;queries.  
Howard

---

<div class="post-metadata">

**Author:** ![Peter3](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/p/958977/32.png) [@Peter3](https://community.thinkgeo.com/u/Peter3)\
**Post date:** [February 20, 2015, 4:05am UTC](https://community.thinkgeo.com/t/filtering-shape-file-on-several-columns/3778/5 "2015-02-20T04:05:33Z")

</div>

Hi&nbsp;Howard,  
  
  
  
You&nbsp;hit&nbsp;the&nbsp;soft&nbsp;spot&nbsp;with&nbsp;#3&nbsp;of&nbsp;your&nbsp;_three&nbsp;things&nbsp;before&nbsp;using&nbsp;it_.  
  
  
  
My&nbsp;DBF&nbsp;files&nbsp;are&nbsp;not&nbsp;guaranteed&nbsp;to&nbsp;have&nbsp;a&nbsp;column&nbsp;with&nbsp;the&nbsp;row\_number.  
  
  
  
Can&nbsp;this&nbsp;situation&nbsp;be&nbsp;handled&nbsp;with&nbsp;MapSuite?  
  
  
  
Thanks,  
  
Peter

---

<div class="post-metadata">

**Author:** ![Howard](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/h/90ced4/32.png) [@Howard](https://community.thinkgeo.com/u/Howard)\
**Post date:** [February 20, 2015, 7:41am UTC](https://community.thinkgeo.com/t/filtering-shape-file-on-several-columns/3778/6 "2015-02-20T07:41:57Z")

</div>

Hi&nbsp;Peter,  
  
  
  
I&nbsp;think&nbsp;it&nbsp;is&nbsp;possible,&nbsp;but&nbsp;need&nbsp;more&nbsp;memory.&nbsp;I&nbsp;tried&nbsp;some&nbsp;FoxPro&nbsp;Sql&nbsp;(RECNO(),&nbsp;@@IDENTITY,&nbsp;REC\_NUMBER()…&nbsp;are&nbsp;all&nbsp;not&nbsp;support)&nbsp;to&nbsp;fetch&nbsp;the&nbsp;record&nbsp;number,&nbsp;but&nbsp;failed.&nbsp;It&nbsp;seems&nbsp;the&nbsp;OLEDB&nbsp;doesn’t&nbsp;support&nbsp;it&nbsp;when&nbsp;loading&nbsp;Dbf&nbsp;files.&nbsp;So&nbsp;I&nbsp;used&nbsp;a&nbsp;workaround&nbsp;to&nbsp;add&nbsp;an&nbsp;id&nbsp;column&nbsp;on&nbsp;the&nbsp;datatable.&nbsp;If&nbsp;we&nbsp;could&nbsp;directly&nbsp;use&nbsp;"select"&nbsp;statement&nbsp;to&nbsp;add&nbsp;the&nbsp;record&nbsp;number&nbsp;column,&nbsp;I’m&nbsp;sure&nbsp;it&nbsp;will&nbsp;be&nbsp;much&nbsp;faster&nbsp;and&nbsp;lower&nbsp;memory&nbsp;usage.&nbsp;Here&nbsp;is&nbsp;a&nbsp;modified&nbsp;version&nbsp;of&nbsp;my&nbsp;extension&nbsp;method.

> `// param: featureLayer: FeatureSource that is going to be queried.`  
> `// param: filter: Filter clause. Doesn’t support "order by". Because it will mess up the records order.`  
> `// param: filterColumnNames: Indicates the column names in the filter statement. You could also parse it from the filter to make the API simpler.`  
> `// param: returningColumnNames: Indicates the returning column names.`  
> `public static Collection<``Feature``> GetFeaturesByFilter(this FeatureLayer featureLayer, string filter, IEnumerable<``string``> filterColumnNames, IEnumerable<``string``> returningColumnNames)`  
> `{`  
> `    ``string sql = @"select {0} from {1}";`  
> `    ``sql = string.Format(CultureInfo.InvariantCulture, sql, string.Join(", ", filterColumnNames), featureLayer.Name);`  
> `    ``DataTable dataTable = featureLayer.QueryTools.ExecuteQuery(sql);`  
> &nbsp;  
> `    ``// Build the TempId column for the DataTable.`  
> `    ``dataTable.Columns.Add(new DataColumn("TEMPID", typeof(int)));`  
> `    ``int id = 1;`  
> `    ``foreach (var row in dataTable.Rows.Cast<``DataRow``>())`  
> `    ``{`  
> `        ``row["TEMPID"] = id;`  
> `        ``id++;`  
> `    ``}`  
> &nbsp;  
> `    ``// Filter the DataTable.`  
> `    ``DataRow[] rows = dataTable.Select(filter);`  
> `    ``string[] ids = rows.Select(r => r["TEMPID"].ToString()).ToArray();`  
> `    ``return featureLayer.QueryTools.GetFeaturesByIds(ids, returningColumnNames);`  
> `}`

  
  
  
  
  
Here&nbsp;is&nbsp;the&nbsp;code&nbsp;using&nbsp;it.  
  

> `ShapeFileFeatureLayer statesLayer = (ShapeFileFeatureLayer)wpfMap1.FindFeatureLayer("StatesLayer");`  
> `statesLayer.Name = "USStates";`  
> `statesLayer.Open();`  
> `Collection<``Feature``> features = statesLayer.GetFeaturesByFilter("PERIMETER > 30", new[] { "PERIMETER" }, new[] { "STATE_ID", "STATE_NAME" });`  
> `Console.WriteLine(features);`

  
  
Hope&nbsp;it&nbsp;helps.  
  
  
  
Thanks,  
Howard  
  
``  
``
