MongoDB C++ Driver  legacy-1.1.2
multilinestring.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 
31 namespace mongo {
32 namespace geo {
33 
34 template <typename TCoordinates>
35 class MultiLineString : public Geometry<TCoordinates> {
36 public:
42  explicit MultiLineString(const BSONObj& bson);
43 
49  explicit MultiLineString(const std::vector<LineString<TCoordinates> >& lineStrings);
50 
56  virtual BSONObj toBSON() const {
57  return _bson;
58  }
59 
66 
72  virtual GeoObjType getType() const {
73  return GeoObjType_MultiLineString;
74  }
75 
81  std::vector<Point<TCoordinates> > getPoints() const;
82 
88  std::vector<LineString<TCoordinates> > getLineStrings() const;
89 
90 private:
91  static BSONObj createBSON(const std::vector<LineString<TCoordinates> >& lineStrings);
92  static std::vector<LineString<TCoordinates> > parseLineStrings(const BSONObj& bson);
93 
94  BSONObj _bson;
95  std::vector<LineString<TCoordinates> > _lineStrings;
96  mutable boost::scoped_ptr<BoundingBox<TCoordinates> > _boundingBox;
97 
104  BoundingBox<TCoordinates>* computeBoundingBox() const;
105 };
106 
107 template <typename TCoordinates>
109  : _bson(GeoObj<TCoordinates>::validateType(bson, kMultiLineStringTypeStr)),
110  _lineStrings(parseLineStrings(bson)),
111  _boundingBox(Geometry<TCoordinates>::parseBoundingBox(bson)) {}
112 
113 template <typename TCoordinates>
115  const std::vector<LineString<TCoordinates> >& lineStrings)
116  : _bson(createBSON(lineStrings)), _lineStrings(lineStrings) {}
117 
118 template <typename TCoordinates>
120  if (!_boundingBox)
121  _boundingBox.reset(computeBoundingBox());
122  return *_boundingBox.get();
123 }
124 
125 template <typename TCoordinates>
126 std::vector<Point<TCoordinates> > MultiLineString<TCoordinates>::getPoints() const {
127  std::vector<Point<TCoordinates> > points, curLineStringPoints;
128  for (size_t i = 0; i < _lineStrings.size(); ++i) {
129  curLineStringPoints = _lineStrings[i].getPoints();
130  points.insert(points.end(), curLineStringPoints.begin(), curLineStringPoints.end());
131  }
132  return points;
133 }
134 
135 template <typename TCoordinates>
136 std::vector<LineString<TCoordinates> > MultiLineString<TCoordinates>::getLineStrings() const {
137  return _lineStrings;
138 }
139 
140 template <typename TCoordinates>
142  const std::vector<LineString<TCoordinates> >& lineStrings) {
143  BSONArrayBuilder bab;
144  for (size_t i = 0; i < lineStrings.size(); ++i)
145  bab.append(lineStrings[i].toBSON()[kCoordsFieldName]);
146  BSONObjBuilder bob;
147  return bob.append(kTypeFieldName, kMultiLineStringTypeStr)
148  .append(kCoordsFieldName, bab.arr())
149  .obj();
150 }
151 
152 template <typename TCoordinates>
153 std::vector<LineString<TCoordinates> > MultiLineString<TCoordinates>::parseLineStrings(
154  const BSONObj& bson) {
155  std::vector<BSONElement> lineStringArr = Geometry<TCoordinates>::getCoordsField(bson).Array();
156 
157  std::vector<LineString<TCoordinates> > lineStrings;
158  for (size_t i = 0; i < lineStringArr.size(); ++i) {
159  LineString<TCoordinates> line(
160  Geometry<TCoordinates>::parsePointArray(lineStringArr[i].Array()));
161  lineStrings.push_back(line);
162  }
163  return lineStrings;
164 }
165 
166 template <typename TCoordinates>
167 BoundingBox<TCoordinates>* MultiLineString<TCoordinates>::computeBoundingBox() const {
168  return Geometry<TCoordinates>::computeBoundingBox(getPoints());
169 }
170 
171 } // namespace geo
172 } // namespace mongo
BSONArray arr()
destructive - ownership moves to returned BSONArray
Definition: bsonobjbuilder.h:804
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
BSON classes.
std::vector< Point< TCoordinates > > getPoints() const
Obtain the points that make up this MultiLineString.
Definition: multilinestring.h:126
BSONObjBuilder & append(const BSONElement &e)
append element to the object we are building
Definition: bsonobjbuilder.h:124
virtual BSONObj toBSON() const
Obtain a BSON representation of the MultiLineString.
Definition: multilinestring.h:56
Definition: geoobj.h:34
Definition: linestring.h:34
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:53
Definition: multilinestring.h:35
virtual GeoObjType getType() const
Get the geometry type of this object.
Definition: multilinestring.h:72
an embedded array
Definition: bsontypes.h:50
virtual BoundingBox< TCoordinates > getBoundingBox() const
Obtain the bounding box surrounding this MultiLineString.
Definition: multilinestring.h:119
Definition: bsonobjbuilder.h:765
std::vector< LineString< TCoordinates > > getLineStrings() const
Obtain the line strings that make up this MultiLineString.
Definition: multilinestring.h:136
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
MultiLineString(const BSONObj &bson)
MultiLineString constructor.
Definition: multilinestring.h:108