| Home | Libraries | People | FAQ | More

##### is_valid (with message and strategy)

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

###### Synopsis

    template<typename Geometry, typename Strategy>
    bool is_valid(Geometry const & geometry, std::string & message, Strategy const & strategy)

###### Parameters

Type | Concept | Name | Description
--- | --- | --- | ---
Geometry const & | Any type fulfilling a Geometry Concept | geometry | A model of the specified concept
std::string & |  | message | A string containing a message stating if the geometry is valid or not, and if not valid a reason why
Strategy const & | Any type fulfilling a Is_valid Strategy Concept | strategy | The strategy which will be used for is_valid calculations

###### 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, prints a message describing the reason

    #include <iostream>
    #include <string>

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

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

        polygon_type poly;
        boost::geometry::read_wkt("POLYGON((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),(2 9,9 2,9 9,2 9))", poly);

        std::string message;
        bool valid = boost::geometry::is_valid(poly, message);
        std::cout << "is valid? " << (valid ? "yes" : "no") << std::endl;
        if (! valid)
        {
            std::cout << "why not valid? " << message << std::endl;
        }

        return 0;
    }

Output:

    is valid? no
    why not valid? Geometry has disconnected interior

###### See also

- is_simple