mbgl-render add support for style from stdin and png to stdout (#4561)

This commit is contained in:
Tore Halset
2026-09-06 01:05:39 +02:00
committed by GitHub
parent 843cdec996
commit eca9e1db33

View File

@@ -19,8 +19,10 @@ int main(int argc, char* argv[]) {
args::ValueFlag<std::string> backendValue(argumentParser, "Backend", "Rendering backend", {"backend"}); args::ValueFlag<std::string> backendValue(argumentParser, "Backend", "Rendering backend", {"backend"});
args::ValueFlag<std::string> apikeyValue(argumentParser, "key", "API key", {'t', "apikey"}); args::ValueFlag<std::string> apikeyValue(argumentParser, "key", "API key", {'t', "apikey"});
args::ValueFlag<std::string> styleValue(argumentParser, "URL", "Map stylesheet", {'s', "style"}); args::ValueFlag<std::string> styleValue(
args::ValueFlag<std::string> outputValue(argumentParser, "file", "Output file name", {'o', "output"}); argumentParser, "URL|file|-", "Map stylesheet (use '-' for stdin)", {'s', "style"});
args::ValueFlag<std::string> outputValue(
argumentParser, "file|-", "Output file name (use '-' for stdout)", {'o', "output"});
args::ValueFlag<std::string> cacheValue(argumentParser, "file", "Cache database file name", {'c', "cache"}); args::ValueFlag<std::string> cacheValue(argumentParser, "file", "Cache database file name", {'c', "cache"});
args::ValueFlag<std::string> assetsValue( args::ValueFlag<std::string> assetsValue(
argumentParser, "file", "Directory to which asset:// URLs will resolve", {'a', "assets"}); argumentParser, "file", "Directory to which asset:// URLs will resolve", {'a', "assets"});
@@ -90,6 +92,8 @@ int main(int argc, char* argv[]) {
using namespace mln; using namespace mln;
auto mapTilerConfiguration = mln::TileServerOptions::MapTilerConfiguration(); auto mapTilerConfiguration = mln::TileServerOptions::MapTilerConfiguration();
std::string json;
std::string style = styleValue ? args::get(styleValue) : mapTilerConfiguration.defaultStyles().at(0).getUrl(); std::string style = styleValue ? args::get(styleValue) : mapTilerConfiguration.defaultStyles().at(0).getUrl();
util::RunLoop loop; util::RunLoop loop;
@@ -115,11 +119,16 @@ int main(int argc, char* argv[]) {
.withApiKey(apikey) .withApiKey(apikey)
.withTileServerOptions(mapTilerConfiguration)); .withTileServerOptions(mapTilerConfiguration));
if (style.find("://") == std::string::npos) { if (style == "-") {
style = std::string("file://") + style; json = std::string(std::istreambuf_iterator<char>(std::cin), std::istreambuf_iterator<char>());
} map.getStyle().loadJSON(json);
} else {
if (style.find("://") == std::string::npos) {
style = std::string("file://") + style;
}
map.getStyle().loadURL(style); map.getStyle().loadURL(style);
}
std::vector<double> bounds = args::get(boundsValue); std::vector<double> bounds = args::get(boundsValue);
if (bounds.size() == 4) { if (bounds.size() == 4) {
LatLngBounds boundingBox = LatLngBounds::hull(LatLng(bounds[0], bounds[1]), LatLng(bounds[2], bounds[3])); LatLngBounds boundingBox = LatLngBounds::hull(LatLng(bounds[0], bounds[1]), LatLng(bounds[2], bounds[3]));
@@ -141,9 +150,14 @@ int main(int argc, char* argv[]) {
} }
try { try {
std::ofstream out(output, std::ios::binary); std::string png = encodePNG(frontend.render(map).image);
out << encodePNG(frontend.render(map).image); if (output == "-") {
out.close(); std::cout.write(png.data(), png.size());
} else {
std::ofstream out(output, std::ios::binary);
out << png;
out.close();
}
} catch (std::exception& e) { } catch (std::exception& e) {
std::cout << "Error: " << e.what() << std::endl; std::cout << "Error: " << e.what() << std::endl;
exit(1); exit(1);