20 #include <boost/scoped_ptr.hpp>
29 #include "mongo/geo/point.h"
34 template <
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;
113 template <
typename TCoordinates>
115 : _bson(
GeoObj<TCoordinates>::validateType(bson, kPolygonTypeStr)),
116 _linearRings(parseLinearRings(bson)),
117 _boundingBox(
Geometry<TCoordinates>::parseBoundingBox(bson)) {}
119 template <
typename TCoordinates>
121 : _bson(createBSON(linearRings)), _linearRings(linearRings) {}
123 template <
typename TCoordinates>
125 : _bson(other._bson), _linearRings(other._linearRings) {
131 if (other._boundingBox)
135 template <
typename TCoordinates>
136 Polygon<TCoordinates>& Polygon<TCoordinates>::operator=(Polygon<TCoordinates> other) {
138 swap(_bson, other._bson);
139 swap(_linearRings, other._linearRings);
140 swap(_boundingBox, other._boundingBox);
144 template <
typename TCoordinates>
147 _boundingBox.reset(computeBoundingBox());
148 return *_boundingBox.get();
151 template <
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]);
162 template <
typename TCoordinates>
167 template <
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();
177 template <
typename TCoordinates>
178 std::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())));
190 template <
typename TCoordinates>
191 BoundingBox<TCoordinates>* Polygon<TCoordinates>::computeBoundingBox()
const {
std::vector< LineString< TCoordinates > > getLinearRings() const
Obtain the linear rings that make up this Polygon.
Definition: polygon.h:163
BSONArray arr()
destructive - ownership moves to returned BSONArray
Definition: bsonobjbuilder.h:804
virtual GeoObjType getType() const
Get the geometry type of this object.
Definition: polygon.h:77
std::vector< Point< TCoordinates > > getPoints() const
Obtain the points that make up this Polygon.
Definition: polygon.h:152
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
virtual BSONObj toBSON() const
Obtain a BSON representation of the polygon.
Definition: polygon.h:61
BSONObjBuilder & append(const BSONElement &e)
append element to the object we are building
Definition: bsonobjbuilder.h:124
Definition: linestring.h:34
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:53
Polygon(const BSONObj &bson)
Polygon constructor.
Definition: polygon.h:114
virtual BoundingBox< TCoordinates > getBoundingBox() const
Obtain the bounding box surrounding this Polygon.
Definition: polygon.h:145
an embedded array
Definition: bsontypes.h:50
Definition: bsonobjbuilder.h:765
Definition: geometry.h:39
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78
static BoundingBox< TCoordinates > * computeBoundingBox(const std::vector< Point< TCoordinates > > &points)
Compute the bounding box around the given points.
Definition: geometry.h:137
Represents a bounding box.
Definition: boundingbox.h:67