NTS and Baffled

Topics: Algorithms, SharpMap v0.9 / v1.x
Jan 13, 2013 at 9:45 PM

I have already had some help from FObermaier and djonio on this and feel like I am SOOOOOO close!

I have put together a Winforms application.  It loads a shapefile as a base map as "layer 0".  I then load a bunch of X,Y points from an Access DB as "layer 1", I then load labels for the X,Y points from the same access DB "layer 2" and then the fun starts...

Using MapBox.ActiveTool = DrawPolygon... I can capture the user input as a new layer "layer 3".

What I want to do is find out which points "layer 1" are within the polygon drawn and captured as "layer 3"...  I have cobbled this code together but would be very grateful for advice as to what I should do next.

==============================

 

 private void mapBox1_GeometryDefined(SharpMap.Geometries.Geometry geometry)

        {

            //do something with it

            if (geometry.GeometryType == SharpMap.Geometries.GeometryType2.Polygon)

            {

                SharpMap.Layers.VectorLayer POLY = new VectorLayer("MyPolygon");

                geoProvider = new SharpMap.Data.Providers.GeometryProvider(new List<SharpMap.Geometries.Geometry>());

                geoProvider.SRID = 2236;

                geoProvider.Geometries.Add(geometry); 

                POLY.DataSource = geoProvider;               /// Assign datasource to layer "POLY"

                mapBox1.Map.Layers.Add(POLY);

                LayerListBox.Items.Add("MyPolygon [3]", true);

                mapBox1.Refresh();

 

            //      Create a feature dataset

                FeatureDataSet ds = new FeatureDataSet();

 

            //    Is this casting my polygon layer into NTS ?

                NtsProvider MyPolygon = new NtsProvider(geoProvider);

           //    Is this casting my points layer into NTS ?

                NtsProvider POINTSLAYER = new NtsProvider((mapBox1.Map.Layers[1] as SharpMap.Layers.VectorLayer).DataSource);   //Layers[1] as SharpMap.Layers.VectorLayer).DataSource

 

                /// Is this where I need to CREATE NTS Geometries and pick out only the points   ????

                /// 

                for (int i = 0; i < ds.Tables[0].Rows.Count - 1; i++)

                {

                    /// I need to do something here to do a "contains or within query?????? 

                }

            }

        }

==============================

 

Hopefully I am getting close to something... I am relatively bad at C# and I am also having some real confusion over using NTS!!!

Any help gratefully received.

 

Cheers

James

Coordinator
Jan 14, 2013 at 8:28 AM

Are you using a precompiled version of SharpMap? If so, grab the source and compile it yourself. This way you won't have to convert between SharpMap and GeoAPI/NTS geometries.

After that you can place the following code in your GeometryDefined handler. Depending on the datasource you might not even need to check for true intersection anymore.

var pPoly = NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory.Prepare(geometry);

var p = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers[1]).DataSource;
var fds = new SharpMap.Data.FeatureDataSet();
p.ExecuteIntersectionQuery(geometry, fds);
foreach (var fdr in fds.Tables[0].Rows)
{
  if (pPoly.Intersects(fdr.Geometry))
  {
    //Do sth with it
  }
}

Hth FObermaier

Jan 15, 2013 at 5:27 PM

Felix

 

Thanks a bunch... I'll get the latest trunk and build at home and see where I get once I get back home.

 

Cheers

James

 

Jan 15, 2013 at 7:47 PM

Felix

I downloaded the latest Trunk... compiled without problems...

 

Now some big errors (red text) with...

 

 

