DotSpatial shape file & Using Labeling of MapWindow 6.0 (Orlander): How to use Feature Labeler to label 2 lines?

Mar 14, 2011 at 5:36 PM

Hi all,

I used the Microsoft Excel 2007 (see the table below) and DotSpatial-Microsoft C# 2007 Express to created a shapefile of 8 points, a shapefile of 2 lines, and a shapefile of a polygon:

Id LocationID   X Y Benzene Elevation
1 EB1 -77.01507 38.870909 1.1  
2 EB2 -77.01507 38.870745 0.2  
3 EB3 -77.01507 38.870615 0.09  
4 EB4 -77.01518 38.870868 0.5  
5 EB5 -77.01531 38.87071 11.7  
6 EB6 -77.01535 38.870614 0.06  
7 EB7 -77.01518 38.870744     ND  
8 EB8 -77.01519 38.870631 111  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Projections;
using DotSpatial.Symbology;
using DotSpatial.Topology;

namespace JiriPtsLinesPolygonFtMN8Pts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void pointsToolStripMenuItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string excelFileName = "c:\\DotSpatial\\FtMcNair8PtsBenzene.xlsx";
            string sheetName = "FtMcNair";

            //this is the connection string to the xlsx file (excel2007)
            string connstr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";", excelFileName);
            using (OleDbConnection conn = new OleDbConnection(connstr))
            {
                string strSQL = string.Format("SELECT * FROM [{0}$]", sheetName);

                OleDbCommand cmd = new OleDbCommand(strSQL, conn);
                System.Data.DataSet ds = new System.Data.DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                da.Fill(ds);
                DataTable excelTable = ds.Tables[0];

                //creating the point features set
                DotSpatial.Data.FeatureSet fs = new DotSpatial.Data.FeatureSet(FeatureType.Point);
                //set the projection to WGS1984
                fs.Projection = KnownCoordinateSystems.Geographic.World.WGS1984;

                //copy the attribute table column names from the excel table
                fs.DataTable = excelTable.Clone();

                //populate the point features
                foreach (DataRow row in excelTable.Rows)
                {
                    //get x, y
                    double x = Convert.ToDouble(row["x"]);
                    double y = Convert.ToDouble(row["y"]);

                    //creates a new coordinate 
                    Coordinate c = new Coordinate(x, y);
                    //passes the coordinate to a new point
                    DotSpatial.Topology.Point p = new DotSpatial.Topology.Point(c);

                    DotSpatial.Data.IFeature currentFeature = fs.AddFeature(p);

                    //add the cell values for each row
                    for (int i = 0; i < excelTable.Columns.Count; i++)
                    {
                        currentFeature.DataRow[i] = row[i];
                    }
                }
                //save to a shapefile
                string pointFileName = @"c:\temp\ftMN8PtsExcel-Benz-Elev.shp";
                fs.SaveAs(pointFileName, true);
                //add shapefile to map
                // map1.AddLayer(pointFileName);
            }

        }

        private void linesToolStripMenuItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string excelFileName = "c:\\DotSpatial\\FtMcNair8PtsBenzene.xlsx";
            string sheetName = "FtMcNair";

            //this is the connection string to the xlsx file (excel2007)
            string connstr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";", excelFileName);
            using (OleDbConnection conn = new OleDbConnection(connstr))
            {
                string strSQL = string.Format("SELECT * FROM [{0}$]", sheetName);

                OleDbCommand cmd = new OleDbCommand(strSQL, conn);
                System.Data.DataSet ds = new System.Data.DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                da.Fill(ds);
                DataTable excelTable = ds.Tables[0];

                //creating the line feature set
                DotSpatial.Data.FeatureSet fs = new DotSpatial.Data.FeatureSet(FeatureType.Line);
                //set the projection to WGS1984
                fs.Projection = KnownCoordinateSystems.Geographic.World.WGS1984;

                //add a 'name' attribute
                fs.DataTable.Columns.Add(new DataColumn("name", typeof(string)));

                //populate the line one, connecting points #1, #2, #3
                List<Coordinate> coordList1 = new List<Coordinate>();
                //point #1
                DataRow row1 = excelTable.Rows[0];
                double x1 = Convert.ToDouble(row1["x"]);
                double y1 = Convert.ToDouble(row1["y"]);
                coordList1.Add(new Coordinate(x1, y1));
                //point #2
                DataRow row2 = excelTable.Rows[1];
                double x2 = Convert.ToDouble(row2["x"]);
                double y2 = Convert.ToDouble(row2["y"]);
                coordList1.Add(new Coordinate(x2, y2));
                //point #3
                DataRow row3 = excelTable.Rows[2];
                double x3 = Convert.ToDouble(row3["x"]);
                double y3 = Convert.ToDouble(row3["y"]);
                coordList1.Add(new Coordinate(x3, y3));
                //create line #1
                LineString line1 = new LineString(coordList1);
                IFeature feature1 = fs.AddFeature(line1);
                //set attributes of line one
                feature1.DataRow["name"] = "LINE 1";

                //populate the line two, connecting #1, #4, #5, #6
                List<Coordinate> coordList2 = new List<Coordinate>();
                //point #1
                coordList2.Add(new Coordinate(x1, y1));
                //point #4
                DataRow row4 = excelTable.Rows[3];
                double x4 = Convert.ToDouble(row4["x"]);
                double y4 = Convert.ToDouble(row4["y"]);
                coordList2.Add(new Coordinate(x4, y4));
                //point #5
                DataRow row5 = excelTable.Rows[4];
                double x5 = Convert.ToDouble(row5["x"]);
                double y5 = Convert.ToDouble(row5["y"]);
                coordList2.Add(new Coordinate(x5, y5));
                //point #6
                DataRow row6 = excelTable.Rows[5];
                double x6 = Convert.ToDouble(row6["x"]);
                double y6 = Convert.ToDouble(row6["y"]);
                coordList2.Add(new Coordinate(x6, y6));
                //create line #2
                LineString line2 = new LineString(coordList2);
                IFeature feature2 = fs.AddFeature(line2);
                //set attributes of line one
                feature2.DataRow["name"] = "LINE 2";

                //save to a shapefile
                string lineFileName = @"c:\temp\ftMN2LinesExcel-Benz-Elev.shp";
                fs.SaveAs(lineFileName, true);
                //add shapefile to map
                //map1.AddLayer(lineFileName);
            }

        }

        private void polygonsToolStripMenuItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string excelFileName = "c:\\DotSpatial\\FtMcNair8PtsBenzene.xlsx";
            string sheetName = "FtMcNair";

            //this is the connection string to the xlsx file (excel2007)
            string connstr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";", excelFileName);
            using (OleDbConnection conn = new OleDbConnection(connstr))
            {
                string strSQL = string.Format("SELECT * FROM [{0}$]", sheetName);

                OleDbCommand cmd = new OleDbCommand(strSQL, conn);
                System.Data.DataSet ds = new System.Data.DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                da.Fill(ds);
                DataTable excelTable = ds.Tables[0];

                //creating the polygon feature set
                DotSpatial.Data.FeatureSet fs = new DotSpatial.Data.FeatureSet(FeatureType.Polygon);
                //set the projection to WGS1984
                fs.Projection = KnownCoordinateSystems.Geographic.World.WGS1984;

                //add a 'name' attribute
                fs.DataTable.Columns.Add(new DataColumn("name", typeof(string)));

                //populate the polygon one, connecting points #1, #2, #3, #8, #6, #5, #4 and #1
                List<Coordinate> coordList1 = new List<Coordinate>();
                //point #1
                DataRow row1 = excelTable.Rows[0];
                double x1 = Convert.ToDouble(row1["x"]);
                double y1 = Convert.ToDouble(row1["y"]);
                coordList1.Add(new Coordinate(x1, y1));
                //point #2
                DataRow row2 = excelTable.Rows[1];
                double x2 = Convert.ToDouble(row2["x"]);
                double y2 = Convert.ToDouble(row2["y"]);
                coordList1.Add(new Coordinate(x2, y2));
                //point #3
                DataRow row3 = excelTable.Rows[2];
                double x3 = Convert.ToDouble(row3["x"]);
                double y3 = Convert.ToDouble(row3["y"]);
                coordList1.Add(new Coordinate(x3, y3));
                //point #8
                DataRow row8 = excelTable.Rows[7];
                double x8 = Convert.ToDouble(row8["x"]);
                double y8 = Convert.ToDouble(row8["y"]);
                coordList1.Add(new Coordinate(x8, y8));
                //point #6
                DataRow row6 = excelTable.Rows[5];
                double x6 = Convert.ToDouble(row6["x"]);
                double y6 = Convert.ToDouble(row6["y"]);
                coordList1.Add(new Coordinate(x6, y6));
                //point #5
                DataRow row5 = excelTable.Rows[4];
                double x5 = Convert.ToDouble(row5["x"]);
                double y5 = Convert.ToDouble(row5["y"]);
                coordList1.Add(new Coordinate(x5, y5));
                //point #4
                DataRow row4 = excelTable.Rows[3];
                double x4 = Convert.ToDouble(row4["x"]);
                double y4 = Convert.ToDouble(row4["y"]);
                coordList1.Add(new Coordinate(x4, y4));
                //closing point #1
                coordList1.Add(new Coordinate(x1, y1));

                //create polygon #1
                Polygon polygon1 = new Polygon(coordList1);
                IFeature feature1 = fs.AddFeature(polygon1);
                //set attributes of polygon one
                feature1.DataRow["name"] = "POLYGON 1";

                //save to a shapefile
                string polygonFileName = @"c:\temp\ftMN1PolygonExcel-Benz-Elev.shp";
                fs.SaveAs(polygonFileName, true);
                //add shapefile to map
                //map1.AddLayer(polygonFileName);

            }
        }
    }
}
It worked nicely and I got the shapefiles of 8 points, 2 lines, and 1 polygon that are saved in my C:/Temp folder.  
Now, I want to label the 2 lines by using the Section 1.3.6. Labeling (on Page 27 of 59) of "Getting Started with MapWindow 6" written 
by Harold (Ted)  Dunsford, Jr., Mark Van Orden, and Jiri Kadle4c (October 2009).  I do not know how to write the Expression
for labeling the 2 lines!!!???  Please help and advise.
Thanks,
Scott Chang