20#include <boost/scoped_ptr.hpp>
29#include "mongo/geo/point.h"
34template <
typename TCoordinates>
78 return GeoObjType_Polygon;
86 std::vector<Point<TCoordinates> >
getPoints()
const;
98 static std::vector<LineString<TCoordinates> > parseLinearRings(
const BSONObj& bson);
101 std::vector<LineString<TCoordinates> > _linearRings;
102 mutable boost::scoped_ptr<BoundingBox<TCoordinates> > _boundingBox;
113template <
typename TCoordinates>
115 : _bson(
GeoObj<TCoordinates>::validateType(bson, kPolygonTypeStr)),
116 _linearRings(parseLinearRings(bson)),
117 _boundingBox(
Geometry<TCoordinates>::parseBoundingBox(bson)) {}
119template <
typename TCoordinates>
121 : _bson(createBSON(linearRings)), _linearRings(linearRings) {}
123template <
typename TCoordinates>
125 : _bson(other._bson), _linearRings(other._linearRings) {
131 if (other._boundingBox)
135template <
typename TCoordinates>
136Polygon<TCoordinates>& Polygon<TCoordinates>::operator=(Polygon<TCoordinates> other) {
138 swap(_bson, other._bson);
139 swap(_linearRings, other._linearRings);
140 swap(_boundingBox, other._boundingBox);
144template <
typename TCoordinates>
147 _boundingBox.reset(computeBoundingBox());
148 return *_boundingBox.get();
151template <
typename TCoordinates>
153 std::vector<Point<TCoordinates> > allPoints, lineStringPoints;
154 for (
size_t i = 0; i < _linearRings.size(); ++i) {
155 lineStringPoints = _linearRings[i].getPoints();
156 for (
size_t j = 0; j < lineStringPoints.size(); ++j)
157 allPoints.push_back(lineStringPoints[j]);
162template <
typename TCoordinates>
167template <
typename TCoordinates>
171 for (
size_t i = 0; i < linearRings.size(); ++i)
172 bab.append(linearRings[i].toBSON()[kCoordsFieldName]);
174 return bob.
append(kTypeFieldName, kPolygonTypeStr).
append(kCoordsFieldName, bab.
arr()).
obj();
177template <
typename TCoordinates>
178std::vector<LineString<TCoordinates> > Polygon<TCoordinates>::parseLinearRings(
180 std::vector<BSONElement> linearRingElems = Geometry<TCoordinates>::getCoordsField(bson).Array();
182 std::vector<LineString<TCoordinates> > linearRings;
183 for (
size_t i = 0; i < linearRingElems.size(); ++i) {
184 linearRings.push_back(LineString<TCoordinates>(
185 Geometry<TCoordinates>::parsePointArray(linearRingElems[i].
Array())));
190template <
typename TCoordinates>
191BoundingBox<TCoordinates>* Polygon<TCoordinates>::computeBoundingBox()
const {
Definition bsonobjbuilder.h:765
BSONArray arr()
destructive - ownership moves to returned BSONArray
Definition bsonobjbuilder.h:804
Utility for creating a BSONObj.
Definition bsonobjbuilder.h:53
BSONObjBuilder & append(const BSONElement &e)
append element to the object we are building
Definition bsonobjbuilder.h:124
BSONObj obj()
destructive The returned BSONObj will free the buffer when it is finished.
Definition bsonobjbuilder.h:618
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition bsonobj.h:78
Represents a bounding box.
Definition boundingbox.h:67
static BoundingBox< TCoordinates > * computeBoundingBox(const std::vector< Point< TCoordinates > > &points)
Compute the bounding box around the given points.
Definition geometry.h:137
Definition linestring.h:34
virtual GeoObjType getType() const
Get the geometry type of this object.
Definition polygon.h:77
virtual BSONObj toBSON() const
Obtain a BSON representation of the polygon.
Definition polygon.h:61
Polygon(const BSONObj &bson)
Polygon constructor.
Definition polygon.h:114
std::vector< Point< TCoordinates > > getPoints() const
Obtain the points that make up this Polygon.
Definition polygon.h:152
virtual BoundingBox< TCoordinates > getBoundingBox() const
Obtain the bounding box surrounding this Polygon.
Definition polygon.h:145
std::vector< LineString< TCoordinates > > getLinearRings() const
Obtain the linear rings that make up this Polygon.
Definition polygon.h:163
GeoObjType
An enum to represent the type of geometry of a Geo object.
Definition constants.h:45
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20
@ Array
an embedded array
Definition bsontypes.h:50