ThinkGeo.com    |     Documentation    |     Premium Support

Province shapefile color change from database table

Hi,

First of all i wants to thanks to you from your help i done my 80% job now i have another issue which i wants to resolved.

i am going to tell my scenario which is the following

1 i have province shapefile display on my map

2 i have database table which have the following columns

province_name,active

in province name should like name like my shape file. and in active column data will be yes or no

3 i wants to match my shapefile province name with my table province_name and if in active column is there is yes than color that province

for example 

there is a province called ‘kabul’ if in my table column of active if value is yes than kabul all background color change. can you help me in this how should i done this how to match my database table column with my shapefile column ?


Hi Jaja,



I think the DrawingFeatures event may helps on your case, some codes as following:


ShapeFileFeatureLayer newLayer = new ShapeFileFeatureLayer();
            newLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
            newLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.RequiredColumnNames.Add(“Province_Name”);
            newLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.RequiredColumnNames.Add(“active”);
            newLayer.DrawingFeatures += newLayer_DrawingFeatures;
             
            Map1.CurrentExtent = new RectangleShape(-155.733, 95.60, 104.42, -81.9);
            WorldMapKitWmsWpfOverlay worldOverlay = new WorldMapKitWmsWpfOverlay();
            Map1.Overlays.Add(“WMK”, worldOverlay);
            Map1.Refresh(); 
        }
 
        void newLayer_DrawingFeatures(object sender, DrawingFeaturesEventArgs e)
        {
            foreach (Feature feature in e.FeaturesToDraw)
            {
                string provinceName = feature.ColumnValues[“Province_Name”];
                // Do the search in database
                if (true)
                {
                    // change the background color by zoomlevel
                    ZoomLevel zoomlevel = e.DrawingZoomLevel;
                    zoomlevel.DefaultAreaStyle = …
                }
            }
        }

In above codes, don’t forget to add the highlight lines codes, as adding the two required column will make them include in the DrawingFeatures feature’s column.



Please let us know if any questions.



Thanks,



Troy

hi i did this with the following code 
 ShapeFileFeatureLayer province = new ShapeFileFeatureLayer(MapPath("~/Layer/afghanistanprovince.shp"));






    
        province.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.City1;
      
            province.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = new TextStyle("Prov_Name", new GeoFont("Verdana", 9),
                new GeoSolidBrush(GeoColor.StandardColors.Blue));
            province.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.RequiredColumnNames.Add("Prov_Name");
           province.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = new AreaStyle(new GeoPen(new GeoColor(255, GeoColor.SimpleColors.Blue)));
           // province.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.HaloPen = new GeoPen(GeoColor.StandardColors.White, 2);
         province.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
       



            Map1.StaticOverlay.Layers.Add("Province", province);
            province.DrawingFeatures += newLayer_DrawingFeatures;
        } 
        }
 
        void newLayer_DrawingFeatures(object sender, DrawingFeaturesEventArgs e)
        {
            foreach (Feature feature in e.FeaturesToDraw)
            {
                string provinceName = feature.ColumnValues["Prov_Name"];
                // Do the search in database



                if (provinceName == "Kabul")
                    {
                    Zoom Level zoom level = e. DrawingZoomLevel;
                    // change the background color by zoomlevel
                    zoomlevel.DefaultAreaStyle = new AreaStyle(AreaStyles.Country1.OutlinePen,
                        new GeoSolidBrush(GeoColor.SimpleColors.Yellow));



                    //  zoomlevel.DefaultAreaStyle = "";
                }
                //else
                //{
                //    ZoomLevel zoomlevel = e.DrawingZoomLevel;
                //    zoomlevel.DefaultAreaStyle = new AreaStyle(new GeoPen(new GeoColor(255, GeoColor.SimpleColors.Blue)));
                //}
            }
        }

but i have some issue with this

      if (provinceName == "Kabul")
                    {}

this condition is running more than one time and also my all province color is yellow i dont know why can you please modify my code. thanks


hi i try this 

ShapeFileFeatureLayer province = new ShapeFileFeatureLayer(MapPath("~/Layer/afghanistanprovince.shp"));

       province.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;

        province.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = new TextStyle("Prov_Name", new GeoFont("Verdana", 9),

          new GeoSolidBrush(GeoColor.StandardColors.Blue));

            province.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.RequiredColumnNames.Add("Prov_Name");

          province.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = new AreaStyle(new GeoPen(new GeoColor(255, GeoColor.SimpleColors.Blue)));



        province.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;



        province.DrawingFeatures += newLayer_DrawingFeatures;

            Map1.StaticOverlay.Layers.Add("Province", province);

           

           

        } 

        }

 

        void newLayer_DrawingFeatures(object sender, DrawingFeaturesEventArgs e)

        {

            ZoomLevel zoomlevel = e.DrawingZoomLevel;

            foreach (Feature feature in e.FeaturesToDraw)

            {

                string provinceName = feature.ColumnValues["Prov_Name"];

          



                if (provinceName == "Kabul")

                    {

                    

                 

        

                        zoomlevel.DefaultAreaStyle = new AreaStyle(new GeoPen(new GeoColor(255, GeoColor.SimpleColors.Yellow)));

                    zoomlevel.ApplyUntilZoomLevel=ApplyUntilZoomLevel.Level20;

           

                }

                else

                {

                   

                    zoomlevel.DefaultAreaStyle = new AreaStyle(new GeoPen(new GeoColor(255, GeoColor.SimpleColors.Red)));

                    zoomlevel.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

                }

            }

        }

why my    if (provinceName == "Kabul") this condition run multiple time i have only one kabul province in my shape file also my shapefile defaultareastyle display of else part like this

else

                {

                   

                    zoomlevel.DefaultAreaStyle = new AreaStyle(new GeoPen(new GeoColor(255, GeoColor.SimpleColors.Red)));

                    zoomlevel.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

                }

                    why is that please tell me reason thanks

basically i wants to color province with different color for example 

province a background should display yellow

and province b background should display green

can i do this ?

Hi Raja, 
  
 As below is a simple sample from your code which can be run in our HowDoISample, wish that’s helpful. 
  
 using System;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WebEdition;

namespace CSSamples.Samples
{
    public partial class DisplayASimpleMap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"));
                Map1.CurrentExtent = new RectangleShape(-125, 72, 50, -46);
                Map1.MapUnit = GeographyUnit.DecimalDegree;

                ShapeFileFeatureLayer newLayer = new ShapeFileFeatureLayer(MapPath("~/SampleData/world/cntry02.shp"));
                newLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
                newLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.RequiredColumnNames.Add(“LONG_NAME”);
                newLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
                newLayer.DrawingFeatures += newLayer_DrawingFeatures;


                InMemoryFeatureLayer highlightLayer = new InMemoryFeatureLayer();
                highlightLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = new AreaStyle(AreaStyles.Country1.OutlinePen,
                        new GeoSolidBrush(GeoColor.SimpleColors.Yellow));
                highlightLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;


                LayerOverlay overlay = new LayerOverlay(“YourOverlay”);
                overlay.Layers.Add(“featureLayer”, newLayer);
                overlay.Layers.Add(“highLightLayer”, highlightLayer);

                Map1.CustomOverlays.Add(overlay);

            }
        }

        void newLayer_DrawingFeatures(object sender, DrawingFeaturesEventArgs e)
        {
            InMemoryFeatureLayer highlightLayer = (Map1.CustomOverlays[“YourOverlay”] as LayerOverlay).Layers[“highLightLayer”] as InMemoryFeatureLayer;
            highlightLayer.InternalFeatures.Clear();

            foreach (Feature feature in e.FeaturesToDraw)
            {
                string longname = feature.ColumnValues[“LONG_NAME”];
                // Do the search in database

                if (longname.Contains(“B”)) // please modify the condition to make sure you only add the features active from database
                {
                    highlightLayer.InternalFeatures.Add(feature);
                }
            }
        }
    }
}
 
 
  
 Regards, 
  
 Don

hi don yes it is working fine 

but i have another question and i am hesitating to ask it (because you always help me in my all scenario now i am ashamed to ask again!)

in my database table value can be repeated and multiples for example a province ‘kabul’ can have multiples entries like kabul can active more than one i am going to give you example here please read it

i am going to give you count of 3 province for example

kabul active count is 20

