read the value of contour from a shp file and display it

Topics: Data Access
Jul 14, 2009 at 6:19 AM

hi , I use a ocx to draw contour line and then I save it as shp file. After that I use the shapefile provider to read it and then paint it . And I wonder because I'm not very familiar with sharpmap and shp file, is there any way that I can get the value of the contour together from the file and display it with sharpmap? or should I do something when I save the origianl file as shp file ?

 Thx in advance.


Jul 14, 2009 at 6:56 AM

Hi Ray_Z, the 0.9 shapefile provider works only with 2D coordinates Z and M values are ignored. As a worksaround you may need to save your contour data as an attribute in the dbf file hth jd

Jul 14, 2009 at 7:41 AM

Hi Jd,thx for your advice, so if I store the data in the dbf file can I use some function  in sharpmap to read it ?or I have to figure out some way other than sharpmap to read and display the data?

Thx in advance.


Jul 14, 2009 at 8:01 AM

If it is in the dbf file you can use the ShapefileProvider to populate a FeatureDataSet/FeatureDataTable from that you can read the attributes in the same way as you would read any other DataTable, also the attributes are available within custom themes so when rendering you can style the feature based on the attribute.. hth jd

Jul 14, 2009 at 9:29 AM

Hi jd, Many thanks , I wil try it out soon and wil let you know the results.

Jul 15, 2009 at 5:56 AM

Hi jd, I try it. This is what I've done.

first i use

layContour.DataSource = new SharpMap.Data.Providers.ShapeFile(@"E:\ContourOCX\temp.shp");

to read the shp file;

then i use

                    SharpMap.Data.FeatureDataTable source = createDataTableSource();
                    SharpMap.Data.Providers.GeometryProvider gfp = new SharpMap.Data.Providers.GeometryProvider(source);
                    VectorLayer layer = myMap.GetLayerByName("layContour") as VectorLayer;
                    layer.DataSource = gfp;

to get the layContour another datasource and the createDataTableSource() which i digged from the forum is to read the feature data .

private  SharpMap.Data.FeatureDataTable createDataTableSource()
            SharpMap.Data.FeatureDataTable source = new SharpMap.Data.FeatureDataTable();

            source.Columns.AddRange(new DataColumn[] {
                new DataColumn("oid", typeof(uint)),
                new DataColumn("x", typeof(double)),
                new DataColumn("y", typeof(double)),
            new DataColumn("value", typeof(double))});
            int lineCount = 0;
            double x1 = 0, y1 = 0, value = 0;
            ContourOCX1.GetLineCount(ref lineCount);
            for (int i = 0; i < lineCount; i++)

                ContourOCX1.GetCtrlPoint(i, 0, ref x1, ref y1, ref value);
                SharpMap.Data.FeatureDataRow row = source.NewRow();
                row["oid"] = i;
                row["x"] = x1;
                row["y"] = y1;
                row["value"] = value;
                row.Geometry = new SharpMap.Geometries.Point(x1, y1);

            return source;




but somehow I still can not get the result , instead i get the exception  " specified method is not supported ". By the way , i can't quite catch up with your words"use the ShapefileProvider to populate a FeatureDataSet/FeatureDataTable", do you mean that the shapefileprovider can also get the dbf data and store it in FeatureDataSet/FeatureDataTable?


Thx in advance.


Jul 15, 2009 at 8:00 AM

Hi,I've got another idea. I checked the DemoWebSite and found that the simple.aspx, more precisely the layers in that demo match my requirement perfectly. I read the code, but I still got confused by the labellayer, it seems that if you use"layCityLabel.DataSource = layCities.DataSource;" to assign the source and the label will be produced automaticly, but I try it without that effect. If I want that work, what should I save to the shp or dbf files?

Jul 15, 2009 at 8:49 AM

Hi Ray, the example just sets the the vector layer and the label  layer to use the same datasource - it means that the provider must support attributes  (for the labeling most providers are ok - except a GeometryProvider which does not support attributes - GeometryFeatureProvider is ok)

the demo code is equivalent to:

SomeProvider provider = new SomeProvider();//any suitable provider

vectorLayer.DataSource = provider;

labelLayer.DataSource = provider;

no magic ;)

So assuming you wish to display the contour line geometries and a label denoting contour height  you will need to provide a shapefile with the geometries and a corresponding dbf containing the contour height (and / or any other info you wish to display)

hth jd

Jul 15, 2009 at 9:20 AM

Hi jd, thx for ur prompt response ,yes, u assume right. And based on your advice and the demo, i got it. here is my code.

                    LabelLayer layContourLabel = new LabelLayer("Contour labels");
                    layContourLabel.DataSource = layContour.DataSource;//use the same datasource as the contour layer and the source defination is "layContour.DataSource = new //SharpMap.Data.Providers.ShapeFile(@"E:\ContourOCX\temp.shp");"
                    layContourLabel.Enabled = true;
                    layContourLabel.LabelColumn = "MinValue";//this is the column in the dbf file you want to read from
                    layContourLabel.Style = new LabelStyle();
                    layContourLabel.Style.ForeColor = Color.Black;
                    layContourLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
                    layContourLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left;
                    layContourLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom;
                    layContourLabel.Style.Offset = new PointF(3, 3);
                    layContourLabel.Style.Halo = new Pen(Color.Yellow, 2);
                    layContourLabel.TextRenderingHint = TextRenderingHint.AntiAlias;
                    layContourLabel.SmoothingMode = SmoothingMode.AntiAlias;
                    layContourLabel.SRID = 4326;

and it's done . Frankly i reused most of the code from the demo but it works.

Thx for ur help again.