2009-05-11 22:06:48 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2009-05-11 22:06:48 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2014-11-20 14:25:50 +00:00
|
|
|
* Copyright (C) 2014 Artem Pavlenko
|
2009-05-11 22:06:48 +00: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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
// mapnik
|
2013-01-08 22:17:31 +00:00
|
|
|
#include <mapnik/value_types.hpp>
|
2009-05-11 22:06:48 +00:00
|
|
|
#include <mapnik/global.hpp>
|
2012-04-08 00:20:56 +00:00
|
|
|
#include <mapnik/debug.hpp>
|
2009-12-16 20:02:06 +00:00
|
|
|
#include <mapnik/box2d.hpp>
|
2009-05-11 22:06:48 +00:00
|
|
|
#include <mapnik/feature.hpp>
|
|
|
|
#include <mapnik/feature_layer_desc.hpp>
|
|
|
|
#include <mapnik/wkb.hpp>
|
|
|
|
#include <mapnik/unicode.hpp>
|
2011-05-18 01:28:22 +00:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
2015-05-20 21:00:30 +00:00
|
|
|
#include <mapnik/geometry_correct.hpp>
|
2009-05-11 22:06:48 +00:00
|
|
|
|
|
|
|
// boost
|
2013-08-13 18:26:48 +00:00
|
|
|
#ifdef SHAPE_MEMORY_MAPPED_FILE
|
|
|
|
#include <mapnik/mapped_memory_cache.hpp>
|
2013-08-13 18:13:56 +00:00
|
|
|
#include <boost/interprocess/mapped_region.hpp>
|
2011-04-07 15:24:52 +00:00
|
|
|
#include <boost/interprocess/streams/bufferstream.hpp>
|
2013-08-13 18:26:48 +00:00
|
|
|
#endif
|
2009-05-11 22:06:48 +00:00
|
|
|
|
|
|
|
// ogr
|
|
|
|
#include "ogr_index_featureset.hpp"
|
|
|
|
#include "ogr_converter.hpp"
|
|
|
|
#include "ogr_index.hpp"
|
|
|
|
|
2015-05-20 05:39:39 +00:00
|
|
|
#include <gdal_version.h>
|
|
|
|
|
2009-05-11 22:06:48 +00:00
|
|
|
using mapnik::query;
|
2009-12-16 20:02:06 +00:00
|
|
|
using mapnik::box2d;
|
2009-05-11 22:06:48 +00:00
|
|
|
using mapnik::feature_ptr;
|
|
|
|
using mapnik::geometry_utils;
|
|
|
|
using mapnik::transcoder;
|
2011-05-18 01:28:22 +00:00
|
|
|
using mapnik::feature_factory;
|
2009-05-11 22:06:48 +00:00
|
|
|
|
|
|
|
template <typename filterT>
|
2012-01-20 16:07:29 +00:00
|
|
|
ogr_index_featureset<filterT>::ogr_index_featureset(mapnik::context_ptr const & ctx,
|
2009-05-11 22:06:48 +00:00
|
|
|
OGRLayer & layer,
|
2012-01-20 16:07:29 +00:00
|
|
|
filterT const& filter,
|
|
|
|
std::string const& index_file,
|
|
|
|
std::string const& encoding)
|
|
|
|
: ctx_(ctx),
|
2011-11-14 03:37:50 +00:00
|
|
|
layer_(layer),
|
|
|
|
layerdef_(layer.GetLayerDefn()),
|
|
|
|
filter_(filter),
|
|
|
|
tr_(new transcoder(encoding)),
|
2013-05-17 15:24:32 +00:00
|
|
|
fidcolumn_(layer_.GetFIDColumn()),
|
|
|
|
feature_envelope_()
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2013-08-13 18:26:48 +00:00
|
|
|
#ifdef SHAPE_MEMORY_MAPPED_FILE
|
2013-04-10 11:44:05 +00:00
|
|
|
boost::optional<mapnik::mapped_region_ptr> memory = mapnik::mapped_memory_cache::instance().find(index_file, true);
|
2011-04-07 15:24:52 +00:00
|
|
|
if (memory)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-11-14 03:37:50 +00:00
|
|
|
boost::interprocess::ibufferstream file(static_cast<char*>((*memory)->get_address()),(*memory)->get_size());
|
2011-04-07 15:24:52 +00:00
|
|
|
ogr_index<filterT,boost::interprocess::ibufferstream >::query(filter,file,ids_);
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
2013-08-13 18:26:48 +00:00
|
|
|
#else
|
|
|
|
#if defined (WINDOWS)
|
|
|
|
std::ifstream file(mapnik::utf8_to_utf16(index_file), std::ios::in | std::ios::binary);
|
|
|
|
#else
|
|
|
|
std::ifstream file(index_file.c_str(), std::ios::in | std::ios::binary);
|
|
|
|
#endif
|
|
|
|
ogr_index<filterT,std::ifstream>::query(filter,file,ids_);
|
|
|
|
#endif
|
2011-11-14 03:37:50 +00:00
|
|
|
|
|
|
|
std::sort(ids_.begin(),ids_.end());
|
|
|
|
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_index_featureset: Query size=" << ids_.size();
|
2009-05-11 22:06:48 +00:00
|
|
|
|
|
|
|
itr_ = ids_.begin();
|
|
|
|
|
2011-11-14 03:37:50 +00:00
|
|
|
// reset reading
|
2009-05-11 22:06:48 +00:00
|
|
|
layer_.ResetReading();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename filterT>
|
|
|
|
ogr_index_featureset<filterT>::~ogr_index_featureset() {}
|
|
|
|
|
|
|
|
template <typename filterT>
|
|
|
|
feature_ptr ogr_index_featureset<filterT>::next()
|
|
|
|
{
|
2012-10-05 20:49:29 +00:00
|
|
|
while (itr_ != ids_.end())
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-04-29 20:00:45 +00:00
|
|
|
int pos = *itr_++;
|
|
|
|
layer_.SetNextByIndex (pos);
|
2009-05-11 22:06:48 +00:00
|
|
|
|
2012-10-21 03:16:17 +00:00
|
|
|
OGRFeature *poFeature = layer_.GetNextFeature();
|
2013-11-27 15:52:47 +00:00
|
|
|
if (poFeature == nullptr)
|
2011-04-21 19:19:07 +00:00
|
|
|
{
|
2012-10-21 03:16:17 +00:00
|
|
|
return feature_ptr();
|
2012-10-05 20:49:29 +00:00
|
|
|
}
|
2012-10-21 03:16:17 +00:00
|
|
|
|
2012-10-05 20:49:29 +00:00
|
|
|
// ogr feature ids start at 0, so add one to stay
|
|
|
|
// consistent with other mapnik datasources that start at 1
|
2012-12-20 03:24:52 +00:00
|
|
|
mapnik::value_integer feature_id = (poFeature->GetFID() + 1);
|
2012-10-05 20:49:29 +00:00
|
|
|
feature_ptr feature(feature_factory::create(ctx_,feature_id));
|
|
|
|
|
2012-10-21 03:16:17 +00:00
|
|
|
OGRGeometry* geom=poFeature->GetGeometryRef();
|
2012-10-05 20:49:29 +00:00
|
|
|
if (geom && !geom->IsEmpty())
|
|
|
|
{
|
2013-05-17 15:24:32 +00:00
|
|
|
geom->getEnvelope(&feature_envelope_);
|
|
|
|
if (!filter_.pass(mapnik::box2d<double>(feature_envelope_.MinX,feature_envelope_.MinY,
|
|
|
|
feature_envelope_.MaxX,feature_envelope_.MaxY))) continue;
|
2015-05-20 21:00:30 +00:00
|
|
|
auto geom_corrected = ogr_converter::convert_geometry(geom);
|
|
|
|
mapnik::geometry::correct(geom_corrected);
|
|
|
|
feature->set_geometry(std::move(geom_corrected));
|
2012-10-05 20:49:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-21 03:16:17 +00:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_index_featureset: Feature with null geometry="
|
|
|
|
<< poFeature->GetFID();
|
|
|
|
OGRFeature::DestroyFeature( poFeature );
|
2012-10-05 20:49:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fld_count = layerdef_->GetFieldCount();
|
|
|
|
for (int i = 0; i < fld_count; i++)
|
|
|
|
{
|
|
|
|
OGRFieldDefn* fld = layerdef_->GetFieldDefn (i);
|
|
|
|
OGRFieldType type_oid = fld->GetType ();
|
|
|
|
std::string fld_name = fld->GetNameRef ();
|
|
|
|
|
|
|
|
switch (type_oid)
|
|
|
|
{
|
|
|
|
case OFTInteger:
|
2015-05-20 05:39:39 +00:00
|
|
|
#if GDAL_VERSION_MAJOR >= 2
|
|
|
|
case OFTInteger64:
|
|
|
|
#endif
|
2012-10-05 20:49:29 +00:00
|
|
|
{
|
2012-12-03 13:12:09 +00:00
|
|
|
feature->put<mapnik::value_integer>(fld_name,poFeature->GetFieldAsInteger (i));
|
2012-10-05 20:49:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OFTReal:
|
|
|
|
{
|
2012-10-21 03:16:17 +00:00
|
|
|
feature->put(fld_name,poFeature->GetFieldAsDouble (i));
|
2012-10-05 20:49:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2012-10-05 20:49:29 +00:00
|
|
|
case OFTString:
|
|
|
|
case OFTWideString: // deprecated !
|
2011-04-21 19:19:07 +00:00
|
|
|
{
|
2013-08-13 22:52:04 +00:00
|
|
|
feature->put(fld_name,tr_->transcode(poFeature->GetFieldAsString (i)));
|
2012-10-05 20:49:29 +00:00
|
|
|
break;
|
2011-04-22 23:28:45 +00:00
|
|
|
}
|
2012-10-05 20:49:29 +00:00
|
|
|
|
|
|
|
case OFTIntegerList:
|
2015-05-20 05:39:39 +00:00
|
|
|
#if GDAL_VERSION_MAJOR >= 2
|
|
|
|
case OFTInteger64List:
|
|
|
|
#endif
|
2012-10-05 20:49:29 +00:00
|
|
|
case OFTRealList:
|
|
|
|
case OFTStringList:
|
|
|
|
case OFTWideStringList: // deprecated !
|
2011-04-22 23:28:45 +00:00
|
|
|
{
|
2012-10-05 20:49:29 +00:00
|
|
|
MAPNIK_LOG_WARN(ogr) << "ogr_index_featureset: Unhandled type_oid=" << type_oid;
|
|
|
|
break;
|
2011-04-22 23:28:45 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2012-10-05 20:49:29 +00:00
|
|
|
case OFTBinary:
|
2011-04-22 23:28:45 +00:00
|
|
|
{
|
2012-10-05 20:49:29 +00:00
|
|
|
MAPNIK_LOG_WARN(ogr) << "ogr_index_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_index_featureset: Unhandled type_oid=" << type_oid;
|
|
|
|
break;
|
|
|
|
}
|
2011-04-21 19:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-21 03:16:17 +00:00
|
|
|
OGRFeature::DestroyFeature( poFeature );
|
2012-10-05 20:49:29 +00:00
|
|
|
return feature;
|
2011-04-21 19:19:07 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-04-21 19:19:07 +00:00
|
|
|
return feature_ptr();
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template class ogr_index_featureset<mapnik::filter_in_box>;
|
|
|
|
template class ogr_index_featureset<mapnik::filter_at_point>;
|