mapnik/plugins/input/geos/geos_featureset.cpp

137 lines
4.1 KiB
C++
Raw Normal View History

/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
2011-10-23 21:19:56 +02:00
* Copyright (C) 2011 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
*
*****************************************************************************/
2011-10-23 21:19:56 +02:00
// stl
#include <iostream>
#include <fstream>
// mapnik
#include <mapnik/global.hpp>
#include <mapnik/datasource.hpp>
#include <mapnik/box2d.hpp>
#include <mapnik/geometry.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/feature_layer_desc.hpp>
#include <mapnik/wkb.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/feature_factory.hpp>
#include "geos_featureset.hpp"
using mapnik::query;
using mapnik::box2d;
using mapnik::coord2d;
using mapnik::CoordTransform;
using mapnik::Feature;
using mapnik::feature_ptr;
using mapnik::geometry_utils;
using mapnik::transcoder;
using mapnik::feature_factory;
geos_featureset::geos_featureset(GEOSGeometry* geometry,
GEOSGeometry* extent,
int identifier,
const std::string& field,
const std::string& field_name,
const std::string& encoding)
: geometry_(geometry),
tr_(new transcoder(encoding)),
extent_(extent),
identifier_(identifier),
field_(field),
field_name_(field_name),
already_rendered_(false),
ctx_(boost::make_shared<mapnik::context_type>())
{
ctx_->push(field_name);
}
geos_featureset::~geos_featureset()
{
}
feature_ptr geos_featureset::next()
{
if (! already_rendered_)
{
already_rendered_ = true;
if (GEOSisValid(geometry_) && ! GEOSisEmpty(geometry_))
{
bool render_geometry = true;
2011-10-23 21:19:56 +02:00
if (*extent_ != NULL && GEOSisValid(*extent_) && ! GEOSisEmpty(*extent_))
{
const int type = GEOSGeomTypeId(*extent_);
render_geometry = false;
2011-10-23 21:19:56 +02:00
switch (type)
{
case GEOS_POINT:
if (GEOSIntersects(*extent_, geometry_))
{
render_geometry = true;
}
break;
2011-10-23 21:19:56 +02:00
case GEOS_POLYGON:
if (GEOSContains(*extent_, geometry_)
|| GEOSWithin(geometry_, *extent_)
|| GEOSEquals(geometry_, *extent_))
{
render_geometry = true;
}
break;
2011-10-23 21:19:56 +02:00
default:
#ifdef MAPNIK_DEBUG
2011-10-23 21:19:56 +02:00
std::clog << "GEOS Plugin: unknown extent geometry_type=" << type << std::endl;
#endif
break;
}
}
if (render_geometry)
{
geos_wkb_ptr wkb(geometry_);
if (wkb.is_valid())
{
feature_ptr feature(feature_factory::create(ctx_,identifier_));
geometry_utils::from_wkb(feature->paths(),
wkb.data(),
wkb.size());
if (field_ != "")
{
feature->put(field_name_, tr_->transcode(field_.c_str()));
}
2012-02-02 02:37:35 +01:00
return feature;
}
}
}
}
return feature_ptr();
}