/// <summary> /// Method splitted to permit testing without reading DHI CrossSection /// </summary> /// <param name="P1"></param> /// <param name="P2"></param> /// <param name="Chainage"></param> public void SetPoints(IXYPoint P1, IXYPoint P2, double Chainage1, double Chainage2, double Chainage) { double dx = P2.X - P1.X; double dy = P2.Y - P1.Y; double dchainage = (Chainage - Chainage1)/( Chainage2 - Chainage1); MidStreamLocation = new XYPoint(P1.X + dchainage * dx, P1.Y + dchainage * dy); double lenght = Math.Pow(Math.Pow(dx,2)+ Math.Pow(dy,2),0.5); UnityVector = new XYPoint(dx / lenght, dy / lenght); //Now build the line where the cross section is located. if (_cs != null) { Line = new XYPolyline(); //MidPoint is set to where Marker 2 is placed double xOffset = _xsec.LowestPoint.X; for (int i = 0; i < _xsec.Points.Count(); i++) { Line.Points.Add(new XYPoint(MidStreamLocation.X - UnityVector.Y * (_xsec.Points[i].X - xOffset), MidStreamLocation.Y + UnityVector.X * (_xsec.Points[i].X - xOffset))); } } }
public void BeginGetHeight(XYPoint point, int UTMZone) { string url = String.Format("http://kmswww3.kms.dk/FindMinHoejde/Default.aspx?display=show&csIn=utm{2}_euref89&csOut=utm32_euref89&x={0}&y={1}&c=dk", point.X, point.Y, UTMZone); request = (HttpWebRequest)WebRequest.Create(url); result= (IAsyncResult) request.BeginGetResponse(new AsyncCallback(RespCallback),null); }
public void SetPointsTest1() { CrossSection target = new CrossSection(); IXYPoint p1 = new HydroNumerics.Geometry.XYPoint(0, 0); IXYPoint p2 = new HydroNumerics.Geometry.XYPoint(10, 2); target.SetPoints(p1, p2, 0, 10, 5); Assert.AreEqual(5, target.MidStreamLocation.X); Assert.AreEqual(1, target.MidStreamLocation.Y); }
public void Equals() { XYPoint p1 = new XYPoint(2,3); XYPoint p2 = new XYPoint(2,3); XYPoint p3 = new XYPoint(2,-3); XYLine l1 = new XYLine(2,3,3,4); Assert.AreEqual(true, p1.Equals(p1),"Test1"); Assert.AreEqual(true, p1.Equals(p2),"Test2"); Assert.AreEqual(false, p1.Equals(p3),"Test3"); Assert.AreEqual(false, p1.Equals(l1),"Test4"); }
public void PropertyTest() { XYPoint xypoint = new XYPoint(2,3); Assert.AreEqual((double) 2, xypoint.X); Assert.AreEqual((double) 3, xypoint.Y); xypoint.X = 6; xypoint.Y = 7; Assert.AreEqual((double) 6, xypoint.X); Assert.AreEqual((double) 7, xypoint.Y); }
public void SetPointsTest() { CrossSection_Accessor target = new CrossSection_Accessor(); HydroNumerics.Geometry.IXYPoint p1 = new HydroNumerics.Geometry.XYPoint(0, 0); HydroNumerics.Geometry.IXYPoint p2 = new HydroNumerics.Geometry.XYPoint(10, 2); target.SetPoints(p1, p2, 0, 10, 5); Assert.AreEqual(5, target.MidStreamLocation.X); Assert.AreEqual(1, target.MidStreamLocation.Y); }
public void DrawdownTest() { TheisAnalysis target = new TheisAnalysis(); // TODO: Initialize to an appropriate value IXYPoint PumpingWell = new XYPoint(0, 0); double PumpingRate = 5000.0/24.0 / 3600.0; //5000 m3/day double Storativity = 1.83e-3; double Transmissivity = 164.49 / 24.0 / 3600.0; //90 m2/day TimeSpan Time = TimeSpan.FromMinutes(100); IXYPoint ObservationPoint = new XYPoint(50, 50); double actual; actual = target.Drawdown(PumpingWell, PumpingRate, Storativity, Transmissivity, Time, ObservationPoint); Assert.AreEqual(2.959, actual, 0.01); }
public void GetImageryTest() { XYPoint point = new XYPoint(715281.56, 6189341.78); double dx = 5000; double dy = 5000; BitmapImage actual; actual = Map.GetImagery(point, dx, dy); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2)); FileStream filestream = new FileStream(@"c:\temp\pict.jpg", FileMode.Create); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(actual)); encoder.Save(filestream); filestream.Dispose(); }
/// <summary> /// The method decides if the triangle formed by P(i-1), P(i) and /// P(i+1) from Polygon are intersected by any of the other points /// of the polygon. /// </summary> /// <param name="i">Middle index for the three points that forms the triangle</param> /// <returns> /// <p>true: If the triangle P(i-1), P(i), P(i+1) is intersected by other parts of Polygon</p> /// <p>false: otherwise</p> /// </returns> protected bool IsIntersected(int i) { double x = 0; double y = 0; int n = Points.Count; int im1 = i - 1; int ip1 = i + 1; if (i == 0) { im1 = n - 1; } else if (i == n - 1) { ip1 = 0; } XYPoint nodeim1 = new XYPoint((XYPoint)Points[im1]); XYPoint nodei = new XYPoint((XYPoint)Points[i]); XYPoint nodeip1 = new XYPoint((XYPoint)Points[ip1]); XYPolygon localPolygon = new XYPolygon(); localPolygon.Points.Add(nodeim1); localPolygon.Points.Add(nodei); localPolygon.Points.Add(nodeip1); int j = 0; bool intersected = false; while (((j < n - 1) && (!intersected))) { x = Points[j].X; y = Points[j].Y; if (((((j != im1) && (j != i)) && (j != ip1)) && XYGeometryTools.IsPointInPolygon(x, y, localPolygon))) { return(true); } else { j++; } } return(false); }
/// <summary> /// Calculate intersection point between two line segments. /// </summary> /// <param name="p1">First point in first line</param> /// <param name="p2">Second point in first line</param> /// <param name="p3">First point in second line</param> /// <param name="p4">Second point in second line</param> /// <returns>Intersection point</returns> public static XYPoint CalculateIntersectionPoint(IXYPoint p1, IXYPoint p2, IXYPoint p3, IXYPoint p4) { if (!DoLineSegmentsIntersect(p1, p2, p3, p4)) { throw new System.Exception("Attempt to calculate intersection point between non intersecting lines. CalculateIntersectionPoint failed."); } XYPoint interSectionPoint = new XYPoint(); double a = p1.X * p2.Y - p2.X * p1.Y; double b = p3.X * p4.Y - p4.X * p3.Y; double c = (p1.X - p2.X) * (p3.Y - p4.Y) - (p3.X - p4.X) * (p1.Y - p2.Y); interSectionPoint.X = (a * (p3.X - p4.X) - (b * (p1.X - p2.X))) / c; interSectionPoint.Y = (a * (p3.Y - p4.Y) - (b * (p1.Y - p2.Y))) / c; return(interSectionPoint); }
static void Main(string[] args) { XYPoint point = new XYPoint(715281.56, 6189341.78); double dx = 5000; double dy = 5000; actual = Map.GetImagery(point, dx, dy); actual.DownloadCompleted += new EventHandler(actual_DownloadCompleted); System.Windows.Controls.Image im = new System.Windows.Controls.Image(); im.Source = actual; im.SourceUpdated += new EventHandler<System.Windows.Data.DataTransferEventArgs>(im_SourceUpdated); im.UpdateLayout(); Console.WriteLine(DateTime.Now.Second); Console.ReadLine(); }
public static BitmapImage GetImagery(XYPoint point, double dx, double dy) { try { string key = "ArfYQbvMgvi7NOpxSAuS3lkBlVc3NqzgBcVo-yxNyr_KPVJbhwR22c9cVfG7AnwY"; MapUriRequest mapUriRequest = new MapUriRequest(); // Set credentials using a valid Bing Maps key mapUriRequest.Credentials = new Credentials(); mapUriRequest.Credentials.ApplicationId = key; // Set the location of the requested image mapUriRequest.Center = new Location(); mapUriRequest.Center.Latitude = point.Latitude; mapUriRequest.Center.Longitude = point.Longitude; // Set the map style and zoom level MapUriOptions mapUriOptions = new MapUriOptions(); mapUriOptions.Style = MapStyle.AerialWithLabels_v1; mapUriOptions.ZoomLevel = 13; // Set the size of the requested image in pixels mapUriOptions.ImageSize = new SizeOfint(); mapUriOptions.ImageSize.Height = (int)(dy / 19.11); //19.11 to get meters mapUriOptions.ImageSize.Width = (int)(dx / 19.11); mapUriRequest.Options = mapUriOptions; //Make the request and return the URI ImageryServiceClient imageryService = new ImageryServiceClient("BasicHttpBinding_IImageryService"); MapUriResponse mapUriResponse = imageryService.GetMapUri(mapUriRequest); return new BitmapImage(new Uri(mapUriResponse.Uri)); } catch (Exception e) { return null; } }
/// <summary> /// Returns an ArrayList of triangles of type XYPolygon describing the /// triangalation of the polygon. /// </summary> /// <param></param> /// <returns> /// A triangulation of the polygon. /// </returns> public List <XYPolygon> GetTriangulation() { if (TriangleList == null) { int i = 0; int im1 = 0; int ip1 = 0; int n = 0; XYPolygon LocalPolygon = new XYPolygon(this); TriangleList = new List <XYPolygon>(); while (LocalPolygon.Points.Count > 3) { i = LocalPolygon.FindEar(); n = LocalPolygon.Points.Count; im1 = i - 1; ip1 = i + 1; if (i == 0) { im1 = n - 1; } else if (i == n - 1) { ip1 = 0; } XYPoint Nodeim1 = new XYPoint((XYPoint)LocalPolygon.Points[im1]); XYPoint Nodei = new XYPoint((XYPoint)LocalPolygon.Points[i]); XYPoint Nodeip1 = new XYPoint((XYPoint)LocalPolygon.Points[ip1]); XYPolygon Triangle = new XYPolygon(); Triangle.Points.Add(Nodeim1); Triangle.Points.Add(Nodei); Triangle.Points.Add(Nodeip1); TriangleList.Add(Triangle); LocalPolygon.Points.RemoveAt(i); } TriangleList.Add(LocalPolygon); } return(TriangleList); }
static void Main(string[] args) { double area = 0; using (ShapeReader sr = new ShapeReader(@"D:/Dropbox/ProjektData/Øvre Suså Ålav/skov2.shp")) { foreach (var gd in sr.GeoData) { area += ((IXYPolygon)gd.Geometry).GetArea(); } } double ha = area / 10000; XYPoint point = new XYPoint(715281.56, 6189341.78); double dx = 5000; double dy = 5000; actual = Map.GetImagery(point, dx, dy); actual.DownloadCompleted += new EventHandler(actual_DownloadCompleted); System.Windows.Controls.Image im = new System.Windows.Controls.Image(); im.Source = actual; im.SourceUpdated += new EventHandler<System.Windows.Data.DataTransferEventArgs>(im_SourceUpdated); im.UpdateLayout(); Console.WriteLine(DateTime.Now.Second); Console.ReadLine(); }
/// <summary> /// Calculates length of line inside polygon. Parts of the line that is on the edge of /// the polygon only counts with half their length. /// </summary> /// <param name="line">Line</param> /// <param name="polygon">Polygon</param> /// <returns> /// Length of line inside polygon. /// </returns> protected static double CalculateLengthOfLineInsidePolygon(XYLine line, XYPolygon polygon) { ArrayList lineList = new ArrayList(); lineList.Add(new XYLine(line)); for (int i = 0; i < polygon.Points.Count; i++) // For all lines in the polygon { for (int n = 0; n < lineList.Count; n++) { if (lineList.Count > 1000) { throw new Exception("Problems in ElementMapper, line has been cut in more than 1000 pieces !!!"); } if (DoLineSegmentsIntersect((XYLine)lineList[n], polygon.GetLine(i))) { // Split the intersecting line into two lines XYPoint IntersectionPoint = new XYPoint(CalculateIntersectionPoint((XYLine)lineList[n], polygon.GetLine(i))); lineList.Add(new XYLine(IntersectionPoint, ((XYLine) lineList[n]).P2)); ((XYLine) lineList[n]).P2.X = IntersectionPoint.X; ((XYLine) lineList[n]).P2.Y = IntersectionPoint.Y; break; } } } for (int i = 0; i < lineList.Count; i++) { if (lineList.Count > 1000) { throw new Exception("Problems in ElementMapper, line has been cuttes in more than 100 pieces !!!"); } for (int j = 0; j < polygon.Points.Count; j++) { if (IsPointInLineInterior( polygon.GetLine(j).P1, ((XYLine) lineList[i]))) { lineList.Add(new XYLine(polygon.GetLine(j).P1, ((XYLine) lineList[i]).P2)); ((XYLine) lineList[i]).P2.X = polygon.GetLine(j).P1.X; ((XYLine) lineList[i]).P2.Y = polygon.GetLine(j).P1.Y; } } } double lengthInside = 0; for (int i = 0; i < lineList.Count; i++) { double sharedLength = 0; for (int j = 0; j < polygon.Points.Count; j++) { sharedLength += CalculateSharedLength(((XYLine) lineList[i]), polygon.GetLine(j)); } if (sharedLength > EPSILON) { lengthInside += sharedLength/2; } else if (IsPointInPolygon(((XYLine) lineList[i]).GetMidpoint(), polygon)) { lengthInside += ((XYLine) lineList[i]).GetLength(); } } return lengthInside; }
/// <summary> /// Calculates the length that two lines overlap. /// </summary> /// <param name="lineA">Line</param> /// <param name="lineB">Line</param> /// <returns> /// Length of shared line segment. /// </returns> protected static double CalculateSharedLength(XYLine lineA, XYLine lineB) { if ( Math.Abs(lineA.P2.X-lineA.P1.X)<EPSILON && Math.Abs(lineB.P2.X-lineB.P1.X)<EPSILON &&Math.Abs(lineA.P1.X-lineB.P1.X)<EPSILON) { double YP1A = Math.Min(lineA.P1.Y, lineA.P2.Y); double YP2A = Math.Max(lineA.P1.Y, lineA.P2.Y); double YP1B = Math.Min(lineB.P1.Y, lineB.P2.Y); double YP2B = Math.Max(lineB.P1.Y, lineB.P2.Y); double YP1 = Math.Max(YP1A, YP1B); double YP2 = Math.Min(YP2A, YP2B); if (YP1 < YP2) { return YP2-YP1; } else { return 0; } } else if(Math.Abs(lineA.P2.X-lineA.P1.X)<EPSILON || Math.Abs(lineB.P2.X-lineB.P1.X)<EPSILON) { return 0; } else { IXYPoint P1A = new XYPoint(); IXYPoint P2A = new XYPoint(); if (lineA.P1.X < lineA.P2.X) { P1A = lineA.P1; P2A = lineA.P2; } else { P1A = lineA.P2; P2A = lineA.P1; } IXYPoint P1B = new XYPoint(); IXYPoint P2B = new XYPoint(); if (lineB.P1.X < lineB.P2.X) { P1B = lineB.P1; P2B = lineB.P2; } else { P1B = lineB.P2; P2B = lineB.P1; } double alphaA = (P2A.Y - P1A.Y)/(P2A.X - P1A.X); double betaA = -alphaA*P2A.X + P2A.Y; double alphaB = (P2B.Y - P1B.Y)/(P2B.X - P1B.X); double betaB = -alphaA*P2B.X + P2B.Y; if (Math.Abs(alphaA-alphaB)<EPSILON && Math.Abs(betaA-betaB)<EPSILON) { double x1 = Math.Max(P1A.X, P1B.X); double x2 = Math.Min(P2A.X, P2B.X); if (x1 < x2) { XYLine line = new XYLine(x1, alphaA*x1+betaA, x2, alphaA*x2+betaA); return line.GetLength(); } else { return 0; } } else { return 0; } } }
/// <summary> /// Calculate intersection point between two line segments. /// </summary> /// <param name="p1">First point in first line</param> /// <param name="p2">Second point in first line</param> /// <param name="p3">First point in second line</param> /// <param name="p4">Second point in second line</param> /// <returns>Intersection point</returns> public static XYPoint CalculateIntersectionPoint(IXYPoint p1, IXYPoint p2, IXYPoint p3, IXYPoint p4) { if (!DoLineSegmentsIntersect(p1,p2,p3,p4)) { throw new System.Exception("Attempt to calculate intersection point between non intersecting lines. CalculateIntersectionPoint failed."); } XYPoint interSectionPoint = new XYPoint(); double a = p1.X * p2.Y - p2.X * p1.Y; double b = p3.X * p4.Y - p4.X * p3.Y; double c = (p1.X - p2.X) * (p3.Y - p4.Y) - (p3.X - p4.X) * (p1.Y - p2.Y); interSectionPoint.X = (a * (p3.X - p4.X) - (b * (p1.X - p2.X))) / c; interSectionPoint.Y = (a * (p3.Y - p4.Y) - (b * (p1.Y - p2.Y))) / c; return interSectionPoint; }
/// <summary> /// The method decides if the triangle formed by P(i-1), P(i) and /// P(i+1) from Polygon are intersected by any of the other points /// of the polygon. /// </summary> /// <param name="i">Middle index for the three points that forms the triangle</param> /// <returns> /// <p>true: If the triangle P(i-1), P(i), P(i+1) is intersected by other parts of Polygon</p> /// <p>false: otherwise</p> /// </returns> protected bool IsIntersected(int i) { double x = 0; double y = 0; int n = Points.Count; int im1 = i-1; int ip1 = i+1; if (i == 0) { im1 = n-1; } else if (i == n-1) { ip1 = 0; } XYPoint nodeim1 = new XYPoint((XYPoint) Points[im1]); XYPoint nodei = new XYPoint((XYPoint) Points[i]); XYPoint nodeip1 = new XYPoint((XYPoint) Points[ip1]); XYPolygon localPolygon = new XYPolygon(); localPolygon.Points.Add(nodeim1); localPolygon.Points.Add(nodei); localPolygon.Points.Add(nodeip1); int j = 0; bool intersected = false; while (((j < n-1) && (!intersected))) { x = Points[j].X; y = Points[j].Y; if (((((j!=im1) && (j!=i)) && (j!=ip1)) && XYGeometryTools.IsPointInPolygon(x,y,localPolygon))) { return true; } else { j++; } } return false; }
public static double ACalculateLineToPointDistance(XYLine line, XYPoint point) { return CalculateLineToPointDistance(line, point); }
public XYPolygon Surround(Func<double,bool> function) { XYPolygon toreturn = new XYPolygon(); List<XYPoint> left = new List<XYPoint>(); List<XYPoint> right = new List<XYPoint>(); for (int i = 0; i < NumberOfRows; i++) { XYPoint x1 = null; XYPoint y1 = null; for (int j = 0; j < NumberOfColumns; j++) { if (function(GetValue(j, i).Value)) { if (x1 == null) x1 = new XYPoint(GetXCenter(j), GetYCenter(NumberOfRows- i)); else y1 = new XYPoint(GetXCenter(j), GetYCenter(NumberOfRows- i)); } } if(x1!=null) left.Add(x1); if(y1!=null) right.Add(y1); } toreturn.Points.AddRange(left); toreturn.Points.AddRange(right); return toreturn; }
/// <summary> /// Interpolates x, y and height. Does not put in lat-long /// </summary> /// <param name="Point"></param> /// <param name="OtherPoint"></param> /// <param name="Distance"></param> /// <returns></returns> public static XYPoint InterpolatePoint(IXYPoint Point, IXYPoint OtherPoint, double Distance) { double relativedistance; if (Distance == 0) relativedistance = 0; else { double d = ((XYPoint)Point).GetDistance(OtherPoint); relativedistance = Distance / d; } XYPoint NewCoordinate = new XYPoint(); NewCoordinate.X = Point.X - relativedistance * (Point.X - OtherPoint.X); NewCoordinate.Y = Point.Y - relativedistance * (Point.Y - OtherPoint.Y); // NewCoordinate. = Point.Height.Value - relativedistance * (Point.Height.Value - OtherPoint.Height.Value); return NewCoordinate; }
public override void Initialize(DateTime Start, DateTime End, IEnumerable<Catchment> Catchments) { base.Initialize(Start, End, Catchments); Dictionary<XYPoint, List<double>> Data = new Dictionary<XYPoint,List<double>>(); XSSFWorkbook hssfwb; using (FileStream file = new FileStream(ExcelFile.FileName, FileMode.Open, FileAccess.Read)) { hssfwb = new XSSFWorkbook(file); } List<IRow> DataRows = new List<IRow>(); var sheet = hssfwb.GetSheet("Ndep_Tot"); for (int row = 1; row <= sheet.LastRowNum; row++) { if (sheet.GetRow(row) != null) //null is when the row only contains empty cells { DataRows.Add(sheet.GetRow(row)); } } using (ShapeReader sr = new ShapeReader(Shapefile.FileName)) { FirstYear = (int)DataRows.First().Cells[0].NumericCellValue; for (int i = 0; i < sr.Data.NoOfEntries; i++) { int icoor = sr.Data.ReadInt(i, "i"); int jcoor = sr.Data.ReadInt(i, "j"); XYPoint point = (XYPoint)sr.ReadNext(i); //Create the timestampseries and set unit to kg/m2/s; var data = DataRows.Where(v => (int)v.Cells[3].NumericCellValue == icoor & (int)v.Cells[4].NumericCellValue == jcoor).OrderBy(v => (int)v.Cells[0].NumericCellValue).Select(v => v.Cells[6].NumericCellValue / (365.0 * 86400.0 * 1.0e6)).ToList(); if(data.Count()>0) Data.Add(point, data); } } foreach (var c in Catchments) { XYPolygon poly = null; if (c.Geometry is XYPolygon) poly = c.Geometry as XYPolygon; else if (c.Geometry is MultiPartPolygon) poly = ((MultiPartPolygon)c.Geometry).Polygons.First(); //Just use the first polygon double LakeArea = c.Lakes.Sum(l => l.Geometry.GetArea()); //Get the area of the lakes if (c.BigLake != null) //Add the big lake LakeArea += c.BigLake.Geometry.GetArea(); if (poly != null) { var point = new XYPoint(poly.PlotPoints.First().Longitude, poly.PlotPoints.First().Latitude); //Take one point in the polygon var closestpoint = Data.Keys.Select(p => new Tuple<XYPoint, double>(p, p.GetDistance(point))).OrderBy(s => s.Item2).First().Item1; deposition.Add(c.ID, new List<double>(Data[closestpoint].Select(v=>v*LakeArea))); } } NewMessage("Initialized"); }
/// <summary> /// The method calculates the intersection points of triangle a and b both /// of type XYPolygon. /// </summary> /// <param name="triangleA">triangle. The search is started along triangleA.</param> /// <param name="triangleB">triangle. Intersection with this triangle are sought.</param> /// <param name="p">Starting point for the search. p must be part of triangleA.</param> /// <param name="i">on input: End index for the first line segment of triangleA in the search. /// on output: End index for the last intersected line segment in triangleA.</param> /// <param name="j">on input: -1 if vertices before intersection is not to be added to list. /// on output: End index for last intersected line segment of triangleB.</param> /// <param name="intersectionPolygon">polygon eventuallu describing the /// intersection area between triangleA and triangleB</param> /// <returns> /// The p, i, j and intersectionPolygon are called by reference and modified in the method. /// </returns> private static void Intersect(XYPolygon triangleA, XYPolygon triangleB, ref IXYPoint p, ref int i, ref int j, ref XYPolygon intersectionPolygon) { XYLine lineA; XYLine lineB; int im1 = Decrease(i, 2); // "i-1" int count1 = 0; bool found = false; while ((count1 < 3) && (!found)) { lineA = triangleA.GetLine(im1); if (count1 == 0) { lineA.P1.X = p.X; lineA.P1.Y = p.Y; } double MinDist = -1; // Distance used when a line is crossed more than once int jm1 = 0; // "j-1" int jm1Store = -1; while (jm1 < 3) { lineB = triangleB.GetLine(jm1); found = IntersectionPoint(lineA, lineB, ref p); double Dist = CalculatePointToPointDistance(lineA.P1, p); if (Dist < EPSILON) { found = false; } if (found) { if ((MinDist < 0) || (Dist < MinDist)) { MinDist = Dist; jm1Store = jm1; } } jm1++; } if (jm1Store > -1) { lineB = triangleB.GetLine(jm1Store); found = IntersectionPoint(lineA, lineB, ref p); XYPoint HelpCoordinate = new XYPoint(p.X, p.Y); XYPoint HelpNode = new XYPoint(HelpCoordinate); intersectionPolygon.Points.Add(HelpNode); j = Increase(jm1Store, 2); } if (!found) { count1++; im1 = Increase(im1, 2); i = Increase(i, 2); if (j != -1) { XYPoint HelpCoordinate = new XYPoint(lineA.P2.X, lineA.P2.Y); XYPoint HelpNode = new XYPoint(HelpCoordinate); intersectionPolygon.Points.Add(HelpNode); } } } lineA = triangleA.GetLine(Decrease(i, 2)); if (CalculatePointToPointDistance(p, lineA.P2) < EPSILON) { i = Increase(i, 2); } lineB = triangleB.GetLine(Decrease(j, 2)); if (CalculatePointToPointDistance(p, lineB.P2) < EPSILON) { j = Increase(j, 2); } }
/// <summary> /// The method calculates the intersection area of triangle a and b both /// of type XYPolygon. /// </summary> /// <param name="triangleA">Triangle of type XYPolygon</param> /// <param name="triangleB">Triangle of type XYPolygon</param> /// <returns> /// Intersection area between the triangles triangleA and triAngleB. /// </returns> protected static double TriangleIntersectionArea(XYPolygon triangleA, XYPolygon triangleB) { try { if (triangleA.Points.Count != 3 || triangleB.Points.Count != 3) { throw new System.Exception("Argument must be a polygon with 3 points"); } int i = 1; // Index for "next" node in polygon a. int j = -1; // Index for "next" node in polygon b. // -1 indicates that the first has not yet been found. double area = 0; // Intersection area. Returned. XYPolygon intersectionPolygon = new XYPolygon(); // Intersection polygon. IXYPoint p = new XYPoint(); // Latest intersection node found p.X = triangleA.Points[0].X; p.Y = triangleA.Points[0].Y; Intersect(triangleA, triangleB, ref p, ref i, ref j, ref intersectionPolygon); if (j != -1) { // ERB 8/30/2012: For efficiency, allocate and initialize pFirst inside if block IXYPoint pFirst = new XYPoint(); // First intersection point between triangles pFirst = p; int jStop = Increase(j, 2); bool complete = false; int count = 0; while (!complete) { // coordinates for vectors pointing to next triangleA and triangleB point respectively double vax = ((XYPoint)triangleA.Points[i]).X - p.X; double vay = ((XYPoint)triangleA.Points[i]).Y - p.Y; double vbx = ((XYPoint)triangleB.Points[j]).X - p.X; double vby = ((XYPoint)triangleB.Points[j]).Y - p.Y; if (IsPointInPolygonOrOnEdge(p.X + EPSILON * vax, p.Y + EPSILON * vay, triangleB)) { Intersect(triangleA, triangleB, ref p, ref i, ref j, ref intersectionPolygon); } else if (IsPointInPolygonOrOnEdge(p.X + EPSILON * vbx, p.Y + EPSILON * vby, triangleA)) { Intersect(triangleB, triangleA, ref p, ref j, ref i, ref intersectionPolygon); } else // triangleA and triangleB only touches one another but do not intersect { area = 0; return(area); } if (intersectionPolygon.Points.Count > 1) { complete = (CalculatePointToPointDistance(p, pFirst) < EPSILON); } count++; if (count > 20) { throw new System.Exception("Failed to find intersection polygon"); } } area = intersectionPolygon.GetArea(); } else { XYPoint pa = new XYPoint(); // internal point in triangle a XYPoint pb = new XYPoint(); // internal point in triangle b pa.X = (triangleA.GetX(0) + triangleA.GetX(1) + triangleA.GetX(2)) / 3; pa.Y = (triangleA.GetY(0) + triangleA.GetY(1) + triangleA.GetY(2)) / 3; pb.X = (triangleB.GetX(0) + triangleB.GetX(1) + triangleB.GetX(2)) / 3; pb.Y = (triangleB.GetY(0) + triangleB.GetY(1) + triangleB.GetY(2)) / 3; if (IsPointInPolygon(pa, triangleB) || IsPointInPolygon(pb, triangleA)) // triangleA is completely inside triangleB { area = Math.Min(triangleA.GetArea(), triangleB.GetArea()); } else // triangleA and triangleB do dot intersect { area = 0; } } return(area); } catch (System.Exception e) { throw new System.Exception("TriangleIntersectionArea failed", e); } }
public void NovoTest() { TheisAnalysis target = new TheisAnalysis(); // TODO: Initialize to an appropriate value IXYPoint PumpingWell = new XYPoint(0, 0); double PumpingRate = 15 / 3600.0; // double Storativity = 5e-3; double Transmissivity = 1e-3; // List<IXYPoint> InjectionWells = new List<IXYPoint>(); // InjectionWells.Add(new XYPoint(704288.01, 6201186.04)); // InjectionWells.Add(new XYPoint(704455.3,6201201.6 )); // InjectionWells.Add(new XYPoint(704621.99, 6201219)); //// InjectionWells.Add(new XYPoint(704951.98, 6201249.18)); List<IXYPoint> ExtractionWells = new List<IXYPoint>(); //ExtractionWells.Add(new XYPoint(704934.54, 6200994.85)); //ExtractionWells.Add(new XYPoint(704896.26, 6200720.37)); //ExtractionWells.Add(new XYPoint(704940, 6200550)); //InjectionWells.Add(new XYPoint(704698, 6202442)); //ExtractionWells.Add(new XYPoint(704850, 6202365)); using (ShapeReader sr = new ShapeReader(@"C:\Users\Jacob\Dropbox\Enopsol\BiogenLocation.shp")) { foreach (var g in sr.GeoData) { if ((int)g.Data[0] == 1) InjectionWells.Add((IXYPoint)g.Geometry); else if ((int)g.Data[0] == 2) ExtractionWells.Add((IXYPoint)g.Geometry); } } var d = target.Drawdown(InjectionWells.First(), 56.0/3600.0, Storativity, Transmissivity, TimeSpan.FromHours(4), new XYPoint(InjectionWells.First().X + 2, InjectionWells.First().Y +2)); List<List<double>> drawdowns = new List<List<double>>(); drawdowns.Add(new List<double>()); drawdowns.Add(new List<double>()); drawdowns.Add(new List<double>()); //Frederiksgade IXYPoint ObservationPoint = new XYPoint(705669, 6202256); int j = 0; for (int i = 0; i < 100; i++) { double s = 0; TimeSpan Time = TimeSpan.FromDays(i); foreach (var w in ExtractionWells) s += target.Drawdown(w, +PumpingRate, Storativity, Transmissivity, Time, ObservationPoint); drawdowns[j].Add(s); foreach (var w in InjectionWells) s += target.Drawdown(w, -PumpingRate, Storativity, Transmissivity, Time, ObservationPoint); drawdowns[j+1].Add(s); double dw = target.Drawdown(ExtractionWells[0], PumpingRate, Storativity, Transmissivity, Time, new XYPoint(ExtractionWells[0].X+0.5,ExtractionWells[0].Y+0.5)); drawdowns[j + 2].Add(dw); } using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"c:\temp\enopsol.txt")) { for (int i = 0; i < 100; i++) { sw.WriteLine(drawdowns[0][i].ToString() + "\t" + drawdowns[1][i].ToString() + "\t" + drawdowns[2][i].ToString()); } } }
/// <summary> /// Calculates the length that two lines overlap. /// </summary> /// <param name="lineA">Line</param> /// <param name="lineB">Line</param> /// <returns> /// Length of shared line segment. /// </returns> protected static double CalculateSharedLength(XYLine lineA, XYLine lineB) { if (Math.Abs(lineA.P2.X - lineA.P1.X) < EPSILON && Math.Abs(lineB.P2.X - lineB.P1.X) < EPSILON && Math.Abs(lineA.P1.X - lineB.P1.X) < EPSILON) { double YP1A = Math.Min(lineA.P1.Y, lineA.P2.Y); double YP2A = Math.Max(lineA.P1.Y, lineA.P2.Y); double YP1B = Math.Min(lineB.P1.Y, lineB.P2.Y); double YP2B = Math.Max(lineB.P1.Y, lineB.P2.Y); double YP1 = Math.Max(YP1A, YP1B); double YP2 = Math.Min(YP2A, YP2B); if (YP1 < YP2) { return(YP2 - YP1); } else { return(0); } } else if (Math.Abs(lineA.P2.X - lineA.P1.X) < EPSILON || Math.Abs(lineB.P2.X - lineB.P1.X) < EPSILON) { return(0); } else { IXYPoint P1A = new XYPoint(); IXYPoint P2A = new XYPoint(); if (lineA.P1.X < lineA.P2.X) { P1A = lineA.P1; P2A = lineA.P2; } else { P1A = lineA.P2; P2A = lineA.P1; } IXYPoint P1B = new XYPoint(); IXYPoint P2B = new XYPoint(); if (lineB.P1.X < lineB.P2.X) { P1B = lineB.P1; P2B = lineB.P2; } else { P1B = lineB.P2; P2B = lineB.P1; } double alphaA = (P2A.Y - P1A.Y) / (P2A.X - P1A.X); double betaA = -alphaA * P2A.X + P2A.Y; double alphaB = (P2B.Y - P1B.Y) / (P2B.X - P1B.X); double betaB = -alphaA * P2B.X + P2B.Y; if (Math.Abs(alphaA - alphaB) < EPSILON && Math.Abs(betaA - betaB) < EPSILON) { double x1 = Math.Max(P1A.X, P1B.X); double x2 = Math.Min(P2A.X, P2B.X); if (x1 < x2) { XYLine line = new XYLine(x1, alphaA * x1 + betaA, x2, alphaA * x2 + betaA); return(line.GetLength()); } else { return(0); } } else { return(0); } } }
/// <summary> /// Reads the next shape /// </summary> /// <returns></returns> public IGeometry ReadNext(int RecordNumber) { IntPtr pShape = ShapeLib.SHPReadObject(_shapePointer, RecordNumber); ShapeLib.SHPObject shpObject = new ShapeLib.SHPObject(); Marshal.PtrToStructure(pShape, shpObject); double[] x = new double[shpObject.nVertices]; Marshal.Copy(shpObject.padfX, x, 0, x.Length); double[] y = new double[shpObject.nVertices]; Marshal.Copy(shpObject.padfY, y, 0, y.Length); double[] z = new double[shpObject.nVertices]; Marshal.Copy(shpObject.padfZ, z, 0, z.Length); int[] partstarts = null; if (shpObject.nParts > 0) { partstarts = new int[shpObject.nParts]; Marshal.Copy(shpObject.paPartStart, partstarts, 0, partstarts.Length); } ShapeLib.SHPDestroyObject(pShape); IGeometry geom = null; switch (type) { case ShapeLib.ShapeType.NullShape: break; case ShapeLib.ShapeType.MultiPoint: case ShapeLib.ShapeType.MultiPointZ: case ShapeLib.ShapeType.MultiPointM: case ShapeLib.ShapeType.PointM: case ShapeLib.ShapeType.PointZ: case ShapeLib.ShapeType.Point: geom = new XYPoint(x[0], y[0]); break; case ShapeLib.ShapeType.PolyLineM: case ShapeLib.ShapeType.PolyLineZ: case ShapeLib.ShapeType.PolyLine: geom = new XYPolyline(); for (int i = 0; i < x.Length; i++) ((XYPolyline)geom).Points.Add(new XYPoint(x[i], y[i])); break; case ShapeLib.ShapeType.PolygonM: case ShapeLib.ShapeType.PolygonZ: case ShapeLib.ShapeType.Polygon: if (partstarts.Count() == 1) { geom = new XYPolygon(); for (int i = 0; i < x.Length; i++) ((XYPolygon)geom).Points.Add(new XYPoint(x[i], y[i])); ((XYPolygon)geom).Points.Reverse(); } else { geom = new MultiPartPolygon(); //foreach (var partstart in partstarts.Reverse()) //{ // var poly = new XYPolygon(); // for (int i = end; i > partstart; i--) // poly.Points.Add(new XYPoint(x[i], y[i])); // end = partstart; // ((MultiPartPolygon)geom).Polygons.Add(poly); //} for (int j = 0; j < partstarts.Count(); j++) { int end; if (j < partstarts.Count() - 1) end = partstarts[j + 1]; else end = x.Length; var poly = new XYPolygon(); for (int i = partstarts[j]; i < end; i++) poly.Points.Add(new XYPoint(x[i], y[i])); poly.Points.Reverse(); ((MultiPartPolygon)geom).Polygons.Add(poly); } } break; case ShapeLib.ShapeType.MultiPatch: break; default: break; } return geom; }
/// <summary> /// Returns an ArrayList of triangles of type XYPolygon describing the /// triangalation of the polygon. /// </summary> /// <param></param> /// <returns> /// A triangulation of the polygon. /// </returns> public List<XYPolygon> GetTriangulation() { if (TriangleList == null) { int i = 0; int im1 = 0; int ip1 = 0; int n = 0; XYPolygon LocalPolygon = new XYPolygon(this); TriangleList = new List<XYPolygon>(); while (LocalPolygon.Points.Count > 3) { i = LocalPolygon.FindEar(); n = LocalPolygon.Points.Count; im1 = i - 1; ip1 = i + 1; if (i == 0) { im1 = n - 1; } else if (i == n - 1) { ip1 = 0; } XYPoint Nodeim1 = new XYPoint((XYPoint)LocalPolygon.Points[im1]); XYPoint Nodei = new XYPoint((XYPoint)LocalPolygon.Points[i]); XYPoint Nodeip1 = new XYPoint((XYPoint)LocalPolygon.Points[ip1]); XYPolygon Triangle = new XYPolygon(); Triangle.Points.Add(Nodeim1); Triangle.Points.Add(Nodei); Triangle.Points.Add(Nodeip1); TriangleList.Add(Triangle); LocalPolygon.Points.RemoveAt(i); } TriangleList.Add(LocalPolygon); } return TriangleList; }
/// <summary> /// The method calculates the intersection area of triangle a and b both /// of type XYPolygon. /// </summary> /// <param name="triangleA">Triangle of type XYPolygon</param> /// <param name="triangleB">Triangle of type XYPolygon</param> /// <returns> /// Intersection area between the triangles triangleA and triAngleB. /// </returns> protected static double TriangleIntersectionArea(XYPolygon triangleA, XYPolygon triangleB) { try { if (triangleA.Points.Count != 3 || triangleB.Points.Count != 3) { throw new System.Exception("Argument must be a polygon with 3 points"); } int i = 1; // Index for "next" node in polygon a. int j = -1; // Index for "next" node in polygon b. // -1 indicates that the first has not yet been found. double area = 0; // Intersection area. Returned. XYPolygon intersectionPolygon = new XYPolygon(); // Intersection polygon. IXYPoint p = new XYPoint(); // Latest intersection node found p.X = triangleA.Points[0].X; p.Y = triangleA.Points[0].Y; Intersect(triangleA, triangleB, ref p, ref i, ref j, ref intersectionPolygon); if (j != -1) { // ERB 8/30/2012: For efficiency, allocate and initialize pFirst inside if block IXYPoint pFirst = new XYPoint(); // First intersection point between triangles pFirst = p; int jStop = Increase(j, 2); bool complete = false; int count = 0; while (!complete) { // coordinates for vectors pointing to next triangleA and triangleB point respectively double vax = ((XYPoint)triangleA.Points[i]).X - p.X; double vay = ((XYPoint)triangleA.Points[i]).Y - p.Y; double vbx = ((XYPoint)triangleB.Points[j]).X - p.X; double vby = ((XYPoint)triangleB.Points[j]).Y - p.Y; if (IsPointInPolygonOrOnEdge(p.X + EPSILON * vax, p.Y + EPSILON * vay, triangleB)) { Intersect(triangleA, triangleB, ref p, ref i, ref j, ref intersectionPolygon); } else if (IsPointInPolygonOrOnEdge(p.X + EPSILON * vbx, p.Y + EPSILON * vby, triangleA)) { Intersect(triangleB, triangleA, ref p, ref j, ref i, ref intersectionPolygon); } else // triangleA and triangleB only touches one another but do not intersect { area = 0; return area; } if (intersectionPolygon.Points.Count > 1) { complete = (CalculatePointToPointDistance(p, pFirst) < EPSILON); } count++; if (count > 20) { throw new System.Exception("Failed to find intersection polygon"); } } area = intersectionPolygon.GetArea(); } else { XYPoint pa = new XYPoint(); // internal point in triangle a XYPoint pb = new XYPoint(); // internal point in triangle b pa.X = (triangleA.GetX(0) + triangleA.GetX(1) + triangleA.GetX(2)) / 3; pa.Y = (triangleA.GetY(0) + triangleA.GetY(1) + triangleA.GetY(2)) / 3; pb.X = (triangleB.GetX(0) + triangleB.GetX(1) + triangleB.GetX(2)) / 3; pb.Y = (triangleB.GetY(0) + triangleB.GetY(1) + triangleB.GetY(2)) / 3; if (IsPointInPolygon(pa, triangleB) || IsPointInPolygon(pb, triangleA)) // triangleA is completely inside triangleB { area = Math.Min(triangleA.GetArea(), triangleB.GetArea()); } else // triangleA and triangleB do dot intersect { area = 0; } } return area; } catch (System.Exception e) { throw new System.Exception("TriangleIntersectionArea failed", e); } }
/// <summary> /// The method calculates the intersection points of triangle a and b both /// of type XYPolygon. /// </summary> /// <param name="triangleA">triangle. The search is started along triangleA.</param> /// <param name="triangleB">triangle. Intersection with this triangle are sought.</param> /// <param name="p">Starting point for the search. p must be part of triangleA.</param> /// <param name="i">on input: End index for the first line segment of triangleA in the search. /// on output: End index for the last intersected line segment in triangleA.</param> /// <param name="j">on input: -1 if vertices before intersection is not to be added to list. /// on output: End index for last intersected line segment of triangleB.</param> /// <param name="intersectionPolygon">polygon eventuallu describing the /// intersection area between triangleA and triangleB</param> /// <returns> /// The p, i, j and intersectionPolygon are called by reference and modified in the method. /// </returns> private static void Intersect (XYPolygon triangleA, XYPolygon triangleB, ref IXYPoint p, ref int i, ref int j, ref XYPolygon intersectionPolygon) { XYLine lineA; XYLine lineB; int im1 = Decrease(i, 2); // "i-1" int count1 = 0; bool found = false; while ((count1 < 3) && (!found)) { lineA = triangleA.GetLine(im1); if (count1 == 0) { lineA.P1.X = p.X; lineA.P1.Y = p.Y; } double MinDist = -1; // Distance used when a line is crossed more than once int jm1 = 0; // "j-1" int jm1Store = -1; while (jm1 < 3) { lineB = triangleB.GetLine(jm1); found = IntersectionPoint(lineA, lineB, ref p); double Dist = CalculatePointToPointDistance(lineA.P1,p); if (Dist < EPSILON) { found = false; } if (found) { if ((MinDist < 0) || (Dist < MinDist)) { MinDist = Dist; jm1Store = jm1; } } jm1++; } if ( jm1Store > -1 ) { lineB = triangleB.GetLine(jm1Store); found = IntersectionPoint(lineA, lineB, ref p); XYPoint HelpCoordinate = new XYPoint(p.X, p.Y); XYPoint HelpNode = new XYPoint(HelpCoordinate); intersectionPolygon.Points.Add(HelpNode); j = Increase(jm1Store,2); } if (!found) { count1++; im1 = Increase(im1,2); i = Increase(i,2); if (j!=-1) { XYPoint HelpCoordinate = new XYPoint(lineA.P2.X, lineA.P2.Y); XYPoint HelpNode = new XYPoint(HelpCoordinate); intersectionPolygon.Points.Add(HelpNode); } } } lineA = triangleA.GetLine(Decrease(i, 2)); if ( CalculatePointToPointDistance(p, lineA.P2)<EPSILON ) { i = Increase(i, 2); } lineB = triangleB.GetLine(Decrease(j, 2)); if ( CalculatePointToPointDistance(p, lineB.P2)<EPSILON ) { j = Increase(j, 2); } }
public void ContainsTest() { XYPolygon target = XYPolygon.GetSquare(25); IXYPoint p = new XYPoint(3, 3); Assert.AreEqual(true, target.Contains(p)); Assert.AreEqual(false, target.Contains(new XYPoint(3, 6))); Assert.IsTrue(target.Contains(new XYPoint(0.1, 0.1))); }
public static bool AIsPointInLineInterior(XYPoint point, XYLine line) { return IsPointInLineInterior(point, line); }
public MUNode(Node N) { Links = new List<MULink>(); pfsnode = N; Location = new XYPoint(pfsnode.X, pfsnode.Y); }
public void Protected_IsPointInLineInterior() { XYPoint point = new XYPoint(); Assert.AreEqual(false,AXYGeometryTools.AIsPointInLineInterior(new XYPoint(0,0), new XYLine(0, 0, 1, 1)),"Test1"); Assert.AreEqual(true,AXYGeometryTools.AIsPointInLineInterior(new XYPoint(0.5,0.5), new XYLine(0, 0, 1, 1)),"Test2"); Assert.AreEqual(false,AXYGeometryTools.AIsPointInLineInterior(new XYPoint(1,1), new XYLine(0, 0, 1, 1)),"Test3"); Assert.AreEqual(false,AXYGeometryTools.AIsPointInLineInterior(new XYPoint(0.5,0), new XYLine(0, 0, 1, 1)),"Test4"); Assert.AreEqual(false,AXYGeometryTools.AIsPointInLineInterior(new XYPoint(20,40), new XYLine(20, 40, 20, 0)),"Test5"); }
/// <summary> /// Constructor. /// </summary> /// <returns>None</returns> public XYPoint(XYPoint xypoint) { _x = xypoint.X; _y = xypoint.Y; }
/// <summary> /// Calculates length of line inside polygon. Parts of the line that is on the edge of /// the polygon only counts with half their length. /// </summary> /// <param name="line">Line</param> /// <param name="polygon">Polygon</param> /// <returns> /// Length of line inside polygon. /// </returns> protected static double CalculateLengthOfLineInsidePolygon(XYLine line, XYPolygon polygon) { ArrayList lineList = new ArrayList(); lineList.Add(new XYLine(line)); for (int i = 0; i < polygon.Points.Count; i++) // For all lines in the polygon { for (int n = 0; n < lineList.Count; n++) { if (lineList.Count > 1000) { throw new Exception("Problems in ElementMapper, line has been cut in more than 1000 pieces !!!"); } if (DoLineSegmentsIntersect((XYLine)lineList[n], polygon.GetLine(i))) { // Split the intersecting line into two lines XYPoint IntersectionPoint = new XYPoint(CalculateIntersectionPoint((XYLine)lineList[n], polygon.GetLine(i))); lineList.Add(new XYLine(IntersectionPoint, ((XYLine)lineList[n]).P2)); ((XYLine)lineList[n]).P2.X = IntersectionPoint.X; ((XYLine)lineList[n]).P2.Y = IntersectionPoint.Y; break; } } } for (int i = 0; i < lineList.Count; i++) { if (lineList.Count > 1000) { throw new Exception("Problems in ElementMapper, line has been cuttes in more than 100 pieces !!!"); } for (int j = 0; j < polygon.Points.Count; j++) { if (IsPointInLineInterior(polygon.GetLine(j).P1, ((XYLine)lineList[i]))) { lineList.Add(new XYLine(polygon.GetLine(j).P1, ((XYLine)lineList[i]).P2)); ((XYLine)lineList[i]).P2.X = polygon.GetLine(j).P1.X; ((XYLine)lineList[i]).P2.Y = polygon.GetLine(j).P1.Y; } } } double lengthInside = 0; for (int i = 0; i < lineList.Count; i++) { double sharedLength = 0; for (int j = 0; j < polygon.Points.Count; j++) { sharedLength += CalculateSharedLength(((XYLine)lineList[i]), polygon.GetLine(j)); } if (sharedLength > EPSILON) { lengthInside += sharedLength / 2; } else if (IsPointInPolygon(((XYLine)lineList[i]).GetMidpoint(), polygon)) { lengthInside += ((XYLine)lineList[i]).GetLength(); } } return(lengthInside); }