| Home | Libraries | People | FAQ | More

##### is_valid (with failure value)

Checks if a geometry is valid (in the OGC sense)

###### Synopsis

    template<typename Geometry>
    bool is_valid(Geometry const & geometry, validity_failure_type & failure)

###### Parameters

Type | Concept | Name | Description
--- | --- | --- | ---
Geometry const & | Any type fulfilling a Geometry Concept | geometry | A model of the specified concept
validity_failure_type & |  | failure | An enumeration value indicating that the geometry is valid or not, and if not valid indicating the reason why

###### Returns

Returns true if the geometry is valid (in the OGC sense); furthermore, the following geometries are considered valid: multi-geometries with no elements, linear geometries containing spikes, areal geometries with duplicate (consecutive) points

###### Header

Either

`#include <boost/geometry.hpp>`

Or

`#include <boost/geometry/algorithms/is_valid.hpp>`

###### Conformance

The function is_valid is not defined by OGC.

###### Supported geometries

Geometry | Status
--- | ---
Point |
Segment |
Box |
Linestring |
Ring |
Polygon |
MultiPoint |
MultiLinestring |
MultiPolygon |
Variant |

###### Complexity

Constant-time for points, segments, boxes and multi-points

Linear for linestrings and multi-linestrings

Linearithmic for rings

Currently, worst-case quadratic for polygons and multi-polygons

###### Example

Checks whether a geometry is valid and, if not valid, checks if it could be fixed by bg::correct; if so bg::correct is called on the geometry

    #include <iostream>

    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/point_xy.hpp>
    #include <boost/geometry/geometries/polygon.hpp>

    int main()
    {
        typedef boost::geometry::model::d2::point_xy<double> point_type;
        typedef boost::geometry::model::polygon<point_type> polygon_type;

        polygon_type poly;
        boost::geometry::read_wkt("POLYGON((0 0,0 10,10 10,10 0),(0 0,9 2,9 1,0 0),(0 0,2 9,1 9,0 0))", poly);

        std::cout << "original geometry: " << boost::geometry::dsv(poly) << std::endl;
        boost::geometry::validity_failure_type failure;
        bool valid = boost::geometry::is_valid(poly, failure);

        // if the invalidity is only due to lack of closing points and/or wrongly oriented rings, then bg::correct can fix it
        bool could_be_fixed = (failure == boost::geometry::failure_not_closed
                               || boost::geometry::failure_wrong_orientation);
        std::cout << "is valid? " << (valid ? "yes" : "no") << std::endl;
        if (! valid)
        {
            std::cout << "can boost::geometry::correct remedy invalidity? " << (could_be_fixed ? "possibly yes" : "no") << std::endl;
            if (could_be_fixed)
            {
                boost::geometry::correct(poly);
                std::cout << "after correction: " << (boost::geometry::is_valid(poly) ? "valid" : "still invalid") << std::endl;
                std::cout << "corrected geometry: " << boost::geometry::dsv(poly) << std::endl;
            }
        }

        return 0;
    }

Output:

    original geometry: (((0, 0), (0, 10), (10, 10), (10, 0)), ((0, 0), (9, 2), (9, 1), (0, 0)), ((0, 0), (2, 9), (1, 9), (0, 0)))
    is valid? no
    can boost::geometry::correct remedy invalidity? possibly yes
    after correction: valid
    corrected geometry: (((0, 0), (0, 10), (10, 10), (10, 0), (0, 0)), ((0, 0), (9, 1), (9, 2), (0, 0)), ((0, 0), (2, 9), (1, 9), (0, 0)))

###### See also

- is_simple
- validity_failure_type