gmapnix/DrawMapCairo.cpp

132 lines
4.3 KiB
C++
Raw Permalink Normal View History

2021-05-24 23:35:02 +02:00
/*
2021-05-24 23:37:17 +02:00
Copyright (C) 2021 Nicole Faerber <nicole.faerber@dpin.de>
based on:
2021-05-24 23:35:02 +02:00
DrawMap - a demo program for libosmscout
Copyright (C) 2009 Tim Teulings
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <iostream>
#include <osmscout/Database.h>
#include <osmscout/MapService.h>
#include <osmscout/BasemapDatabase.h>
#include <osmscout/MapPainterCairo.h>
#include <DrawMapCairo.h>
/*
Example for the nordrhein-westfalen.osm (to be executed in meson build directory):
Demos/DrawMapCairo ../maps/nordrhein-westfalen ../stylesheets/standard.oss 800 480 51.51241 7.46525 70000 test.png
*/
static osmscout::DatabaseParameter databaseParameter;
static osmscout::DatabaseRef database;
static osmscout::MapServiceRef mapService;
static osmscout::StyleConfigRef styleConfig;
2021-05-24 23:35:02 +02:00
static osmscout::BasemapDatabaseRef basemapDatabase;
static osmscout::MercatorProjection projection;
static osmscout::MapParameter drawParameter;
static osmscout::AreaSearchParameter searchParameter;
static osmscout::MapData data;
static std::list<osmscout::TileRef> tiles;
static osmscout::MapPainterCairo *cairoMapPainter{nullptr};
2021-05-24 23:35:02 +02:00
int DrawMapCairoInit(int argc, char* argv[])
{
2021-05-25 22:50:16 +02:00
std::list<std::string> iconPaths;
2021-05-24 23:35:02 +02:00
database=std::make_shared<osmscout::Database>(databaseParameter);
if (!database->Open("data/nordrhein-westfalen-latest/")) {
std::cerr << "Cannot open database" << std::endl;
return -1;
}
mapService=std::make_shared<osmscout::MapService>(database);
styleConfig = std::make_shared<osmscout::StyleConfig>(database->GetTypeConfig());
if (!styleConfig->Load("data/standard.oss")) {
std::cerr << "Cannot open style" << std::endl;
return -1;
}
drawParameter.SetFontName("/usr/share/fonts/TTF/LiberationSans-Regular.ttf");
drawParameter.SetFontSize(3.0);
drawParameter.SetRenderSeaLand(true);
drawParameter.SetRenderUnknowns(false);
drawParameter.SetRenderBackground(false);
drawParameter.SetRenderContourLines(false);
drawParameter.SetRenderHillShading(false);
2021-05-25 22:50:16 +02:00
drawParameter.SetIconMode(osmscout::MapParameter::IconMode::Scalable);
iconPaths.push_back("data/icons/svg/standard/");
drawParameter.SetIconPaths(iconPaths);
drawParameter.SetPatternMode(osmscout::MapParameter::PatternMode::Scalable);
drawParameter.SetPatternPaths(iconPaths);
2021-05-24 23:35:02 +02:00
drawParameter.SetDebugData(false);
drawParameter.SetDebugPerformance(false);
drawParameter.SetLabelLineMinCharCount(15);
drawParameter.SetLabelLineMaxCharCount(30);
drawParameter.SetLabelLineFitToArea(true);
//LoadBaseMapTiles(data.baseMapTiles);
2021-05-24 23:35:02 +02:00
assert(database);
assert(database->IsOpen());
assert(mapService);
assert(styleConfig);
cairoMapPainter = new osmscout::MapPainterCairo(styleConfig);
2021-05-24 23:35:02 +02:00
return 0;
}
int DrawMapCairo(cairo_surface_t *surface, double lat, double lon, unsigned int zoomlevel, double direction)
{
osmscout::GeoCoord center(lat, lon);
2021-05-25 22:50:16 +02:00
osmscout::Magnification zoom; /*{osmscout::Magnification::magClose};*/
osmscout::GeoBox boundingBox;
2021-05-24 23:35:02 +02:00
cairo_t *cairo=cairo_create(surface);
assert(database);
assert(database->IsOpen());
assert(mapService);
assert(styleConfig);
zoom.SetMagnification(zoomlevel);
projection.Set(center, direction, zoom, 96, 800, 600);
projection.GetDimensions(boundingBox);
mapService->GetGroundTiles(projection, data.groundTiles);
2021-05-24 23:35:02 +02:00
mapService->LookupTiles(projection, tiles);
mapService->LoadMissingTileData(searchParameter,*styleConfig, tiles);
mapService->AddTileDataToMapData(tiles, data);
if (!cairoMapPainter->DrawMap(projection, drawParameter, data, cairo))
2021-05-24 23:35:02 +02:00
std::cerr << "Cannot paint" << std::endl;;
2021-05-24 23:35:02 +02:00
cairo_destroy(cairo);
return 0;
}