/// <summary> /// When the user clicks on a source point we will need to trabnslate that location to the nearest point on the network /// </summary> /// <param name="userPoint"></param> /// <param name="tolerence"></param> /// <param name="growValue"></param> /// <param name="theLayer"></param> /// <returns></returns> public IntersectionPackage TranslateToPointOnLine(Coordinate userPoint, double tolerence, double growValue, VectorLayer theLayer) { try { // How much should we increase the search area by until we reach tolerence. double GROWVALUE = growValue; // Record how many features are in the bounding box int featureCount = 0; // The features that are withinthe boundbox of GROWVALUE of UserPoints. Collection<IGeometry> geometrysInTolerence = null; IGeometry clickPointAsNts = null; while ((featureCount == 0) && (GROWVALUE < tolerence)) { // Take the point where the user clicked. Grow it by a set a given amount. We use the boundry // box to find all lines within a given tolerence. clickPointAsNts = _geomFactory.CreatePoint(userPoint); var clickPointBoundry = new Envelope(userPoint); clickPointBoundry = clickPointBoundry.Grow(GROWVALUE); var originalLayer = theLayer; originalLayer.DataSource.Open(); geometrysInTolerence = originalLayer.DataSource.GetGeometriesInView(clickPointBoundry); GROWVALUE *= 2; if (geometrysInTolerence != null) featureCount = geometrysInTolerence.Count; } // If there are any geometries in the boundry box then we loop around them them. We are looking for the cloest point so // we can **try** to perform a snap operation. NOTE: This entire procedure is a bit flawed. if (geometrysInTolerence == null) return null; if (geometrysInTolerence.Count > 0) { double closestDistance = double.MaxValue; Coordinate intersectionPointAsNts = null; ILineString closestToThisLine = null; foreach (IGeometry geometryInTolerence in geometrysInTolerence) { var nearestPoints = NetTopologySuite.Operation.Distance.DistanceOp.NearestPoints(clickPointAsNts, geometryInTolerence); if (nearestPoints == null) return null; // We get two points back. The point where we clicked and the point of intersection (?????) on the line). If // we calculate the distance between the two we can var p1 = nearestPoints[0]; var p2 = nearestPoints[1]; var lDistance = p1.Distance(p2); if (lDistance < closestDistance) { closestDistance = lDistance; intersectionPointAsNts = p2; closestToThisLine = (ILineString)geometryInTolerence; } } // Getting Here would mean that we now know the line we if (closestDistance < double.MaxValue) { var theDeliveryPackage = new IntersectionPackage((IPoint)clickPointAsNts, _geomFactory.CreatePoint(intersectionPointAsNts), closestToThisLine); return theDeliveryPackage; } } else return null; return null; } catch (Exception e1) { System.Diagnostics.Debug.WriteLine(e1.ToString()); return null; } }
/// <summary> /// When the user clicks on a source point we will need to trabnslate that location to the nearest point on the network /// </summary> /// <param name="userPoint"></param> /// <param name="tolerence"></param> /// <param name="growValue"></param> /// <param name="theLayer"></param> /// <returns></returns> public IntersectionPackage TranslateToPointOnLine(Coordinate userPoint, double tolerence, double growValue, VectorLayer theLayer) { try { // How much should we increase the search area by until we reach tolerence. double GROWVALUE = growValue; // Record how many features are in the bounding box int featureCount = 0; // The features that are withinthe boundbox of GROWVALUE of UserPoints. Collection <IGeometry> geometrysInTolerence = null; IGeometry clickPointAsNts = null; while ((featureCount == 0) && (GROWVALUE < tolerence)) { // Take the point where the user clicked. Grow it by a set a given amount. We use the boundry // box to find all lines within a given tolerence. clickPointAsNts = _geomFactory.CreatePoint(userPoint); var clickPointBoundry = new Envelope(userPoint); clickPointBoundry = clickPointBoundry.Grow(GROWVALUE); var originalLayer = theLayer; originalLayer.DataSource.Open(); geometrysInTolerence = originalLayer.DataSource.GetGeometriesInView(clickPointBoundry); GROWVALUE *= 2; if (geometrysInTolerence != null) { featureCount = geometrysInTolerence.Count; } } // If there are any geometries in the boundry box then we loop around them them. We are looking for the cloest point so // we can **try** to perform a snap operation. NOTE: This entire procedure is a bit flawed. if (geometrysInTolerence == null) { return(null); } if (geometrysInTolerence.Count > 0) { double closestDistance = double.MaxValue; Coordinate intersectionPointAsNts = null; ILineString closestToThisLine = null; foreach (IGeometry geometryInTolerence in geometrysInTolerence) { var nearestPoints = NetTopologySuite.Operation.Distance.DistanceOp.NearestPoints(clickPointAsNts, geometryInTolerence); if (nearestPoints == null) { return(null); } // We get two points back. The point where we clicked and the point of intersection (?????) on the line). If // we calculate the distance between the two we can var p1 = nearestPoints[0]; var p2 = nearestPoints[1]; var lDistance = p1.Distance(p2); if (lDistance < closestDistance) { closestDistance = lDistance; intersectionPointAsNts = p2; closestToThisLine = (ILineString)geometryInTolerence; } } // Getting Here would mean that we now know the line we if (closestDistance < double.MaxValue) { var theDeliveryPackage = new IntersectionPackage((IPoint)clickPointAsNts, _geomFactory.CreatePoint(intersectionPointAsNts), closestToThisLine); return(theDeliveryPackage); } } else { return(null); } return(null); } catch (Exception e1) { System.Diagnostics.Debug.WriteLine(e1.ToString()); return(null); } }