MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
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"
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
31namespace mongo {
32namespace geo {
33
34template <typename TCoordinates>
35class MultiLineString : public Geometry<TCoordinates> {
36public:
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
90private:
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
107template <typename TCoordinates>
109 : _bson(GeoObj<TCoordinates>::validateType(bson, kMultiLineStringTypeStr)),
110 _lineStrings(parseLineStrings(bson)),
111 _boundingBox(Geometry<TCoordinates>::parseBoundingBox(bson)) {}
112
113template <typename TCoordinates>
115 const std::vector<LineString<TCoordinates> >& lineStrings)
116 : _bson(createBSON(lineStrings)), _lineStrings(lineStrings) {}
117
118template <typename TCoordinates>
120 if (!_boundingBox)
121 _boundingBox.reset(computeBoundingBox());
122 return *_boundingBox.get();
123}
124
125template <typename TCoordinates>
126std::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
135template <typename TCoordinates>
136std::vector<LineString<TCoordinates> > MultiLineString<TCoordinates>::getLineStrings() const {
137 return _lineStrings;
138}
139
140template <typename TCoordinates>
142 const std::vector<LineString<TCoordinates> >& lineStrings) {
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
152template <typename TCoordinates>
153std::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
166template <typename TCoordinates>
167BoundingBox<TCoordinates>* MultiLineString<TCoordinates>::computeBoundingBox() const {
169}
170
171} // namespace geo
172} // 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
Definition multilinestring.h:35
MultiLineString(const BSONObj &bson)
MultiLineString constructor.
Definition multilinestring.h:108
std::vector< LineString< TCoordinates > > getLineStrings() const
Obtain the line strings that make up this MultiLineString.
Definition multilinestring.h:136
virtual GeoObjType getType() const
Get the geometry type of this object.
Definition multilinestring.h:72
virtual BoundingBox< TCoordinates > getBoundingBox() const
Obtain the bounding box surrounding this MultiLineString.
Definition multilinestring.h:119
std::vector< Point< TCoordinates > > getPoints() const
Obtain the points that make up this MultiLineString.
Definition multilinestring.h:126
virtual BSONObj toBSON() const
Obtain a BSON representation of the MultiLineString.
Definition multilinestring.h:56
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
@ Array
an embedded array
Definition bsontypes.h:50