Geometry Processing

Reduce Precision of a Geometry

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
Point point = new Point(1.344, 2.345, 3.345, 4.345);
point.RoundXY(digits: 2);
// 1.34, 2.35, 3.345, 4.345
Console.WriteLine("{0}, {1}, {2}, {3}", point.X, point.Y, point.Z, point.M);
point.RoundZ(digits: 1);
// 1.34, 2.35, 3.3, 4.345
Console.WriteLine("{0}, {1}, {2}, {3}", point.X, point.Y, point.Z, point.M);
LineString line = new LineString();
line.AddPoint(1.2, 2.3);
line.AddPoint(2.4, 3.1);
line.RoundXY(digits: 0);
// 1, 2
Console.WriteLine("{0}, {1}", line[0].X, line[0].Y);
// 2, 3
Console.WriteLine("{0}, {1}", line[1].X, line[1].Y);

Get Area of Geometry

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
var triangleRing = new LinearRing();
triangleRing.AddPoint(4, 6);
triangleRing.AddPoint(1, 3);
triangleRing.AddPoint(8, 7);
triangleRing.AddPoint(4, 6);
var triangle = new Polygon(triangleRing);
var squareRing = new LinearRing();
squareRing.AddPoint(0, 9);
squareRing.AddPoint(0, 7);
squareRing.AddPoint(2, 7);
squareRing.AddPoint(2, 9);
squareRing.AddPoint(0, 9);
var square = new Polygon(squareRing);
var multiPolygon = new MultiPolygon { triangle, square };
Console.WriteLine("{0:F}", triangle.GetArea()); // 4.50
Console.WriteLine("{0:F}", square.GetArea()); // 4.00
Console.WriteLine("{0:F}", multiPolygon.GetArea()); // 8.50

Get Centroid

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
var polygon = new Polygon();
polygon.ExteriorRing = new LinearRing(new[]
{
new Point(1, 0),
new Point(2, 2),
new Point(0, 4),
new Point(5, 5),
new Point(6, 1),
new Point(1, 0),
});
IPoint centroid = polygon.GetCentroid();
Console.WriteLine("{0:F} {1:F}", centroid.X, centroid.Y); // 3.33 2.58

Get Convex Hull of Geometry

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
var geometry = new MultiPoint
{
new Point(3, 2),
new Point(0, 0),
new Point(6, 5),
new Point(5, 10),
new Point(10, 0),
new Point(8, 2),
new Point(4, 3),
};
var convexHull = geometry.GetConvexHull();
// [0] = (0 0)
// [1] = (5 10)
// [2] = (10 0)
// [3] = (0 0)
var ring = (ILinearRing)convexHull;
for (int i = 0; i < ring.Count; ++i)
{
Console.WriteLine("[{0}] = ({1} {2})", i, ring[i].X, ring[i].Y);
}

Get Geometry Buffer

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
var line = new LineString();
line.AddPoint(0, 0);
line.AddPoint(3, 3);
// buffer with positive distance contains all points whose distance to input geometry is less or equal to 'distance' argument.
var lineBuffer = line.GetBuffer(distance: 1);
Console.WriteLine(lineBuffer.SpatiallyContains(new Point(1, 2))); // True
Console.WriteLine(lineBuffer.SpatiallyContains(new Point(3.1, 3.1))); // True
var polygon = new Polygon();
polygon.ExteriorRing = new LinearRing(new[]
{
new Point(0, 0),
new Point(0, 3),
new Point(3, 3),
new Point(3, 0),
new Point(0, 0),
});
// buffer with negative distance 'shrinks' geometry.
var polygonBuffer = (IPolygon)polygon.GetBuffer(distance: -1);
// [0] = (1 1)
// [1] = (1 2)
// [2] = (2 2)
// [3] = (2 1)
// [4] = (1 1)
var ring = polygonBuffer.ExteriorRing;
for (int i = 0; i < ring.Count; ++i)
{
Console.WriteLine("[{0}] = ({1} {2})", i, ring[i].X, ring[i].Y);
}

Get Length of Geometry

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
var line = new LineString();
line.AddPoint(0, 0);
line.AddPoint(2, 2);
line.AddPoint(2, 0);
Console.WriteLine("{0:F}", line.GetLength()); // 4.83
var rectangle = new Polygon(new LinearRing(new[]
{
new Point(0, 0),
new Point(0, 1),
new Point(1, 1),
new Point(1, 0),
new Point(0, 0),
}));
// GetLength() returns perimeter for polygons
Console.WriteLine("{0:F}", rectangle.GetLength()); // 4.00

Get Point on Surface

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
var polygon = new Polygon();
polygon.ExteriorRing = new LinearRing(new[]
{
new Point(0, 0),
new Point(0, 1),
new Point(1, 1),
new Point(0, 0),
});
IPoint pointOnSurface = polygon.GetPointOnSurface();
// point on surface is guaranteed to be inside a polygon.
Console.WriteLine(polygon.SpatiallyContains(pointOnSurface)); // True

Replace Polygons By Lines

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
{
var srcGeometry = Geometry.FromText(@"GeometryCollection (POLYGON((1 2, 1 4, 3 4, 3 2)), Point (5 1))");
var dstGeometry = srcGeometry.ReplacePolygonsByLines();
Console.WriteLine($"source: {srcGeometry.AsText()}");
Console.WriteLine($"result: {dstGeometry.AsText()}");
}