MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
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"
26#include "mongo/geo/constants.h"
27#include "mongo/geo/geometry.h"
28#include "mongo/geo/point.h"
29
30namespace mongo {
31namespace geo {
32
33template <typename TCoordinates>
34class LineString : public Geometry<TCoordinates> {
35public:
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
87private:
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
103template <typename TCoordinates>
105 : _bson(GeoObj<TCoordinates>::validateType(bson, kLineStringTypeStr)),
106 _points(Geometry<TCoordinates>::parseAllPoints(bson)),
107 _boundingBox(Geometry<TCoordinates>::parseBoundingBox(bson)) {}
108
109template <typename TCoordinates>
111 : _bson(createBSON(points)), _points(points) {}
112
113template <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
125template <typename TCoordinates>
126LineString<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
134template <typename TCoordinates>
136 if (!_boundingBox)
137 _boundingBox.reset(computeBoundingBox());
138 return *_boundingBox.get();
139}
140
141template <typename TCoordinates>
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
150template <typename TCoordinates>
151BoundingBox<TCoordinates>* LineString<TCoordinates>::computeBoundingBox() const {
153}
154
155} // namespace geo
156} // namespace mongo
Definition bsonobjbuilder.h:765
BSONArray arr()
destructive - ownership moves to returned BSONArray
Definition bsonobjbuilder.h:804
Utility for creating a BSONObj.
Definition bsonobjbuilder.h:53
BSONObjBuilder & append(const BSONElement &e)
append element to the object we are building
Definition bsonobjbuilder.h:124
BSONObj obj()
destructive The returned BSONObj will free the buffer when it is finished.
Definition bsonobjbuilder.h:618
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition bsonobj.h:78
Represents a bounding box.
Definition boundingbox.h:67
Definition geoobj.h:34
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 linestring.h:34
LineString(const BSONObj &bson)
LineString constructor.
Definition linestring.h:104
virtual BSONObj toBSON() const
Obtain a BSON representation of the line string.
Definition linestring.h:58
virtual GeoObjType getType() const
Get the geometry type of this object.
Definition linestring.h:74
virtual BoundingBox< TCoordinates > getBoundingBox() const
Obtain the bounding box surrounding this line string.
Definition linestring.h:135
std::vector< Point< TCoordinates > > getPoints() const
Obtain the points that make up this LineString.
Definition linestring.h:83
Represents a Point.
Definition point.h:52
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:20