This project is read-only.
An example of automatic label generation for US states shapefile with subsequent creation of visualization categories by Area field.

/// <summary>
/// Displays labels for USA states shapefile
/// </summary>
private void button10_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    MapWinGIS.Shapefile sf = new Shapefile();
    dlg.Filter = sf.CdlgFilter;
    
    if (dlg.ShowDialog() == DialogResult.OK)    // choosing states.shp
    {
        if (sf.Open(dlg.FileName, null))
        {
            // this setting will be used by all categories
            sf.Labels.FrameVisible = true;  

            // 1 - [StateName] field; true - LargestPartOnly parameter
            int count = sf.GenerateLabels(1, tkLabelPositioning.lpCentroid, true);  
            
            if (count == 0)
            {
                // the major source of error is invalid field index
                MessageBox.Show("No labels were generated: " + sf.get_ErrorMsg(sf.LastErrorCode));
            }
            else
            {
                // GenerateCategories routine adds categories and creates expressions for them
                // 0 - [Area] field; 5 - number of categories
                if (sf.Labels.GenerateCategories(0, tkClassificationType.ctNaturalBreaks, 5))   
                {
                    // here the expressions are analyzed and Label.Category property is set
                    sf.Labels.ApplyCategories();

                    // applying color scheme
                    MapWinGIS.ColorScheme scheme = new ColorScheme();
                    scheme.SetColors2(tkMapColor.Orange, tkMapColor.LemonChiffon);  // there are 3 more methods
                    
                    // colors can be applied to font, frame background (see last parameter)
                    sf.Labels.ApplyColorScheme2(tkColorSchemeType.ctSchemeGraduated, scheme, tkLabelElements.leFrameBackground);
                    
                    // applying varaible font size
                    int minSize = 10, maxSize = 14;
                    for (int i = 0; i < sf.Labels.NumCategories; i++)
                    {
                        sf.Labels.get_Category(i).FontSize = minSize + (maxSize - minSize) * i/(sf.Labels.NumCategories - 1);
                    }
                }
                else
                {
                    // the major source of error is invalid field index
                    MessageBox.Show("Failed to create categories: " + sf.get_ErrorMsg(sf.LastErrorCode));
                }
            }
            
            int handle = axMap1.AddLayer(sf, true);
            axMap1.ZoomToLayer(handle);
        }
        else
        {
            // probably corrupted shapefile
            MessageBox.Show("Failed to open shapefile: " + sf.get_ErrorMsg(sf.LastErrorCode));
        }
    }
}


Results should be like this:

Last edited Apr 10, 2011 at 2:14 PM by sleschinski, version 13

Comments

No comments yet.