File: shape_io.cpp

package info (click to toggle)
mapnik 3.0.12%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,084 kB
  • ctags: 18,454
  • sloc: cpp: 142,516; python: 1,416; sh: 769; makefile: 170; xml: 140; lisp: 13
file content (288 lines) | stat: -rw-r--r-- 8,603 bytes parent folder | download | duplicates (2)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2015 Artem Pavlenko
 *
 * 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
 *
 *****************************************************************************/

#include "shape_io.hpp"

// mapnik
#include <mapnik/debug.hpp>
#include <mapnik/make_unique.hpp>
#include <mapnik/datasource.hpp>
#include <mapnik/util/is_clockwise.hpp>
#include <mapnik/geometry_correct.hpp>

using mapnik::datasource_exception;
const std::string shape_io::SHP = ".shp";
const std::string shape_io::SHX = ".shx";
const std::string shape_io::DBF = ".dbf";
const std::string shape_io::INDEX = ".index";

shape_io::shape_io(std::string const& shape_name, bool open_index)
    : type_(shape_null),
      shp_(shape_name + SHP),
      shx_(shape_name + SHX),
      dbf_(shape_name + DBF),
      reclength_(0),
      id_(0)
{
    bool ok = (shp_.is_open() && dbf_.is_open());
    if (!ok)
    {
        throw datasource_exception("Shape Plugin: cannot read shape file '" + shape_name + "'");
    }

    if (open_index)
    {
        try
        {
            index_ = std::make_unique<shape_file>(shape_name + INDEX);
        }
        catch (...)
        {
            MAPNIK_LOG_WARN(shape) << "shape_io: Could not open index=" << shape_name << INDEX;
        }
    }
    if  (!index_ && !shx_.is_open())
    {
        throw datasource_exception("Shape Plugin: cannot read shape index file '" + shape_name + ".shx'");
    }
}

shape_io::~shape_io() {}

void shape_io::move_to(std::streampos pos)
{
    shp_.seek(pos);
    id_ = shp_.read_xdr_integer();
    reclength_ = shp_.read_xdr_integer();
}

shape_file& shape_io::shp()
{
    return shp_;
}

shape_file& shape_io::shx()
{
    return shx_;
}

dbf_file& shape_io::dbf()
{
    return dbf_;
}

void shape_io::read_bbox(shape_file::record_type & record, mapnik::box2d<double> & bbox)
{
    double lox = record.read_double();
    double loy = record.read_double();
    double hix = record.read_double();
    double hiy = record.read_double();
    bbox.init(lox, loy, hix, hiy);
}

mapnik::geometry::geometry<double> shape_io::read_polyline(shape_file::record_type & record)
{
    mapnik::geometry::geometry<double> geom; // default empty
    int num_parts = record.read_ndr_integer();
    int num_points = record.read_ndr_integer();

    if (num_parts == 1)
    {
        mapnik::geometry::line_string<double> line;
        line.reserve(num_points);
        record.skip(4);
        for (int i = 0; i < num_points; ++i)
        {
            double x = record.read_double();
            double y = record.read_double();
            line.add_coord(x, y);
        }
        geom = std::move(line);
    }
    else
    {
        std::vector<int> parts;
        parts.resize(num_parts);
        std::for_each(parts.begin(), parts.end(), [&](int & part) { part = record.read_ndr_integer();});
        int start, end;
        mapnik::geometry::multi_line_string<double> multi_line;
        multi_line.reserve(num_parts);
        for (int k = 0; k < num_parts; ++k)
        {
            start = parts[k];
            if (k == num_parts - 1)
            {
                end = num_points;
            }
            else
            {
                end = parts[k + 1];
            }

            mapnik::geometry::line_string<double> line;
            line.reserve(end - start);
            for (int j = start; j < end; ++j)
            {
                double x = record.read_double();
                double y = record.read_double();
                line.add_coord(x, y);
            }
            multi_line.push_back(std::move(line));
        }
        geom = std::move(multi_line);
    }
    return geom;
}