private void mapBox1_GeometryDefined(GeoAPI.Geometries.IGeometry geometry)

        {

            //do something with it

            if (geometry.GeometryType == SharpMap.Geometries.GeometryType2.Polygon)

            {

                SharpMap.Layers.VectorLayer POLY = new VectorLayer("MyPolygon");

                geoProvider = new SharpMap.Data.Providers.GeometryProvider(new List<SharpMap.Geometries.Geometry>());

                geoProvider.SRID = 2236;

                geoProvider.Geometries.Add(geometry);

                POLY.DataSource = geoProvider;               /// Assign datasource to layer "POLY"

                POLY.Style = new SharpMap.Styles.VectorStyle();

                Color c = Color.FromArgb(50, 254, 0, 0);

                Brush b = new SolidBrush(c);

                POLY.Style.Fill = b;

                POLY.Style.Line.Color = Color.Red;

                POLY.Style.Line.Width = 3.0f;

                POLY.Style.PointColor = Brushes.Red;

                POLY.Style.EnableOutline = true;

                POLY.Style.Outline = new Pen(Color.Red);

                mapBox1.Map.Layers.Add(POLY);

                mapBox1.Refresh();

 

                //      Create a feature dataset

                FeatureDataSet ds = new FeatureDataSet();

                //    Is this casting my polygon layer into NTS ?

                NtsProvider MyPolygon = new NtsProvider(geoProvider);

                //    Is this casting my points layer into NTS ?

                NtsProvider POINTSLAYER = new NtsProvider((mapBox1.Map.Layers[1] as SharpMap.Layers.VectorLayer).DataSource);   //Layers[1] as SharpMap.Layers.VectorLayer).DataSource

 

                /// Is this where I need to CREATE NTS Geometries and pick out only the points   ????

                /// 

                for (int i = 0; i < ds.Tables[0].Rows.Count - 1; i++)

                {

                    /// I need to do something here to do a "contains or within query?????? 

                }

            }

        }

 

Obviously the syntax/referencing has changed since the binary release!!!  What am I missing.

 

Cheers

James

Coordinator
Jan 16, 2013 at 8:36 AM

SharpMap.Geometries have been ditched in favor of GeoAPI/NTS geometries. Therefore you

  • need to check for geometry.OgcGeometryType == GeoAPI.Geometry.OgcGeometryTypes.Polygon
  • need to setup the geometryProvider with a list of GeoAPI.Geometries.IGeometry
  • don't need the NTSProviders anymore

Hth FObermaier

Jan 16, 2013 at 9:16 AM
Felix

Thanks for that. One question... how do I set up the geometryProvider with a list of GeoAPI.Geometries.IGeometry considering I have just "captured" the user drawn polygon as a new layer (Layer 3) via Activetool = DrawPolygon ?

James

On 16 January 2013 08:36, FObermaier <notifications@codeplex.com> wrote:

From: FObermaier

SharpMap.Geometries have been ditched in favor of GeoAPI/NTS geometries. Therefore you

  • need to check for geometry.OgcGeometryType == GeoAPI.Geometry.OgcGeometryTypes.Polygon
  • need to setup the geometryProvider with a list of GeoAPI.Geometries.IGeometry
  • don't need the NTSProviders anymore

Hth FObermaier

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


Jan 16, 2013 at 8:25 PM

The fun continues...

 

MapBox.cs throws... an exception {"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}

when activeTool is DrawPolygon

Stack dump below...

'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\James\Documents\Visual Studio 2010\Projects\CSHARPMAP_MYBUILD\CSHARPMAP_MYBUILD\bin\Debug\CSHARPMAP_MYBUILD.vshost.exe', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Deployment\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Deployment.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.The thread 'vshost.NotifyLoad' (0x860) has exited with code 0 (0x0).The thread '<No Name>' (0x13fc) has exited with code 0 (0x0).The thread 'vshost.LoadReference' (0x1250) has exited with code 0 (0x0).'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\James\Documents\Visual Studio 2010\Projects\CSHARPMAP_MYBUILD\CSHARPMAP_MYBUILD\bin\Debug\CSHARPMAP_MYBUILD.exe', Symbols loaded.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\James\Documents\Visual Studio 2010\Projects\CSHARPMAP_MYBUILD\CSHARPMAP_MYBUILD\bin\Debug\SharpMap.UI.dll', Symbols loaded.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\James\Documents\Visual Studio 2010\Projects\CSHARPMAP_MYBUILD\CSHARPMAP_MYBUILD\bin\Debug\GeoAPI.dll''CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\James\Documents\Visual Studio 2010\Projects\CSHARPMAP_MYBUILD\CSHARPMAP_MYBUILD\bin\Debug\Common.Logging.dll''CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.no configuration section <common/logging> found - suppressing logging output'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\James\Documents\Visual Studio 2010\Projects\CSHARPMAP_MYBUILD\CSHARPMAP_MYBUILD\bin\Debug\SharpMap.dll', Symbols loaded.'CSHARPMAP_MYBUILD.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\James\Documents\Visual Studio 2010\Projects\CSHARPMAP_MYBUILD\CSHARPMAP_MYBUILD\bin\Debug\NetTopologySuite.dll'A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Jan 23, 2013 at 10:42 PM

Felix

You suggested using...

var pPoly = NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory.Prepare(geometry);

var p = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers[1]).DataSource;
var fds = new SharpMap.Data.FeatureDataSet();
p.ExecuteIntersectionQuery(geometry, fds);
foreach (var fdr in fds.Tables[0].Rows)
{
  if (pPoly.Intersects(fdr.Geometry))
  {
    //Do sth with it
  }
}

 

