# Thematic reports based on an external database query

**URL:** <https://community.thinkgeo.com/t/thematic-reports-based-on-an-external-database-query/419>\
**Category:** WinForms\
**Created:** [May 14, 2009, 8:33am UTC](https://community.thinkgeo.com/t/thematic-reports-based-on-an-external-database-query/419 "2009-05-14T08:33:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvandyk](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jvandyk](https://community.thinkgeo.com/u/jvandyk)\
**Post date:** [May 14, 2009, 8:33am UTC](https://community.thinkgeo.com/t/thematic-reports-based-on-an-external-database-query/419/1 "2009-05-14T08:33:15Z")

</div>

Whats&nbsp;the&nbsp;best&nbsp;way&nbsp;to&nbsp;create&nbsp;a&nbsp;thematic&nbsp;map&nbsp;based&nbsp;on&nbsp;an&nbsp;external&nbsp;database&nbsp;query?

For&nbsp;our&nbsp;Management&nbsp;systems&nbsp;we&nbsp;generally&nbsp;link&nbsp;shape&nbsp;file&nbsp;data&nbsp;to&nbsp;records&nbsp;in&nbsp;a&nbsp;database&nbsp;based&nbsp;on&nbsp;a&nbsp;common&nbsp;ID&nbsp;in&nbsp;the&nbsp;DB&nbsp;and&nbsp;in&nbsp;the&nbsp;attribute&nbsp;table&nbsp;of&nbsp;the&nbsp;shape&nbsp;file.&nbsp;Often&nbsp;we&nbsp;need&nbsp;to&nbsp;create&nbsp;thematic&nbsp;reports&nbsp;based&nbsp;on&nbsp;the&nbsp;results&nbsp;of&nbsp;queries&nbsp;run&nbsp;the&nbsp;the&nbsp;database.&nbsp;What&nbsp;is&nbsp;the&nbsp;best&nbsp;way&nbsp;to&nbsp;achieve&nbsp;this.&nbsp;Thus&nbsp;far&nbsp;in&nbsp;your&nbsp;examples&nbsp;all&nbsp;your&nbsp;thematic&nbsp;reports&nbsp;are&nbsp;based&nbsp;on&nbsp;a&nbsp;query&nbsp;in&nbsp;the&nbsp;attribute&nbsp;table.

---

<div class="post-metadata">

**Author:** ![Lee](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@Lee](https://community.thinkgeo.com/u/Lee)\
**Post date:** [May 15, 2009, 4:51am UTC](https://community.thinkgeo.com/t/thematic-reports-based-on-an-external-database-query/419/2 "2009-05-15T04:51:47Z")

</div>

Jeremy,&nbsp;  
&nbsp;&nbsp;  
&nbsp;You&nbsp;can&nbsp;use&nbsp;CustomColumnFetchEvent&nbsp;event&nbsp;to&nbsp;integrate&nbsp;your&nbsp;data&nbsp;to&nbsp;the&nbsp;shape&nbsp;file&nbsp;attributes.&nbsp;&nbsp;We&nbsp;have&nbsp;a&nbsp;sample&nbsp;HowDoISamples\FeatureLayer\AddMyOwnCustomDataToAFeatureLayer&nbsp;which&nbsp;shows&nbsp;how&nbsp;to&nbsp;do&nbsp;it.&nbsp;  
&nbsp;&nbsp;&nbsp;  
&nbsp;And&nbsp;there&nbsp;is&nbsp;a&nbsp;known&nbsp;issue&nbsp;about&nbsp;it&nbsp;that&nbsp;the&nbsp;event&nbsp;won’t&nbsp;be&nbsp;raised&nbsp;if&nbsp;you&nbsp;call&nbsp;FeatureSource.GetFeaturesByColumnValues&nbsp;method.&nbsp;We&nbsp;will&nbsp;fix&nbsp;it&nbsp;in&nbsp;the&nbsp;upcoming&nbsp;version&nbsp;which&nbsp;will&nbsp;be&nbsp;released&nbsp;around&nbsp;next&nbsp;week.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks,&nbsp;  
&nbsp;ThinkGeo&nbsp;Support&nbsp;

---

<div class="post-metadata">

**Author:** ![jvandyk](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jvandyk](https://community.thinkgeo.com/u/jvandyk)\
**Post date:** [May 15, 2009, 5:46am UTC](https://community.thinkgeo.com/t/thematic-reports-based-on-an-external-database-query/419/3 "2009-05-15T05:46:21Z")

</div>

Thanks&nbsp;I&nbsp;managed&nbsp;to&nbsp;find&nbsp;a&nbsp;similar&nbsp;post&nbsp;by&nbsp;another&nbsp;Jeremy&nbsp;that&nbsp;helped&nbsp;me&nbsp;and&nbsp;I&nbsp;got&nbsp;a&nbsp;test&nbsp;report.&nbsp;The&nbsp;problem&nbsp;I’m&nbsp;facing&nbsp;here&nbsp;is&nbsp;that&nbsp;this&nbsp;seems&nbsp;like&nbsp;an&nbsp;extremely&nbsp;complicated&nbsp;way&nbsp;of&nbsp;creating&nbsp;a&nbsp;thematic&nbsp;report&nbsp;from&nbsp;a&nbsp;database&nbsp;external&nbsp;query.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Surely&nbsp;instead&nbsp;of&nbsp;actually&nbsp;integrating&nbsp;my&nbsp;data&nbsp;with&nbsp;the&nbsp;shape&nbsp;file&nbsp;attributes&nbsp;it&nbsp;would&nbsp;be&nbsp;better&nbsp;to&nbsp;come&nbsp;up&nbsp;with&nbsp;a&nbsp;way&nbsp;for&nbsp;to&nbsp;create&nbsp;temporary&nbsp;joins&nbsp;to&nbsp;the&nbsp;external&nbsp;database.&nbsp;  
&nbsp;&nbsp;  
&nbsp;I&nbsp;mean&nbsp;If&nbsp;I&nbsp;have&nbsp;to&nbsp;create&nbsp;say&nbsp;20&nbsp;thematic&nbsp;reports&nbsp;for&nbsp;about&nbsp;12&nbsp;different&nbsp;shapefiles&nbsp;based&nbsp;of&nbsp;referenced&nbsp;data&nbsp;in&nbsp;the&nbsp;database,&nbsp;&nbsp;which&nbsp;happens&nbsp;fairly&nbsp;often,&nbsp;my&nbsp;code&nbsp;is&nbsp;going&nbsp;to&nbsp;get&nbsp;very&nbsp;very&nbsp;busy.

---

<div class="post-metadata">

**Author:** ![Ben](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/ben/32/25720_2.png) [@Ben](https://community.thinkgeo.com/u/Ben)\
**Post date:** [May 18, 2009, 4:58am UTC](https://community.thinkgeo.com/t/thematic-reports-based-on-an-external-database-query/419/4 "2009-05-18T04:58:00Z")

</div>

Jeremy,  
  
  
  
Here&nbsp;I&nbsp;made&nbsp;a&nbsp;wrapper&nbsp;with&nbsp;which,&nbsp;you&nbsp;can&nbsp;use&nbsp;the&nbsp;external&nbsp;database&nbsp;much&nbsp;easier.&nbsp;You&nbsp;can&nbsp;change&nbsp;the&nbsp;code&nbsp;a&nbsp;little&nbsp;bit&nbsp;to&nbsp;meet&nbsp;your&nbsp;requirements.

Here&nbsp;is&nbsp;the&nbsp;code:

` public class CustomDataShapeFileFeatureLayer : ShapeFileFeatureLayer    {        private Dictionary<string, string> idLinkFieldDictionary = new Dictionary<string, string>();        private Dictionary<string, Dictionary<string, string>> customFieldsDictionaries = new Dictionary<string, Dictionary<string, string>>();        private CustomDataShapeFileFeatureLayer()            : this(string.Empty)        { }        public CustomDataShapeFileFeatureLayer(string shapeFileFilePath)            : base(shapeFileFilePath)        {            FeatureSource.CustomColumnFetch += new EventHandler<CustomColumnFetchEventArgs>(FeatureSource_CustomColumnFetch);        }        public void InitDictionaries(string LinkFieldName, DataTable ExternalDataBase, params string[] CustomColumnNames)        {            ShapeFileFeatureLayer.BuildRecordIdColumn(this.ShapePathFileName, "RecId", BuildRecordIdMode.DoNotRebuild);            FeatureSource.Open();            DataTable dataTable = FeatureSource.ExecuteQuery(string.Format("select RecId, {0} from {1}", LinkFieldName, Path.GetFileNameWithoutExtension(this.ShapePathFileName)));            foreach (DataRow dataRow in dataTable.Rows)            {                idLinkFieldDictionary.Add(dataRow["RecId"].ToString(), dataRow[LinkFieldName].ToString());            }            foreach (string customColumnName in CustomColumnNames)            {                Dictionary<string, string> dictionary = new Dictionary<string, string>();                // Here is you own code getting the dictionary from the LinkField To The customColumn you want                // This sample is using DataTable as the external database. You can change it to consume your own databases.                foreach (DataRow dataRow in ExternalDataBase.Rows)                {                    dictionary.Add(dataRow[LinkFieldName].ToString(), dataRow[customColumnName].ToString());                }                customFieldsDictionaries.Add(customColumnName, dictionary);            }        }        void FeatureSource_CustomColumnFetch(object sender, CustomColumnFetchEventArgs e)        {            string LinkFieldValue = idLinkFieldDictionary[e.Id];            if (customFieldsDictionaries[e.ColumnName].ContainsKey(LinkFieldValue))            {                e.ColumnValue = customFieldsDictionaries[e.ColumnName][LinkFieldValue];            }        }    }// Here is how to use this class private void DisplayMap_Load(object sender, EventArgs e)        {            System.Diagnostics.Debug.WriteLine(WinformsMap.GetVersion());            winformsMap1.MapUnit = GeographyUnit.DecimalDegree;            CustomDataShapeFileFeatureLayer worldLayer = new CustomDataShapeFileFeatureLayer(@"..\..\SampleData\Data\Countries02.shp");            worldLayer.InitDictionaries("CNTRY_NAME", GetTestDataTable(), "History", "Population");            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.Country1("History");            //worldLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.Country1("Population");            worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;            winformsMap1.StaticOverlay.Layers.Add("WorldLayer", worldLayer);            winformsMap1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);            winformsMap1.CurrentExtent = new RectangleShape(-143.4, 109.3, 116.7, -76.3);            winformsMap1.Refresh();        }        private DataTable GetTestDataTable()        {            DataTable dataTable = new DataTable();            dataTable.Columns.Add("CNTRY_NAME");            dataTable.Columns.Add("History", Type.GetType("System.Int32"));            dataTable.Columns.Add("Population", Type.GetType("System.Int32"));            DataRow rowChina = dataTable.NewRow();            rowChina["CNTRY_NAME"] = "China";            rowChina["History"] = 5000;            rowChina["Population"] = 1300000000;            dataTable.Rows.Add(rowChina);            DataRow rowUSA = dataTable.NewRow();            rowUSA["CNTRY_NAME"] = "United States";            rowUSA["History"] = 250;            rowUSA["Population"] = 300000000;            dataTable.Rows.Add(rowUSA);            return dataTable;        }`

Here&nbsp;is&nbsp;the&nbsp;result:

 ![](https://community.thinkgeo.com/uploads/default/original/2X/d/d3ebb13d625e6eb238e77482cbc50125bc2a2097.png)

Thanks,

&nbsp;

Ben

---

<div class="post-metadata">

**Author:** ![jvandyk](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jvandyk](https://community.thinkgeo.com/u/jvandyk)\
**Post date:** [July 23, 2009, 9:20am UTC](https://community.thinkgeo.com/t/thematic-reports-based-on-an-external-database-query/419/5 "2009-07-23T09:20:17Z")

</div>

Thanks&nbsp;Ben,&nbsp;I&nbsp;needed&nbsp;to&nbsp;adjust&nbsp;it&nbsp;to&nbsp;fit&nbsp;my&nbsp;needs,&nbsp;but&nbsp;it&nbsp;appears&nbsp;to&nbsp;work&nbsp;perfectly.

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [July 23, 2009, 7:27pm UTC](https://community.thinkgeo.com/t/thematic-reports-based-on-an-external-database-query/419/6 "2009-07-23T19:27:24Z")

</div>

Thanks&nbsp;for&nbsp;your&nbsp;post!&nbsp;Jeremy!&nbsp;  
&nbsp;&nbsp;  
&nbsp;Let&nbsp;me&nbsp;know&nbsp;if&nbsp;any&nbsp;more&nbsp;questions!&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Yale
