This project has moved and is read-only. For the latest updates, please go here.


Topics: SharpMap v0.9 / v1.x, WinForms Controls
Mar 1, 2013 at 1:32 PM
Hi Folks

I have built a WinForms app. The user can draw a polygon.... which I can then add on its own layer to a MAP. the geometry created I can save via "MyGeometry.AsText();" which looks like this...
POLYGON ((382713.12965714838 274799.48096023488, 382668.62684654753 274778.11961114651, 382723.81033169257 274719.37590115337, 382778.99381683761 274739.55050862575, 382766.53302986937 274797.10747700284, 382766.53302986937 274797.10747700284, 382713.12965714838 274799.48096023488))
and save it to a *.TXT file.

How do I load the *.TXT file and create a new layer/geometry?

Any help gratefully appreciated.

Mar 1, 2013 at 5:00 PM
You need to use a NetTopologySuite.IO.WKTReader.
Mar 1, 2013 at 5:31 PM

Thanks... I'll have a try over the weekend.

Best regards
Mar 3, 2013 at 8:29 PM

I have generated the following which all seems to work without a hitch.
  private void button4_Click_1(object sender, EventArgs e)   /// Read in a TXT file containing POLYGON data
            // Pick a previously saved text file (WKT)
            // Read in the file and stick the text into a string "UpLoadFileText"
            StreamReader streamReader = new StreamReader(openFileDialog2.FileName);
            string UpLoadFiletext = streamReader.ReadToEnd();
            Upload_TB.Text = UpLoadFiletext; // put string in textBox to confirm that file has been read properly

            // Read data from "UpLoadFileText" into newly created NetTopologySuite.IO.WKTReader...
            NetTopologySuite.IO.WKTReader MyWKTReader = new NetTopologySuite.IO.WKTReader();
            // now what????
            // I want to add the MyWKTReader data as a new layer to my Map
           SharpMap.Layers.VectorLayer MyNewLayer = new SharpMap.Layers.VectorLayer("My WKT Layer");
///  I now need to somehow use MyWKTReader to recreate the polygon and put it onto the 
///  "My WKT Layer" immediately above any clues gratefully received

            /// the following adds the layer name to a comboBox

How do I convert the contents of the MyWKTReader ( NetTopologySuite.IO.WKTReader) into a geometry which can be used as a datasource for a new layer in my Map?

Mar 4, 2013 at 6:56 AM
var MyReadGeometry = MyWKTReader.Read(UpLoadFileText);
var MyNewProvider = new SharpMap.Data.Providers.GeometryProvider(MyReadGeometry);
var MyNewVectorLayer = new SharpMap.Layers.VectorLayer("My WKT Layer", MyNewProvider);
//... Styling, add to layer
Mar 5, 2013 at 8:36 PM
Edited Mar 5, 2013 at 9:21 PM

All working great... how do I now convert the newly read WKT layer into a geometry for intersection queries... like ...
                var pPoly = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers[QueryLayerIndex]).DataSource;
                var p = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers.[1]).DataSource;
                var fds = new SharpMap.Data.FeatureDataSet();

                 var MyGeometry= ?????????????  /////   here is the snag!

                    p.ExecuteIntersectionQuery(MyGeometry, fds);  /// ???????  obviously need to cast something to "MyGeometry"

                    dataGridView1.DataSource = fds.Tables[0];  //// etc
basically the loaded polygon from the WKT layer "pPoly = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers[QueryLayerIndex]).DataSource;" i need to be the querylayer... i.e., "MyGeometry"

Any help would be wonderful!!!!

Mar 6, 2013 at 7:17 AM
var p = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers.[1]).DataSource;
var MyGeometry = p.GetGeometryByID(0);
var gp = p as SharpMap.Data.Providers.GeometryProvider;
GeoAPI.Geometries.IGeometry MyGeometry = null;
if (gp != null)
    MyGeometry = gp.Geometries[0];
if (MyGeometry != null)
    // ...

Mar 6, 2013 at 9:12 AM

Thanks again for the quick response... I just need a little clarification... I need to query layer[1] (a points layer) to see if the points are within the loaded WKT polygon. Is the following correct based on what you have suggested so far?
/// pPoly below relates to the loaded WKT polygon
var pPoly = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers[QueryLayerIndex]).DataSource;
/// p below relates to a layer of points from OLEDBProvider
var p = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers.[1]).DataSource;
Am I correct in assuming that I can cast the pPoly using...
var pPoly = ((SharpMap.Layers.VectorLayer)mapBox1.Map.Layers.[QueryLayerIndex]).DataSource;
var MyGeometry = pPoly.GetGeometryByID(0);  // there is only one geometry that being the loaded polygon from the WKT 

var gp = pPoly as SharpMap.Data.Providers.GeometryProvider;
GeoAPI.Geometries.IGeometry MyGeometry = null;

/// then to see if points are within pPoly would the following also be correct
MyGeometry = gp.Geometries[0];
p.ExecuteIntersectionQuery(MyGeometry, fds);  
dataGridView1.DataSource = fds.Tables[0];   // show intersection query results in a dataGridView
I do not have access to my development machine at the moment but knowing the answer to the above would help me later (tonight) when I do get access.

Mar 6, 2013 at 10:57 AM
I pointed out two possible ways, either one should work.
Mar 6, 2013 at 11:34 AM
Thanks again

I'll let you know how I get on


On 6 March 2013 10:57, FObermaier <> wrote:

From: FObermaier

I pointed out two possible ways, either one should work.

Read the full discussion online.

To add a post to this discussion, reply to this email (

To start a new discussion for this project, email

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at

Mar 9, 2013 at 2:18 PM

Taken on board your advice and coding tips and it all works brilliantly.

Thank you VERY much!