mapnik::geometry::geometry<double> shape_io::read_polyline_parts(shape_file::record_type & record, std::vector<std::pair<int, int>> const& parts)
{
    mapnik::geometry::geometry<double> geom; // default empty
    int total_num_parts = record.read_ndr_integer();
    int num_parts = parts.size();
    mapnik::geometry::multi_line_string<double> multi_line;
    multi_line.reserve(num_parts);
    for (int k = 0; k < num_parts; ++k)
    {
        int start = parts[k].first;
        int end = parts[k].second;
        unsigned pos = 4 + 32 + 8 + 4 * total_num_parts + start * 16;
        record.set_pos(pos);

        mapnik::geometry::line_string<double> line;
        line.reserve(end - start);
        for (int j = start; j < end; ++j)
        {
            double x = record.read_double();
            double y = record.read_double();
            line.emplace_back(x, y);
        }
        multi_line.push_back(std::move(line));
    }
    geom = std::move(multi_line);
    return geom;
}


mapnik::geometry::geometry<double> shape_io::read_polygon(shape_file::record_type & record)
{
    mapnik::geometry::geometry<double> geom; // default empty
    int num_parts = record.read_ndr_integer();
    int num_points = record.read_ndr_integer();

    std::vector<int> parts;
    parts.resize(num_parts);
    std::for_each(parts.begin(), parts.end(), [&](int & part) { part = record.read_ndr_integer();});
    mapnik::geometry::polygon<double> poly;
    mapnik::geometry::multi_polygon<double> multi_poly;
    for (int k = 0; k < num_parts; ++k)
    {
        int start = parts[k];
        int end;
        if (k == num_parts - 1) end = num_points;
        else end = parts[k + 1];

        mapnik::geometry::linear_ring<double> ring;
        ring.reserve(end - start);
        for (int j = start; j < end; ++j)
        {
            double x = record.read_double();
            double y = record.read_double();
            ring.emplace_back(x, y);
        }
        if (k == 0)
        {
            poly.set_exterior_ring(std::move(ring));
        }
        else if (mapnik::util::is_clockwise(ring))
        {
            multi_poly.emplace_back(std::move(poly));
            poly.interior_rings.clear();
            poly.set_exterior_ring(std::move(ring));
        }
        else
        {
            poly.add_hole(std::move(ring));
        }
    }

    if (multi_poly.size() > 0) // multi
    {
        multi_poly.emplace_back(std::move(poly));
        geom = std::move(multi_poly);
    }
    else
    {
        geom = std::move(poly);
    }
    mapnik::geometry::correct(geom);
    return geom;
}

mapnik::geometry::geometry<double> shape_io::read_polygon_parts(shape_file::record_type & record, std::vector<std::pair<int,int>> const& parts)
{
    mapnik::geometry::geometry<double> geom; // default empty
    int total_num_parts = record.read_ndr_integer();
    mapnik::geometry::polygon<double> poly;
    mapnik::geometry::multi_polygon<double> multi_poly;
    int num_parts = parts.size();
    for (int k = 0; k < num_parts; ++k)
    {
        int start = parts[k].first;
        int end = parts[k].second;
        unsigned pos = 4 + 32 + 8 + 4 * total_num_parts + start * 16;
        record.set_pos(pos);
        mapnik::geometry::linear_ring<double> ring;
        ring.reserve(end - start);
        for (int j = start; j < end; ++j)
        {
            double x = record.read_double();
            double y = record.read_double();
            ring.emplace_back(x, y);
        }
        if (k == 0)
        {
            poly.set_exterior_ring(std::move(ring));
        }
        else if (mapnik::util::is_clockwise(ring))
        {
            multi_poly.emplace_back(std::move(poly));
            poly.interior_rings.clear();
            poly.set_exterior_ring(std::move(ring));
        }
        else
        {
            poly.add_hole(std::move(ring));
        }
    }

    if (multi_poly.size() > 0) // multi
    {
        multi_poly.emplace_back(std::move(poly));
        geom = std::move(multi_poly);
    }
    else
    {
        geom = std::move(poly);
    }
    mapnik::geometry::correct(geom);
    return geom;
}