Geometry Accessors
Contents
[
Hide
]
The examples show how to access geometry data.
Iterate Over Geometries in Geometry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |