2022-02-02 17:20:29 +01:00
|
|
|
#include <mapnik/mapnik.hpp>
|
2014-02-10 19:46:48 +01:00
|
|
|
#include <mapnik/map.hpp>
|
|
|
|
#include <mapnik/load_map.hpp>
|
|
|
|
#include <mapnik/agg_renderer.hpp>
|
|
|
|
#include <mapnik/version.hpp>
|
|
|
|
#include <mapnik/debug.hpp>
|
|
|
|
#include <mapnik/image_util.hpp>
|
2014-11-04 21:31:51 +01:00
|
|
|
#include <mapnik/unicode.hpp>
|
2014-02-10 19:46:48 +01:00
|
|
|
#include <mapnik/datasource_cache.hpp>
|
2014-08-22 22:22:12 +02:00
|
|
|
#include <mapnik/font_engine_freetype.hpp>
|
2023-02-24 12:11:16 +01:00
|
|
|
#include <mapnik/proj_transform.hpp>
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-08 02:53:09 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2014-02-10 19:46:48 +01:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
2023-02-24 12:11:16 +01:00
|
|
|
#include <boost/spirit/home/x3.hpp>
|
|
|
|
#include <boost/fusion/adapted/struct.hpp>
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2014-10-22 01:37:27 +02:00
|
|
|
|
2014-02-10 19:46:48 +01:00
|
|
|
#include <string>
|
2023-02-24 12:11:16 +01:00
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
#include <filesystem>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
#else
|
2023-02-26 15:40:00 +01:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
namespace fs = boost::filesystem;
|
2023-02-24 12:11:16 +01:00
|
|
|
#endif
|
|
|
|
|
2023-02-24 18:51:40 +01:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(mapnik::box2d<double>, (double, minx_)(double, miny_)(double, maxx_)(double, maxy_))
|
2014-02-10 19:46:48 +01:00
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
int main(int argc, char** argv)
|
2014-02-10 19:46:48 +01:00
|
|
|
{
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2022-02-02 17:20:29 +01:00
|
|
|
mapnik::setup();
|
|
|
|
|
2014-02-10 19:46:48 +01:00
|
|
|
bool verbose = false;
|
2016-04-25 16:38:29 +02:00
|
|
|
bool auto_open = false;
|
2014-02-10 19:46:48 +01:00
|
|
|
int return_value = 0;
|
|
|
|
std::string xml_file;
|
|
|
|
std::string img_file;
|
2014-11-04 21:31:51 +01:00
|
|
|
double scale_factor = 1;
|
|
|
|
bool params_as_variables = false;
|
2014-02-10 19:46:48 +01:00
|
|
|
mapnik::logger logger;
|
|
|
|
logger.set_severity(mapnik::logger::error);
|
2019-05-05 18:02:47 +02:00
|
|
|
int map_width = 600;
|
|
|
|
int map_height = 400;
|
2014-02-10 19:46:48 +01:00
|
|
|
try
|
|
|
|
{
|
2015-12-06 16:10:43 +01:00
|
|
|
po::options_description desc("mapnik-render utility");
|
2022-01-26 20:41:37 +01:00
|
|
|
// clang-format off
|
2014-02-10 19:46:48 +01:00
|
|
|
desc.add_options()
|
|
|
|
("help,h", "produce usage message")
|
|
|
|
("version,V","print version string")
|
|
|
|
("verbose,v","verbose output")
|
2016-04-25 18:56:40 +02:00
|
|
|
("open","automatically open the file after rendering")
|
2014-02-10 19:46:48 +01:00
|
|
|
("xml",po::value<std::string>(),"xml map to read")
|
|
|
|
("img",po::value<std::string>(),"image to render")
|
2014-11-04 21:31:51 +01:00
|
|
|
("scale-factor",po::value<double>(),"scale factor for rendering")
|
2019-05-05 18:02:47 +02:00
|
|
|
("map-width",po::value<int>(),"map width in pixels")
|
|
|
|
("map-height",po::value<int>(),"map height in pixels")
|
2014-11-04 21:31:51 +01:00
|
|
|
("variables","make map parameters available as render-time variables")
|
2023-02-24 12:11:16 +01:00
|
|
|
("bbox", po::value<std::string>(), "bounding box e.g <minx,miny,maxx,maxy> in Map's SRS")
|
|
|
|
("geographic,g","bounding box is in WGS 84 lon/lat")
|
|
|
|
("plugins-dir", po::value<std::string>(), "directory containing input plug-ins (default: ./plugins/input)")
|
|
|
|
("fonts-dir", po::value<std::string>(), "directory containing fonts (default: relative to <plugins-dir> or ./fonts if no <plugins-dir> specified)");
|
2022-01-26 20:41:37 +01:00
|
|
|
// clang-format on
|
2014-02-10 19:46:48 +01:00
|
|
|
po::positional_options_description p;
|
2022-01-26 20:41:37 +01:00
|
|
|
p.add("xml", 1);
|
|
|
|
p.add("img", 1);
|
2014-02-10 19:46:48 +01:00
|
|
|
po::variables_map vm;
|
|
|
|
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
|
|
|
|
po::notify(vm);
|
|
|
|
|
|
|
|
if (vm.count("version"))
|
|
|
|
{
|
2022-01-26 20:41:37 +01:00
|
|
|
std::clog << "version " << MAPNIK_VERSION_STRING << std::endl;
|
2014-02-10 19:46:48 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (vm.count("help"))
|
|
|
|
{
|
|
|
|
std::clog << desc << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.count("verbose"))
|
|
|
|
{
|
|
|
|
verbose = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.count("open"))
|
|
|
|
{
|
|
|
|
auto_open = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.count("xml"))
|
|
|
|
{
|
2022-01-26 20:41:37 +01:00
|
|
|
xml_file = vm["xml"].as<std::string>();
|
2014-02-10 19:46:48 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::clog << "please provide an xml map as first argument!" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.count("img"))
|
|
|
|
{
|
2022-01-26 20:41:37 +01:00
|
|
|
img_file = vm["img"].as<std::string>();
|
2014-02-10 19:46:48 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::clog << "please provide an img as second argument!" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-04 21:31:51 +01:00
|
|
|
if (vm.count("scale-factor"))
|
|
|
|
{
|
2022-01-26 20:41:37 +01:00
|
|
|
scale_factor = vm["scale-factor"].as<double>();
|
2014-11-04 21:31:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.count("variables"))
|
|
|
|
{
|
|
|
|
params_as_variables = true;
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
if (vm.count("map-width"))
|
|
|
|
{
|
|
|
|
map_width = vm["map-width"].as<int>();
|
2019-05-05 18:02:47 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
if (vm.count("map-height"))
|
|
|
|
{
|
|
|
|
map_height = vm["map-height"].as<int>();
|
2019-05-05 18:02:47 +02:00
|
|
|
}
|
|
|
|
|
2023-02-24 12:11:16 +01:00
|
|
|
if (vm.count("plugins-dir"))
|
|
|
|
{
|
|
|
|
mapnik::datasource_cache::instance().register_datasources(vm["plugins-dir"].as<std::string>());
|
|
|
|
if (vm.count("fonts-dir"))
|
|
|
|
{
|
|
|
|
mapnik::freetype_engine::register_fonts(vm["fonts-dir"].as<std::string>(), true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// relative to plugins-dir
|
|
|
|
try
|
|
|
|
{
|
|
|
|
fs::path p(vm["plugins-dir"].as<std::string>());
|
|
|
|
p = p.parent_path() / "fonts";
|
2023-02-27 10:22:17 +01:00
|
|
|
mapnik::freetype_engine::register_fonts(p.string(), true);
|
2023-02-24 12:11:16 +01:00
|
|
|
}
|
2023-02-24 18:51:40 +01:00
|
|
|
catch (...)
|
|
|
|
{}
|
2023-02-24 12:11:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mapnik::datasource_cache::instance().register_datasources("./plugins/input");
|
|
|
|
mapnik::freetype_engine::register_fonts("./fonts", true);
|
|
|
|
}
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
auto plugin_names = mapnik::datasource_cache::instance().plugin_names();
|
|
|
|
if (plugin_names.empty())
|
|
|
|
{
|
|
|
|
std::cerr << "*WARNING*: no datasource plug-ings registered" << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "Registered datasource plug-ins:";
|
|
|
|
for (auto const& name : plugin_names)
|
|
|
|
{
|
|
|
|
std::cerr << name << " ";
|
|
|
|
}
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
mapnik::Map map(map_width, map_height);
|
|
|
|
mapnik::load_map(map, xml_file, true);
|
2023-02-24 12:11:16 +01:00
|
|
|
|
|
|
|
if (vm.count("bbox"))
|
|
|
|
{
|
|
|
|
namespace x3 = boost::spirit::x3;
|
|
|
|
|
|
|
|
mapnik::box2d<double> bbox;
|
|
|
|
std::string str = vm["bbox"].as<std::string>();
|
|
|
|
|
|
|
|
auto start = str.begin();
|
|
|
|
auto end = str.end();
|
2023-02-24 18:51:40 +01:00
|
|
|
if (!x3::phrase_parse(start,
|
|
|
|
end,
|
|
|
|
x3::double_ >> -x3::lit(',') >> x3::double_ >> -x3::lit(',') >> x3::double_ >>
|
|
|
|
-x3::lit(',') >> x3::double_,
|
2023-02-24 12:11:16 +01:00
|
|
|
x3::space,
|
|
|
|
bbox))
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to parse BBOX: " << str << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!bbox.valid())
|
|
|
|
{
|
|
|
|
std::cerr << "Invalid BBOX: " << str << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (vm.count("geographic"))
|
|
|
|
{
|
|
|
|
mapnik::projection source("epsg:4326");
|
|
|
|
mapnik::projection destination(map.srs());
|
|
|
|
mapnik::proj_transform tr(source, destination);
|
|
|
|
if (!tr.forward(bbox))
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to project input BBOX into " << map.srs() << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2023-02-24 18:51:40 +01:00
|
|
|
std::cerr << "zoom to:" << bbox << std::endl;
|
2023-02-24 12:11:16 +01:00
|
|
|
map.zoom_to_box(bbox);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
map.zoom_all();
|
|
|
|
}
|
2022-01-26 20:41:37 +01:00
|
|
|
mapnik::image_rgba8 im(map.width(), map.height());
|
|
|
|
mapnik::request req(map.width(), map.height(), map.get_current_extent());
|
2014-11-04 21:31:51 +01:00
|
|
|
req.set_buffer_size(map.buffer_size());
|
|
|
|
mapnik::attributes vars;
|
2014-11-04 23:53:50 +01:00
|
|
|
if (params_as_variables)
|
2014-11-04 21:31:51 +01:00
|
|
|
{
|
2014-11-04 23:53:50 +01:00
|
|
|
mapnik::transcoder tr("utf-8");
|
|
|
|
for (auto const& param : map.get_extra_parameters())
|
2014-11-04 21:31:51 +01:00
|
|
|
{
|
2014-11-04 23:53:50 +01:00
|
|
|
std::string const& name = param.first.substr(1);
|
|
|
|
if (!name.empty())
|
2014-11-04 21:31:51 +01:00
|
|
|
{
|
2014-11-04 23:53:50 +01:00
|
|
|
if (param.second.is<mapnik::value_integer>())
|
|
|
|
{
|
|
|
|
vars[name] = param.second.get<mapnik::value_integer>();
|
|
|
|
}
|
|
|
|
else if (param.second.is<mapnik::value_double>())
|
|
|
|
{
|
|
|
|
vars[name] = param.second.get<mapnik::value_double>();
|
|
|
|
}
|
|
|
|
else if (param.second.is<std::string>())
|
|
|
|
{
|
|
|
|
vars[name] = tr.transcode(param.second.get<std::string>().c_str());
|
|
|
|
}
|
2014-11-04 21:31:51 +01:00
|
|
|
}
|
2015-05-15 17:54:50 +02:00
|
|
|
}
|
2014-11-04 21:31:51 +01:00
|
|
|
}
|
2022-01-26 20:41:37 +01:00
|
|
|
mapnik::agg_renderer<mapnik::image_rgba8> ren(map, req, vars, im, scale_factor, 0, 0);
|
2014-02-10 19:46:48 +01:00
|
|
|
ren.apply();
|
2022-01-26 20:41:37 +01:00
|
|
|
mapnik::save_to_file(im, img_file);
|
2014-02-10 19:46:48 +01:00
|
|
|
if (auto_open)
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
2014-09-01 02:32:48 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
s << "open ";
|
|
|
|
#elif _WIN32
|
|
|
|
s << "start ";
|
2014-02-10 19:46:48 +01:00
|
|
|
#else
|
2014-09-01 02:32:48 +02:00
|
|
|
s << "xdg-open ";
|
2014-02-10 19:46:48 +01:00
|
|
|
#endif
|
2014-09-01 02:32:48 +02:00
|
|
|
s << img_file;
|
2014-02-10 19:46:48 +01:00
|
|
|
int ret = system(s.str().c_str());
|
|
|
|
if (ret != 0)
|
|
|
|
return_value = ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::clog << "rendered to: " << img_file << "\n";
|
|
|
|
}
|
2022-11-10 16:57:38 +01:00
|
|
|
}
|
|
|
|
catch (std::exception const& ex)
|
2014-02-10 19:46:48 +01:00
|
|
|
{
|
|
|
|
std::clog << "Error " << ex.what() << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return return_value;
|
|
|
|
}
|