SimplePointInAreaLocator.cs :  » GIS » DeepEarth » GisSharpBlog » NetTopologySuite » Algorithm » 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 » DeepEarth 
DeepEarth » GisSharpBlog » NetTopologySuite » Algorithm » SimplePointInAreaLocator.cs
using System;
using System.Collections;
using GeoAPI.Geometries;
using GisSharpBlog.NetTopologySuite.Geometries;

namespace GisSharpBlog.NetTopologySuite.Algorithm{
    /// <summary>
    /// Computes whether a point
    /// lies in the interior of an area <c>Geometry</c>.
    /// The algorithm used is only guaranteed to return correct results
    /// for points which are not on the boundary of the Geometry.
    /// </summary>
    public class SimplePointInAreaLocator
    {
        /// <summary>
        /// 
        /// </summary>
        private SimplePointInAreaLocator() { }

        /// <summary> 
        /// Locate is the main location function.  It handles both single-element
        /// and multi-element Geometries.  The algorithm for multi-element Geometries
        /// is more complex, since it has to take into account the boundaryDetermination rule.
        /// </summary>
        /// <param name="geom"></param>
        /// <param name="p"></param>
        public static Locations Locate(ICoordinate p, IGeometry geom)
        {
            if (geom.IsEmpty)
                return Locations.Exterior;

            if (ContainsPoint(p, geom))
                return Locations.Interior;
            return Locations.Exterior;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="geom"></param>
        /// <returns></returns>
        private static bool ContainsPoint(ICoordinate p, IGeometry geom)
        {
            if (geom is IPolygon) 
                return ContainsPointInPolygon(p, (IPolygon) geom);            
            else if(geom is IGeometryCollection) 
            {
                IEnumerator geomi = new GeometryCollectionEnumerator((IGeometryCollection) geom);
                while (geomi.MoveNext()) 
                {
                    IGeometry g2 = (IGeometry) geomi.Current;
                    // if(g2 != geom)  --- Diego Guidi say's: Java code tests reference equality: in C# with operator overloads we tests the object.equals()... more slower!                    
                    if (!ReferenceEquals(g2, geom)) 
                        if (ContainsPoint(p, g2))
                            return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="poly"></param>
        /// <returns></returns>
        public static bool ContainsPointInPolygon(ICoordinate p, IPolygon poly)
        {
            if (poly.IsEmpty) 
                return false;
            ILinearRing shell = (ILinearRing) poly.ExteriorRing;
            if (!CGAlgorithms.IsPointInRing(p, shell.Coordinates)) 
                return false;
            // now test if the point lies in or on the holes
            for (int i = 0; i < poly.NumInteriorRings; i++)
            {
                ILinearRing hole = (ILinearRing) poly.GetInteriorRingN(i);
                if (CGAlgorithms.IsPointInRing(p, hole.Coordinates)) 
                    return false;
            }
            return true;
        }
    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.