Geometry Relations
Contents
[
Hide
]
Works with Geometry Relations/Relationships
Determine if Geometries Are Spacially Equal
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 geometry1 = new MultiLineString | |
{ | |
new LineString(new [] { new Point(0, 0), new Point(1, 1) }), | |
new LineString(new [] { new Point(1, 1), new Point(2, 2) }), | |
}; | |
var geometry2 = new LineString(new[] | |
{ | |
new Point(0, 0), new Point(2, 2), | |
}); | |
Console.WriteLine(geometry1.SpatiallyEquals(geometry2)); // True | |
geometry2.AddPoint(3, 3); | |
Console.WriteLine(geometry1.SpatiallyEquals(geometry2)); // False |
Determine if Geometries Intersect
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 geometry1 = new Polygon(new LinearRing(new[] | |
{ | |
new Point(0, 0), | |
new Point(0, 3), | |
new Point(3, 3), | |
new Point(3, 0), | |
new Point(0, 0), | |
})); | |
var geometry2 = new Polygon(new LinearRing(new[] | |
{ | |
new Point(1, 1), | |
new Point(1, 4), | |
new Point(4, 4), | |
new Point(4, 1), | |
new Point(1, 1), | |
})); | |
Console.WriteLine(geometry1.Intersects(geometry2)); // True | |
Console.WriteLine(geometry2.Intersects(geometry1)); // True | |
// 'Disjoint' is opposite to 'Intersects' | |
Console.WriteLine(geometry1.Disjoint(geometry2)); // False |
Determine if One Geometry Contains Another 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 geometry1 = new Polygon(); | |
geometry1.ExteriorRing = new LinearRing(new[] | |
{ | |
new Point(0, 0), | |
new Point(0, 4), | |
new Point(4, 4), | |
new Point(4, 0), | |
new Point(0, 0), | |
}); | |
geometry1.AddInteriorRing(new LinearRing(new[] | |
{ | |
new Point(1, 1), | |
new Point(1, 3), | |
new Point(3, 3), | |
new Point(3, 1), | |
new Point(1, 1), | |
})); | |
var geometry2 = new Point(2, 2); | |
Console.WriteLine(geometry1.SpatiallyContains(geometry2)); // False | |
var geometry3 = new Point(0.5, 0.5); | |
Console.WriteLine(geometry1.SpatiallyContains(geometry3)); // True | |
// 'a.SpatiallyContains(b)' equals to 'b.Within(a)' | |
Console.WriteLine(geometry3.Within(geometry1)); // True |
Determine if Geometries Touch Each Other
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 geometry1 = new LineString(); | |
geometry1.AddPoint(0, 0); | |
geometry1.AddPoint(2, 2); | |
var geometry2 = new LineString(); | |
geometry2.AddPoint(2, 2); | |
geometry2.AddPoint(3, 3); | |
Console.WriteLine(geometry1.Touches(geometry2)); // True | |
Console.WriteLine(geometry2.Touches(geometry1)); // True | |
var geometry3 = new Point(2, 2); | |
Console.WriteLine(geometry1.Touches(geometry3)); // True | |
var geometry4 = new LineString(); | |
geometry4.AddPoint(1, 1); | |
geometry4.AddPoint(4, 4); | |
Console.WriteLine(geometry1.Touches(geometry4)); // False |
Determine if Geometries Cross Each Other
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 geometry1 = new LineString(); | |
geometry1.AddPoint(0, 0); | |
geometry1.AddPoint(2, 2); | |
var geometry2 = new LineString(); | |
geometry2.AddPoint(1, 1); | |
geometry2.AddPoint(3, 3); | |
Console.WriteLine(geometry1.Crosses(geometry2)); // False | |
var geometry3 = new LineString(); | |
geometry3.AddPoint(0, 2); | |
geometry3.AddPoint(2, 0); | |
Console.WriteLine(geometry1.Crosses(geometry3)); // True |
Determine if Geometries Overlap
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 geometry1 = new LineString(); | |
geometry1.AddPoint(0, 0); | |
geometry1.AddPoint(0, 2); | |
var geometry2 = new LineString(); | |
geometry2.AddPoint(0, 2); | |
geometry2.AddPoint(0, 3); | |
Console.WriteLine(geometry1.Overlaps(geometry2)); // False | |
var geometry3 = new LineString(); | |
geometry3.AddPoint(0, 1); | |
geometry3.AddPoint(0, 3); | |
Console.WriteLine(geometry1.Overlaps(geometry3)); // True |
Determine Spacial Relation using Relate Method
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 geometry1 = new LineString(); | |
geometry1.AddPoint(0, 0); | |
geometry1.AddPoint(0, 2); | |
var geometry2 = new LineString(); | |
geometry2.AddPoint(0, 1); | |
geometry2.AddPoint(0, 3); | |
// Relate method takes a string representation of DE-9IM matrix | |
// (Dimensionally Extended Nine-Intersection Model matrix). | |
// see Simple Feature Access specification for more details on DE-9IM. | |
// this is the equivalent of 'geometry1.SpatiallyEquals(geometry2)' | |
Console.WriteLine(geometry1.Relate(geometry2, "T*F**FFF*")); // False | |
// this is the equivalent of 'geometry1.Disjoint(geometry2)' | |
Console.WriteLine(geometry1.Relate(geometry2, "FF*FF****")); // False | |
// this is the equivalent of 'geometry1.Overlaps(geometry2)' | |
Console.WriteLine(geometry1.Relate(geometry2, "1*T***T**")); // True |
Determine if One Geometry Covers Another
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 line = new LineString(); | |
line.AddPoint(0, 0); | |
line.AddPoint(1, 1); | |
var point = new Point(0, 0); | |
Console.WriteLine(line.Covers(point)); // True | |
Console.WriteLine(point.CoveredBy(line)); // True |
Get Distance between Geometries
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 polygon = new Polygon(); | |
polygon.ExteriorRing = new LinearRing(new[] | |
{ | |
new Point(0, 0), | |
new Point(0, 1), | |
new Point(1, 1), | |
new Point(1, 0), | |
new Point(0, 0), | |
}); | |
var line = new LineString(); | |
line.AddPoint(2, 0); | |
line.AddPoint(1, 3); | |
double distance = polygon.GetDistanceTo(line); | |
Console.WriteLine(distance.ToString("F")); // 0.63 |
Find Overlay of Geometries
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 | |
// find intersection, union, difference and symmetric difference of two geometries. | |
var polygon1 = new Polygon(); | |
polygon1.ExteriorRing = new LinearRing(new[] | |
{ | |
new Point(0, 0), | |
new Point(0, 2), | |
new Point(2, 2), | |
new Point(2, 0), | |
new Point(0, 0), | |
}); | |
var polygon2 = new Polygon(); | |
polygon2.ExteriorRing = new LinearRing(new[] | |
{ | |
new Point(1, 1), | |
new Point(1, 3), | |
new Point(3, 3), | |
new Point(3, 1), | |
new Point(1, 1), | |
}); | |
var intersection = polygon1.Intersection(polygon2); | |
Console.WriteLine("Intersection type is {0}", intersection.GeometryType); // Polygon | |
PrintRing(((IPolygon)intersection).ExteriorRing); | |
// [0] - (1 1) | |
// [1] - (1 2) | |
// [2] - (2 2) | |
// [3] - (2 1) | |
// [4] - (1 1) | |
var union = polygon1.Union(polygon2); | |
Console.WriteLine("Union type is {0}", union.GeometryType); // Polygon | |
PrintRing(((IPolygon)union).ExteriorRing); | |
// [0] - (0 0) | |
// [1] - (0 2) | |
// [2] - (1 2) | |
// [3] - (1 3) | |
// [4] - (3 3) | |
// [5] - (3 1) | |
// [6] - (2 1) | |
// [7] - (2 0) | |
// [8] - (0 0) | |
var difference = polygon1.Difference(polygon2); | |
Console.WriteLine("Difference type is {0}", difference.GeometryType); // Polygon | |
PrintRing(((IPolygon)difference).ExteriorRing); | |
// [0] - (0 0) | |
// [1] - (0 2) | |
// [2] - (1 2) | |
// [3] - (1 1) | |
// [4] - (2 1) | |
// [5] - (2 0) | |
// [6] - (0 0) | |
var symDifference = polygon1.SymDifference(polygon2); | |
Console.WriteLine("Symmetric Difference type is {0}", symDifference.GeometryType); // MultiPolygon | |
var multiPolygon = (IMultiPolygon)symDifference; | |
Console.WriteLine("Polygons count is {0}", multiPolygon.Count); // 2 | |
PrintRing(((IPolygon)multiPolygon[0]).ExteriorRing); | |
// [0] - (0 0) | |
// [1] - (0 2) | |
// [2] - (1 2) | |
// [3] - (1 1) | |
// [4] - (2 1) | |
// [5] - (2 0) | |
// [6] - (0 0) | |
PrintRing(((IPolygon)multiPolygon[1]).ExteriorRing); | |
// [0] - (1 2) | |
// [1] - (1 3) | |
// [2] - (3 3) | |
// [3] - (3 1) | |
// [4] - (2 1) | |
// [5] - (2 2) | |
// [6] - (1 2) |