TrueIntersectionTestingWithNTS.cs :  » GIS » SharpMap » ExampleCodeSnippets » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GIS » SharpMap 
SharpMap » ExampleCodeSnippets » TrueIntersectionTestingWithNTS.cs
//This example requires references to 
//SharpMap.dll (or project)
//SharpMap.Extensions.dll (or project) 
//NetTopologySuite.dll (from the ExternalReferences directory)
//GeoAPI.dll (from the ExternalReferences directory)

#region Using Statements

using GisSharpBlog.NetTopologySuite.Geometries;
using SharpMap.Converters.NTS;
using SharpMap.Data;
using SharpMap.Data.Providers;
using GeometrySharpMap.Geometries.Geometry;

#endregion

namespace ExampleCodeSnippets{
    /// <summary>
    /// Contains two methods for doing true intersection testing on geometries using NetTopologySuite
    /// </summary>
    public class TrueIntersectionTestingWithNTS
    {
        /// <summary>
        /// This method returns a FeatureDataTable containing all the rows from the shapefile that intersect the testGeometry.
        /// The ShapeFile.ExecuteIntersectionQuery method only tests bounding boxes so we use the FilterDelegate property to add a true 
        /// intersection test using NetTopologySuite
        /// </summary>
        /// <param name="pathToShapefile">The path to the shapefile</param>
        /// <param name="testGeometry">The geometry that we want to test against</param>
        /// <returns></returns>
        public FeatureDataTable GetIntersectingFeaturesUsingFilterDelegate(string pathToShapefile, Geometry testGeometry)
        {
            //create a new shapefile provider 
            using (ShapeFile shapefile = new ShapeFile(pathToShapefile))
            {
                //create an nts GeometryFactory
                GeometryFactory geometryFactory = new GeometryFactory();

                //convert the testGeometry into the equivalent NTS geometry
                GisSharpBlog.NetTopologySuite.Geometries.Geometry testGeometryAsNtsGeom =
                    GeometryConverter.ToNTSGeometry(testGeometry, geometryFactory);

                //set the shapefile providers' FilterDelegate property to a new anonymous method
                //this delegate method will be called for each potential row
                shapefile.FilterDelegate = delegate(FeatureDataRow featureDataRow)
                                               {
                                                   //get the geometry from the featureDataRow
                                                   Geometry rowGeometry = featureDataRow.Geometry;
                                                   //convert it to the equivalent NTS geometry
                                                   GisSharpBlog.NetTopologySuite.Geometries.Geometry
                                                       compareGeometryAsNtsGeometry =
                                                           GeometryConverter.ToNTSGeometry(rowGeometry,
                                                                                           geometryFactory);
                                                   //do the test. Note that the testGeometryAsNtsGeometry is available here because it is 
                                                   //declared in the same scope as the anonymous method.
                                                   bool intersects =
                                                       testGeometryAsNtsGeom.Intersects(compareGeometryAsNtsGeometry);
                                                   //return the result
                                                   return intersects;
                                               };


                //create a new FeatureDataSet
                FeatureDataSet featureDataSet = new FeatureDataSet();
                //open the shapefile
                shapefile.Open();
                //call ExecuteIntersectionQuery. The FilterDelegate will be used to limit the result set
                shapefile.ExecuteIntersectionQuery(testGeometry, featureDataSet);
                //close the shapefile
                shapefile.Close();
                //return the populated FeatureDataTable
                return featureDataSet.Tables[0];
            }
        }

        /// <summary>
        /// This method takes a pre-populated FeatureDataTable and removes rows that do not truly intersect testGeometry
        /// </summary>
        /// <param name="featureDataTable">The FeatureDataTable instance to filter</param>
        /// <param name="testGeometry">the geometry to compare against</param>
        public void PostFilterExistingFeatureDataTable(FeatureDataTable featureDataTable, Geometry testGeometry)
        {
            //first we create a new GeometryFactory.
            GeometryFactory geometryFactory = new GeometryFactory();


            //then we convert the testGeometry into the equivalent NTS geometry
            GisSharpBlog.NetTopologySuite.Geometries.Geometry testGeometryAsNtsGeom =
                GeometryConverter.ToNTSGeometry(testGeometry, geometryFactory);


            //now we loop backwards through the FeatureDataTable 
            for (int i = featureDataTable.Rows.Count - 1; i > -1; i--)
            {
                //we get each row
                FeatureDataRow featureDataRow = featureDataTable.Rows[i] as FeatureDataRow;
                //and get the rows' geometry
                Geometry compareGeometry = featureDataRow.Geometry;
                //convert the rows' geometry into the equivalent NTS geometry
                GisSharpBlog.NetTopologySuite.Geometries.Geometry compareGeometryAsNts =
                    GeometryConverter.ToNTSGeometry(compareGeometry, geometryFactory);
                //now test for intesection (note other operations such as Contains, Within, Disjoint etc can all be done the same way)
                bool intersects = testGeometryAsNtsGeom.Intersects(compareGeometryAsNts);

                //if it doesn't intersect remove the row.
                if (!intersects)
                    featureDataTable.Rows.RemoveAt(i);
            }
        }
    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.