Валидиране на геометрия
Contents
[
Hide
]
Aspose.GIS API ви позволява да валидирате геометрии за редица операции. Той ви предоставя възможността да валидирате геометрия за правила, свързани с Геометрии, както и спецификации от Геопространствени файлови формати.
Проверка на геометрията за валидност
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 |
Проверка на геометрията за простота
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) |
Валидиране на геометрии при записване
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); | |
} | |
} |
Валидиране на геометрии при записване, спазвайки спецификациите
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); | |
} | |
} |