Compare commits
10 commits
master
...
stats_proc
Author | SHA1 | Date | |
---|---|---|---|
|
37daca644a | ||
|
8665291dc4 | ||
|
c1b3f3000d | ||
|
4273a08e52 | ||
|
249cb3e2ca | ||
|
7ebc714b82 | ||
|
cdeb10e3c5 | ||
|
c0c6962780 | ||
|
508f580395 | ||
|
8b1ddd8766 |
5 changed files with 349 additions and 0 deletions
|
@ -193,6 +193,19 @@ boost::shared_ptr<mapnik::parameter> create_parameter_from_string(UnicodeString
|
|||
return boost::make_shared<mapnik::parameter>(key_utf8, ustr_utf8);
|
||||
}
|
||||
|
||||
boost::python::dict to_json(const parameters& p)
|
||||
{
|
||||
using namespace boost::python;
|
||||
dict d;
|
||||
parameters::const_iterator pos=p.begin();
|
||||
while(pos!=p.end())
|
||||
{
|
||||
d[pos->first] = pos->second;
|
||||
++pos;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
void export_parameters()
|
||||
{
|
||||
using namespace boost::python;
|
||||
|
@ -221,5 +234,6 @@ void export_parameters()
|
|||
.def("__len__",¶meters::size)
|
||||
.def("append",add_parameter)
|
||||
.def("iteritems",iterator<parameters>())
|
||||
.def("to_json",&to_json)
|
||||
;
|
||||
}
|
||||
|
|
|
@ -103,6 +103,8 @@ void clear_cache()
|
|||
mapnik::mapped_memory_cache::instance().clear();
|
||||
}
|
||||
|
||||
#include <mapnik/stats_processor.hpp>
|
||||
|
||||
#if defined(HAVE_CAIRO) && defined(HAVE_PYCAIRO)
|
||||
#include <pycairo.h>
|
||||
static Pycairo_CAPI_t *Pycairo_CAPI;
|
||||
|
@ -115,6 +117,14 @@ bool python_thread::thread_support = true;
|
|||
#endif
|
||||
boost::thread_specific_ptr<PyThreadState> python_thread::state;
|
||||
|
||||
std::string render_stats(const mapnik::Map& map)
|
||||
{
|
||||
python_unblock_auto_block b;
|
||||
mapnik::stats_processor ren(map);
|
||||
ren.apply();
|
||||
return ren.to_json();
|
||||
}
|
||||
|
||||
void render(const mapnik::Map& map,
|
||||
mapnik::image_32& image,
|
||||
double scale_factor = 1.0,
|
||||
|
@ -732,6 +742,7 @@ BOOST_PYTHON_MODULE(_mapnik)
|
|||
def("has_cairo", &has_cairo, "Get cairo library status");
|
||||
def("has_pycairo", &has_pycairo, "Get pycairo module status");
|
||||
|
||||
def("render_stats", &render_stats);
|
||||
python_optional<mapnik::stroke>();
|
||||
python_optional<mapnik::font_set>();
|
||||
python_optional<mapnik::color>();
|
||||
|
|
89
include/mapnik/stats_processor.hpp
Normal file
89
include/mapnik/stats_processor.hpp
Normal file
|
@ -0,0 +1,89 @@
|
|||
/*****************************************************************************
|
||||
*
|
||||
* This file is part of Mapnik (c++ mapping toolkit)
|
||||
*
|
||||
* 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
|
||||
*
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef MAPNIK_STATS_PROCESSOR_HPP
|
||||
#define MAPNIK_STATS_PROCESSOR_HPP
|
||||
|
||||
// mapnik
|
||||
#include <mapnik/map.hpp>
|
||||
|
||||
#include <boost/scoped_ptr.hpp>
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
#include <boost/property_tree/json_parser.hpp>
|
||||
|
||||
|
||||
// stl
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mapnik
|
||||
{
|
||||
|
||||
class Map;
|
||||
class layer;
|
||||
class projection;
|
||||
class proj_transform;
|
||||
class feature_type_style;
|
||||
|
||||
struct stats
|
||||
{
|
||||
stats()
|
||||
: elapsed(0),
|
||||
count(0),
|
||||
reprojected(false)
|
||||
{}
|
||||
unsigned int elapsed;
|
||||
unsigned int count;
|
||||
bool reprojected;
|
||||
};
|
||||
|
||||
class stats_processor
|
||||
{
|
||||
public:
|
||||
explicit stats_processor(Map const& m);
|
||||
void apply();
|
||||
std::string to_json()
|
||||
{
|
||||
std::ostringstream ss;
|
||||
write_json(ss,tree);
|
||||
return ss.str();
|
||||
}
|
||||
private:
|
||||
void apply_to_layer(layer const& lay,
|
||||
projection const& proj0,
|
||||
double scale_denom,
|
||||
std::set<std::string>& names);
|
||||
void render_style(layer const& lay,
|
||||
feature_type_style* style,
|
||||
std::string const& style_name,
|
||||
featureset_ptr features,
|
||||
proj_transform const& prj_trans,
|
||||
double scale_denom);
|
||||
|
||||
Map const& m_;
|
||||
boost::property_tree::ptree tree;
|
||||
boost::scoped_ptr<stats> s;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // MAPNIK_STATS_PROCESSOR_HPP
|
|
@ -148,6 +148,7 @@ source = Split(
|
|||
rule.cpp
|
||||
save_map.cpp
|
||||
shield_symbolizer.cpp
|
||||
stats_processor.cpp
|
||||
text_symbolizer.cpp
|
||||
tiff_reader.cpp
|
||||
wkb.cpp
|
||||
|
|
234
src/stats_processor.cpp
Normal file
234
src/stats_processor.cpp
Normal file
|
@ -0,0 +1,234 @@
|
|||
/*****************************************************************************
|
||||
*
|
||||
* This file is part of Mapnik (c++ mapping toolkit)
|
||||
*
|
||||
* 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
|
||||
*
|
||||
*****************************************************************************/
|
||||
|
||||
//mapnik
|
||||
#include <mapnik/stats_processor.hpp>
|
||||
#include <mapnik/map.hpp>
|
||||
#include <mapnik/debug.hpp>
|
||||
#include <mapnik/feature.hpp>
|
||||
#include <mapnik/feature_style_processor.hpp>
|
||||
#include <mapnik/query.hpp>
|
||||
#include <mapnik/feature.hpp>
|
||||
#include <mapnik/datasource.hpp>
|
||||
#include <mapnik/memory_datasource.hpp>
|
||||
#include <mapnik/feature_type_style.hpp>
|
||||
#include <mapnik/box2d.hpp>
|
||||
#include <mapnik/layer.hpp>
|
||||
#include <mapnik/rule.hpp>
|
||||
#include <mapnik/attribute_collector.hpp>
|
||||
#include <mapnik/expression_evaluator.hpp>
|
||||
#include <mapnik/utils.hpp>
|
||||
#include <mapnik/scale_denominator.hpp>
|
||||
#include <mapnik/projection.hpp>
|
||||
#include <mapnik/proj_transform.hpp>
|
||||
|
||||
// boost
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/variant/apply_visitor.hpp>
|
||||
#include <boost/variant/static_visitor.hpp>
|
||||
|
||||
//stl
|
||||
#include <vector>
|
||||
|
||||
#include <mapnik/timer.hpp>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
namespace mapnik
|
||||
{
|
||||
|
||||
|
||||
stats_processor::stats_processor(Map const& m)
|
||||
: m_(m),
|
||||
tree(),
|
||||
s(new stats())
|
||||
{}
|
||||
|
||||
void stats_processor::apply()
|
||||
{
|
||||
|
||||
mapnik::timer t;
|
||||
projection proj(m_.srs());
|
||||
|
||||
double scale_denom = mapnik::scale_denominator(m_,proj.is_geographic());
|
||||
|
||||
BOOST_FOREACH ( layer const& lyr, m_.layers() )
|
||||
{
|
||||
if (lyr.visible(scale_denom))
|
||||
{
|
||||
std::set<std::string> names;
|
||||
apply_to_layer(lyr, proj, scale_denom, names);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void stats_processor::apply_to_layer(layer const& lay, projection const& proj0,
|
||||
double scale_denom,
|
||||
std::set<std::string>& names)
|
||||
{
|
||||
boost::property_tree::ptree ltree;
|
||||
mapnik::timer t;
|
||||
|
||||
std::vector<std::string> const& style_names = lay.styles();
|
||||
|
||||
unsigned int num_styles = style_names.size();
|
||||
if (!num_styles) {
|
||||
std::clog << "WARNING: No style for layer '" << lay.name() << "'\n";
|
||||
return;
|
||||
}
|
||||
|
||||
mapnik::datasource_ptr ds = lay.datasource();
|
||||
if (!ds)
|
||||
{
|
||||
std::clog << "WARNING: No datasource for layer '" << lay.name() << "'\n";
|
||||
return;
|
||||
}
|
||||
|
||||
projection proj1(lay.srs());
|
||||
proj_transform prj_trans(proj0,proj1);
|
||||
|
||||
ltree.put("reprojected",!prj_trans.equal());
|
||||
|
||||
box2d<double> map_ext = m_.get_buffered_extent();
|
||||
|
||||
// clip buffered extent by maximum extent, if supplied
|
||||
boost::optional<box2d<double> > const& maximum_extent = m_.maximum_extent();
|
||||
if (maximum_extent) {
|
||||
map_ext.clip(*maximum_extent);
|
||||
}
|
||||
|
||||
box2d<double> layer_ext = lay.envelope();
|
||||
|
||||
// first, try intersection of map extent forward projected into layer srs
|
||||
if (prj_trans.forward(map_ext, PROJ_ENVELOPE_POINTS) && map_ext.intersects(layer_ext))
|
||||
{
|
||||
layer_ext.clip(map_ext);
|
||||
}
|
||||
// if no intersection and projections are also equal, early return
|
||||
else if (prj_trans.equal())
|
||||
{
|
||||
return;
|
||||
}
|
||||
// next try intersection of layer extent back projected into map srs
|
||||
else if (prj_trans.backward(layer_ext, PROJ_ENVELOPE_POINTS) && map_ext.intersects(layer_ext))
|
||||
{
|
||||
layer_ext.clip(map_ext);
|
||||
// forward project layer extent back into native projection
|
||||
if (!prj_trans.forward(layer_ext, PROJ_ENVELOPE_POINTS))
|
||||
std::clog << "WARNING: layer " << lay.name()
|
||||
<< " extent " << layer_ext << " in map projection "
|
||||
<< " did not reproject properly back to layer projection\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ltree.put("clipped_extent",layer_ext);
|
||||
|
||||
box2d<double> query_ext = m_.get_current_extent();
|
||||
box2d<double> unbuffered_extent = m_.get_current_extent();
|
||||
prj_trans.forward(query_ext, PROJ_ENVELOPE_POINTS);
|
||||
query::resolution_type res(m_.width()/query_ext.width(),
|
||||
m_.height()/query_ext.height());
|
||||
|
||||
query q(layer_ext,res,scale_denom,unbuffered_extent);
|
||||
|
||||
std::vector<feature_type_style*> active_styles;
|
||||
attribute_collector collector(names);
|
||||
double filt_factor = 1;
|
||||
directive_collector d_collector(filt_factor);
|
||||
|
||||
// iterate through all named styles collecting active styles and attribute names
|
||||
BOOST_FOREACH(std::string const& style_name, style_names)
|
||||
{
|
||||
boost::optional<feature_type_style const&> style=m_.find_style(style_name);
|
||||
if (!style)
|
||||
{
|
||||
std::clog << "WARNING: style '" << style_name << "' required for layer '"
|
||||
<< lay.name() << "' does not exist.\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
const std::vector<rule>& rules=(*style).get_rules();
|
||||
bool active_rules=false;
|
||||
|
||||
BOOST_FOREACH(rule const& r, rules)
|
||||
{
|
||||
if (r.active(scale_denom))
|
||||
{
|
||||
active_rules = true;
|
||||
if (ds->type() == datasource::Vector)
|
||||
{
|
||||
collector(r);
|
||||
}
|
||||
// TODO - in the future rasters should be able to be filtered.
|
||||
}
|
||||
}
|
||||
if (active_rules)
|
||||
{
|
||||
active_styles.push_back(const_cast<feature_type_style*>(&(*style)));
|
||||
}
|
||||
}
|
||||
|
||||
// Don't even try to do more work if there are no active styles.
|
||||
if (active_styles.size() > 0)
|
||||
{
|
||||
// push all property names
|
||||
BOOST_FOREACH(std::string const& name, names)
|
||||
{
|
||||
q.add_property_name(name);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
BOOST_FOREACH (feature_type_style * style, active_styles)
|
||||
{
|
||||
boost::property_tree::ptree stree;
|
||||
stree.put("name",style_names[i++]);
|
||||
featureset_ptr features = ds->features(q);
|
||||
if (features) {
|
||||
feature_ptr feature;
|
||||
while ((feature = features->next()))
|
||||
{
|
||||
BOOST_FOREACH(rule * r, style->get_if_rules(scale_denom) )
|
||||
{
|
||||
expression_ptr const& expr=r->get_filter();
|
||||
value_type result = boost::apply_visitor(evaluate<Feature,value_type>(*feature),*expr);
|
||||
if (result.to_bool())
|
||||
{
|
||||
// todo
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// http://stackoverflow.com/questions/2114466/creating-json-arrays-in-boost-using-property-trees
|
||||
ltree.push_back( std::make_pair("", stree ) );
|
||||
}
|
||||
}
|
||||
|
||||
ltree.put("wall_clock_elapsed", t.wall_clock_elapsed());
|
||||
ltree.put("cpu_elapsed", t.cpu_elapsed());
|
||||
|
||||
tree.put_child(lay.name(),ltree);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue