MongoDB C++ Driver  legacy-1.1.2
linestring.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/point.h"
29 
30 namespace mongo {
31 namespace geo {
32 
33 template <typename TCoordinates>
34 class LineString : public Geometry<TCoordinates> {
35 public:
41  explicit LineString(const BSONObj& bson);
42 
48  explicit LineString(const std::vector<Point<TCoordinates> >& points);
49 
51  LineString& operator=(LineString<TCoordinates> other);
52 
58  virtual BSONObj toBSON() const {
59  return _bson;
60  }
61 
68 
74  virtual GeoObjType getType() const {
75  return GeoObjType_LineString;
76  }
77 
83  std::vector<Point<TCoordinates> > getPoints() const {
84  return _points;
85  }
86 
87 private:
88  static BSONObj createBSON(const std::vector<Point<TCoordinates> >& points);
89 
90  BSONObj _bson;
91  std::vector<Point<TCoordinates> > _points;
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, kLineStringTypeStr)),
106  _points(Geometry<TCoordinates>::parseAllPoints(bson)),
107  _boundingBox(Geometry<TCoordinates>::parseBoundingBox(bson)) {}
108 
109 template <typename TCoordinates>
111  : _bson(createBSON(points)), _points(points) {}
112 
113 template <typename TCoordinates>
115  : _bson(other._bson), _points(other._points) {
116  // TODO: consider refactoring this to not make deep copies,
117  // and instead use a boost::shared_ptr to share the same bounding
118  // box across all copies of a Point. This would also let the
119  // compiler generate copy and assignment constructors, so we can drop
120  // them from the implementation.
121  if (other._boundingBox)
122  _boundingBox.reset(new BoundingBox<TCoordinates>(*other._boundingBox));
123 }
124 
125 template <typename TCoordinates>
126 LineString<TCoordinates>& LineString<TCoordinates>::operator=(LineString<TCoordinates> other) {
127  using std::swap;
128  swap(_bson, other._bson);
129  swap(_points, other._points);
130  swap(_boundingBox, other._boundingBox);
131  return *this;
132 }
133 
134 template <typename TCoordinates>
136  if (!_boundingBox)
137  _boundingBox.reset(computeBoundingBox());
138  return *_boundingBox.get();
139 }
140 
141 template <typename TCoordinates>
143  BSONArrayBuilder bab;
144  for (size_t i = 0; i < points.size(); ++i)
145  bab.append(points[i].toBSON()[kCoordsFieldName]);
146  BSONObjBuilder bob;
147  return bob.append(kTypeFieldName, kLineStringTypeStr).append(kCoordsFieldName, bab.arr()).obj();
148 }
149 
150 template <typename TCoordinates>
151 BoundingBox<TCoordinates>* LineString<TCoordinates>::computeBoundingBox() const {
153 }
154 
155 } // namespace geo
156 } // 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
std::vector< Point< TCoordinates > > getPoints() const
Obtain the points that make up this LineString.
Definition: linestring.h:83
BSON classes.
virtual BoundingBox< TCoordinates > getBoundingBox() const
Obtain the bounding box surrounding this line string.
Definition: linestring.h:135
BSONObjBuilder & append(const BSONElement &e)
append element to the object we are building
Definition: bsonobjbuilder.h:124
virtual GeoObjType getType() const
Get the geometry type of this object.
Definition: linestring.h:74
Definition: geoobj.h:34
Definition: linestring.h:34
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:53
Represents a Point.
Definition: geometry.h:36
virtual BSONObj toBSON() const
Obtain a BSON representation of the line string.
Definition: linestring.h:58
Definition: bsonobjbuilder.h:765
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
LineString(const BSONObj &bson)
LineString constructor.
Definition: linestring.h:104