MongoDB C++ Driver legacy-1.1.1
Loading...
Searching...
No Matches
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"
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
32namespace mongo {
33namespace geo {
34
35// forward declaration needed to parse arbitrary geometries
36template <typename TCoordinates>
37class Parser;
38
39template <typename TCoordinates>
40class GeometryCollection : public Geometry<TCoordinates> {
41public:
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
107private:
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
129template <typename TCoordinates>
131 : _bson(GeoObj<TCoordinates>::validateType(bson, kGeometryCollectionTypeStr)),
132 _geometries(parseGeometries(bson)),
133 _boundingBox(Geometry<TCoordinates>::parseBoundingBox(bson)) {}
134
135template <typename TCoordinates>
137 for (size_t i = 0; i < _geometries.size(); ++i)
138 delete _geometries[i];
139}
140
141template <typename TCoordinates>
143 if (!_boundingBox)
144 _boundingBox.reset(computeBoundingBox());
145 return *_boundingBox.get();
146}
147
148template <typename TCoordinates>
149const std::vector<const GeoObj<TCoordinates>*>& GeometryCollection<TCoordinates>::getGeometries()
150 const {
151 return _geometries;
152}
153
154template <typename TCoordinates>
155std::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
170template <typename TCoordinates>
171BoundingBox<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
BSONElement represents an "element" in a BSONObj.
Definition bsonelement.h:55
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
BSONType type() const
Returns the type of the element.
Definition bsonelement.h:154
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition bsonobj.h:78
BSONElement getField(const StringData &name) const
Get the field of the specified name.
Represents a bounding box.
Definition boundingbox.h:67
Definition geoobj.h:34
Definition geometrycollection.h:40
virtual BoundingBox< TCoordinates > getBoundingBox() const
Obtain the bounding box surrounding the set of geometries in this geometry collection.
Definition geometrycollection.h:142
~GeometryCollection()
Frees the heap memory for each geometry stored in this collection.
Definition geometrycollection.h:136
virtual BSONObj toBSON() const
Obtain a BSON representation of this geometry collection.
Definition geometrycollection.h:75
virtual GeoObjType getType() const
Get the geometry type of this object.
Definition geometrycollection.h:92
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
GeometryCollection(const BSONObj &bson)
GeometryCollection constructor.
Definition geometrycollection.h:130
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 parser.h:30
const char kGeometryCollectionTypeStr[]
The different possible geometry type names in GeoJSON.
Definition constants.h:34
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