Validación de Geometría
Contents
[
Hide
]
La API de Aspose.GIS le permite validar geometrías para una serie de operaciones. Le proporciona la capacidad de validar una geometría para reglas relacionadas con Geometrías, así como especificaciones por formatos de archivo Geoespacial.
Comprobando la validez de la geometría
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 |
Comprobando la simplicidad de la geometría
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) |
Validar geometrías al escribir
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); | |
} | |
} |
Validar geometrías al escribir cumpliendo con las especificaciones
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); | |
} | |
} |