Draw Polygon, Select Points Inside, Return FeatureDataset

Topics: SharpMap v0.9 / v1.x, WinForms Controls
Sep 6, 2010 at 9:50 AM

Hi All

I want to be able to do the following with a map with two layers (Layer1 = SHAPEfile, Layer2 is a set of X,Y point data from MSAccess Database)....

1.  Draw a polygon on the map

2.  Select all the points (from Layer2) within that polygon

3.  Return a FeatureDataset/table... giving point X,Y coords and other data from underlying MSAccess db).

 

Cheers

James

Coordinator
Sep 6, 2010 at 12:23 PM

Hello James,

steps 2 and 3 are fairly easy:

  • For Layer2 use OleDbPoint-Provider.
  • Set Map.QueryLayerIndex to 1 to query that point layer.

The first step you need to implement yourself:

  1. Add new Tool to MapTools enum, e.g. QueryPolygon
  2. Add List<Point> _polygonPoints to MapBox control.
  3. Add logic to store clicked points (MouseClicked | MouseMove) and draw LinearRing on top of the image.
  4. Add logic to run query upon input terminating signal.

Hth FObermaier

Sep 6, 2010 at 1:45 PM

Hi Felix

 

The adding a points layer and making it active is the easy bit!  ;o)

 

Your points 1 through to 4 are where I hit the snag.  I am a bit flumoxed on that.  I am not a natural C#.NET programmer (originally Delphi Win32 and a little VB.NET more recently).

 

Do you have any code snippets or any of your experts got a branch with any such code?

 

Cheers

James

Dec 11, 2012 at 1:06 PM

Hi

 

It's been a while...

 

