Write from data onto new layer with GDAL extension

Topics: SharpMap v0.9 / v1.x
Mar 14, 2009 at 9:38 PM
Compile problem using GDAL extensions when trying to write info from data (not from a shape file provider) to a new layer.  Would appreciate any advice, as well as being pointed to any documentation/tutorials explaining data providers and geometry, have searched, have not found anything.  relatively unskilled at c#, but have gottent things working ok in past prior to adding this new capability.

I have previously had success using the GDAL extensions to get a registered jpg/jpgw to display in a c# desktop application.  I now wish to add a new layer with data that will ultimately be derived from a database, but is I am currently simply trying to test with hard-wired data.  I have been following an example I obtained that was compiled with SharpMap v0.9 RC1, v 0.9.2781.25862, which does not make use of GDAL but does set a layer's data source as follows:

// code snippet that works, no compile errors
            
                List<SharpMap.Geometries.Geometry> nList = new List<SharpMap.Geometries.Geometry>();
                SharpMap.Layers.VectorLayer reachLayer = new VectorLayer("Reachlayer");
                reachLayer.DataSource = new SharpMap.Data.Providers.GeometryProvider(nList);
                foreach (CNavigationReach curReach in mySimulatedWorld.mReachCatalog.Values)
                {
                    List<SharpMap.Geometries.Point> lList = new List<SharpMap.Geometries.Point>();
                    lList.Add(new SharpMap.Geometries.Point(curReach.Origin.WorldUTMEasting, curReach.Origin.WorldUTMNorthing));
                    lList.Add(new SharpMap.Geometries.Point(curReach.Destination.WorldUTMEasting, curReach.Destination.WorldUTMNorthing));

                    SharpMap.Geometries.LineString line1 = new SharpMap.Geometries.LineString(lList);
                    nList.Add(line1);
                }
                reachLayer.Enabled = true;
                mapNetView.Map.Layers.Add(reachLayer);
 
// end working code snippet

I am using v 0.9.3015.27723.  I get compile errors as follows:
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using GeoPoint = SharpMap.Geometries.Point;
using SharpMap.Geometries;
using SharpMap.Layers;
using SharpMap.Forms;
using SharpMap.Data.Providers;

namespace SharpMapTest
{
    public partial class SharpMapForm : Form
    {
        private SharpMap.Map myMap;
        public SharpMapForm()
        {
            InitializeComponent();
            myMap = new SharpMap.Map(new Size(400, 300));
            myMap.BackColor = Color.Aqua; //Set background
            mapBox1.Map = myMap;
                    
            SharpMap.Layers.GdalRasterLayer layer;
            OpenFileDialog dialog = new OpenFileDialog();

            if (dialog.ShowDialog() != DialogResult.OK)
                return;

            try
            {
                layer = new SharpMap.Layers.GdalRasterLayer(dialog.FileName, dialog.FileName);
                myMap.Layers.Add(layer);
                 
                myMap.ZoomToExtents();
                
                // try to add a line on a new layer
                SharpMap.Layers.VectorLayer reachLayer = new VectorLayer("Reachlayer");

                // create two points of the line
                SharpMap.Geometries.Point point = new SharpMap.Geometries.Point();
                point.X =11003339.0;
                point.Y = 4210795.0;
                SharpMap.Geometries.Point point2 = new SharpMap.Geometries.Point();
                point2.X = 11193000.0;
                point2.Y = 4216795.0;

                List<SharpMap.Geometries.Geometry> nList = new List<SharpMap.Geometries.Geometry>();
              
                /*
                 *
                 Error    1    The best overloaded method match for
                 'SharpMap.Data.Providers.GeometryProvider.GeometryProvider(System.Collections.ObjectModel.Collection<SharpMap.Geometries.Geometry>)' \
                 has some invalid arguments
                 *
                 *
                 Error    2    Argument '1': cannot convert from 'System.Collections.Generic.List<SharpMap.Geometries.Geometry>'
                 to 'System.Collections.ObjectModel.Collection<SharpMap.Geometries.Geometry>'

                 */
                reachLayer.DataSource = new SharpMap.Data.Providers.GeometryProvider(nList); // <==== error 1 and 2 at this line

                List<SharpMap.Geometries.Point> lList = new List<SharpMap.Geometries.Point>();
                lList.Add(point);
                lList.Add(point2);

                /*
                 *
                 Error    3    The best overloaded method match for
                 'SharpMap.Geometries.LineString.LineString(System.Collections.ObjectModel.Collection<SharpMap.Geometries.Point>)' \
                  has some invalid arguments
                                 
                  Error    4    Argument '1': cannot convert from
                 'System.Collections.Generic.List<SharpMap.Geometries.Point>' to 'System.Collections.ObjectModel.Collection<SharpMap.Geometries.Point>'
                 
                 *
                 * */

                SharpMap.Geometries.LineString line1 = new SharpMap.Geometries.LineString(lList); // < ==== error 3 and 4 at this line
                nList.Add(line1);
                 
                reachLayer.Enabled = true;
                myMap.Layers.Add(reachLayer);
                              
                mapBox1.Refresh();
            }
            catch (Exception error)
            {
                MessageBox.Show("There was a problem loading raster.\n\n" + error.Message);
            }
                        
        }

    }
}

thanks in advance.

R. Males
Cincinnati, OH

Coordinator
Mar 16, 2009 at 10:24 AM
Hi rbmales, if you declare nList as Collection<Geometry> you should be ok for the first issue, and declaring lList  as Collection<Point> for the second issue.
Alternatively if you can pull the trunk I have updated the source to use the interfaces IList<T> instead of Collection<T> so the issue should not occur in the future.. hth jd
Mar 16, 2009 at 3:12 PM
Thank you for your prompt reply.  With your suggestions, it now works exactly as desired. 

For the record, code changes per your suggestion are:

  Collection<Geometry> nList = new Collection<Geometry>();

  Collection<SharpMap.Geometries.Point> lList = new Collection<SharpMap.Geometries.Point>();


I gather there is no intention of updating the v 0.9 binaries of august 2006.  Is the suggested approach for those of us currently working in 0.9 to get the trunk and compile?


R. Males
Cincinnati, OH,, USA
Coordinator
Mar 16, 2009 at 3:26 PM
>> I gather there is no intention of updating the v 0.9 binaries of august 2006.  Is the suggested approach for those of us currently working in 0.9 to get the trunk and compile?

I think so, it is stable and any changes are of the very minor / bug fix type hth jd