Initial import / commit

This commit is contained in:
Nicole Faerber 2021-05-24 23:35:02 +02:00
parent 5ccc9d2da1
commit fb70a04263
4 changed files with 279 additions and 0 deletions

114
DrawMapCairo.cpp Normal file
View file

@ -0,0 +1,114 @@
/*
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
*/
osmscout::DatabaseParameter databaseParameter;
osmscout::DatabaseRef database;
osmscout::MapServiceRef mapService;
osmscout::StyleConfigRef styleConfig;
osmscout::BasemapDatabaseRef basemapDatabase;
osmscout::MercatorProjection projection;
osmscout::MapParameter drawParameter;
osmscout::AreaSearchParameter searchParameter;
osmscout::MapData data;
int DrawMapCairoInit(int argc, char* argv[])
{
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);
//drawParameter.SetIconMode(args.iconMode);
//drawParameter.SetIconPaths(args.iconPaths);
drawParameter.SetDebugData(false);
drawParameter.SetDebugPerformance(false);
drawParameter.SetLabelLineMinCharCount(15);
drawParameter.SetLabelLineMaxCharCount(30);
drawParameter.SetLabelLineFitToArea(true);
return 0;
}
int DrawMapCairo(cairo_surface_t *surface, double lat, double lon, unsigned int zoomlevel, double direction)
{
osmscout::GeoCoord center(lat, lon);
osmscout::Magnification zoom{osmscout::Magnification::magClose};
std::list<osmscout::TileRef> tiles;
osmscout::MapPainterCairo painter(styleConfig);
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);
mapService->LookupTiles(projection, tiles);
mapService->LoadMissingTileData(searchParameter,*styleConfig, tiles);
mapService->AddTileDataToMapData(tiles, data);
mapService->GetGroundTiles(projection, data.groundTiles);
//LoadBaseMapTiles(data.baseMapTiles);
osmscout::GeoBox boundingBox;
projection.GetDimensions(boundingBox);
if (!painter.DrawMap(projection, drawParameter, data, cairo))
std::cerr << "Cannot paint" << std::endl;;
cairo_destroy(cairo);
return 0;
}

14
Makefile Normal file
View file

@ -0,0 +1,14 @@
LDFLAGS=`pkg-config --libs cairo` `pkg-config --libs gtk+-3.0` -losmscout_map_cairo -losmscout_map -losmscout
CFLAGS=-Wall -O2 -I./include/ `pkg-config --cflags cairo` `pkg-config --cflags gtk+-3.0`
CXXFLAGS=-Wall -O2 -std=c++17 -I./include/ `pkg-config --cflags cairo` `pkg-config --cflags pango`
OBJS=gmapnix.o DrawMapCairo.o
PRG=gmapnix
all: $(PRG)
gmapnix: $(OBJS)
$(CXX) -o $(PRG) $(OBJS) $(LDFLAGS)
clean:
rm -f $(PRG) $(OBJS)

137
gmapnix.c Normal file
View file

@ -0,0 +1,137 @@
#include <stdio.h>
#include <gtk/gtk.h>
#include <cairo.h>
#include <DrawMapCairo.h>
#define DEFAULT_WIDTH 800
#define DEFAULT_HEIGHT 600
// UI variables
static GtkWidget *window = NULL;
static GtkWidget *image_darea = NULL;
// we paint everything in here and then into the drawing area widget
static cairo_surface_t *psurface;
static unsigned int zoomlevel=70000;
static double direction=0.0;
static double center_lat=50.8838492;
static double center_lon=8.0209591;
static gboolean need_redraw=TRUE;
static gboolean
draw_event (GtkWidget *widget,
cairo_t *wcr,
gpointer data)
{
static cairo_surface_t *surface=NULL;
if (need_redraw) {
if (surface != NULL)
cairo_surface_destroy(surface);
surface=cairo_image_surface_create(CAIRO_FORMAT_RGB24, DEFAULT_WIDTH, DEFAULT_HEIGHT);
DrawMapCairo(surface, center_lat, center_lon, zoomlevel, direction);
need_redraw=FALSE;
}
cairo_set_source_surface (wcr, surface, 0, 0);
cairo_paint (wcr);
return FALSE;
}
void
move_clicked(GtkWidget *button, gpointer user_data)
{
gchar *dir=(gchar *)user_data;
switch (dir[0]) {
case 'l':
center_lon-=(100. / zoomlevel);
break;
case 'r':
center_lon+=(100. / zoomlevel);
break;
case 'u':
center_lat+=(100. / zoomlevel);
break;
case 'd':
center_lat-=(100. / zoomlevel);
break;
case '+':
zoomlevel+=(zoomlevel/10);
break;
case '-':
zoomlevel-=(zoomlevel/10);
break;
}
fprintf(stderr, "%f %f @ %d\n", center_lat, center_lon, zoomlevel);
need_redraw=TRUE;
gtk_widget_queue_draw(image_darea);
}
void
create_main_window (void)
{
GtkWidget *box;
GtkWidget *hbox;
GtkWidget *w, *i;
GtkWidget *grid;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
psurface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 800, 600);
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
gtk_container_add (GTK_CONTAINER (window), box);
image_darea = gtk_drawing_area_new ();
gtk_widget_set_size_request (image_darea, 800, 600);
gtk_container_add (GTK_CONTAINER (box), image_darea);
g_signal_connect (image_darea, "draw",
G_CALLBACK (draw_event), NULL);
grid = gtk_grid_new();
w=gtk_button_new_with_label("left");
gtk_grid_attach(GTK_GRID(grid), w, 0, 1, 1, 1);
g_signal_connect (w, "clicked",
G_CALLBACK (move_clicked), "l");
w=gtk_button_new_with_label("right");
gtk_grid_attach(GTK_GRID(grid), w, 2, 1, 1, 1);
g_signal_connect (w, "clicked",
G_CALLBACK (move_clicked), "r");
w=gtk_button_new_with_label("up");
gtk_grid_attach(GTK_GRID(grid), w, 1, 0, 1, 1);
g_signal_connect (w, "clicked",
G_CALLBACK (move_clicked), "u");
w=gtk_button_new_with_label("down");
g_signal_connect (w, "clicked",
G_CALLBACK (move_clicked), "d");
gtk_grid_attach(GTK_GRID(grid), w, 1, 2, 1, 1);
w=gtk_button_new_with_label("+");
g_signal_connect (w, "clicked",
G_CALLBACK (move_clicked), "+");
gtk_grid_attach(GTK_GRID(grid), w, 3, 0, 1, 1);
w=gtk_button_new_with_label("-");
g_signal_connect (w, "clicked",
G_CALLBACK (move_clicked), "-");
gtk_grid_attach(GTK_GRID(grid), w, 3, 2, 1, 1);
gtk_container_add (GTK_CONTAINER (box), grid);
}
int main(int argc, char **argv)
{
cairo_surface_t *surface;
if (DrawMapCairoInit(argc, argv) != 0) {
fprintf(stderr, "DrawMapCairoInit() failed\n");
return 1;
}
gtk_init(&argc, &argv);
create_main_window();
gtk_widget_show_all(window);
gtk_main();
return 0;
}

14
include/DrawMapCairo.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef DrawMapCairo_H
#define DrawMapCairo_H
#ifdef __cplusplus
extern "C"
#endif
int DrawMapCairoInit(int, char**);
#ifdef __cplusplus
extern "C"
#endif
int DrawMapCairo(cairo_surface_t *, double, double, unsigned int, double);
#endif