2010-07-06 02:37:05 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Hermann Kraus
|
|
|
|
*
|
|
|
|
* 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/metawriter.hpp>
|
|
|
|
#include <mapnik/metawriter_json.hpp>
|
2010-07-12 17:27:33 +02:00
|
|
|
|
|
|
|
// Boost
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2010-07-06 02:37:05 +02:00
|
|
|
|
|
|
|
// STL
|
|
|
|
#include <iomanip>
|
2010-07-12 17:27:33 +02:00
|
|
|
#include <cstdio>
|
2010-07-06 02:37:05 +02:00
|
|
|
|
2010-08-02 03:35:18 +02:00
|
|
|
#define HEADER_NOT_WRITTEN -1
|
|
|
|
#define STOPPED -2
|
|
|
|
#define STARTED 0
|
|
|
|
|
2010-07-06 02:37:05 +02:00
|
|
|
namespace mapnik {
|
|
|
|
|
2010-07-26 00:41:18 +02:00
|
|
|
UnicodeString const& metawriter_property_map::operator[](std::string const& key) const
|
2010-07-06 02:37:05 +02:00
|
|
|
{
|
2010-07-26 00:41:18 +02:00
|
|
|
std::map<std::string, UnicodeString>::const_iterator it;
|
|
|
|
it = m_.find(key);
|
2010-07-29 18:49:47 +02:00
|
|
|
if (it == m_.end()) return not_found_;
|
2010-07-26 00:41:18 +02:00
|
|
|
return (*it).second;
|
2010-07-06 02:37:05 +02:00
|
|
|
}
|
|
|
|
|
2010-07-29 18:49:47 +02:00
|
|
|
metawriter_properties::metawriter_properties(boost::optional<std::string> str)
|
|
|
|
{
|
|
|
|
if (str) {
|
|
|
|
boost::split(*this, *str, boost::is_any_of(", "), boost::token_compress_on);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2010-07-26 00:41:18 +02:00
|
|
|
void metawriter_json_stream::start(metawriter_property_map const& properties)
|
2010-08-02 03:35:18 +02:00
|
|
|
{
|
|
|
|
if (!only_nonempty_) {
|
|
|
|
write_header();
|
|
|
|
} else {
|
|
|
|
count_ = HEADER_NOT_WRITTEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void metawriter_json_stream::write_header()
|
2010-07-06 02:37:05 +02:00
|
|
|
{
|
2010-07-26 00:41:18 +02:00
|
|
|
assert(f_);
|
|
|
|
*f_ << "{ \"type\": \"FeatureCollection\", \"features\": [\n";
|
2010-08-02 03:35:18 +02:00
|
|
|
count_ = STARTED;
|
2010-07-12 17:27:33 +02:00
|
|
|
}
|
|
|
|
|
2010-07-26 00:41:18 +02:00
|
|
|
void metawriter_json_stream::stop()
|
2010-07-12 17:27:33 +02:00
|
|
|
{
|
2010-08-02 03:35:18 +02:00
|
|
|
if (count_ >= STARTED && f_) {
|
2010-07-26 00:41:18 +02:00
|
|
|
*f_ << " ] }\n";
|
2010-07-12 17:27:33 +02:00
|
|
|
}
|
2010-08-02 03:35:18 +02:00
|
|
|
count_ = STOPPED;
|
2010-07-29 18:49:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
metawriter_json_stream::~metawriter_json_stream()
|
|
|
|
{
|
2010-08-02 03:35:18 +02:00
|
|
|
if (count_ >= STARTED) {
|
2010-07-29 18:49:47 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::cerr << "WARNING: GeoJSON metawriter not stopped before destroying it.";
|
|
|
|
#endif
|
|
|
|
stop();
|
|
|
|
}
|
2010-07-12 17:27:33 +02:00
|
|
|
}
|
|
|
|
|
2010-07-26 00:41:18 +02:00
|
|
|
metawriter_json_stream::metawriter_json_stream(metawriter_properties dflt_properties)
|
2010-08-02 03:35:18 +02:00
|
|
|
: metawriter(dflt_properties), count_(-1), only_nonempty_(true), f_(0) {}
|
2010-07-06 02:37:05 +02:00
|
|
|
|
2010-07-26 00:41:18 +02:00
|
|
|
void metawriter_json_stream::add_box(box2d<double> box, Feature const &feature,
|
2010-07-12 17:27:33 +02:00
|
|
|
proj_transform const& prj_trans, CoordTransform const &t,
|
|
|
|
const metawriter_properties& properties)
|
2010-07-06 02:37:05 +02:00
|
|
|
{
|
2010-08-02 03:35:18 +02:00
|
|
|
/* Check if feature is in bounds. */
|
|
|
|
if (box.maxx() < 0 || box.maxy() < 0 || box.minx() > width_ || box.miny() > height_) return;
|
|
|
|
|
|
|
|
if (count_ == HEADER_NOT_WRITTEN) write_header();
|
2010-07-29 18:49:47 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-08-02 03:35:18 +02:00
|
|
|
if (count_ == STOPPED)
|
2010-07-29 18:49:47 +02:00
|
|
|
{
|
|
|
|
std::cerr << "WARNING: Metawriter not started before using it.\n";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-07-06 02:37:05 +02:00
|
|
|
/* Coordinate transform in renderer:
|
|
|
|
input: layer srs
|
|
|
|
prj_trans.backwards() [prj_trans: map -> layer]
|
|
|
|
intermediate: map srs
|
|
|
|
t_.forward()
|
|
|
|
output: pixels
|
|
|
|
|
|
|
|
Our transform:
|
|
|
|
input: pixels
|
|
|
|
t_.backward()
|
|
|
|
intermediate: map srs (available via prj_trans.source())
|
|
|
|
trans.forward()
|
|
|
|
output: WGS84
|
|
|
|
*/
|
|
|
|
|
|
|
|
proj_transform trans(prj_trans.source(), projection("+proj=latlong +datum=WGS84"));
|
|
|
|
//t_ coord transform has transform for box2d combined with prj_trans
|
|
|
|
box = t.backward(box, trans);
|
|
|
|
|
|
|
|
double minx = box.minx();
|
|
|
|
double miny = box.miny();
|
|
|
|
double maxx = box.maxx();
|
|
|
|
double maxy = box.maxy();
|
|
|
|
|
2010-08-02 02:37:15 +02:00
|
|
|
if (count_++) *f_ << ",\n";
|
2010-07-26 00:41:18 +02:00
|
|
|
*f_ << std::fixed << std::setprecision(8) << "{ \"type\": \"Feature\",\n \"geometry\": { \"type\": \"Polygon\",\n \"coordinates\": [ [ [" <<
|
2010-07-06 02:37:05 +02:00
|
|
|
minx << ", " << miny << "], [" <<
|
|
|
|
maxx << ", " << miny << "], [" <<
|
|
|
|
maxx << ", " << maxy << "], [" <<
|
|
|
|
minx << ", " << maxy << "] ] ] }," <<
|
2010-07-12 17:27:33 +02:00
|
|
|
"\n \"properties\": {";
|
|
|
|
std::map<std::string, value> fprops = feature.props();
|
|
|
|
int i = 0;
|
2010-07-31 12:20:56 +02:00
|
|
|
BOOST_FOREACH(std::string p, properties)
|
2010-07-12 17:27:33 +02:00
|
|
|
{
|
|
|
|
std::map<std::string, value>::const_iterator itr = fprops.find(p);
|
|
|
|
std::string text;
|
|
|
|
if (itr != fprops.end())
|
|
|
|
{
|
|
|
|
//Property found
|
|
|
|
text = boost::replace_all_copy(boost::replace_all_copy(itr->second.to_string(), "\\", "\\\\"), "\"", "\\\"");
|
|
|
|
}
|
2010-07-26 00:41:18 +02:00
|
|
|
if (i++) *f_ << ",";
|
|
|
|
*f_ << "\n \"" << p << "\":\"" << text << "\"";
|
2010-07-12 17:27:33 +02:00
|
|
|
}
|
|
|
|
|
2010-07-26 00:41:18 +02:00
|
|
|
*f_ << "\n} }";
|
2010-07-06 02:37:05 +02:00
|
|
|
}
|
|
|
|
|
2010-07-26 00:41:18 +02:00
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
metawriter_json::metawriter_json(metawriter_properties dflt_properties, path_expression_ptr fn)
|
|
|
|
: metawriter_json_stream(dflt_properties), fn_(fn) {}
|
|
|
|
|
|
|
|
|
|
|
|
void metawriter_json::start(metawriter_property_map const& properties)
|
|
|
|
{
|
2010-08-02 03:35:18 +02:00
|
|
|
filename_ =
|
2010-07-26 00:41:18 +02:00
|
|
|
path_processor<metawriter_property_map>::evaluate(*fn_, properties);
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-08-02 03:35:18 +02:00
|
|
|
std::clog << "Metawriter JSON: filename=" << filename_ << "\n";
|
2010-07-26 00:41:18 +02:00
|
|
|
#endif
|
|
|
|
metawriter_json_stream::start(properties);
|
|
|
|
}
|
|
|
|
|
2010-08-02 03:35:18 +02:00
|
|
|
void metawriter_json::write_header()
|
|
|
|
{
|
|
|
|
f_.open(filename_.c_str(), std::fstream::out | std::fstream::trunc);
|
|
|
|
if (f_.fail()) perror((std::string("Metawriter JSON: Failed to open file ") + filename_).c_str());
|
|
|
|
set_stream(&f_);
|
|
|
|
metawriter_json_stream::write_header();
|
|
|
|
}
|
|
|
|
|
2010-07-26 00:41:18 +02:00
|
|
|
|
|
|
|
void metawriter_json::stop()
|
|
|
|
{
|
|
|
|
metawriter_json_stream::stop();
|
2010-07-29 18:49:47 +02:00
|
|
|
if (f_.is_open()) {
|
|
|
|
f_.close();
|
2010-07-12 17:27:33 +02:00
|
|
|
}
|
2010-07-29 18:49:47 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
else {
|
|
|
|
std::clog << "WARNING: File not open in metawriter_json::stop()!\n";
|
|
|
|
}
|
|
|
|
#endif
|
2010-07-12 17:27:33 +02:00
|
|
|
}
|
2010-07-29 18:49:47 +02:00
|
|
|
|
2010-08-02 02:37:15 +02:00
|
|
|
void metawriter_json::set_filename(path_expression_ptr fn)
|
|
|
|
{
|
|
|
|
fn_ = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
path_expression_ptr metawriter_json::get_filename() const
|
|
|
|
{
|
|
|
|
return fn_;
|
|
|
|
}
|
|
|
|
|
2010-07-06 02:37:05 +02:00
|
|
|
};
|