2009-01-28 21:16:31 +01:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
2009-01-28 21:16:31 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2016-12-20 17:57:22 +01:00
|
|
|
* Copyright (C) 2016 Artem Pavlenko
|
2009-01-28 21:16:31 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-10-22 14:29:08 +02:00
|
|
|
// mapnik
|
2009-01-28 21:16:31 +01:00
|
|
|
#include <mapnik/global.hpp>
|
2016-08-02 16:34:59 +02:00
|
|
|
#include <mapnik/value/types.hpp>
|
2012-04-08 02:20:56 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2017-01-26 09:51:37 +01:00
|
|
|
#include <mapnik/geometry/box2d.hpp>
|
2009-01-28 21:16:31 +01:00
|
|
|
#include <mapnik/feature.hpp>
|
|
|
|
#include <mapnik/feature_layer_desc.hpp>
|
|
|
|
#include <mapnik/wkb.hpp>
|
|
|
|
#include <mapnik/unicode.hpp>
|
2016-08-02 16:34:59 +02:00
|
|
|
#include <mapnik/value/types.hpp>
|
2011-05-18 03:28:22 +02:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
2016-08-02 15:43:02 +02:00
|
|
|
#include <mapnik/geometry/correct.hpp>
|
2009-01-28 21:16:31 +01:00
|
|
|
|
2009-01-29 05:20:34 +01:00
|
|
|
// ogr
|
2009-01-28 21:16:31 +01:00
|
|
|
#include "ogr_featureset.hpp"
|
2009-05-12 00:06:48 +02:00
|
|
|
#include "ogr_converter.hpp"
|
2009-01-29 05:20:34 +01:00
|
|
|
|
2009-01-28 21:16:31 +01:00
|
|
|
using mapnik::query;
|
2009-12-16 21:02:06 +01:00
|
|
|
using mapnik::box2d;
|
2009-01-28 21:16:31 +01:00
|
|
|
using mapnik::feature_ptr;
|
|
|
|
using mapnik::geometry_utils;
|
2009-01-29 17:20:30 +01:00
|
|
|
using mapnik::transcoder;
|
2011-05-18 03:28:22 +02:00
|
|
|
using mapnik::feature_factory;
|
2009-01-28 21:16:31 +01:00
|
|
|
|
2013-11-28 07:50:15 +01:00
|
|
|
|
|
|
|
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,
|
2009-01-28 21:16:31 +01:00
|
|
|
OGRLayer & layer,
|
2012-01-20 17:07:29 +01:00
|
|
|
mapnik::box2d<double> const& extent,
|
|
|
|
std::string const& encoding)
|
|
|
|
: ctx_(ctx),
|
2011-11-14 04:37:50 +01:00
|
|
|
layer_(layer),
|
|
|
|
layerdef_(layer.GetLayerDefn()),
|
|
|
|
tr_(new transcoder(encoding)),
|
2014-02-01 04:12:31 +01:00
|
|
|
fidcolumn_(layer_.GetFIDColumn()), // TODO - unused
|
2011-11-14 04:37:50 +01:00
|
|
|
count_(0)
|
2009-01-28 21:16:31 +01:00
|
|
|
{
|
2009-05-12 00:06:48 +02:00
|
|
|
layer_.SetSpatialFilterRect (extent.minx(),
|
|
|
|
extent.miny(),
|
|
|
|
extent.maxx(),
|
|
|
|
extent.maxy());
|
2009-01-28 21:16:31 +01:00
|
|
|
}
|
|
|
|
|
2011-10-22 02:27:06 +02:00
|
|
|
ogr_featureset::~ogr_featureset()
|
|
|
|
{
|
|
|
|
}
|
2009-01-28 21:16:31 +01:00
|
|
|
|
|
|
|
feature_ptr ogr_featureset::next()
|
|
|
|
{
|
2014-02-01 04:12:31 +01:00
|
|
|
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();
|
|
|
|
}
|
2012-10-21 05:16:17 +02:00
|
|
|
OGRFeature *poFeature;
|
2013-11-27 16:52:47 +01:00
|
|
|
while ((poFeature = layer_.GetNextFeature()) != nullptr)
|
2011-04-21 21:19:07 +02:00
|
|
|
{
|
2011-04-29 22:00:45 +02:00
|
|
|
// ogr feature ids start at 0, so add one to stay
|
|
|
|
// consistent with other mapnik datasources that start at 1
|
2012-12-20 04:24:52 +01:00
|
|
|
mapnik::value_integer feature_id = (poFeature->GetFID() + 1);
|
2012-01-19 18:55:07 +01:00
|
|
|
feature_ptr feature(feature_factory::create(ctx_,feature_id));
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2012-10-21 05:16:17 +02:00
|
|
|
OGRGeometry* geom = poFeature->GetGeometryRef();
|
2011-10-22 02:27:06 +02:00
|
|
|
if (geom && ! geom->IsEmpty())
|
2011-04-21 21:19:07 +02:00
|
|
|
{
|
2015-05-20 23:00:30 +02:00
|
|
|
auto geom_corrected = ogr_converter::convert_geometry(geom);
|
|
|
|
mapnik::geometry::correct(geom_corrected);
|
|
|
|
feature->set_geometry(std::move(geom_corrected));
|
2011-04-21 21:19:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-21 05:16:17 +02:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_featureset: Feature with null geometry="
|
|
|
|
<< poFeature->GetFID();
|
|
|
|
OGRFeature::DestroyFeature( poFeature );
|
2012-10-05 22:49:29 +02:00
|
|
|
continue;
|
2011-04-21 21:19:07 +02:00
|
|
|
}
|
2012-04-09 12:05:49 +02:00
|
|
|
|
2011-04-21 21:19:07 +02:00
|
|
|
++count_;
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-04-21 21:19:07 +02:00
|
|
|
int fld_count = layerdef_->GetFieldCount();
|
|
|
|
for (int i = 0; i < fld_count; i++)
|
|
|
|
{
|
2011-10-22 02:27:06 +02:00
|
|
|
OGRFieldDefn* fld = layerdef_->GetFieldDefn(i);
|
|
|
|
const OGRFieldType type_oid = fld->GetType();
|
|
|
|
const std::string fld_name = fld->GetNameRef();
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-04-21 21:19:07 +02:00
|
|
|
switch (type_oid)
|
|
|
|
{
|
2011-11-14 04:37:50 +01:00
|
|
|
case OFTInteger:
|
2015-08-05 20:47:17 +02:00
|
|
|
{
|
|
|
|
feature->put<mapnik::value_integer>( fld_name, poFeature->GetFieldAsInteger(i));
|
|
|
|
break;
|
|
|
|
}
|
2015-06-16 11:36:17 +02:00
|
|
|
#if GDAL_VERSION_MAJOR >= 2
|
|
|
|
case OFTInteger64:
|
2011-11-14 04:37:50 +01:00
|
|
|
{
|
2015-08-05 20:47:17 +02:00
|
|
|
feature->put<mapnik::value_integer>( fld_name, poFeature->GetFieldAsInteger64(i));
|
2011-11-14 04:37:50 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-08-05 20:47:17 +02:00
|
|
|
#endif
|
2011-11-14 04:37:50 +01:00
|
|
|
|
|
|
|
case OFTReal:
|
|
|
|
{
|
2012-10-21 05:16:17 +02:00
|
|
|
feature->put( fld_name, poFeature->GetFieldAsDouble(i));
|
2011-11-14 04:37:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OFTString:
|
|
|
|
case OFTWideString: // deprecated !
|
|
|
|
{
|
2013-08-14 00:52:04 +02:00
|
|
|
feature->put( fld_name, tr_->transcode(poFeature->GetFieldAsString(i)));
|
2011-11-14 04:37:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OFTIntegerList:
|
2015-06-16 11:36:17 +02:00
|
|
|
#if GDAL_VERSION_MAJOR >= 2
|
|
|
|
case OFTInteger64List:
|
|
|
|
#endif
|
2011-11-14 04:37:50 +01:00
|
|
|
case OFTRealList:
|
|
|
|
case OFTStringList:
|
|
|
|
case OFTWideStringList: // deprecated !
|
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(ogr) << "ogr_featureset: Unhandled type_oid=" << type_oid;
|
2011-11-14 04:37:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OFTBinary:
|
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(ogr) << "ogr_featureset: Unhandled type_oid=" << type_oid;
|
2012-01-19 18:55:07 +01:00
|
|
|
//feature->put(name,feat->GetFieldAsBinary (i, size));
|
2011-11-14 04:37:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OFTDate:
|
|
|
|
case OFTTime:
|
|
|
|
case OFTDateTime: // unhandled !
|
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(ogr) << "ogr_featureset: Unhandled type_oid=" << type_oid;
|
2011-11-14 04:37:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: // unknown
|
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(ogr) << "ogr_featureset: Unknown type_oid=" << type_oid;
|
2011-11-14 04:37:50 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-04-21 21:19:07 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-21 05:16:17 +02:00
|
|
|
OGRFeature::DestroyFeature( poFeature );
|
2011-04-21 21:19:07 +02:00
|
|
|
return feature;
|
|
|
|
}
|
2009-01-28 21:16:31 +01:00
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_featureset: " << count_ << " features";
|
2012-04-08 02:20:56 +02:00
|
|
|
|
2011-11-14 04:37:50 +01:00
|
|
|
return feature_ptr();
|
2009-01-29 05:20:34 +01:00
|
|
|
}
|