still having issues.  Being a bit of a C# nooby I still cannot get my head around...

  • Add new Tool to MapTools enum, e.g. QueryPolygon       How do I do this???
  • Add List<Point> _polygonPoints to MapBox control.       How do I do this???
  • Add logic to store clicked points (MouseClicked | MouseMove) and draw LinearRing on top of the image.     Eeek!       How do I do this???
  • Add logic to run query upon input terminating signal.

       Still baffled!

     

    Any help gratefully received.

     

    cheers

    James

  • Dec 12, 2012 at 4:02 PM

    James,

    This is not exactly what you are asking for but it should give you some idea of what you can do now days...

    Set up a provider for your user drawn polygons:
    private SharpMap.Data.Providers.GeometryProvider geoProvider;

    Define a vector layer for your draw item(s): can be as simple as this...
        public void LoadVLLayer(SharpMap.Map _map)
            {
                SharpMap.Layers.VectorLayer vl = new VectorLayer("MyGeometries");
                geoProvider = new SharpMap.Data.Providers.GeometryProvider(new List<SharpMap.Geometries.Geometry>());
                geoProvider.SRID = 2236;
                vl.DataSource = geoProvider;
                vl.Style = new SharpMap.Styles.VectorStyle();
                Color c = Color.FromArgb(50, 254, 0, 0);
                Brush b = new SolidBrush(c);
                vl.Style.Fill = b;
                vl.Style.Line.Color = Color.Red;
                vl.Style.Line.Width = 3.0f;
                vl.Style.PointColor = Brushes.Red;
                vl.Style.EnableOutline = true;
                vl.Style.Outline = new Pen(Color.Red);
                _map.Layers.Add(vl);
            }

    Wire up the: this.mapBox1.GeometryDefined += new SharpMap.Forms.MapBox.GeometryDefinedHandler(mapBox1_GeometryDefined);
    ...of course then you need:
            private void mapBox1_GeometryDefined(SharpMap.Geometries.Geometry geometry)
            {
                //do something with it
                if (geometry.GeometryType == SharpMap.Geometries.GeometryType2.Polygon)
                {
                    geoProvider.Geometries.Add(geometry);
                }
            }

    Do as FObermaier says:
        For Layer2 use OleDbPoint-Provider.
        Set Map.QueryLayerIndex to 1 to query that point layer.
       
    Set the Map's active tool:
        this.mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.DrawPolygon;
       
    Draw the polygon on the map .... your "mapBox1_GeometryDefined" method will have your polygon.

    r,

    dennis

    Dec 12, 2012 at 4:40 PM
    Thanks Dennis

    I'll try this once I can get to my dev machine.


    James D Nelson
    BSc MSc CSci CChem MRSC SiLC
    Associate

    Discovery CE Limited
    The Granary
    Broadwell House Farm
    Broadwell
    Warwickshire
    CV23 8HF
    Tel 01926 813 909
    Web: www.dce-services.co.uk


    On 12 December 2012 17:02, djonio <notifications@codeplex.com> wrote:
    > From: djonio
    >
    > James,
    >
    > This is not exactly what you are asking for but it should give you some idea
    > of what you can do now days...
    >
    > Set up a provider for your user drawn polygons:
    > private SharpMap.Data.Providers.GeometryProvider geoProvider;
    >
    > Define a vector layer for your draw item(s): can be as simple as this...
    > public void LoadVLLayer(SharpMap.Map _map)
    > {
    > SharpMap.Layers.VectorLayer vl = new
    > VectorLayer("MyGeometries");
    > geoProvider = new SharpMap.Data.Providers.GeometryProvider(new
    > List<SharpMap.Geometries.Geometry>());
    > geoProvider.SRID = 2236;
    > vl.DataSource = geoProvider;
    > vl.Style = new SharpMap.Styles.VectorStyle();
    > Color c = Color.FromArgb(50, 254, 0, 0);
    > Brush b = new SolidBrush(c);
    > vl.Style.Fill = b;
    > vl.Style.Line.Color = Color.Red;
    > vl.Style.Line.Width = 3.0f;
    > vl.Style.PointColor = Brushes.Red;
    > vl.Style.EnableOutline = true;
    > vl.Style.Outline = new Pen(Color.Red);
    > _map.Layers.Add(vl);
    > }
    >
    > Wire up the: this.mapBox1.GeometryDefined += new
    > SharpMap.Forms.MapBox.GeometryDefinedHandler(mapBox1_GeometryDefined);
    > ...of course then you need:
    > private void mapBox1_GeometryDefined(SharpMap.Geometries.Geometry
    > geometry)
    > {
    > //do something with it
    > if (geometry.GeometryType ==
    > SharpMap.Geometries.GeometryType2.Polygon)
    > {
    > geoProvider.Geometries.Add(geometry);
    > }
    > }
    >
    > Do as FObermaier says:
    > For Layer2 use OleDbPoint-Provider.
    > Set Map.QueryLayerIndex to 1 to query that point layer.
    >
    > Set the Map's active tool:
    > this.mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.DrawPolygon;
    >
    > Draw the polygon on the map .... your "mapBox1_GeometryDefined" method will
    > have your polygon.
    >
    > r,
    >
    > dennis
    >
    > Read the full discussion online.
    >
    > To add a post to this discussion, reply to this email
    > ([email removed])
    >
    > To start a new discussion for this project, email
    > [email removed]
    >
    > You are receiving this email because you subscribed to this discussion on
    > CodePlex. You can unsubscribe on CodePlex.com.
    >
    > Please note: Images and attachments will be removed from emails. Any posts
    > to this discussion will also be available online at CodePlex.com
    Dec 17, 2012 at 7:47 PM
    OK

    I seem to be getting there... not sure what i do with

    " Wire up the: this.mapBox1.GeometryDefined += new SharpMap.Forms.MapBox.GeometryDefinedHandler(mapBox1_GeometryDefined); "

    Where do I put this lump of code?


    Cheers
    James



    On 12 December 2012 17:40, James Nelson <jdnelson.web@googlemail.com> wrote:
    Thanks Dennis

    I'll try this once I can get to my dev machine.


    James D Nelson
    BSc MSc CSci CChem MRSC SiLC
    Associate

    Discovery CE Limited
    The Granary
    Broadwell House Farm
    Broadwell
    Warwickshire
    CV23 8HF
    Tel 01926 813 909
    Web: www.dce-services.co.uk


    On 12 December 2012 17:02, djonio <notifications@codeplex.com> wrote:
    > From: djonio
    >
    > James,
    >
    > This is not exactly what you are asking for but it should give you some idea
    > of what you can do now days...
    >
    > Set up a provider for your user drawn polygons:
    > private SharpMap.Data.Providers.GeometryProvider geoProvider;
    >
    > Define a vector layer for your draw item(s): can be as simple as this...
    > public void LoadVLLayer(SharpMap.Map _map)
    > {
    > SharpMap.Layers.VectorLayer vl = new
    > VectorLayer("MyGeometries");
    > geoProvider = new SharpMap.Data.Providers.GeometryProvider(new
    > List<SharpMap.Geometries.Geometry>());
    > geoProvider.SRID = 2236;
    > vl.DataSource = geoProvider;
    > vl.Style = new SharpMap.Styles.VectorStyle();
    > Color c = Color.FromArgb(50, 254, 0, 0);
    > Brush b = new SolidBrush(c);
    > vl.Style.Fill = b;
    > vl.Style.Line.Color = Color.Red;
    > vl.Style.Line.Width = 3.0f;
    > vl.Style.PointColor = Brushes.Red;
    > vl.Style.EnableOutline = true;
    > vl.Style.Outline = new Pen(Color.Red);
    > _map.Layers.Add(vl);
    > }
    >
    > Wire up the: this.mapBox1.GeometryDefined += new
    > SharpMap.Forms.MapBox.GeometryDefinedHandler(mapBox1_GeometryDefined);
    > ...of course then you need:
    > private void mapBox1_GeometryDefined(SharpMap.Geometries.Geometry
    > geometry)
    > {
    > //do something with it
    > if (geometry.GeometryType ==
    > SharpMap.Geometries.GeometryType2.Polygon)
    > {
    > geoProvider.Geometries.Add(geometry);
    > }
    > }
    >
    > Do as FObermaier says:
    > For Layer2 use OleDbPoint-Provider.
    > Set Map.QueryLayerIndex to 1 to query that point layer.
    >
    > Set the Map's active tool:
    > this.mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.DrawPolygon;
    >
    > Draw the polygon on the map .... your "mapBox1_GeometryDefined" method will
    > have your polygon.
    >
    > r,
    >
    > dennis
    >
    > Read the full discussion online.
    >
    > To add a post to this discussion, reply to this email
    > (SharpMap@discussions.codeplex.com)
    >
    > To start a new discussion for this project, email
    > SharpMap@discussions.codeplex.com
    >
    > You are receiving this email because you subscribed to this discussion on
    > CodePlex. You can unsubscribe on CodePlex.com.
    >
    > Please note: Images and attachments will be removed from emails. Any posts
    > to this discussion will also be available online at CodePlex.com

    Dec 17, 2012 at 7:56 PM
    OK Dennis

    I sort of have it all compiling now... but getting an exceptions at

    private void mapBox1_GeometryDefined(SharpMap.Geometries.Geometry geometry)
    {
    //do something with it
    if (geometry.GeometryType == SharpMap.Geometries.GeometryType2.Polygon)
    {
    geoProvider.Geometries.Add(geometry); //// THIS IS WHERE THE CODE CONKS OUT
    }
    }

    Error is flagged as "Object reference not set to an instance of an object."

    Any clues?

    Tah
    James



    On 17 December 2012 20:46, James Nelson <jdnelson.web@googlemail.com> wrote:
    OK

    I seem to be getting there... not sure what i do with

    " Wire up the: this.mapBox1.GeometryDefined += new SharpMap.Forms.MapBox.GeometryDefinedHandler(mapBox1_GeometryDefined); "

    Where do I put this lump of code?


    Cheers
    James



    On 12 December 2012 17:40, James Nelson <jdnelson.web@googlemail.com> wrote:
    Thanks Dennis

    I'll try this once I can get to my dev machine.


    James D Nelson
    BSc MSc CSci CChem MRSC SiLC
    Associate

    Discovery CE Limited
    The Granary
    Broadwell House Farm
    Broadwell
    Warwickshire
    CV23 8HF
    Tel 01926 813 909
    Web: www.dce-services.co.uk


    On 12 December 2012 17:02, djonio <notifications@codeplex.com> wrote:
    > From: djonio
    >
    > James,
    >
    > This is not exactly what you are asking for but it should give you some idea
    > of what you can do now days...
    >
    > Set up a provider for your user drawn polygons:
    > private SharpMap.Data.Providers.GeometryProvider geoProvider;
    >
    > Define a vector layer for your draw item(s): can be as simple as this...
    > public void LoadVLLayer(SharpMap.Map _map)
    > {
    > SharpMap.Layers.VectorLayer vl = new
    > VectorLayer("MyGeometries");
    > geoProvider = new SharpMap.Data.Providers.GeometryProvider(new
    > List<SharpMap.Geometries.Geometry>());
    > geoProvider.SRID = 2236;
    > vl.DataSource = geoProvider;
    > vl.Style = new SharpMap.Styles.VectorStyle();
    > Color c = Color.FromArgb(50, 254, 0, 0);
    > Brush b = new SolidBrush(c);
    > vl.Style.Fill = b;
    > vl.Style.Line.Color = Color.Red;
    > vl.Style.Line.Width = 3.0f;
    > vl.Style.PointColor = Brushes.Red;
    > vl.Style.EnableOutline = true;
    > vl.Style.Outline = new Pen(Color.Red);
    > _map.Layers.Add(vl);
    > }
    >
    > Wire up the: this.mapBox1.GeometryDefined += new
    > SharpMap.Forms.MapBox.GeometryDefinedHandler(mapBox1_GeometryDefined);
    > ...of course then you need:
    > private void mapBox1_GeometryDefined(SharpMap.Geometries.Geometry
    > geometry)
    > {
    > //do something with it
    > if (geometry.GeometryType ==
    > SharpMap.Geometries.GeometryType2.Polygon)
    > {
    > geoProvider.Geometries.Add(geometry);
    > }
    > }
    >
    > Do as FObermaier says:
    > For Layer2 use OleDbPoint-Provider.
    > Set Map.QueryLayerIndex to 1 to query that point layer.
    >
    > Set the Map's active tool:
    > this.mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.DrawPolygon;
    >
    > Draw the polygon on the map .... your "mapBox1_GeometryDefined" method will
    > have your polygon.
    >
    > r,
    >
    > dennis
    >
    > Read the full discussion online.
    >
    > To add a post to this discussion, reply to this email
    > (SharpMap@discussions.codeplex.com)
    >
    > To start a new discussion for this project, email
    > SharpMap@discussions.codeplex.com
    >
    > You are receiving this email because you subscribed to this discussion on
    > CodePlex. You can unsubscribe on CodePlex.com.
    >
    > Please note: Images and attachments will be removed from emails. Any posts
    > to this discussion will also be available online at CodePlex.com


    Dec 17, 2012 at 7:59 PM
    Do we need to call the "LoadVLLayer" routine?


    Thanks
    James



    On 17 December 2012 20:56, James Nelson <jdnelson.web@googlemail.com> wrote:
    OK Dennis

    I sort of have it all compiling now... but getting an exceptions at

    private void mapBox1_GeometryDefined(SharpMap.Geometries.Geometry geometry)
    {
    //do something with it
    if (geometry.GeometryType == SharpMap.Geometries.GeometryType2.Polygon)
    {
    geoProvider.Geometries.Add(geometry); //// THIS IS WHERE THE CODE CONKS OUT
    }
    }

    Error is flagged as "Object reference not set to an instance of an object."

    Any clues?

    Tah
    James



    On 17 December 2012 20:46, James Nelson <jdnelson.web@googlemail.com> wrote:
    OK

    I seem to be getting there... not sure what i do with

    " Wire up the: this.mapBox1.GeometryDefined += new SharpMap.Forms.MapBox.GeometryDefinedHandler(mapBox1_GeometryDefined); "

    Where do I put this lump of code?


    Cheers
    James



    On 12 December 2012 17:40, James Nelson <jdnelson.web@googlemail.com> wrote:
    Thanks Dennis

    I'll try this once I can get to my dev machine.


    James D Nelson
    BSc MSc CSci CChem MRSC SiLC
    Associate

    Discovery CE Limited
    The Granary
    Broadwell House Farm
    Broadwell
    Warwickshire
    CV23 8HF
    Tel 01926 813 909
    Web: www.dce-services.co.uk


    On 12 December 2012 17:02, djonio <notifications@codeplex.com> wrote:
    > From: djonio
    >
    > James,
    >
    > This is not exactly what you are asking for but it should give you some idea
    > of what you can do now days...
    >
    > Set up a provider for your user drawn polygons:
    > private SharpMap.Data.Providers.GeometryProvider geoProvider;
    >
    > Define a vector layer for your draw item(s): can be as simple as this...
    > public void LoadVLLayer(SharpMap.Map _map)
    > {
    > SharpMap.Layers.VectorLayer vl = new
    > VectorLayer("MyGeometries");
    > geoProvider = new SharpMap.Data.Providers.GeometryProvider(new
    > List<SharpMap.Geometries.Geometry>());
    > geoProvider.SRID = 2236;
    > vl.DataSource = geoProvider;
    > vl.Style = new SharpMap.Styles.VectorStyle();
    > Color c = Color.FromArgb(50, 254, 0, 0);
    > Brush b = new SolidBrush(c);
    > vl.Style.Fill = b;
    > vl.Style.Line.Color = Color.Red;
    > vl.Style.Line.Width = 3.0f;
    > vl.Style.PointColor = Brushes.Red;
    > vl.Style.EnableOutline = true;
    > vl.Style.Outline = new Pen(Color.Red);
    > _map.Layers.Add(vl);
    > }
    >
    > Wire up the: this.mapBox1.GeometryDefined += new
    > SharpMap.Forms.MapBox.GeometryDefinedHandler(mapBox1_GeometryDefined);
    > ...of course then you need:
    > private void mapBox1_GeometryDefined(SharpMap.Geometries.Geometry
    > geometry)
    > {
    > //do something with it
    > if (geometry.GeometryType ==
    > SharpMap.Geometries.GeometryType2.Polygon)
    > {
    > geoProvider.Geometries.Add(geometry);
    > }
    > }
    >
    > Do as FObermaier says:
    > For Layer2 use OleDbPoint-Provider.
    > Set Map.QueryLayerIndex to 1 to query that point layer.
    >
    > Set the Map's active tool:
    > this.mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.DrawPolygon;
    >
    > Draw the polygon on the map .... your "mapBox1_GeometryDefined" method will
    > have your polygon.
    >
    > r,
    >
    > dennis
    >
    > Read the full discussion online.
    >
    > To add a post to this discussion, reply to this email
    > (SharpMap@discussions.codeplex.com)
    >
    > To start a new discussion for this project, email
    > SharpMap@discussions.codeplex.com
    >
    > You are receiving this email because you subscribed to this discussion on
    > CodePlex. You can unsubscribe on CodePlex.com.
    >
    > Please note: Images and attachments will be removed from emails. Any posts
    > to this discussion will also be available online at CodePlex.com