SAVE & RETRIEVE GEOMETRY

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.

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

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

Best regards
James
Mar 3, 2013 at 8:29 PM
Felix

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)
            openFileDialog2.ShowDialog();
            // 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
            streamReader.Close();

            // Read data from "UpLoadFileText" into newly created NetTopologySuite.IO.WKTReader...
            NetTopologySuite.IO.WKTReader MyWKTReader = new NetTopologySuite.IO.WKTReader();
            MyWKTReader.Read(UpLoadFiletext);
           
            // 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
           Polygons_CB.Items.Add(Path.GetFileName(openFileDialog2.FileName));

        }
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?

Cheers
James
Coordinator
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
Felix

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!

                try
                {
                    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!!!!

TIA
James
Coordinator
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
Felix

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.

TIA
James
Coordinator
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

Cheers
James


On 6 March 2013 10:57, FObermaier <notifications@codeplex.com> 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 (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


Mar 9, 2013 at 2:18 PM
Felix

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

Thank you VERY much!

Cheers
James