MongoDB C++ Driver legacy-1.1.1
Loading...
Searching...
No Matches
multipolygon.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/client/export_macros.h"
24#include "mongo/db/jsobj.h"
26#include "mongo/geo/constants.h"
27#include "mongo/geo/geometry.h"
28#include "mongo/geo/geoobj.h"
29#include "mongo/geo/point.h"
30#include "mongo/geo/polygon.h"
31
32namespace mongo {
33namespace geo {
34
35template <typename TCoordinates>
36class MultiPolygon : public Geometry<TCoordinates> {
37public:
43 explicit MultiPolygon(const BSONObj& bson);
44
50 virtual BSONObj toBSON() const {
51 return _bson;
52 }
53
60
66 virtual GeoObjType getType() const {
67 return GeoObjType_MultiPolygon;
68 }
69
75 std::vector<Point<TCoordinates> > getPoints() const;
76
82 std::vector<Polygon<TCoordinates> > getPolygons() const {
83 return _polygons;
84 }
85
86private:
87 static Polygon<TCoordinates> parsePolygon(const BSONElement& polygon);
88 static std::vector<Polygon<TCoordinates> > parseAllPolygons(const BSONObj& bson);
89
90 BSONObj _bson;
91 std::vector<Polygon<TCoordinates> > _polygons;
92 mutable boost::scoped_ptr<BoundingBox<TCoordinates> > _boundingBox;
93
100 BoundingBox<TCoordinates>* computeBoundingBox() const;
101};
102
103template <typename TCoordinates>
105 : _bson(GeoObj<TCoordinates>::validateType(bson, kMultiPolygonTypeStr)),
106 _polygons(parseAllPolygons(bson)),
107 _boundingBox(Geometry<TCoordinates>::parseBoundingBox(bson)) {}
108
109template <typename TCoordinates>
111 if (!_boundingBox)
112 _boundingBox.reset(computeBoundingBox());
113 return *_boundingBox.get();
114}
115
116template <typename TCoordinates>
117std::vector<Point<TCoordinates> > MultiPolygon<TCoordinates>::getPoints() const {
118 std::vector<Point<TCoordinates> > points, curPolygonPoints;
119 for (size_t i = 0; i < _polygons.size(); ++i) {
120 curPolygonPoints = _polygons[i].getPoints();
121 points.insert(points.end(), curPolygonPoints.begin(), curPolygonPoints.end());
122 }
123 return points;
124}
125
126template <typename TCoordinates>
128 std::vector<BSONElement> linearRingElems = polygon.Array();
129 std::vector<LineString<TCoordinates> > linearRings;
130 for (size_t i = 0; i < linearRingElems.size(); ++i) {
131 linearRings.push_back(LineString<TCoordinates>(
132 Geometry<TCoordinates>::parsePointArray(linearRingElems[i].Array())));
133 }
134 return Polygon<TCoordinates>(linearRings);
135}
136
137template <typename TCoordinates>
138std::vector<Polygon<TCoordinates> > MultiPolygon<TCoordinates>::parseAllPolygons(
139 const BSONObj& bson) {
140 std::vector<BSONElement> polygonArr = Geometry<TCoordinates>::getCoordsField(bson).Array();
141 std::vector<Polygon<TCoordinates> > polygons;
142 for (size_t i = 0; i < polygonArr.size(); ++i) {
143 polygons.push_back(parsePolygon(polygonArr[i]));
144 }
145 return polygons;
146}
147
148template <typename TCoordinates>
149BoundingBox<TCoordinates>* MultiPolygon<TCoordinates>::computeBoundingBox() const {
151}
152
153} // namespace geo
154} // namespace mongo
BSONElement represents an "element" in a BSONObj.
Definition bsonelement.h:55
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 multipolygon.h:36
std::vector< Polygon< TCoordinates > > getPolygons() const
Obtain the Polygons that make up this MultiPolygon.
Definition multipolygon.h:82
virtual GeoObjType getType() const
Get the geometry type of this object.
Definition multipolygon.h:66
virtual BSONObj toBSON() const
Obtain a BSON representation of the MultiPolygon.
Definition multipolygon.h:50
MultiPolygon(const BSONObj &bson)
MultiPolygon constructor.
Definition multipolygon.h:104
virtual BoundingBox< TCoordinates > getBoundingBox() const
Obtain the bounding box surrounding this MultiPolygon.
Definition multipolygon.h:110
std::vector< Point< TCoordinates > > getPoints() const
Obtain the points that make up this MultiPolygon.
Definition multipolygon.h:117
Definition polygon.h:35
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:32
@ Array
an embedded array
Definition bsontypes.h:50