| Home | Libraries | People | FAQ | More

##### correct

Corrects a geometry.

###### Description

Corrects a geometry: all rings which are wrongly oriented with respect to their expected orientation are reversed. To all rings which do not have a closing point and are typed as they should have one, the first point is appended. Also boxes can be corrected.

###### Synopsis

    template<typename Geometry>
    void correct(Geometry & geometry)

###### Parameters

Type | Concept | Name | Description
--- | --- | --- | ---
Geometry & | Any type fulfilling a Geometry Concept | geometry | A model of the specified concept which will be corrected if necessary

###### Header

Either

`#include <boost/geometry.hpp>`

Or

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

###### Conformance

The function correct is not defined by OGC.

###### Supported geometries

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

###### Behavior

Case | Behavior
--- | ---
Ring | Ring is corrected
Polygon | Polygon is corrected
Multi Polygon | Multi Polygon is corrected
Box | Box is corrected with respect to minimal and maximal corners
Other geometries | Nothing happens, geometry is unchanged

###### Complexity

Linear

###### Example

Shows how to correct a polygon with respect to its orientation and closure

    #include <iostream>

    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/polygon.hpp>
    #include <boost/geometry/geometries/adapted/boost_tuple.hpp>

    BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

    int main()
    {
        using clockwise_closed_polygon = boost::geometry::model::polygon
            <
                boost::tuple<int, int>
            >;

        clockwise_closed_polygon cwcp;

        // Fill it counterclockwise (so wrongly), forgetting the closing point
        boost::geometry::exterior_ring(cwcp) = {{0, 0}, {10, 10}, {0, 9}};

        // Add a counterclockwise closed inner ring (this is correct)
        boost::geometry::interior_rings(cwcp).push_back({{1, 2}, {4, 6}, {2, 8}, {1, 2}});

        // Its area should be negative (because of wrong orientation)
        //     and wrong (because of omitted closing point)
        const auto area_before = boost::geometry::area(cwcp);
        const auto count_before = boost::geometry::num_points(cwcp);

        // Correct it!
        boost::geometry::correct(cwcp);

        // Check its new area
        const auto area_after = boost::geometry::area(cwcp);
        const auto count_after = boost::geometry::num_points(cwcp);

        // And output it
        std::cout << boost::geometry::dsv(cwcp) << std::endl;
        std::cout << area_before << " -> " << area_after << std::endl;
        std::cout << count_before << " -> " << count_after << std::endl;

        return 0;
    }

Output:

    (((0, 0), (0, 9), (10, 10), (0, 0)), ((1, 2), (4, 6), (2, 8), (1, 2)))
    -7 -> 38
    7 -> 8