MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
polygon.h
Go to the documentation of this file.
1/* Copyright 2014 MongoDB Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
18#pragma once
19
20#include <boost/scoped_ptr.hpp>
21#include <vector>
22
23#include "mongo/db/jsobj.h"
25#include "mongo/geo/constants.h"
26#include "mongo/geo/geometry.h"
28#include "mongo/geo/geoobj.h"
29#include "mongo/geo/point.h"
30
31namespace mongo {
32namespace geo {
33
34template <typename TCoordinates>
35class Polygon : public Geometry<TCoordinates> {
36public:
42 explicit Polygon(const BSONObj& bson);
43
51 explicit Polygon(const std::vector<LineString<TCoordinates> >& linearRings);
52
53 Polygon(const Polygon<TCoordinates>& other);
54 Polygon& operator=(Polygon<TCoordinates> other);
55
61 virtual BSONObj toBSON() const {
62 return _bson;
63 }
64
71
77 virtual GeoObjType getType() const {
78 return GeoObjType_Polygon;
79 }
80
86 std::vector<Point<TCoordinates> > getPoints() const;
87
94 std::vector<LineString<TCoordinates> > getLinearRings() const;
95
96private:
97 static BSONObj createBSON(const std::vector<LineString<TCoordinates> >& linearRings);
98 static std::vector<LineString<TCoordinates> > parseLinearRings(const BSONObj& bson);
99
100 BSONObj _bson;
101 std::vector<LineString<TCoordinates> > _linearRings;
102 mutable boost::scoped_ptr<BoundingBox<TCoordinates> > _boundingBox;
103
110 BoundingBox<TCoordinates>* computeBoundingBox() const;
111};
112
113template <typename TCoordinates>
115 : _bson(GeoObj<TCoordinates>::validateType(bson, kPolygonTypeStr)),
116 _linearRings(parseLinearRings(bson)),
117 _boundingBox(Geometry<TCoordinates>::parseBoundingBox(bson)) {}
118
119template <typename TCoordinates>
121 : _bson(createBSON(linearRings)), _linearRings(linearRings) {}
122
123template <typename TCoordinates>
125 : _bson(other._bson), _linearRings(other._linearRings) {
126 // TODO: consider refactoring this to not make deep copies,
127 // and instead use a boost::shared_ptr to share the same bounding
128 // box across all copies of a Point. This would also let the
129 // compiler generate copy and assignment constructors, so we can drop
130 // them from the implementation.
131 if (other._boundingBox)
132 _boundingBox.reset(new BoundingBox<TCoordinates>(*other._boundingBox));
133}
134
135template <typename TCoordinates>
136Polygon<TCoordinates>& Polygon<TCoordinates>::operator=(Polygon<TCoordinates> other) {
137 using std::swap;
138 swap(_bson, other._bson);
139 swap(_linearRings, other._linearRings);
140 swap(_boundingBox, other._boundingBox);
141 return *this;
142}
143
144template <typename TCoordinates>
146 if (!_boundingBox)
147 _boundingBox.reset(computeBoundingBox());
148 return *_boundingBox.get();
149}
150
151template <typename TCoordinates>
152std::vector<Point<TCoordinates> > Polygon<TCoordinates>::getPoints() const {
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]);
158 }
159 return allPoints;
160}
161
162template <typename TCoordinates>
163std::vector<LineString<TCoordinates> > Polygon<TCoordinates>::getLinearRings() const {
164 return _linearRings;
165}
166
167template <typename TCoordinates>
169 const std::vector<LineString<TCoordinates> >& linearRings) {
171 for (size_t i = 0; i < linearRings.size(); ++i)
172 bab.append(linearRings[i].toBSON()[kCoordsFieldName]);
173 BSONObjBuilder bob;
174 return bob.append(kTypeFieldName, kPolygonTypeStr).append(kCoordsFieldName, bab.arr()).obj();
175}
176
177template <typename TCoordinates>
178std::vector<LineString<TCoordinates> > Polygon<TCoordinates>::parseLinearRings(
179 const BSONObj& bson) {
180 std::vector<BSONElement> linearRingElems = Geometry<TCoordinates>::getCoordsField(bson).Array();
181
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())));
186 }
187 return linearRings;
188}
189
190template <typename TCoordinates>
191BoundingBox<TCoordinates>* Polygon<TCoordinates>::computeBoundingBox() const {
193}
194
195} // namespace geo
196} // namespace mongo
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
Definition geoobj.h:34
Definition geometry.h:39
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
Definition polygon.h:35
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
BSON classes.
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20
@ Array
an embedded array
Definition bsontypes.h:50