in our application users can modify the vertices collection of line/polygon shape. To find out if the modified shape is valid I use the shapes validate() method. But if you can see in the picture above the validate() method still returns 'True' although the last vertex of polygon shape has been deleted.
Is this a bug. How can I check wether the shape is valid?
Yes, as you already have noticed, the validation will still pass even though the last vertex was removed from the ring of the polygon, like the following code snippet shows:
PolygonShape polygonShape = new PolygonShape(); polygonShape.OuterRing = ringShape; double area = polygonShape.GetArea(GeographyUnit.Meter, AreaUnit.SquareMeters); ShapeValidationResult result = polygonShape.Validate(ShapeValidationMode.Simple);
The reason for this is that we only did very simple validation on the shapes, for the lineshape, we only check the vertex number exceeds 2 and for the RingShape we only check the vertex number is greater than 3. We reverse the complex logic to advanced validation mode. For now, we can need to write the validate logic as we want, following is some code snippet which add the vertex checking for the out ring of the polgyonshape.
int ourRingVertexCount = polygonShape.OuterRing.Vertices.Count; if (polygonShape.OuterRing.Vertices[0] != polygonShape.OuterRing.Vertices[ourRingVertexCount - 1]) { result = new ShapeValidationResult(false, "The first vertex is not the last vertex"); }
as I can see I can implement my own validation logic but this is not what I want. I do not know what parameters have to be checked to decide a shape is valid. Valid for me means that your drawing logic or the ThinkGeo methods they work with shape did not throw an Exception e.g. "points must form a closed linestring".
I think you have to improve your validation method to check all the parameters of the shpe to avoid an Exception is throw in your core methods.
I agree with you that we may need to enhance the validation for the shapes. The non-closed ring shape passed our validation while the NTS will be throwing exception when we try to create Geometry from it. The exception “points must form a closed linestring” is thrown out by nts GisSharpBlog.NetTopologySuite.IO.WKTReader when we try to use it to create Geometry.
I have added this enhancement to our working list and hope we can get it done soon.