File: ogr_featureset.cpp

package info (click to toggle)
mapnik 4.2.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,592 kB
  • sloc: cpp: 163,859; python: 1,332; sh: 690; xml: 161; makefile: 123; perl: 28; lisp: 13
file content (179 lines) | stat: -rw-r--r-- 6,192 bytes parent folder | download
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
/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2025 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
 *
 *****************************************************************************/

// mapnik
#include <mapnik/global.hpp>
#include <mapnik/value/types.hpp>
#include <mapnik/debug.hpp>
#include <mapnik/geometry/box2d.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/feature_layer_desc.hpp>
#include <mapnik/wkb.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/value/types.hpp>
#include <mapnik/feature_factory.hpp>
#include <mapnik/geometry/correct.hpp>

// ogr
#include "ogr_featureset.hpp"
#include "ogr_converter.hpp"

using mapnik::box2d;
using mapnik::feature_factory;
using mapnik::feature_ptr;
using mapnik::geometry_utils;
using mapnik::query;
using mapnik::transcoder;

ogr_featureset::ogr_featureset(mapnik::context_ptr const& ctx,
                               OGRLayer& layer,
                               OGRGeometry& extent,
                               std::string const& encoding)
    : ctx_(ctx),
      layer_(layer),
      layerdef_(layer.GetLayerDefn()),
      tr_(new transcoder(encoding)),
      fidcolumn_(layer_.GetFIDColumn()),
      count_(0)

{
    layer_.SetSpatialFilter(&extent);
}

ogr_featureset::ogr_featureset(mapnik::context_ptr const& ctx,
                               OGRLayer& layer,
                               mapnik::box2d<double> const& extent,
                               std::string const& encoding)
    : ctx_(ctx),
      layer_(layer),
      layerdef_(layer.GetLayerDefn()),
      tr_(new transcoder(encoding)),
      fidcolumn_(layer_.GetFIDColumn()), // TODO - unused
      count_(0)
{
    layer_.SetSpatialFilterRect(extent.minx(), extent.miny(), extent.maxx(), extent.maxy());
}

ogr_featureset::~ogr_featureset() {}

feature_ptr ogr_featureset::next()
{
    if (count_ == 0)
    {
        // Reset the layer reading on the first feature read
        // this is a hack, but needed due to https://github.com/mapnik/mapnik/issues/2048
        // Proper solution is to avoid storing layer state in featureset
        layer_.ResetReading();
    }
    OGRFeature* poFeature;
    while ((poFeature = layer_.GetNextFeature()) != nullptr)
    {
        // ogr feature ids start at 0, so add one to stay
        // consistent with other mapnik datasources that start at 1
        mapnik::value_integer feature_id = (poFeature->GetFID() + 1);
        feature_ptr feature(feature_factory::create(ctx_, feature_id));

        OGRGeometry* geom = poFeature->GetGeometryRef();
        if (geom && !geom->IsEmpty())
        {
            auto geom_corrected = ogr_converter::convert_geometry(geom);
            mapnik::geometry::correct(geom_corrected);
            feature->set_geometry(std::move(geom_corrected));
        }
        else
        {
            MAPNIK_LOG_DEBUG(ogr) << "ogr_featureset: Feature with null geometry=" << poFeature->GetFID();
            OGRFeature::DestroyFeature(poFeature);
            continue;
        }

        ++count_;

        int fld_count = layerdef_->GetFieldCount();
        for (int i = 0; i < fld_count; i++)
        {
            OGRFieldDefn* fld = layerdef_->GetFieldDefn(i);
            OGRFieldType const type_oid = fld->GetType();
            std::string const fld_name = fld->GetNameRef();

            switch (type_oid)
            {
                case OFTInteger: {
                    feature->put<mapnik::value_integer>(fld_name, poFeature->GetFieldAsInteger(i));
                    break;
                }
                case OFTInteger64: {
                    feature->put<mapnik::value_integer>(fld_name, poFeature->GetFieldAsInteger64(i));
                    break;
                }

                case OFTReal: {
                    feature->put(fld_name, poFeature->GetFieldAsDouble(i));
                    break;
                }

                case OFTString:
                case OFTWideString: // deprecated !
                {
                    feature->put(fld_name, tr_->transcode(poFeature->GetFieldAsString(i)));
                    break;
                }

                case OFTIntegerList:
                case OFTInteger64List:
                case OFTRealList:
                case OFTStringList:
                case OFTWideStringList: // deprecated !
                {
                    MAPNIK_LOG_WARN(ogr) << "ogr_featureset: Unhandled type_oid=" << type_oid;
                    break;
                }

                case OFTBinary: {
                    MAPNIK_LOG_WARN(ogr) << "ogr_featureset: Unhandled type_oid=" << type_oid;
                    // feature->put(name,feat->GetFieldAsBinary (i, size));
                    break;
                }

                case OFTDate:
                case OFTTime:
                case OFTDateTime: // unhandled !
                {
                    MAPNIK_LOG_WARN(ogr) << "ogr_featureset: Unhandled type_oid=" << type_oid;
                    break;
                }

                default: // unknown
                {
                    MAPNIK_LOG_WARN(ogr) << "ogr_featureset: Unknown type_oid=" << type_oid;
                    break;
                }
            }
        }
        OGRFeature::DestroyFeature(poFeature);
        return feature;
    }

    MAPNIK_LOG_DEBUG(ogr) << "ogr_featureset: " << count_ << " features";

    return feature_ptr();
}