public void BufferAroundLine() { var form = new Form {BackColor = Color.White, Size = new Size(500, 200)}; form.Paint += delegate { Graphics g = form.CreateGraphics(); List<ICoordinate> vertices = new List<ICoordinate>(); vertices.Add(new Coordinate(0, 4)); vertices.Add(new Coordinate(40, 15)); vertices.Add(new Coordinate(50, 50)); vertices.Add(new Coordinate(100, 62)); vertices.Add(new Coordinate(240, 45)); vertices.Add(new Coordinate(350, 5)); IGeometry geometry = new LineString(vertices.ToArray()); g.DrawLines(new Pen(Color.Blue, 1), GetPoints(geometry)); BufferOp bufferOp = new BufferOp(geometry); bufferOp.EndCapStyle = BufferStyle.CapButt; bufferOp.QuadrantSegments = 0; IGeometry bufGeo = bufferOp.GetResultGeometry(5); bufGeo = bufGeo.Union(geometry); g.FillPolygon(new SolidBrush(Color.Pink), GetPoints(bufGeo)); }; WindowsFormsTestHelper.ShowModal(form); }
public void RenderManyLinesShouldBeFast() { using (var bmp = new Bitmap(200, 200)) { using (var g = Graphics.FromImage(bmp)) { var map = new Map(new Size(200, 200)) {Center = new Coordinate(1, 0)}; var line = new LineString(new[] { new Coordinate(0, 0), new Coordinate(1, 0), new Coordinate(1, 1) }); map.Zoom = 0.000001; //difficult zoom level to make sure limiting kicks in TestHelper.AssertIsFasterThan( 200, () => { for (int i = 0; i < 5000; i++) { //orig (without limiting): 75 ms for 5000 lines on W510 i7 VectorRenderingHelper.DrawLineString(g, line, Pens.Black, map); } }); } } }
/// <summary> /// Draws this object on the specified display /// </summary> /// <param name="display">The display to draw to</param> /// <param name="style">The drawing style</param> public virtual void Render(ISpatialDisplay display, IDrawStyle style) { if (m_Geometry is NTS.Polygon) { NTS.Polygon p = (NTS.Polygon)m_Geometry; NTS.LineString edge = p.ExteriorRing; NTS.LineString[] holes = p.InteriorRings; IPosition[][] outlines = new IPosition[1 + holes.Length][]; outlines[0] = GetPositionArray(edge.Coordinates); for (int i = 0; i < holes.Length; i++) { outlines[i + 1] = GetPositionArray(holes[i].Coordinates); } foreach (IPosition[] pa in outlines) { style.Render(display, pa); } if (style is HighlightStyle) { style.Render(display, outlines); } } else { style.Render(display, PositionArray); } }
public static INetwork GetSnakeHydroNetwork(bool generateIDs, params Point[] points) { var network = new Network(); for (int i = 0; i < points.Length; i++) { var nodeName = "node" + (i + 1); network.Nodes.Add(new Node(nodeName) { Geometry = points[i] }); } for (int i = 1; i < points.Length; i++) { var lineGeometry = new LineString(new[] { new Coordinate(points[i-1].X, points[i-1].Y), new Coordinate(points[i].X, points[i].Y) }); var branchName = "branch" + i; var branch = new Branch(network.Nodes[i - 1], network.Nodes[i], lineGeometry.Length) { Geometry = lineGeometry, Name = branchName, }; //setting id is optional ..needed for netcdf..but fatal for nhibernate (thinks it saved already) if (generateIDs) { branch.Id = i; } network.Branches.Add(branch); } return network; }
private static Network GetNetwork() { var network = new Network(); var node1 = new Node("node1"); var node2 = new Node("node2"); var node3 = new Node("node3"); var geometry1 = new LineString(new[] { new Coordinate(0, 0), new Coordinate(0, 100) }); var geometry2 = new LineString(new[] { new Coordinate(0, 100), new Coordinate(0, 200) }); IBranch branch1 = new Branch(node1, node2, 100) { Geometry = geometry1, Name = "branch1" }; IBranch branch2 = new Branch(node2, node3, 100) { Geometry = geometry2, Name = "branch2" }; network.Nodes.Add(node1); network.Nodes.Add(node2); network.Nodes.Add(node3); network.Branches.Add(branch1); network.Branches.Add(branch2); return network; }
public void GetGridValuesTest() { var vertices = new List<ICoordinate> { new Coordinate(0, 0), new Coordinate(100, 100) }; ILineString gridProfile = new LineString(vertices.ToArray()); IRegularGridCoverage regularGridCoverage = new RegularGridCoverage(2,3,100,50) { Name = "pressure", }; regularGridCoverage.Components.Clear(); regularGridCoverage.Components.Add(new Variable<float>("pressure")); regularGridCoverage.SetValues(new[] { 1.1, 2.0, 3.0, 4.0, 5.0, 6.0 }); Function gridValues = RegularGridCoverageHelper.GetGridValues(regularGridCoverage, gridProfile); Assert.AreEqual(101, gridValues.Components[0].Values.Count); Assert.AreEqual(1.1f, (float)gridValues[0.0], 1e-3f); // We can not use the linestring's length directly due to rounding errors Assert.AreEqual((double)gridValues.Arguments[0].Values[gridValues.Components[0].Values.Count - 1], gridProfile.Length, 1e-6); double length = (double)gridValues.Arguments[0].Values[gridValues.Components[0].Values.Count - 1]; Assert.AreEqual(6.0, gridValues[length]); }
internal static Geometries.LineString ToSharpMapLineString(LineString lineString) { Collection <Point> vertices = new Collection <Point>(); foreach (Coordinate coordinate in lineString.Coordinates) { vertices.Add(ToSharpMapPoint(coordinate)); } return(new Geometries.LineString(vertices)); }
public void LineGeometryBuffer() { double tolerance = 20.0; var line = new LineString(new ICoordinate[] {new Coordinate(0, 100), new Coordinate(100, 0)}); var bufferedEnvelope = line.Envelope.Buffer(tolerance, 1); Assert.IsTrue(bufferedEnvelope.Contains(new Point(90, 105))); Assert.IsFalse(bufferedEnvelope.Contains(new Point(200, 80))); Assert.IsTrue(bufferedEnvelope.Contains(new Point(109, 109))); Assert.IsFalse(bufferedEnvelope.Contains(new Point(111, 111))); }
internal static MultiLineString ToNTSMultiLineString(Geometries.MultiLineString multiLineString, GeometryFactory factory) { LineString[] lstrings = new LineString[multiLineString.LineStrings.Count]; int index = 0; foreach (Geometries.LineString lstring in multiLineString.LineStrings) { lstrings[index++] = ToNTSLineString(lstring, factory); } return(factory.CreateMultiLineString(lstrings) as MultiLineString); }
public void GetIntersectionCoordinatesEmptyGridProfileTest() { var vertices = new List<ICoordinate> { new Coordinate(0, 0), new Coordinate(0, 0) }; ILineString gridProfile = new LineString(vertices.ToArray()); var stepSize = gridProfile.Length / 100; IEnumerable<ICoordinate> intersection = CoverageProfile.GetGridProfileCoordinates(gridProfile, stepSize); var stepCount = (gridProfile.Length / stepSize) + 1; Assert.AreEqual(stepCount, intersection.Count()); }
public void TabulatedToSimpleGeometryTest() { var branchGeometry = new LineString(new[] {new Coordinate(0, 0), new Coordinate(100, 0)}); IList<HeightFlowStorageWidth> tabulatedCrossSectionData = new List<HeightFlowStorageWidth> { // height, totalwidth, flowingwidth new HeightFlowStorageWidth(0.0, 20.0, 10.0), new HeightFlowStorageWidth(10.0, 30.0, 12.0), new HeightFlowStorageWidth(20.0, 20.0, 10.0) }; IGeometry geometry = CrossSectionHelper.CreateCrossSectionGeometryFromTabulated(branchGeometry, 30, tabulatedCrossSectionData); // The expected length of the geometry is the maximum of the flowwidth Assert.AreEqual(12.0, geometry.Length, 1.0e-6); Assert.IsInstanceOfType(typeof(LineString), geometry); Assert.AreEqual(2, geometry.Coordinates.Length); }
public void TestWritingEmptyLineString() { var wkbWriter = new WKBWriter(); var memoryStream = new MemoryStream(); var linestring = new LineString(new ICoordinate[0]); Assert.IsNull(linestring.Coordinate); try { wkbWriter.Write(linestring, memoryStream); } finally { memoryStream.Close(); memoryStream.Dispose(); } }
public void GetIntersectionCoordinatesTest() { var vertices = new List<ICoordinate> { new Coordinate(0, 0), new Coordinate(1000, 0) }; ILineString gridProfile = new LineString(vertices.ToArray()); var stepSize = gridProfile.Length / 100; IEnumerable<ICoordinate> intersection = RegularGridCoverageHelper.GetGridProfileCoordinates(gridProfile, stepSize); var stepCount = (gridProfile.Length/stepSize) + 1; Assert.AreEqual(stepCount, intersection.Count()); IList<ICoordinate> coordinates = intersection.ToArray(); Assert.AreEqual(coordinates[0].X, 0, 1e-6); Assert.AreEqual(coordinates[0].Y, 0, 1e-6); Assert.AreEqual(coordinates[coordinates.Count - 1].X, 1000, 1e-6); Assert.AreEqual(coordinates[coordinates.Count - 1].Y, 0, 1e-6); }
private void TestShapeCreation() { ICoordinate[] points = new ICoordinate[3]; points[0] = new Coordinate(0, 0); points[1] = new Coordinate(1, 0); points[2] = new Coordinate(1, 1); LineString line_string = new LineString(points); AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("FOO", "FOO"); Feature feature = new Feature(Factory.CreateMultiLineString(new ILineString[] { line_string }), attributes); Feature[] features = new Feature[1]; features[0] = feature; ShapefileDataWriter shp_writer = new ShapefileDataWriter("C:\\line_string"); shp_writer.Header = ShapefileDataWriter.GetHeader(features[0], features.Length); shp_writer.Write(features); }
public void GetGridValuesTest() { var vertices = new List<ICoordinate> { new Coordinate(0, 0), new Coordinate(100, 100) }; var gridProfile = new LineString(vertices.ToArray()); var regularGridCoverage = new RegularGridCoverage(2, 3, 100, 50) { Name = "pressure", }; regularGridCoverage.Components.Clear(); regularGridCoverage.Components.Add(new Variable<float>("pressure")); regularGridCoverage.SetValues(new[] { 1.1f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f }); var gridValues = CoverageProfile.GetCoverageValues(regularGridCoverage, gridProfile, null); Assert.AreEqual(101, gridValues.Components[0].Values.Count); Assert.AreEqual(1.1f, (float)gridValues[0.0], 1e-3f); // We can not use the linestring's length directly due to rounding errors Assert.AreEqual((double)gridValues.Arguments[0].Values[gridValues.Components[0].Values.Count - 1], gridProfile.Length, 1e-6); var length = (double)gridValues.Arguments[0].Values[gridValues.Components[0].Values.Count - 1]; Assert.AreEqual(6.0, gridValues[length]); gridValues = CoverageProfile.GetCoverageValues(regularGridCoverage, gridProfile, null, 10.0); Assert.AreEqual(15, gridValues.Components[0].Values.Count); Assert.AreEqual(1.1f, (float)gridValues[0.0], 1e-3f); length = (double)gridValues.Arguments[0].Values[gridValues.Components[0].Values.Count - 1]; // We can not use the linestring's length directly due to rounding errors Assert.AreEqual(((int)(gridProfile.Length / 10.0))*10.0, length, 1e-6); Assert.AreEqual(3.0, gridValues[length], "value at end is 3.0 due to rounding that caused the evaluated line to fall short to fall into cell with value of 6.0"); }
private static void TestLinesAreDrawnCorrectly(double zoom) { using (var bmp = new Bitmap(200, 200)) { using (var g = Graphics.FromImage(bmp)) { var map = new Map(new Size(200, 200)) { Center = new Coordinate(1, -0.2) }; var line = new LineString(new[] { new Coordinate(0, 0.2), new Coordinate(1, -0.2), new Coordinate(1, 1) }); map.Zoom = zoom; VectorRenderingHelper.DrawLineString(g, line, Pens.Black, map); Assert.IsTrue(ColorEquals(Color.Black, bmp.GetPixel(0, 60)), "Visual glitch detected"); Assert.IsTrue(ColorEquals(Color.Black, bmp.GetPixel(100, 50)), "Visual glitch detected"); } } }
private static ILineString ReadLineString(JsonTextReader jreader) { if (jreader == null) { throw new ArgumentNullException("reader", "A valid JSON reader object is required."); } ILineString line = null; if (jreader.TokenClass == JsonTokenClass.Array) { jreader.ReadToken(JsonTokenClass.Array); List <ICoordinate> list = new List <ICoordinate>(); while (jreader.TokenClass == JsonTokenClass.Array) { ICoordinate item = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(); Read(ref item, jreader); list.Add(item); } jreader.ReadToken(JsonTokenClass.EndArray); line = new GisSharpBlog.NetTopologySuite.Geometries.LineString(list.ToArray()); } return(line); }
internal static MultiLineString ToNTSMultiLineString(Geometries.MultiLineString multiLineString, GeometryFactory factory) { LineString[] lstrings = new LineString[multiLineString.LineStrings.Count]; int index = 0; foreach (Geometries.LineString lstring in multiLineString.LineStrings) lstrings[index++] = ToNTSLineString(lstring, factory); return factory.CreateMultiLineString(lstrings) as MultiLineString; }
private static LineString ReadLineString(ref LineString line, TextReader reader) { if (reader == null) throw new ArgumentNullException("reader", "A valid text reader object is required."); JsonTextReader jreader = new JsonTextReader(reader); return ReadLineString(jreader); }
private static LineString ReadLineString(JsonTextReader jreader) { if (jreader == null) throw new ArgumentNullException("reader", "A valid JSON reader object is required."); LineString line = null; if (jreader.TokenClass == JsonTokenClass.Array) { jreader.ReadToken(JsonTokenClass.Array); List<Coordinate> list = new List<Coordinate>(); while (jreader.TokenClass == JsonTokenClass.Array) { Coordinate item = new Coordinate(); Read(ref item, jreader); list.Add(item); } jreader.ReadToken(JsonTokenClass.EndArray); line = new LineString(list.ToArray()); } return line; }
public InMemoryGISLineStringFeature(Coordinate[] points) { Shape = new LineString(points); }
public static void Write(LineString line, TextWriter writer) { if (line == null) return; if (writer == null) throw new ArgumentNullException("writer", "A valid text writer object is required."); JsonTextWriter jwriter = new JsonTextWriter(writer); Write(line, jwriter); }
public static void Write(LineString line, JsonTextWriter jwriter) { if (line == null) return; if (jwriter == null) throw new ArgumentNullException("jwriter", "A valid JSON writer object is required."); jwriter.WriteStartObject(); jwriter.WriteMember("type"); jwriter.WriteString("LineString"); jwriter.WriteMember("coordinates"); jwriter.WriteStartArray(); foreach (Coordinate entry in line.Coordinates) { Write(entry, jwriter); } jwriter.WriteEndArray(); jwriter.WriteEndObject(); }
public static string ToXML(LineString line) { StringBuilder text = new StringBuilder(); XmlWriter writer = XmlWriter.Create(text); ToXML(line, writer); writer.Flush(); return text.ToString(); }
public static void ToXML(LineString line, XmlWriter writer) { writer.WriteStartElement("LineString"); ToXML(line.Coordinates, writer); writer.WriteEndElement(); }
public static void ToXML(LineString[] lines, XmlWriter writer) { writer.WriteStartElement("LineStrings"); foreach (LineString line in lines) { ToXML(line, writer); } writer.WriteEndElement(); }
internal static Geometries.LineString ToSharpMapLineString(LineString lineString) { Collection<Point> vertices = new Collection<Point>(); foreach (Coordinate coordinate in lineString.Coordinates) vertices.Add(ToSharpMapPoint(coordinate)); return new Geometries.LineString(vertices); }
[Category(TestCategory.Jira)] //TOOLS-7021 public void DistanceShouldNotThrowForGeometryNull() { var lineString = new LineString(new[] { new Coordinate(0, 0), new Coordinate(10, 0) }); Coordinate nullCoordinate = null; Point nullGeometry = null; var distance = GeometryHelper.Distance(null, new Coordinate(0, 0)); Assert.AreEqual(double.MaxValue, distance); distance = GeometryHelper.Distance(lineString, nullCoordinate); Assert.AreEqual(double.MaxValue, distance); distance = GeometryHelper.Distance(null, new Point(0, 0)); Assert.AreEqual(double.MaxValue, distance); distance = GeometryHelper.Distance(lineString, nullGeometry); Assert.AreEqual(double.MaxValue, distance); }
public void LineString3D() { const int size = 10; ICoordinate[] points = new ICoordinate[size]; for (int i = 0; i < size; i++) { // just some arbitrary values points[i] = new Coordinate(100*Math.Sin(i), 200*Math.Cos(i), 300*Math.Tan(i)); } ILineString source = new LineString(points); byte[] bytes = new PostGisWriter().Write(source); ILineString target = (ILineString)new PostGisReader().Read(bytes); for (int i = 0; i < size; i++) { Assert.AreEqual(source.Coordinates[i].X, target.Coordinates[i].X); Assert.AreEqual(source.Coordinates[i].Y, target.Coordinates[i].Y); Assert.AreEqual(source.Coordinates[i].Z, target.Coordinates[i].Z); } }
public void IntersectPolygonWithLine() { ICoordinate[] coordinates = new ICoordinate[5]; coordinates[0] = new Coordinate(0, 0); coordinates[1] = new Coordinate(10, 0); coordinates[2] = new Coordinate(10, 10); coordinates[3] = new Coordinate(0, 10); coordinates[4] = new Coordinate(0, 0); IGeometry pol2 = new Polygon(new LinearRing(coordinates)); var line = new LineString(new[] { new Coordinate(5,5), new Coordinate(6,20),new Coordinate(7,5) }); var result = pol2.Intersection(line); }
private static void RenderMissingSegments(Graphics g, ILayer layer) { var coverage = ((NetworkCoverageFeatureCollection)layer.DataSource).RenderedCoverage; var locations = coverage.Locations.Values; for (int i = 0; i < locations.Count - 1; i++) { var loc1 = locations[i]; var loc2 = locations[i + 1]; var segments = NetworkHelper.GetShortestPathBetweenBranchFeaturesAsNetworkSegments(coverage.Network, loc1, loc2); if (segments.Count != 0) { continue; } var line = new LineString(new[] { loc1.Geometry.Coordinate, loc2.Geometry.Coordinate }); var linePen = new Pen(Color.Red, 5f) { DashStyle = DashStyle.Dot }; var missingSegmentStyle = new VectorStyle(Brushes.Black, linePen, false, linePen, 1.0f, typeof(ILineString), ShapeType.Rectangle, 3); VectorRenderingHelper.RenderGeometry(g, layer.Map, line, missingSegmentStyle, null, true); linePen.Dispose(); } }
/// <summary> /// Calculate distance in meters between the two selected points /// </summary> private void CalculateDistance() { var points = pointGeometries.ToList(); if (points.Count >= 2) { // Convert the world coordinates into actual meters using a geodetic calculation helper // class. The height is not taken into account (since this information is missing). // TODO: Use the elipsoid appropriate to the current map layers loaded /*GeodeticCalculator calc = new GeodeticCalculator(); GlobalCoordinates gp0 = new GlobalCoordinates(pointGometries[0].Coordinate.X, pointGometries[0].Coordinate.Y); GlobalCoordinates gp1 = new GlobalCoordinates(pointGometries[1].Coordinate.X, pointGometries[1].Coordinate.Y); GeodeticCurve gc = calc.CalculateGeodeticCurve(Ellipsoid.WGS84, gp0, gp1); distanceInMeters = gc.EllipsoidalDistance;*/ // HACK: For now use a plain coordinates to meters calculation like Sobek normally uses (RD or Amersfoort) distanceInMeters = 0.0; for (int i = 1; i < points.Count; i++) { distanceInMeters += Math.Sqrt( Math.Pow(points[i].Coordinate.X - points[i - 1].Coordinate.X, 2) + Math.Pow(points[i].Coordinate.Y - points[i - 1].Coordinate.Y, 2)); } // Show a line indicator //pointLayer.DataSource.Features var existingLine = pointLayer.DataSource.Features.OfType<LineString>().FirstOrDefault(); if (existingLine != null) { pointLayer.DataSource.Features.Remove(existingLine); } var lineGeometry = new LineString(points.Select(g => g.Coordinate).ToArray()); pointLayer.DataSource.Add(lineGeometry); } }
public void GetHashCodeMustBeComputed() { var lineString = new LineString(new[] { new Coordinate(1.0, 2.0), new Coordinate(3.0, 4.0) }); Assert.AreEqual(2024009049, lineString.GetHashCode()); }
public void GeometryToYzToGeometryOrThereAndBackAgain() { var branchGeometry = new LineString(new[] { new Coordinate(111, 0), new Coordinate(11, 0) }); var channel = new Channel { Geometry = branchGeometry }; IList<ICoordinate> yzCoordinates = new List<ICoordinate> { // note: x, y of coordinate are interpreted as the yz for // the cross section. new Coordinate(0.0, 0.0), new Coordinate(5.0, -20.0), new Coordinate(15.0, -20.0), new Coordinate(20.0, 0.0) }; IGeometry geometry = CrossSectionHelper.CreateCrossSectionGeometryForXyzCrossSectionFromYZ(branchGeometry, 30, yzCoordinates); CrossSection crossSection = new CrossSection { Geometry = geometry, Offset = 10 }; NetworkHelper.AddBranchFeatureToBranch(channel, crossSection, crossSection.Offset); Assert.AreEqual(CrossSectionType.GeometryBased, crossSection.CrossSectionType); const int coordinateCount = 4; Assert.AreEqual(coordinateCount, crossSection.Geometry.Coordinates.Length); Assert.AreEqual(coordinateCount, crossSection.YZValues.Count); CrossSectionHelper.ConvertCrossSectionType(crossSection, CrossSectionType.YZTable); Assert.AreEqual(coordinateCount, crossSection.YZValues.Count); Assert.AreEqual(2, crossSection.Geometry.Coordinates.Length); Assert.AreEqual(4, crossSection.YZValues.Count); CrossSectionHelper.ConvertCrossSectionType(crossSection, CrossSectionType.GeometryBased); Assert.AreEqual(CrossSectionType.GeometryBased, crossSection.CrossSectionType); Assert.AreEqual(coordinateCount, crossSection.Geometry.Coordinates.Length); }