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)
|
|
|
|
*
|
2011-10-23 19:21:28 +00:00
|
|
|
* Copyright (C) 2011 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
//$Id$
|
|
|
|
|
|
|
|
// mapnik
|
|
|
|
#include <mapnik/global.hpp>
|
|
|
|
#include <mapnik/datasource.hpp>
|
2009-12-16 20:02:06 +00:00
|
|
|
#include <mapnik/box2d.hpp>
|
2009-05-11 22:06:48 +00:00
|
|
|
#include <mapnik/geometry.hpp>
|
|
|
|
#include <mapnik/feature.hpp>
|
|
|
|
#include <mapnik/feature_layer_desc.hpp>
|
|
|
|
#include <mapnik/wkb.hpp>
|
|
|
|
#include <mapnik/unicode.hpp>
|
2011-04-07 15:24:52 +00:00
|
|
|
#include <mapnik/mapped_memory_cache.hpp>
|
2011-05-18 01:28:22 +00:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
2009-05-11 22:06:48 +00:00
|
|
|
|
|
|
|
// boost
|
2011-04-07 15:24:52 +00:00
|
|
|
#include <boost/interprocess/streams/bufferstream.hpp>
|
2009-05-11 22:06:48 +00:00
|
|
|
|
|
|
|
// ogr
|
|
|
|
#include "ogr_index_featureset.hpp"
|
|
|
|
#include "ogr_converter.hpp"
|
|
|
|
#include "ogr_index.hpp"
|
|
|
|
#include "ogr_feature_ptr.hpp"
|
|
|
|
|
|
|
|
using mapnik::query;
|
2009-12-16 20:02:06 +00:00
|
|
|
using mapnik::box2d;
|
2009-05-11 22:06:48 +00:00
|
|
|
using mapnik::CoordTransform;
|
|
|
|
using mapnik::Feature;
|
|
|
|
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>
|
|
|
|
ogr_index_featureset<filterT>::ogr_index_featureset(OGRDataSource & dataset,
|
|
|
|
OGRLayer & layer,
|
|
|
|
const filterT& filter,
|
|
|
|
const std::string& index_file,
|
|
|
|
const std::string& encoding,
|
|
|
|
const bool multiple_geometries)
|
2011-11-14 03:37:50 +00:00
|
|
|
: dataset_(dataset),
|
|
|
|
layer_(layer),
|
|
|
|
layerdef_(layer.GetLayerDefn()),
|
|
|
|
filter_(filter),
|
|
|
|
tr_(new transcoder(encoding)),
|
|
|
|
fidcolumn_(layer_.GetFIDColumn()),
|
|
|
|
multiple_geometries_(multiple_geometries)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-04-07 15:24:52 +00:00
|
|
|
boost::optional<mapnik::mapped_region_ptr> memory = mapnik::mapped_memory_cache::find(index_file.c_str(),true);
|
|
|
|
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
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
|
|
|
std::sort(ids_.begin(),ids_.end());
|
|
|
|
|
2009-05-11 22:06:48 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 22:59:21 +00:00
|
|
|
std::clog << "OGR Plugin: query size=" << ids_.size() << std::endl;
|
2009-05-11 22:06:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
if (itr_ != ids_.end())
|
|
|
|
{
|
2011-04-29 20:00:45 +00:00
|
|
|
int pos = *itr_++;
|
|
|
|
layer_.SetNextByIndex (pos);
|
2009-05-11 22:06:48 +00:00
|
|
|
|
2011-04-29 20:00:45 +00:00
|
|
|
ogr_feature_ptr feat (layer_.GetNextFeature());
|
2011-04-22 23:28:45 +00:00
|
|
|
if ((*feat) != NULL)
|
2011-04-21 19:19:07 +00:00
|
|
|
{
|
2011-04-29 20:00:45 +00:00
|
|
|
// ogr feature ids start at 0, so add one to stay
|
|
|
|
// consistent with other mapnik datasources that start at 1
|
|
|
|
int feature_id = ((*feat)->GetFID() + 1);
|
2011-05-18 01:28:22 +00:00
|
|
|
feature_ptr feature(feature_factory::create(feature_id));
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-04-22 23:28:45 +00:00
|
|
|
OGRGeometry* geom=(*feat)->GetGeometryRef();
|
|
|
|
if (geom && !geom->IsEmpty())
|
2011-04-21 19:19:07 +00:00
|
|
|
{
|
2011-04-22 23:28:45 +00:00
|
|
|
ogr_converter::convert_geometry (geom, feature, multiple_geometries_);
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-04-22 23:28:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::clog << "### Warning: feature with null geometry: " << (*feat)->GetFID() << "\n";
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
#endif
|
|
|
|
|
2011-04-22 23:28:45 +00:00
|
|
|
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 ();
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-04-22 23:28:45 +00:00
|
|
|
switch (type_oid)
|
2011-04-21 19:19:07 +00:00
|
|
|
{
|
2011-11-14 03:37:50 +00:00
|
|
|
case OFTInteger:
|
|
|
|
{
|
|
|
|
boost::put(*feature,fld_name,(*feat)->GetFieldAsInteger (i));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OFTReal:
|
|
|
|
{
|
|
|
|
boost::put(*feature,fld_name,(*feat)->GetFieldAsDouble (i));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OFTString:
|
|
|
|
case OFTWideString: // deprecated !
|
|
|
|
{
|
|
|
|
UnicodeString ustr = tr_->transcode((*feat)->GetFieldAsString (i));
|
|
|
|
boost::put(*feature,fld_name,ustr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OFTIntegerList:
|
|
|
|
case OFTRealList:
|
|
|
|
case OFTStringList:
|
|
|
|
case OFTWideStringList: // deprecated !
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "OGR Plugin: unhandled type_oid=" << type_oid << std::endl;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OFTBinary:
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "OGR Plugin: unhandled type_oid=" << type_oid << std::endl;
|
|
|
|
#endif
|
|
|
|
//boost::put(*feature,name,feat->GetFieldAsBinary (i, size));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OFTDate:
|
|
|
|
case OFTTime:
|
|
|
|
case OFTDateTime: // unhandled !
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "OGR Plugin: unhandled type_oid=" << type_oid << std::endl;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: // unknown
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "OGR Plugin: unknown type_oid=" << type_oid << std::endl;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2011-04-21 19:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-22 23:28:45 +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>;
|
|
|
|
|