source: trunk/src/main.c @ 151

Last change on this file since 151 was 151, checked in by simon, 5 years ago

Make cfgRules private

  • Property svn:eol-style set to native
  • Property svn:keywords set to "Date Author Id Revision HeadURL"
  • Property svn:mime-type set to text/x-csrc
File size: 5.8 KB
Line 
1/*
2 * Memphis - Cairo Rederer for OSM in C
3 * Copyright (C) 2008  Marius Rieder <marius.rieder@durchmesser.ch>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#include <glib.h>
21#include <stdlib.h>
22#include <string.h>
23#include <math.h>
24
25#include "main.h"
26#include "memphis.h"
27
28/* Renderer Options */
29static memphisOpt opts_storage = {
30    .debug = 1,
31    .mode = MODE_MAP,
32    .minlayer = MEMPHIS_MIN_LAYER,
33    .maxlayer = MEMPHIS_MAX_LAYER,
34    .cfgfn = NULL,
35    .osmfn = NULL,
36    .outdir = MEMPHIS_DEFAULT_OUTPUT_DIRECTORY,
37};
38memphisOpt  *opts = &opts_storage;
39
40static int draw (MemphisRenderer *renderer) {
41  gint z, zoom;
42  gchar *filename;
43
44  // Initialize all layers
45  for (z = 0; z <= (opts->maxlayer - opts->minlayer); z++) {
46    zoom = z + opts->minlayer;
47    // Save Images
48    filename = g_strdup_printf ("%s/%02i.png", opts->outdir, zoom);
49    memphis_renderer_draw_png (renderer, filename, zoom);
50    g_free (filename);
51  }
52
53  return 0;
54}
55
56static gboolean set_verbosity_level(const gchar *option_name,
57                                    const gchar *value,
58                                    gpointer data,
59                                    GError **error)
60{
61    memphisOpt *o = data;
62    if ((strcmp(option_name, "-q") == 0) ||
63        (strcmp(option_name, "--quiet") == 0))
64    {
65        --o->debug;
66    }
67    else if ((strcmp(option_name, "-v") == 0) ||
68             (strcmp(option_name, "--verbose") == 0))
69    {
70        ++o->debug;
71    }
72    return TRUE;
73}
74
75static gboolean set_layer_option_cb(const gchar *option_name,
76                                    const gchar *value,
77                                    gpointer data,
78                                    GError **error)
79{
80    memphisOpt *o = data;
81    int v = atoi(value);
82    v = CLAMP(v, MEMPHIS_MIN_LAYER, MEMPHIS_MAX_LAYER);
83    if (strcmp(option_name, "--minlayer") == 0)
84        o->minlayer = v;
85    else if (strcmp(option_name, "--maxlayer") == 0)
86        o->maxlayer = v;
87    else
88        g_error("Unhandled option name: %s", option_name);
89    return TRUE;
90}
91
92static gboolean set_map_mode(const gchar *option_name,
93                             const gchar *value,
94                             gpointer data,
95                             GError **error)
96{
97    memphisOpt *o = data;
98    o->mode = MODE_MAP;
99    return TRUE;
100}
101
102/* Option parser */
103static GOptionEntry memphis_option_entries[] = {
104    { "quiet", 'q',     G_OPTION_FLAG_NO_ARG,
105                        G_OPTION_ARG_CALLBACK, set_verbosity_level,
106                        "Be quiet", NULL },
107    { "verbose", 'v',   G_OPTION_FLAG_NO_ARG,
108                        G_OPTION_ARG_CALLBACK, set_verbosity_level,
109                        "Be verbose", NULL },
110    { "map", 'm',       G_OPTION_FLAG_NO_ARG,
111                        G_OPTION_ARG_CALLBACK, set_map_mode,
112                        "Set map mode", NULL },
113    { "minlayer", 0,    0,
114                        G_OPTION_ARG_CALLBACK, set_layer_option_cb,
115                        "minimum layer to render", "LAYER" },
116    { "maxlayer", 0,    0,
117                        G_OPTION_ARG_CALLBACK, set_layer_option_cb,
118                        "maximum layer to render", "LAYER" },
119    { "out", 'o',       0,
120                        G_OPTION_ARG_FILENAME, &opts_storage.outdir,
121                        "tiles output directory (default: tiles)", "DIR" },
122};
123
124static void banner() {
125    g_print("Memphis OSM Renderer " MEMPHIS_VERSION "\n");
126}
127
128int main(int argc, char **argv) {
129
130    g_type_init ();
131
132    MemphisRuleSet *ruleset;
133    MemphisMap *osm;
134    MemphisRenderer *renderer;
135    GError *error = NULL;
136    GOptionContext *optctx;
137    GOptionGroup *grp;
138
139    grp = g_option_group_new("memphis",
140                             "memphis renderer options",
141                             "memphis renderer options",
142                             opts, NULL);
143    g_option_group_add_entries(grp, memphis_option_entries);
144    optctx = g_option_context_new("Memphis OSM Renderer " MEMPHIS_VERSION);
145    g_option_context_set_summary(optctx,
146            "memphis [-qvmt] [--minlayer] [--maxlayer] [-o DIR] <RULESFILE> <OSMFILE>");
147    g_option_context_set_main_group(optctx, grp);
148    if (!g_option_context_parse(optctx, &argc, &argv, &error)) {
149        g_print("option parsing failed: %s\n", error->message);
150        return 1;
151    }
152
153    if (argc > 1)
154        opts->cfgfn = argv[1];
155    if (argc > 2)
156        opts->osmfn = argv[2];
157
158    if (opts->cfgfn == NULL || opts->osmfn == NULL) {
159        g_print("error: rules file or osm map file missing:\n\n%s\n",
160                g_option_context_get_summary(optctx));
161        return(-1);
162    }
163
164    g_option_context_free(optctx);
165
166    banner();
167
168    ruleset = memphis_rule_set_new ();
169    memphis_rule_set_load_from_file (ruleset, opts->cfgfn);
170    //if (ruleset->ruleset == NULL)
171    //    return(-1);
172
173    osm = memphis_map_new ();
174    memphis_map_load_from_file (osm, opts->osmfn);
175
176    //if (osm->map == NULL)
177    //    return(-1);
178
179    renderer = memphis_renderer_new_full (ruleset, osm);
180    memphis_renderer_set_debug_level (renderer, opts->debug);
181   
182    draw (renderer);
183
184    memphis_map_free (osm);
185    memphis_rule_set_free (ruleset);
186    memphis_renderer_free (renderer);
187
188    return(0);
189}
190
191/*
192 * vim: expandtab shiftwidth=4 tabstop=4:
193 */
194
Note: See TracBrowser for help on using the repository browser.