From a81e822ef003c5618ababd720103e1b828276988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Thu, 11 Dec 2014 17:58:36 +0100 Subject: add cli tool for rendering a map to an image --- bin/render.cpp | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 bin/render.cpp (limited to 'bin/render.cpp') diff --git a/bin/render.cpp b/bin/render.cpp new file mode 100644 index 0000000000..d545c09292 --- /dev/null +++ b/bin/render.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#if __APPLE__ +#include +#else +#include +#endif + +#include +namespace po = boost::program_options; + +#include +#include +#include + +int main(int argc, char *argv[]) { + + std::string style_path; + double lat = 0, lon = 0; + double zoom = 0; + double bearing = 0; + + int width = 256; + int height = 256; + double pixelRatio = 1.0; + const char *output = "out.png"; + std::vector classes; + std::string token; + + po::options_description desc("Allowed options"); + desc.add_options() + ("style,s", po::value(&style_path)->required()->value_name("json"),"Map stylesheet") + ("lon,x", po::value(&lon)->value_name("degrees"), "Longitude") + ("lat,y", po::value(&lat)->value_name("degrees"), "Latitude in degrees") + ("zoom,z", po::value(&zoom)->value_name("number"), "Zoom level") + ("bearing,b", po::value(&bearing)->value_name("degrees"), "Bearing") + ("width,w", po::value(&width)->value_name("pixels"), "Image width") + ("height,h", po::value(&height)->value_name("pixels"), "Image height") + ("class,c", po::value(&classes)->value_name("name"), "Class name") + ("token,t", po::value(&token)->value_name("key"), "Mapbox access token") + ; + + try { + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + } catch(std::exception& e) { + std::cout << "Error: " << e.what() << std::endl << desc; + exit(1); + } + + std::string style = mbgl::util::read_file(style_path); + + using namespace mbgl; + + +#if __APPLE__ + Log::Set(); +#else + Log::Set(); +#endif + + CachingHTTPFileSource fileSource(""); + + // Try to load the token from the environment. + if (!token.size()) { + token = getenv("MAPBOX_ACCESS_TOKEN"); + } + + // Set access token if present + if (token.size()) { + fileSource.setAccessToken(std::string(token)); + } + + HeadlessView view; + Map map(view, fileSource); + + map.setStyleJSON(style, "."); + map.setAppliedClasses(classes); + + view.resize(width, height, pixelRatio); + map.resize(width, height, pixelRatio); + map.setLonLatZoom(lon, lat, zoom); + map.setBearing(bearing); + + std::unique_ptr pixels; + + // Run the loop. It will terminate when we don't have any further listeners. + map.run(); + + // Get the data from the GPU. + pixels = view.readPixels(); + + const unsigned int w = width * pixelRatio; + const unsigned int h = height * pixelRatio; + const std::string image = util::compress_png(w, h, pixels.get()); + util::write_file(output, image); +} -- cgit v1.2.1