rendering a thematic layer

Topics: Data Access
Feb 26, 2009 at 8:47 PM
Edited Feb 26, 2009 at 9:09 PM
I am working on sharp map v 0.9 in a desktop application with data provider as shapefiles, the attribute table of the shape files contains a unique key, with that it makes the join with an external data base.The problem is when i want to highlight some polygons in the shapefile with string values of an external data base.

The custom theme class it seems that doesn't do the work; there is another way to defining your own thematic rendering without using a custom get-style-delegate?. Or using SharpMap.Data.FeatureDataRow to fill.style individually, Row per row that match the given condition of the external data base.
Coordinator
Feb 27, 2009 at 11:25 AM
Hi betoens, I am not sure of the problem, can you elaborate further? As far as I can tell you want to highlight particular features which have a particular attribute - this is possible with the get style delegate - remember the method you use can be an instance member of a class which can call out or store extra information if necessary.. so perhaps you could initialize your class with a list of ids to highlight, or if necessary you could call the database within the get style delegate (though this would be unadvisable on large datasets).. hth jd
Feb 27, 2009 at 11:06 PM
Hi Johndiss, thank you for the replay. ok i have this:

 for (int i = 0; i < Rows of the shape layer ; i++)
            {
                for (int j = 0; j < rows of the database; j++)
                {

                    SharpMap.Data.FeatureDataRow selectedFeature = HG_colLayer.DataSource.GetFeature(Convert.ToUInt32(i)) as SharpMap.Data.FeatureDataRow;
                    if (Convert.ToString(selectedFeature[index column]) == Convert.ToString(DB_table.Rows[j]["index column"]))
                    {
                       //i want to highlight all the polygons of the shape layer that exist in the database table, but first a need to know if exist, then call get style delegate to color that geometry only.
                       SharpMap.Rendering.Thematics.CustomTheme myTheme = new SharpMap.Rendering.Thematics.CustomTheme(Get_Identificar);
                       HG_colLayer.Theme = myTheme;
                       //
                      //but every time i use get style delegate it it need to refresh the main map to apply the action. how i save all the geomtrys colored ? for later do the MainMap.Refresh();
                      //
                    }
                }

            }
MainMap.Refresh();

private SharpMap.Styles.VectorStyle Get_Identificar(SharpMap.Data.FeatureDataRow row)
        {
            SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();
            if (Convert.ToString(row[index column]) == value of the external database)
            {
                style.Fill = Brushes.Yellow;
                return style;
            }
            else
            {
                style.Fill = Brushes.LightSalmon;
                style.Outline = Pens.Tomato;
                style.EnableOutline = true;
                return style;
            }
        }

i hope i got my self clear.
Coordinator
Feb 28, 2009 at 4:01 PM

Hi betoens, If I follow the problem correctly the following should work.. hth jd

 

using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using SharpMap;
using SharpMap.Data;
using SharpMap.Data.Providers;
using SharpMap.Layers;
using SharpMap.Rendering.Thematics;
using SharpMap.Styles;

namespace Demo
{
    public class DatabaseStyler<TDbField>
    {
        public VectorStyle LowlightStyle { get; protected set; }
        public VectorStyle HighlightStyle { get; protected set; }
        public string ColumnName { get; protected set; }

        private readonly List<TDbField> _highlightIds = new List<TDbField>();

        public DatabaseStyler(VectorStyle lowlight, VectorStyle highlight, string compareColumnName)
        {
            LowlightStyle = lowlight;
            HighlightStyle = highlight;
            ColumnName = compareColumnName;

            PopulateIds();
        }

        private void PopulateIds()
        {
            //You can populate the test ids any way you like; here we use a database
            using (IDbConnection conn = new SqlConnection("connctionstring"))
            {
                using (IDbCommand cmd = conn.CreateCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = "select ID from table where ....";
                    cmd.CommandType = CommandType.Text;
                    conn.Open();
                    using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            HighlighIds.Add((TDbField)reader[0]);
                        }
                    }
                }
            }
        }

        public VectorStyle GetStyle(FeatureDataRow fdr)
        {
            return _highlightIds.Contains((TDbField)fdr[ColumnName])
                ? HighlightStyle
                : LowlightStyle;
        }

        public IList<TDbField> HighlighIds
        {
            get { return _highlightIds; }
        }
    }

    public class Usage
    {
        public void Demonstate()
        {
            VectorStyle lowlight = new VectorStyle();//use default
            VectorStyle highlight = new VectorStyle
                                   {
                                       Line = new Pen(Color.Red, 3)
                                   };

            DatabaseStyler<string> styler = new DatabaseStyler<string>(lowlight, highlight, "importantColumnName");

            Map map = new Map(new Size(800, 600));
           
            VectorLayer polygons = new VectorLayer( "polygons", new ShapeFile("myshapefile.shp"));
            polygons.Theme = new CustomTheme(styler.GetStyle);//we use the GetStyle method of our instance of DatabaseStyler to delegate to
            map.Layers.Add(polygons);

            .............

        }
    }

}

Mar 2, 2009 at 11:03 PM
Edited Mar 2, 2009 at 11:06 PM
Thank you very much johndiss, it works really fine!