MongoDB C++ Driver  legacy-1.1.2
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"
25 #include "mongo/geo/boundingbox.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 
32 namespace mongo {
33 namespace geo {
34 
35 template <typename TCoordinates>
36 class MultiPolygon : public Geometry<TCoordinates> {
37 public:
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 
86 private:
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 
103 template <typename TCoordinates>
105  : _bson(GeoObj<TCoordinates>::validateType(bson, kMultiPolygonTypeStr)),
106  _polygons(parseAllPolygons(bson)),
107  _boundingBox(Geometry<TCoordinates>::parseBoundingBox(bson)) {}
108 
109 template <typename TCoordinates>
111  if (!_boundingBox)
112  _boundingBox.reset(computeBoundingBox());
113  return *_boundingBox.get();
114 }
115 
116 template <typename TCoordinates>
117 std::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 
126 template <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 
137 template <typename TCoordinates>
138 std::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 
148 template <typename TCoordinates>
149 BoundingBox<TCoordinates>* MultiPolygon<TCoordinates>::computeBoundingBox() const {
150  return Geometry<TCoordinates>::computeBoundingBox(getPoints());
151 }
152 
153 } // namespace geo
154 } // namespace mongo
virtual BoundingBox< TCoordinates > getBoundingBox() const
Obtain the bounding box surrounding this MultiPolygon.
Definition: multipolygon.h:110
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
BSON classes.
Definition: geoobj.h:34
Definition: linestring.h:34
Definition: multipolygon.h:36
virtual GeoObjType getType() const
Get the geometry type of this object.
Definition: multipolygon.h:66
std::vector< Point< TCoordinates > > getPoints() const
Obtain the points that make up this MultiPolygon.
Definition: multipolygon.h:117
an embedded array
Definition: bsontypes.h:50
Definition: polygon.h:35
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:55
std::vector< Polygon< TCoordinates > > getPolygons() const
Obtain the Polygons that make up this MultiPolygon.
Definition: multipolygon.h:82
Definition: geometry.h:39
virtual BSONObj toBSON() const
Obtain a BSON representation of the MultiPolygon.
Definition: multipolygon.h:50
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
MultiPolygon(const BSONObj &bson)
MultiPolygon constructor.
Definition: multipolygon.h:104