MongoDB C++ Driver  legacy-1.1.2
geometrycollection.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/parser.h"
30 #include "mongo/util/assert_util.h"
31 
32 namespace mongo {
33 namespace geo {
34 
35 // forward declaration needed to parse arbitrary geometries
36 template <typename TCoordinates>
37 class Parser;
38 
39 template <typename TCoordinates>
40 class GeometryCollection : public Geometry<TCoordinates> {
41 public:
47  explicit GeometryCollection(const BSONObj& bson);
48 
53 
75  virtual BSONObj toBSON() const {
76  return _bson;
77  }
78 
86 
92  virtual GeoObjType getType() const {
93  return GeoObjType_GeometryCollection;
94  }
95 
105  const std::vector<const GeoObj<TCoordinates>*>& getGeometries() const;
106 
107 private:
114  static std::vector<const GeoObj<TCoordinates>*> parseGeometries(const BSONObj& bson);
115 
116  BSONObj _bson;
117  std::vector<const GeoObj<TCoordinates>*> _geometries;
118  mutable boost::scoped_ptr<BoundingBox<TCoordinates> > _boundingBox;
119 
126  BoundingBox<TCoordinates>* computeBoundingBox() const;
127 };
128 
129 template <typename TCoordinates>
131  : _bson(GeoObj<TCoordinates>::validateType(bson, kGeometryCollectionTypeStr)),
132  _geometries(parseGeometries(bson)),
133  _boundingBox(Geometry<TCoordinates>::parseBoundingBox(bson)) {}
134 
135 template <typename TCoordinates>
137  for (size_t i = 0; i < _geometries.size(); ++i)
138  delete _geometries[i];
139 }
140 
141 template <typename TCoordinates>
143  if (!_boundingBox)
144  _boundingBox.reset(computeBoundingBox());
145  return *_boundingBox.get();
146 }
147 
148 template <typename TCoordinates>
149 const std::vector<const GeoObj<TCoordinates>*>& GeometryCollection<TCoordinates>::getGeometries()
150  const {
151  return _geometries;
152 }
153 
154 template <typename TCoordinates>
155 std::vector<const GeoObj<TCoordinates>*> GeometryCollection<TCoordinates>::parseGeometries(
156  const BSONObj& bson) {
157  BSONElement geometriesField = bson.getField(kGeometriesFieldName);
158  uassert(0,
159  "bson must contain a field \"geometries\" of type Array",
160  !geometriesField.eoo() && geometriesField.type() == Array);
161 
162  std::vector<BSONElement> geometriesArr = geometriesField.Array();
163  std::vector<const GeoObj<TCoordinates>*> geometries;
164  for (size_t i = 0; i < geometriesArr.size(); ++i) {
165  geometries.push_back(Parser<TCoordinates>::parse(geometriesArr[i].Obj()));
166  }
167  return geometries;
168 }
169 
170 template <typename TCoordinates>
171 BoundingBox<TCoordinates>* GeometryCollection<TCoordinates>::computeBoundingBox() const {
172  std::vector<BoundingBox<TCoordinates> > bboxes;
173  for (size_t i = 0; i < _geometries.size(); ++i)
174  bboxes.push_back(_geometries[i]->getBoundingBox());
176 }
177 
178 } // namespace geo
179 } // namespace mongo
virtual GeoObjType getType() const
Get the geometry type of this object.
Definition: geometrycollection.h:92
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
virtual BSONObj toBSON() const
Obtain a BSON representation of this geometry collection.
Definition: geometrycollection.h:75
BSON classes.
~GeometryCollection()
Frees the heap memory for each geometry stored in this collection.
Definition: geometrycollection.h:136
Definition: geometrycollection.h:37
Definition: geoobj.h:34
virtual BoundingBox< TCoordinates > getBoundingBox() const
Obtain the bounding box surrounding the set of geometries in this geometry collection.
Definition: geometrycollection.h:142
BSONType type() const
Returns the type of the element.
Definition: bsonelement.h:154
Definition: geometrycollection.h:40
BSONElement getField(const StringData &name) const
Get the field of the specified name.
GeometryCollection(const BSONObj &bson)
GeometryCollection constructor.
Definition: geometrycollection.h:130
bool eoo() const
Indicates if it is the end-of-object element, which is present at the end of every BSON object...
Definition: bsonelement.h:172
an embedded array
Definition: bsontypes.h:50
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:55
const std::vector< const GeoObj< TCoordinates > * > & getGeometries() const
Get a vector of pointers to the geometries contained in this geometry collection. ...
Definition: geometrycollection.h:149
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