Geometry Accessors

The examples show how to access geometry data.

Iterate Over Geometries in Geometry

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
Point pointGeometry = new Point(40.7128, -74.006);
LineString lineGeometry = new LineString();
lineGeometry.AddPoint(78.65, -32.65);
lineGeometry.AddPoint(-98.65, 12.65);
GeometryCollection geometryCollection = new GeometryCollection();
geometryCollection.Add(pointGeometry);
geometryCollection.Add(lineGeometry);
foreach (Geometry geometry in geometryCollection)
{
switch (geometry.GeometryType)
{
case GeometryType.Point:
Point point = (Point)geometry;
break;
case GeometryType.LineString:
LineString line = (LineString)geometry;
break;
}
}

Iterate Over Points In Geometry

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
LineString line = new LineString();
line.AddPoint(78.65, -32.65);
line.AddPoint(-98.65, 12.65);
foreach (IPoint point in line)
{
Console.WriteLine(point.X + "," + point.Y);
}

Get Geometry Type

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
Point point = new Point(40.7128, -74.006);
GeometryType geometryType = point.GeometryType;
Console.WriteLine(geometryType); // Point

Count Geometries In Geometry

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
Point point = new Point(40.7128, -74.006);
LineString line = new LineString();
line.AddPoint(78.65, -32.65);
line.AddPoint(-98.65, 12.65);
GeometryCollection geometryCollection = new GeometryCollection();
geometryCollection.Add(point);
geometryCollection.Add(line);
int geometriesCount = geometryCollection.Count;
Console.WriteLine(geometriesCount); // 2

Count Points In Geometry

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
LineString line = new LineString();
line.AddPoint(78.65, -32.65);
line.AddPoint(-98.65, 12.65);
int pointsCount = line.Count;
Console.WriteLine(pointsCount); // 2

Set Spatial Reference System for Geometry

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
var point = new Point(2, 3);
point.SpatialReferenceSystem = SpatialReferenceSystem.Wgs84;
var srs = point.SpatialReferenceSystem; // WGS 84
var line = new LineString();
srs = line.SpatialReferenceSystem; // null
line.AddPoint(point); // line takes SRS from point.
srs = line.SpatialReferenceSystem; // WGS 84
line.AddPoint(new Point(3, 4)); // point takes SRS of line.
srs = line[1].SpatialReferenceSystem; // WGS84
try
{
point = new Point(3, 4);
point.SpatialReferenceSystem = SpatialReferenceSystem.Wgs72;
line.AddPoint(point); // point has different non null SRS, exception is thrown
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}

Convert Geometry to Editable

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
ILineString readOnlyLine = (ILineString)Geometry.FromText("LINESTRING (1 1, 2 2)");
// Interfaces inherited from IGeometry represent read-only geometries, while
// concrete classes inherited from Geometry represent editable geometries.
// If you need to edit a geometry represented by an interface, 'ToEditable' method should be used to
// get an editable copy.
LineString editableLine = readOnlyLine.ToEditable();
// Line can be edited now
editableLine.AddPoint(3, 3);
Console.WriteLine(editableLine.AsText()); // LINESTRING (1 1, 2 2, 3 3)
// Initial geometry did not change
Console.WriteLine(readOnlyLine.AsText()); // LINESTRING (1 1, 2 2)