mapnik/plugins/input/ogr/ogr_featureset.cpp

185 lines
6.1 KiB
C++
Raw Normal View History

/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
2021-01-05 15:39:07 +01:00
* Copyright (C) 2021 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>
2012-04-08 02:20:56 +02:00
#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"
2009-12-16 21:02:06 +01:00
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);
}
2012-01-20 17:07:29 +01:00
ogr_featureset::ogr_featureset(mapnik::context_ptr const& ctx,
OGRLayer& layer,
2012-01-20 17:07:29 +01:00
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;
2013-11-27 16:52:47 +01:00
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);
const OGRFieldType type_oid = fld->GetType();
const std::string fld_name = fld->GetNameRef();
switch (type_oid)
{
case OFTInteger: {
feature->put<mapnik::value_integer>(fld_name, poFeature->GetFieldAsInteger(i));
break;
}
#if GDAL_VERSION_MAJOR >= 2
case OFTInteger64: {
feature->put<mapnik::value_integer>(fld_name, poFeature->GetFieldAsInteger64(i));
break;
}
#endif
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:
#if GDAL_VERSION_MAJOR >= 2
case OFTInteger64List:
#endif
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";
2012-04-08 02:20:56 +02:00
return feature_ptr();
}