Adding passing python test for statistics.
This commit is contained in:
parent
f3249c8fda
commit
441c13646c
2 changed files with 161 additions and 0 deletions
110
include/mapnik/ds_statistics.hpp
Normal file
110
include/mapnik/ds_statistics.hpp
Normal file
|
@ -0,0 +1,110 @@
|
|||
/*****************************************************************************
|
||||
*
|
||||
* 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_LAYER_STATISTICS_HPP
|
||||
#define MAPNIK_LAYER_STATISTICS_HPP
|
||||
|
||||
// mapnik
|
||||
#include <mapnik/attribute_descriptor.hpp>
|
||||
|
||||
// stl
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
namespace mapnik
|
||||
{
|
||||
|
||||
class ds_statistics
|
||||
{
|
||||
public:
|
||||
layer_descriptor(std::string const& name, std::string const& encoding)
|
||||
: name_(name),
|
||||
encoding_(encoding) {}
|
||||
|
||||
layer_descriptor(layer_descriptor const& other)
|
||||
: name_(other.name_),
|
||||
encoding_(other.encoding_),
|
||||
desc_ar_(other.desc_ar_) {}
|
||||
|
||||
void set_name(std::string const& name)
|
||||
{
|
||||
name_ = name;
|
||||
}
|
||||
|
||||
std::string const& get_name() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
void set_encoding(std::string const& encoding)
|
||||
{
|
||||
encoding_ = encoding;
|
||||
}
|
||||
|
||||
std::string const& get_encoding() const
|
||||
{
|
||||
return encoding_;
|
||||
}
|
||||
|
||||
void add_descriptor(attribute_descriptor const& desc)
|
||||
{
|
||||
desc_ar_.push_back(desc);
|
||||
}
|
||||
|
||||
std::vector<attribute_descriptor> const& get_descriptors() const
|
||||
{
|
||||
return desc_ar_;
|
||||
}
|
||||
|
||||
std::vector<attribute_descriptor>& get_descriptors()
|
||||
{
|
||||
return desc_ar_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::string encoding_;
|
||||
std::vector<attribute_descriptor> desc_ar_;
|
||||
};
|
||||
|
||||
template <typename charT,typename traits>
|
||||
inline std::basic_ostream<charT,traits>&
|
||||
operator << (std::basic_ostream<charT,traits>& out,
|
||||
layer_descriptor const& ld)
|
||||
{
|
||||
out << "name: " << ld.get_name() << "\n";
|
||||
out << "encoding: " << ld.get_encoding() << "\n";
|
||||
std::vector<attribute_descriptor> const& desc_ar = ld.get_descriptors();
|
||||
std::vector<attribute_descriptor>::const_iterator pos = desc_ar.begin();
|
||||
while (pos != desc_ar.end())
|
||||
{
|
||||
out << *pos++ << "\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
#endif // MAPNIK_LAYER_STATISTICS_HPP
|
51
tests/python_tests/statistics_test.py
Normal file
51
tests/python_tests/statistics_test.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
#encoding: utf8
|
||||
import itertools
|
||||
import unittest
|
||||
|
||||
class MemoryDatasource(unittest.TestCase):
|
||||
ids = itertools.count(0)
|
||||
|
||||
def makeOne(self, *args, **kw):
|
||||
from mapnik import MemoryDatasource
|
||||
return MemoryDatasource(*args, **kw)
|
||||
|
||||
def makeFeature(self, wkt, **properties):
|
||||
from mapnik import Feature
|
||||
f = Feature(self.ids.next())
|
||||
f.add_geometries_from_wkt(wkt)
|
||||
for k,v in properties.iteritems():
|
||||
f[k] = v
|
||||
return f
|
||||
|
||||
def test_default_constructor(self):
|
||||
f = self.makeOne()
|
||||
self.failUnless(f is not None)
|
||||
|
||||
def test_add_feature(self):
|
||||
md = self.makeOne()
|
||||
self.failUnlessEqual(md.num_features(), 0)
|
||||
md.add_feature(self.makeFeature('Point(2 3)', foo='bar'))
|
||||
self.failUnlessEqual(md.num_features(), 1)
|
||||
|
||||
from mapnik import Coord
|
||||
retrieved = md.features_at_point(Coord(2,3)).features
|
||||
self.failUnlessEqual(len(retrieved), 1)
|
||||
f = retrieved[0]
|
||||
self.failUnlessEqual(f['foo'], 'bar')
|
||||
|
||||
retrieved = md.features_at_point(Coord(20,30)).features
|
||||
self.failUnlessEqual(len(retrieved), 0)
|
||||
|
||||
def test_statistics(self):
|
||||
md = self.makeOne()
|
||||
self.failUnlessEqual(md.num_features(), 0)
|
||||
md.add_feature(self.makeFeature('Point(2 3)', a=1))
|
||||
self.failUnlessEqual(md.num_features(), 1)
|
||||
stats = md.statistics()
|
||||
self.failUnless(stats.has_key('a'))
|
||||
self.failUnlessEqual(stats['a']['min'], 1)
|
||||
self.failUnlessEqual(stats['a']['max'], 1)
|
||||
self.failUnlessEqual(stats['a']['mean'], 1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
[eval(run)() for run in dir() if 'test_' in run]
|
Loading…
Add table
Reference in a new issue