another province active count is 10

another province active count is 5

can i display color density on my database count ? for example kabul active count is 20 so its color density should display high and after that so on counts go low so density goes low?

thanks again anyway your form is really helping and great

please reply me how do i used classbreakstyle in inmemoryfeaturelayer or may be you provide me a better way to change color on my count rows thanks

hi i wants to do like 

samples.thinkgeo.com/webedition/howdoisamples/

but how i do this with sql table thanks please reply me soon

Hi Raja, 
  
 For ClassBreakStyle you can found many related topic in our forum, for example this one: 
  
 thinkgeo.com/forums/MapSuite/tabid/143/aft/5135/Default.aspx 
  
 And I think you can query the result from SQLserver after you copy features to inmemory feature layer, you can caeate a special column in the feature then assign the queried result into it. 
  
 Wish that’s helpful. 
  
 Regards, 
  
 Don 
  


hi i check the form but not very helpful and i also did this with like that 

 foreach (Feature feature in e.FeaturesToDraw)
            {
               
ClassBreakStyle classBreakStyle = new ClassBreakStyle("Prov_Name");
                            highlightLayer.InternalFeatures.Add(feature);
                            classBreakStyle.ClassBreaks.Add(new ClassBreak(0,
         new AreaStyle(AreaStyles.Country1.OutlinePen,
                                   new GeoSolidBrush(GeoColor.SimpleColors.Yellow))));



    



                            highlightLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle =
                                new AreaStyle(AreaStyles.Country1.OutlinePen,
                                    new GeoSolidBrush(GeoColor.SimpleColors.Yellow));
              
                  highlightLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(classBreakStyle);






                            highlightLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;


and it not working can you please provide me sample data that data is coming from database table, which help me in this scenario.

i am going to give you count of 3 province for example

kabul database count is 20 than color it yellow

another province  database count active count is 10 than color it red
another province  database count active count is is 5 color it yellow dont worry about my database code you just provide me sample code with just example i can done database count and query part but i am stuck on thematic map part which i wants from you just provide me sample code which comes from just one table and change the color and that is my last job everything is done just waiting for your help than i am hoping to deploy my project live thanks



Hi Raja, 
  
 In fact we have a HowDoISamples named DrawThematicFeatures is very helpful for use ClassBreakStyle. 
  
 I did small change for previous code, please let me know whether that works for you.  
  
 InMemoryFeatureLayer highlightLayer = new InMemoryFeatureLayer();
            
            // Draw thematic features
            ClassBreakStyle classBreakStyle = new ClassBreakStyle("POP_CNTRY");
            classBreakStyle.ClassBreaks.Add(new ClassBreak(double.MinValue, AreaStyles.Grass1));
            classBreakStyle.ClassBreaks.Add(new ClassBreak(10, AreaStyles.Evergreen2));
            classBreakStyle.ClassBreaks.Add(new ClassBreak(20, AreaStyles.Evergreen1));
            classBreakStyle.ClassBreaks.Add(new ClassBreak(30, AreaStyles.Crop1));
            classBreakStyle.ClassBreaks.Add(new ClassBreak(50, AreaStyles.Forest1));


            highlightLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(AreaStyles.Country1);
            highlightLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
            highlightLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(classBreakStyle);


  void newLayer_DrawingFeatures(object sender, DrawingFeaturesEventArgs e)
        {
            InMemoryFeatureLayer highlightLayer = (Map1.CustomOverlays["YourOverlay"] as LayerOverlay).Layers["highLightLayer"] as InMemoryFeatureLayer;
            highlightLayer.InternalFeatures.Clear();

            // Query on database

            DataTable dt;

           

            foreach (Feature feature in e.FeaturesToDraw)
            {
                foreach (DataRow dr in dt)
                {
                    if (dr["featureName"] == feature.ColumnValues["Feature_Name"])
                    {
                        feature.ColumnValues.Add("classBreakValue", dr["value"]);
                    }
                    highlightLayer.InternalFeatures.Add(feature);
                }
            }
        }
 
  
 Regards, 
  
 Don

hi thanks that is very kind and yes it is working.

Raja, 
  
 Please let us know if any other questions. 
  
 Thanks, 
  
 Troy