Geometrievalidierung
Contents
[
Hide
]
Die Aspose.GIS API ermöglicht es Ihnen, Geometrien für eine Reihe von Operationen zu validieren. Sie bietet Ihnen die Möglichkeit, eine Geometrie auf Regeln bezüglich Geometrien sowie Spezifikationen von geospatialen Dateiformaten zu validieren.
Überprüfung der Geometrie auf Gültigkeit
This file contains hidden or 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 | |
LinearRing linearRing = new LinearRing(); | |
linearRing.AddPoint(0, 0); | |
linearRing.AddPoint(0, 1); | |
linearRing.AddPoint(1, 0); | |
bool valid = linearRing.IsValid; // valid == false | |
linearRing.AddPoint(0, 0); | |
valid = linearRing.IsValid; // valid == true |
Überprüfung der Geometrie auf Einfachheit
This file contains hidden or 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 lineString = new LineString(); | |
bool simple = lineString.IsSimple; // simple == true | |
lineString.AddPoint(0, 0); | |
lineString.AddPoint(1, 0); | |
simple = lineString.IsSimple; // simple == true | |
lineString.AddPoint(0.5, 0); | |
simple = lineString.IsSimple; // simple == false (line string crosses itself) |
Validierung von Geometrien beim Schreiben
This file contains hidden or 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 exteriorRing = new LinearRing(); | |
exteriorRing.AddPoint(0, 0); | |
exteriorRing.AddPoint(0, 1); | |
exteriorRing.AddPoint(1, 1); | |
exteriorRing.AddPoint(1, 0); | |
exteriorRing.AddPoint(0, 0); | |
var interiorRing = new LinearRing(); | |
interiorRing.AddPoint(0.5, 0.5); | |
interiorRing.AddPoint(1, 0.5); | |
interiorRing.AddPoint(1, 1); | |
interiorRing.AddPoint(0.5, 1); | |
interiorRing.AddPoint(0.5, 0.5); | |
var invalidPolygon = new Polygon(); | |
invalidPolygon.ExteriorRing = exteriorRing; | |
invalidPolygon.AddInteriorRing(interiorRing); | |
// invalidPolygon.IsValid == false, since polygon rings share segments (have infinite number of intersection points) | |
GeoJsonOptions options = new GeoJsonOptions(); | |
options.ValidateGeometriesOnWrite = false; // false is default | |
File.Delete(dataDir + "not_validated_data_out.shp"); | |
using (var nonValidatingLayer = Drivers.GeoJson.CreateLayer(dataDir + "not_validated_data_out.shp", options)) | |
{ | |
Feature feature = nonValidatingLayer.ConstructFeature(); | |
feature.Geometry = invalidPolygon; | |
// no exception is thrown, since ValidateGeometriesOnWrite == false, and GeoJson specification doesn't say that rings of polygon can't share segments. | |
nonValidatingLayer.Add(feature); | |
} | |
options.ValidateGeometriesOnWrite = true; | |
File.Delete(dataDir + "validated_data_out.shp"); | |
using (var validatingLayer = Drivers.GeoJson.CreateLayer(dataDir + "validated_data_out.shp", options)) | |
{ | |
Feature feature = validatingLayer.ConstructFeature(); | |
feature.Geometry = invalidPolygon; | |
try | |
{ | |
validatingLayer.Add(feature); // GisException is thrown, since polygon is not valid | |
} | |
catch (GisException e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} |
Validierung von Geometrien beim Schreiben unter Einhaltung von Spezifikationen
This file contains hidden or 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 lineStrinWithOnePoint = new LineString(); | |
lineStrinWithOnePoint.AddPoint(0, 0); | |
GeoJsonOptions options = new GeoJsonOptions(); | |
options.ValidateGeometriesOnWrite = false; | |
using (var layer = Drivers.GeoJson.CreateLayer(dataDir + "ValidateOnWriteObeyingSpecifications_out.json", options)) | |
{ | |
Feature feature = layer.ConstructFeature(); | |
// GeoJSON specification says that line string must have at least two coordinates. | |
feature.Geometry = lineStrinWithOnePoint; | |
try | |
{ | |
// Geometry of feature doesn't match data format specification, so exception is thrown | |
// regardless what ValidateGeometriesOnWrite option is. | |
layer.Add(feature); | |
} | |
catch (GisException e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} |