-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmongodb_converter.cpp
101 lines (85 loc) · 3.62 KB
/
mongodb_converter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 Artem Pavlenko
* 2013 Oleksandr Novychenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
// mapnik
#include <mapnik/global.hpp>
#include <mapnik/box2d.hpp>
#include <mapnik/geometry.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/feature_layer_desc.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/params.hpp>
// std
#include <string>
#include "mongodb_converter.hpp"
using mapnik::feature_ptr;
using mapnik::geometry_type;
void mongodb_converter::convert_geometry(const mongo::BSONElement &loc, feature_ptr feature) {
std::string type = loc["type"].String();
std::vector<mongo::BSONElement> coords = loc["coordinates"].Array();
if (type == "Point")
convert_point(coords, feature);
else if (type == "LineString")
convert_linestring(coords, feature);
else if (type == "Polygon")
convert_polygon(coords, feature);
}
void mongodb_converter::convert_point(const std::vector<mongo::BSONElement> &coords, feature_ptr feature) {
std::auto_ptr<geometry_type> point(new geometry_type(mapnik::Point));
point->move_to(coords[0].Number(), coords[1].Number());
feature->paths().push_back(point);
}
void mongodb_converter::convert_linestring(const std::vector<mongo::BSONElement> &coords, feature_ptr feature) {
std::vector<mongo::BSONElement> point = coords[0].Array();
int num_points = coords.size();
std::auto_ptr<geometry_type> line(new geometry_type(mapnik::LineString));
line->move_to(point[0].Number(), point[1].Number());
for (int i = 1; i < num_points; ++i) {
point = coords[i].Array();
line->line_to(point[0].Number(), point[1].Number());
}
feature->paths().push_back(line);
}
void mongodb_converter::convert_polygon(const std::vector<mongo::BSONElement> &coords, feature_ptr feature) {
std::vector<mongo::BSONElement> ring = coords[0].Array(), point = ring[0].Array();
int num_points = ring.size();
int num_interior = coords.size() - 1; // minus exterior
std::auto_ptr<geometry_type> poly(new geometry_type(mapnik::Polygon));
poly->move_to(point[0].Number(), point[1].Number());
for (int i = 1; i < num_points; ++i) {
point = ring[i].Array();
poly->line_to(point[0].Number(), point[1].Number());
}
poly->close_path();
for (int r = 0; r < num_interior; ++r) {
ring = coords[r + 1].Array(); // coords[0] is exterior
num_points = ring.size();
point = ring[0].Array();
poly->move_to(point[0].Number(), point[1].Number());
for (int i = 1; i < num_points; ++i) {
point = ring[i].Array();
poly->line_to(point[0].Number(), point[1].Number());
}
poly->close_path();
}
feature->paths().push_back(poly);
}