Here is a simple code to generate shapefile categories (C#).

        /// <summary>
/// Creates categories for USA counties by [Area] field
/// </summary> private void btnSetCategories_Click(object sender, EventArgs e) { string filename = @"c:\Dev\Installation Projects\United States\Shapefiles\Counties.shp"; if (System.IO.File.Exists(filename) == false) { MessageBox.Show("File doesn't exist: " + filename); return; } MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile(); if (sf.Open(filename, null) == false) { MessageBox.Show("Failed to open shapefile: " + sf.get_ErrorMsg(sf.LastErrorCode)); return; } int fieldIndex = sf.Table.get_FieldIndexByName("Area"); if (fieldIndex == -1) { MessageBox.Show("No Area field was found"); return; } // adding to map int handle = axMap1.AddLayer(sf, true); // creating classification sf.Categories.Generate(fieldIndex, tkClassificationType.ctNaturalBreaks, 25); sf.Categories.ApplyExpressions(); // this call isn't needed in the latest versions, but can be required by some older ones // applying colors MapWinGIS.ColorScheme scheme = new ColorScheme(); scheme.SetColors2(tkMapColor.LightGreen, tkMapColor.Yellow); sf.Categories.ApplyColorScheme(tkColorSchemeType.ctSchemeGraduated, scheme); } }


Results should be like this:

Second example with more advanced ShapefileCategories.AddRange method (available from 04-July-2011):

        /// <summary>
        /// Creats categories for USA counties by [Area] field
        /// </summary>
        private void btnSetCategories_Click(object sender, EventArgs e)
        {
            string filename = @"c:\Dev\Installation Projects\United States\Shapefiles\Counties.shp";
            if (System.IO.File.Exists(filename) == false)
            {
                MessageBox.Show("File doewn't exist: " + filename);
                return;
            }

            MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile();
            if (sf.Open(filename, null) == false)
            {
                MessageBox.Show("Failed to open shapefile: " + sf.get_ErrorMsg(sf.LastErrorCode));
                return;
            }

            int fieldIndex = sf.Table.get_FieldIndexByName("Area");
            if (fieldIndex == -1)
            {
                MessageBox.Show("No Area field was found");
                return;
            }

            // adding to map
            int handle = axMap1.AddLayer(sf, true);
            
            // outlines are turned off
            sf.DefaultDrawingOptions.LineVisible = false;

            // adding first range
            sf.Categories.AddRange(fieldIndex, tkClassificationType.ctNaturalBreaks, 5, 1000.0, 25000.0);
            MapWinGIS.ColorScheme scheme = new ColorScheme();
            scheme.SetColors2(tkMapColor.LightGray, tkMapColor.White);      // this will provide light random colors in the next line
            
            // 0 and 4 are indices of categories, since 5 categories were added - from 0 to 4
            sf.Categories.ApplyColorScheme3(tkColorSchemeType.ctSchemeRandom, scheme, tkShapeElements.shElementFill, 0, 4);   

            // adding second range
            sf.Categories.AddRange(fieldIndex, tkClassificationType.ctEqualIntervals, 5, 0.0, 1000.0);
            scheme.SetColors2(tkMapColor.Black, tkMapColor.White);
            sf.Categories.ApplyColorScheme3(tkColorSchemeType.ctSchemeGraduated, scheme, tkShapeElements.shElementFill, 5, 9);

            // apply expresions should be called exlicitly
            sf.Categories.ApplyExpressions();
            
            // saving options to see categories desription in XML
            axMap1.SaveLayerOptions(handle, "categories_sample", true, "");
        }

 

Results should be like this:

Layer options in XML format:

<MapWinGIS OcxVersion="4.8" FileType="LayerFile" FileVersion="0" Description="">
  <Layer LayerType="Shapefile" LayerName="" LayerVisible="1" LayerKey="" Filename="">
    <ShapefileClass>
      <DefaultDrawingOptions FillColor="13946833" LineColor="9868950" />
      <ShapefileCategoriesClass>
        <ShapefileCategoryClass Name="1000 - 5.80k" Expression="[AREA] &gt;= 1000.000000 AND [AREA] &lt; 5800.000000">
          <ShapeDrawingOptionsClass FillColor="8421616" LineColor="9868950" />
        </ShapefileCategoryClass>
        <ShapefileCategoryClass Name="5.80k - 10.60k" Expression="[AREA] &gt;= 5800.000000 AND [AREA] &lt; 10600.000000">
          <ShapeDrawingOptionsClass FillColor="6326771" LineColor="9868950" />
        </ShapefileCategoryClass>
        <ShapefileCategoryClass Name="10.60k - 15.40k" Expression="[AREA] &gt;= 10600.000000 AND [AREA] &lt; 15400.000000">
          <ShapeDrawingOptionsClass FillColor="4231927" LineColor="9868950" />
        </ShapefileCategoryClass>
        <ShapefileCategoryClass Name="15.40k - 20.20k" Expression="[AREA] &gt;= 15400.000000 AND [AREA] &lt; 20200.000000">
          <ShapeDrawingOptionsClass FillColor="2137083" LineColor="9868950" />
        </ShapefileCategoryClass>
        <ShapefileCategoryClass Name="20.20k - 25.00k" Expression="[AREA] &gt;= 20200.000000 AND [AREA] &lt;= 25000.000000">
          <ShapeDrawingOptionsClass FillColor="42495" LineColor="9868950" />
        </ShapefileCategoryClass>
        <ShapefileCategoryClass Name="0 - 200" Expression="[AREA] &gt;= 0.000000 AND [AREA] &lt; 200.000000">
          <ShapeDrawingOptionsClass FillColor="0" LineColor="9868950" />
        </ShapefileCategoryClass>
        <ShapefileCategoryClass Name="200 - 400" Expression="[AREA] &gt;= 200.000000 AND [AREA] &lt; 400.000000">
          <ShapeDrawingOptionsClass FillColor="4144959" LineColor="9868950" />
        </ShapefileCategoryClass>
        <ShapefileCategoryClass Name="400 - 600" Expression="[AREA] &gt;= 400.000000 AND [AREA] &lt; 600.000000">
          <ShapeDrawingOptionsClass FillColor="8355711" LineColor="9868950" />
        </ShapefileCategoryClass>
        <ShapefileCategoryClass Name="600 - 800" Expression="[AREA] &gt;= 600.000000 AND [AREA] &lt; 800.000000">
          <ShapeDrawingOptionsClass FillColor="12566463" LineColor="9868950" />
        </ShapefileCategoryClass>
        <ShapefileCategoryClass Name="800 - 1000" Expression="[AREA] &gt;= 800.000000 AND [AREA] &lt;= 1000.000000">
          <ShapeDrawingOptionsClass FillColor="16777215" LineColor="9868950" />
        </ShapefileCategoryClass>
      </ShapefileCategoriesClass>
      <LabelsClass Generated="0" Positioning="1" />
      <ChartsClass />
    </ShapefileClass>
  </Layer>
</MapWinGIS>

To apply these options:
        /// <summary>
        /// To apply the same options on next loading
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            string filename = @"c:\Dev\Installation Projects\United States\Shapefiles\Counties.shp";
            MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile();
            sf.Open(filename, null);
            int handle = axMap1.AddLayer(sf, true);
            string description = "";
            axMap1.LoadLayerOptions(handle, "categories_sample", ref description);
        }

Last edited Jul 4, 2011 at 3:14 PM by sleschinski, version 1

Comments

zzhi Dec 29, 2011 at 1:37 AM 
Categories.Generate(,,,,)
axMap1.RemoveAllLayers();
didn't delete file of .dbf ?