It almost works but picks up points close to but not within the polygon... how would I convert to do TRUE intersection queries.

 

Cheers

James

Coordinator
Jan 24, 2013 at 6:50 AM

The way outlined should work properly. Could you give a WKT example of a polygon and another geometry where

pPoly.Intersects(fdr.Geometry)

returns true and it should actually return false? Does

geometry.Intersects(fdr.Geometry)

produce a different result?

Feb 2, 2013 at 11:54 AM
Edited Feb 2, 2013 at 11:54 AM
Felix

I do not really understand how to do the WKT stuff!!! ;o(

The drawn polygon pulls out points LAYER 1 within the polygon which is great. The issue is that some points outside the polygon are also picked up... I can send screen captures if this helps (not sure how to do that though?
Imagine the letters A - M are my points (layer 1)... point labels are on Layer 2 and the lines are the polygon (Layer 3)...
                 ______________
  A             |    B          |          C
                 |                |
                 |                |
   D            |      E        |            F
                 |                | _____________                   
   G            |      H                I         |
                 |___________________________ |

   K                L             M 
I always get the points B, E, H and I (which is exactly what I need), but I also get F and frequently C too. It is as if the polygon has a ghostly rectangular outline which picks up other points. I only want those which are actually WITHIN the polygon drawn by the user (managed by MapGeometryDefined)...

Any other clues...

Sorry to keep asking these amateur questions.

Best regards
James
Coordinator
Feb 4, 2013 at 9:28 AM
JDNelson wrote:
I do not really understand how to do the WKT stuff!!! ;o(
To get the WKT representation of your geometries, all you need to do is call
geom.AsText();
I always get the points B, E, H and I (which is exactly what I need), but I also get F and frequently C too. It is as if the polygon has a ghostly rectangular outline which picks up other points. I only want those which are actually WITHIN the polygon drawn by the user (managed by MapGeometryDefined)...
It seems to me that you are querying the bounding box of your digitized geometry.
Feb 4, 2013 at 10:13 AM
Edited Feb 4, 2013 at 10:15 AM
Felix

OK once I get some time I'll try the "geom,AsText();" code and see what pops up - I'll let you know.

The next question is: How do I query the points layer with the digitized geometry (polygon) without using the bounding box? Is this where a cast to NTS or GeoAPI comes in??? Not sure what to do here.

Thanks James
Feb 5, 2013 at 5:57 PM
FObermaier

I have tried the geom.AsText(); function which returns the coordinates of the polygon like this...
POLYGON ((428854.94396656379 360465.93309698609, 428848.58062906645 360457.77142497862, 428861.72230433271 360452.37642144825, 428861.72230433271 360452.37642144825, 428854.94396656379 360465.93309698609))
How do I do the query without the bounding box? that is to say only the points within the actual polygon?

Cheers
James
Coordinator
Feb 6, 2013 at 7:41 AM
James,

to determine whether or not the algorithm goes wrong I need more than just the polygon. The polygon you posted is actually a triangle, is that correct? I need the coordinates of points that are reported to be intersecting the polygon but are actually not.

Please post the GeometryDefined handler code as it is now.
Feb 6, 2013 at 12:33 PM
<div dir="ltr">Felix <div><br> </div> <div style="">I will post the details and code when I get home to my development box this evening.</div> <div style=""><br> </div> <div style="">Cheers</div> <div style="">James</div> <div><br clear="all"> <div>On 6 February 2013 07:41, FObermaier <span dir="ltr">&lt;<a href="mailto:notifications@codeplex.com" target="_blank">notifications@codeplex.com</a>&gt;</span> wrote:<br> </div> <div> <blockquote style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex"> <div> <p>From: FObermaier</p> <div>James, to determine whether or not the algorithm goes wrong I need more than just the polygon. The polygon you posted is actually a triangle, is that correct? I need the coordinates of points that are reported to be intersecting the polygon but are actually not. Please post the GeometryDefined handler code as it is now. </div> <div> <div> <p>Read the <a href="http://sharpmap.codeplex.com/discussions/429469#post997075" target="_blank"> full discussion online</a>.</p> <p>To add a post to this discussion, reply to this email (<a href="mailto:SharpMap@discussions.codeplex.com?subject=[SharpMap:429469]" target="_blank">SharpMap@discussions.codeplex.com</a>)</p> <p>To start a new discussion for this project, email <a href="mailto:SharpMap@discussions.codeplex.com" target="_blank"> SharpMap@discussions.codeplex.com</a></p> <p>You are receiving this email because you subscribed to this discussion on CodePlex. You can <a href="https://sharpmap.codeplex.com/discussions/429469/unsubscribe/" target="_blank"> unsubscribe</a> on CodePlex.com.</p> <p>Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com</p> </div> </div> </div> </blockquote> </div> <br> </div> </div>
Feb 6, 2013 at 9:12 PM
The polygon discussed in my earlier post was indeed a triangle.

The code for GeometryDefined is this...
private void mapBox1_GeometryDefined(GeoAPI.Geometries.IGeometry geometry)
        {
            if (geometry.OgcGeometryType == GeoAPI.Geometries.OgcGeometryType.Polygon)
            {
                //Add Polygon layer:
                string MyPolygonName ="My Polygon";

                SharpMap.Layers.VectorLayer POLY = new VectorLayer(MyPolygonName);

                SharpMap.Data.Providers.GeometryProvider geoProvider = new SharpMap.Data.Providers.GeometryProvider(new List<GeoAPI.Geometries.IGeometry>());

                geoProvider.SRID = 2236;
                geoProvider.Geometries.Add(geometry);
                POLY.DataSource = geoProvider;               /// Assign datasource to layer "POLY"
                POLY.Style = new SharpMap.Styles.VectorStyle();
                Color c = Color.FromArgb(50, 254, 0, 0);
                Brush b = new SolidBrush(c);
                POLY.Style.Fill = b;
                POLY.Style.Line.Color = Color.Gold;
                POLY.Style.Line.Width = 1.0f;
                POLY.Style.PointColor = Brushes.Red;
                POLY.Style.EnableOutline = true;
                POLY.Style.Outline = new Pen(Color.Red);
                mapBox1.Map.Layers.Add(POLY);
              //  LayerListBox.Items.Add(MyPolygonName, true);
                mapBox1.Refresh();

                var pPoly = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers[3]).DataSource;
                var p = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers[1]).DataSource;
                var fds = new SharpMap.Data.FeatureDataSet();

                try
                {
                    p.ExecuteIntersectionQuery(geometry, fds);


                    dataGridView1.DataSource = fds.Tables[0];
                    String MyHoleList = "You selected the following holes...";
                    String QueryINList = "";

                    foreach (DataRow MyRow in fds.Tables[0].Rows)
                    {
                        {
                            MyHoleList = MyHoleList + MyRow["PointID"].ToString() + ", ";
                            QueryINList = QueryINList + "'" + MyRow["PointID"].ToString() + "',";
                        }
                    }

                   MessageBox.Show(QueryINList, "RESULT", MessageBoxButtons.OK, MessageBoxIcon.Information);
              //     MessageBox.Show(geometry.AsText());
                }
                catch //(System.Exception excep)
                {
                    MessageBox.Show("No Points selected in polygon");
                }
            }
        }
Now in my app I use the activeTool = polygon to draw a quadrilateral shape thus

POLYGON ((428999.76819468878 360451.93329044303, 428998.25517286535 360420.80827007542, 429023.1119599645 360406.75878171506, 429004.52340613387 360451.71714446822, 429004.52340613387 360451.71714446822, 428999.76819468878 360451.93329044303))

... which from the code above generates a layer with the defined polygon and then tries the intersectionQuery with the following being selected from the points layer (generated from X,Y coordinates in an access database table).

The points selected are have coordinates thus

429012.5 360443.18 X
429001.59 360446.98
429003.31 360425.45
429016.9 360413.04

The point with coordinates 429012.5 360443.18 (marked X) is outside the polygon but looks like it might be within the bounding box of the defined polygon.

Does this help to define the problem better. I am stumped.

Any help you can give will be gratefully received.

Many thanks
James
Coordinator
Feb 8, 2013 at 10:03 AM
A quick test proved that NTS actually does compute correct results.

Now it seems that the Provider used does not take advantage of NTS and simply performs a query against a bounding box / envelope. Which provider is it?
Feb 8, 2013 at 11:43 AM
Felix

not sure. I just "captured" the drawn polygon as per the code I posted on Wed at 9:12 PM (see above) so I assume it relates to the line
SharpMap.Data.Providers.GeometryProvider geoProvider = new SharpMap.Data.Providers.GeometryProvider(new List<GeoAPI.Geometries.IGeometry>());  // ???
based on your TEST code I might just have to use a workaround and make the...
var poly = reader.Read(@ geometry.AsText()); 
and adapt the code...
var points = new System.Collections.Generic.List<GeoAPI.Geometries.IGeometry>(new [].... 
to include the points layer (how would I do that by the way????).

Also where is the resulting featureDataSet?

Thanks in anticipation
James
Coordinator
Feb 8, 2013 at 12:43 PM
The question is where do the points come from (OleDbProvider?).
Frankly you have two choices
  1. Fix the underlying provider to perform true intersection testing
  2. Perform true intersection testing on the result.
    For doing that you don't have to read the polygon again, you already have it. Just iterate over the featuredatarows and remove those that don't suffice
geometry.Intersects(MyRow.Geometry)
Hth FObermaier
Feb 8, 2013 at 12:53 PM
<div dir="ltr">Yes the underlying points (X, Y) are from an Access DB using OleDBProvider. <div><br> </div> <div style="">Is there a way to pull in the points from Access without OleDBProvider?</div> <div style=""><br> </div> <div style="">Cheers</div> <div style="">James</div> <div><br> <br> <br> <div>On 8 February 2013 12:43, FObermaier <span dir="ltr">&lt;<a href="mailto:notifications@codeplex.com" target="_blank">notifications@codeplex.com</a>&gt;</span> wrote:<br> <blockquote style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex"> <div> <p>From: FObermaier</p> <div>The question is where do the points come from (OleDbProvider?). <br> Frankly you have two choices<br> <ol> <li>Fix the underlying provider to perform true intersection testing </li><li>Perform true intersection testing on the result. <br> For doing that you don't have to read the polygon again, you already have it. Just iterate over the featuredatarows and remove those that don't suffice </li></ol> <pre><code>geometry.Intersects(MyRow.Geometry)</code></pre> Hth FObermaier<br> </div> <div> <div> <p>Read the <a href="http://sharpmap.codeplex.com/discussions/429469#post998478" target="_blank"> full discussion online</a>.</p> <p>To add a post to this discussion, reply to this email (<a href="mailto:SharpMap@discussions.codeplex.com?subject=[SharpMap:429469]" target="_blank">SharpMap@discussions.codeplex.com</a>)</p> <p>To start a new discussion for this project, email <a href="mailto:SharpMap@discussions.codeplex.com" target="_blank"> SharpMap@discussions.codeplex.com</a></p> <p>You are receiving this email because you subscribed to this discussion on CodePlex. You can <a href="https://sharpmap.codeplex.com/discussions/429469/unsubscribe/" target="_blank"> unsubscribe</a> on CodePlex.com.</p> <p>Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com</p> </div> </div> </div> </blockquote> </div> <br> </div> </div>
Coordinator
Feb 8, 2013 at 3:10 PM
I updated the test to use OleDbPoint provider and fixed a minor issue that prevented true intersection testing.
Hth FObermaier
Feb 8, 2013 at 3:16 PM
<div dir="ltr">So how would this impact my oringinal GeometryDefined code?... (I do not use Unit tests (maybe I should start!!!) <div><br> </div> <div style="">Thanks</div> <div> <div>James <br> </div> <br> <br> <div>On 8 February 2013 15:10, FObermaier <span dir="ltr">&lt;<a href="mailto:notifications@codeplex.com" target="_blank">notifications@codeplex.com</a>&gt;</span> wrote:<br> <blockquote style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex"> <div> <p>From: FObermaier</p> <div>I updated the test to use OleDbPoint provider and fixed a minor issue that prevented true intersection testing. <br> Hth FObermaier<br> </div> <div> <div> <p>Read the <a href="http://sharpmap.codeplex.com/discussions/429469#post998569" target="_blank"> full discussion online</a>.</p> <p>To add a post to this discussion, reply to this email (<a href="mailto:SharpMap@discussions.codeplex.com?subject=[SharpMap:429469]" target="_blank">SharpMap@discussions.codeplex.com</a>)</p> <p>To start a new discussion for this project, email <a href="mailto:SharpMap@discussions.codeplex.com" target="_blank"> SharpMap@discussions.codeplex.com</a></p> <p>You are receiving this email because you subscribed to this discussion on CodePlex. You can <a href="https://sharpmap.codeplex.com/discussions/429469/unsubscribe/" target="_blank"> unsubscribe</a> on CodePlex.com.</p> <p>Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com</p> </div> </div> </div> </blockquote> </div> <br> </div> </div>
Coordinator
Feb 8, 2013 at 3:17 PM
Update your SharpMap source and you should be all set
Feb 8, 2013 at 3:21 PM
So if I download the latest source code an recompile it and reference it then my original code should be OK?

Sorry for my continued thickness!

Cheers
James
Feb 9, 2013 at 6:22 PM
I have downloaded the latest source code. Built/Compiled it. Added the references to my project. Added the appropriate tools to my tool bar and replaced all of them on my project forms. I relinked the key pieces of code and now have two problems.

1) The labels for my points layer do not show on the map... I have not changed my code from Friday which worked before the new SharpMap source code...
 private void button2_Click(object sender, EventArgs e)
        {
            /// Define local variables
            String MyDBName = "";
            String MySwitch = "";
            String tablename = "";
            String oid = "";
            String xColumn = "";
            String yColumn = "";
            String LabelsCol = "";

            string connStr = "";
            openFileDialog1.ShowDialog();

            if (openFileDialog1.FileName != "")
                connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + openFileDialog1.FileName;

////// Add POINTS layer:
            string MyPointsName ="Points Layer Name";

                 SharpMap.Layers.VectorLayer layPoints = new SharpMap.Layers.VectorLayer(MyPointsName);
                    //Set the datasource to the database
                    layPoints.DataSource = new SharpMap.Data.Providers.OleDbPoint(connStr, tablename, oid, xColumn, yColumn);
                    //Add layer to map
                    mapBox1.Map.Layers.Add(layPoints);
                    LayerListBox.Items.Add(MyPointsName, true);
                   // mapBox1.Map.ZoomToExtents();
                    mapBox1.Refresh();

////// Add labels for POINTS layer:

                    SharpMap.Layers.LabelLayer layLabels = new SharpMap.Layers.LabelLayer(MyPointsName+" LABELS");

                    //Set the datasource to that of layPoints.
                    layLabels.DataSource = layPoints.DataSource;
                    layLabels.Enabled = true;
                    LabelsCol = "PointID";
                    //Specifiy field that contains the label string.
                    layLabels.LabelColumn = "PointID";

                    //Set the label style
                    layLabels.Style = new SharpMap.Styles.LabelStyle();
                    layLabels.Style.ForeColor = Color.Black;
                    layLabels.Style.Font = new Font(FontFamily.GenericSansSerif, 8);
                    layLabels.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Left;
                    layLabels.Style.VerticalAlignment = SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Bottom;
                    layLabels.Style.Offset = new PointF(2, 2);
                    layLabels.Style.Halo = new Pen(Color.GhostWhite, 2);
                    layLabels.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

                    mapBox1.Map.Layers.Add(layLabels);
                    //mapBox1.Map.ZoomToExtents();
                    mapBox1.Refresh();
        }
The labels code used to work without a hitch but now nothing. Any clues?


2) Now when I use geometry defined... I have used the geometry.AsText(); to see if I get the polygon (and indeed I do as before). However... no matter what sort of polygon I draw I never get anything selected even when there are say 10 or more points within the drawn polygon.... code I used is...
 private void mapBox1_GeometryDefined(GeoAPI.Geometries.IGeometry geometry)
        {
            if (geometry.OgcGeometryType == GeoAPI.Geometries.OgcGeometryType.Polygon)
            {
                /// Check a polygon has been selected/created
                MessageBox.Show(geometry.AsText());

                //Add Polygon layer:
                string MyPolygonName = "Polygon Layer Name";

                SharpMap.Layers.VectorLayer POLY = new VectorLayer(MyPolygonName);

                SharpMap.Data.Providers.GeometryProvider geoProvider = new SharpMap.Data.Providers.GeometryProvider(new List<GeoAPI.Geometries.IGeometry>());

                geoProvider.SRID = 2236;
                geoProvider.Geometries.Add(geometry);
                POLY.DataSource = geoProvider;               /// Assign datasource to layer "POLY"
                POLY.Style = new SharpMap.Styles.VectorStyle();
                Color c = Color.FromArgb(50, 254, 0, 0);
                Brush b = new SolidBrush(c);
                POLY.Style.Fill = b;
                POLY.Style.Line.Color = Color.Gold;
                POLY.Style.Line.Width = 1.0f;
                POLY.Style.PointColor = Brushes.Red;
                POLY.Style.EnableOutline = true;
                POLY.Style.Outline = new Pen(Color.Red);
                mapBox1.Map.Layers.Add(POLY);
                LayerListBox.Items.Add(MyPolygonName, true);
                mapBox1.Refresh();

                var pPoly = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers[3]).DataSource;
                var p = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers[1]).DataSource;
                var fds = new SharpMap.Data.FeatureDataSet();

                try
                {
                    p.ExecuteIntersectionQuery(geometry, fds);

                    dataGridView1.DataSource = fds.Tables[0];
                    String MyHoleList = "You selected the following holes...";
                    String QueryINList = "";

                    foreach (DataRow MyRow in fds.Tables[0].Rows)
                    {
                        {
                            MyHoleList = MyHoleList + MyRow["PointID"].ToString() + ", ";
                            QueryINList = QueryINList + "'" + MyRow["PointID"].ToString() + "',";
                        }
                    }

                    QueryINList = QueryINList.Substring(0, (QueryINList.Length - 1));

                    MessageBox.Show(QueryINList, "RESULT", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch //(System.Exception excep)
                {
                    MessageBox.Show("No POINTS selected in polygon");
                }
            }

        }
This code did work but had the bounding box selection problem which was selecting too many points (i.e., outside the drawn polygon.

Do you have any ideas why this is no longer working?... it seemed like we were so close.

Thanks
James
Coordinator
Feb 9, 2013 at 8:53 PM
Not knowing how you tend to work, I'd suggest you use the SharpMap solution as your base and add all your personal SharpMap related projects to the sharpmap solution. This way you can debug SharpMap code yourself and find setup errors easier.
You can use TortoiseSVN to keep the sharpmap source in sync, that way you don't need to download the whole thing and readjust you solution again and again.

Hth FObermaier
Feb 9, 2013 at 10:14 PM
Hi

I understand what you are saying. My project is using all the SharpMap assemblies/DLLs built from the Trunk 101580. It all looks great... just now have missing labels and the polygon selection which I have been using all along doesn't work.

I understand you have modified/updated the OleDBProvider... this has resulted in labels not showing and the intersection query in the code above returning nothing when previously it did return something although using the bounding box of the polygon.

Perhaps I should revert to a previous version and just make do.

Cheers

James
Feb 9, 2013 at 10:45 PM
Please also note that No errors are flagged when rendering labels - the label just do not appear on the map. Similarly the geometry Defined intersection query just does not return anything into the feature dataset.

James
Coordinator
Feb 10, 2013 at 10:07 AM
fixed ExecuteIntersectionQuery against a bounding box to return rows
Hth FObermaier
Feb 10, 2013 at 5:04 PM
Felix

The latest version you have created works perfectly so far. The labels are back and the polygon selection works a treat. I will now make a concerted effort to learn C# properly and also to use some form of SVN system like Tortoise rather than using binaries all the time... a little bit frightening for me but worth the effort and hopefully easier on you and your colleagues answering my non-stop questions.

I thank you very much for your help on this which may have been frustrating for you and a bit baffling for me. Your patience and continuing hard work are greatly appreciated.

Thank you again so very much

James