This project is read-only.

trouble coloring shapes

Jan 12, 2012 at 2:29 PM

hi,

i'm trying to color shapes by attribute value and can't get it to work...

 

 public void SetAttributeColor(int hnd, string AttributeName)
        {
            int fieldIndex = -1;
            
            Shapefile sf;
            string attValue = string.Empty;
            ColorIterator.Colors col;
            Dictionary<string,Color> values = new Dictionary<string,Color>();
           
            try
            {
                axMap1.set_ShapeLayerDrawFill(hnd, true);
                sf = axMap1.get_Shapefile(hnd);
                if(sf==null)return;
                fieldIndex=sf.Table.get_FieldIndexByName(AttributeName);
                if(fieldIndex<0)return;
                col=new ColorIterator.Colors();
               
                for (int i = 0; i < sf.NumShapes-1; i++)
                {
                  attValue=(string)sf.get_CellValue(fieldIndex, i);
                  if (values.ContainsKey(attValue))
                  {
                      axMap1.set_ShapeFillColor(hnd, i, ColorIterator.Colors.ColorToUInt(values[attValue]));
                      
                  }
                  else
                  {
                      col.MoveNext();
                      values.Add(attValue,(Color) col.Current);
                      axMap1.set_ShapeFillColor(hnd, i, ColorIterator.Colors.ColorToUInt(values[attValue]));
                  }
                  

                }
               
                axMap1.SetDrawingLayerVisible(hnd, true);
               axMap1.Redraw();
            }
            catch (Exception ex)
            {
                string str = string.Empty;
                do
                {
                    str += ex.Message +Environment.NewLine+ "***";
                    ex = ex.InnerException;
                    
                } while (ex!=null);
                MessageBox.Show(str);
            }

        }

any ideas?

Jan 20, 2012 at 1:17 AM
Edited Jan 20, 2012 at 1:19 AM

 

Hi eladme30,

Use Shapefile.Categories. Below is my code in VB, but I'm sure you can figure it out:

        With ShpLayer
            .Categories.Generate(0, MapWinGIS.tkClassificationType.ctUniquevalues, 2)
            .Categories.ApplyExpressions()
            For i = 0 To .NumShapes - 1
                Select Case .CellValue(.Table.FieldIndexByName("NAME"), i)
                    Case "ABC"
                        .Categories.Item(i).DrawingOptions.FillColor = RGB(239, 228, 176)
                        .Categories.Item(i).DrawingOptions.LineColor = RGB(222, 200, 95)
                    Case Else
                        .Categories.Item(i).DrawingOptions.FillColor = RGB(248, 242, 220)
                        .Categories.Item(i).DrawingOptions.LineColor = RGB(222, 200, 95)
                End If
                End Select
             Next
        End With