forked from MaZderMind/osm-history-splitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitter.cpp
185 lines (157 loc) · 5.45 KB
/
splitter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <unistd.h>
#define OSMIUM_MAIN
#define OSMIUM_WITH_PBF_INPUT
#define OSMIUM_WITH_XML_INPUT
#define OSMIUM_WITH_PBF_OUTPUT
#define OSMIUM_WITH_XML_OUTPUT
#include <osmium.hpp>
#include <osmium/output/pbf.hpp>
#include <osmium/output/xml.hpp>
#include <geos/geom/MultiPolygon.h>
#include <geos/algorithm/locate/IndexedPointInAreaLocator.h>
#include "softcut.hpp"
#include "hardcut.hpp"
template <class TExtractInfo> bool readConfig(char *conffile, CutInfo<TExtractInfo> &info);
int main(int argc, char *argv[]) {
bool softcut = true;
bool debug = false;
char *filename, *conffile;
static struct option long_options[] = {
{"debug", no_argument, 0, 'd'},
{"softcut", no_argument, 0, 's'},
{"hardcut", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
while (1) {
int c = getopt_long(argc, argv, "dsh", long_options, 0);
if (c == -1)
break;
switch (c) {
case 'd':
debug = true;
break;
case 's':
softcut = true;
break;
case 'h':
softcut = false;
break;
}
}
if (optind > argc-2) {
fprintf(stderr, "Usage: %s [OPTIONS] OSMFILE CONFIGFILE\n", argv[0]);
return 1;
}
filename = argv[optind];
conffile = argv[optind+1];
if(softcut & !strcmp(filename, "-")) {
fprintf(stderr, "Can't read from stdin when in softcut\n");
return 1;
}
Osmium::OSMFile infile(filename);
if(softcut) {
SoftcutInfo info;
if(!readConfig(conffile, info))
{
fprintf(stderr, "error reading config\n");
return 1;
}
SoftcutPassOne one(&info);
one.debug = debug;
Osmium::Input::read(infile, one);
SoftcutPassTwo two(&info);
two.debug = debug;
Osmium::Input::read(infile, two);
} else {
HardcutInfo info;
if(!readConfig(conffile, info))
{
fprintf(stderr, "error reading config\n");
return 1;
}
Hardcut cutter(&info);
cutter.debug = debug;
Osmium::Input::read(infile, cutter);
}
return 0;
}
template <class TExtractInfo> bool readConfig(char *conffile, CutInfo<TExtractInfo> &info) {
const int linelen = 4096;
FILE *fp = fopen(conffile, "r");
if(!fp) {
fprintf(stderr, "unable to open config file %s\n", conffile);
return false;
}
char line[linelen];
while(fgets(line, linelen-1, fp)) {
line[linelen-1] = '\0';
if(line[0] == '#' || line[0] == '\r' || line[0] == '\n' || line[0] == '\0')
continue;
int n = 0;
char *tok = strtok(line, "\t ");
const char *name = NULL;
double minlon = 0, minlat = 0, maxlon = 0, maxlat = 0;
char type = '\0';
char file[linelen];
while(tok) {
switch(n) {
case 0:
name = tok;
break;
case 1:
if(0 == strcmp("BBOX", tok))
type = 'b';
else if(0 == strcmp("POLY", tok))
type = 'p';
else if(0 == strcmp("OSM", tok))
type = 'o';
else {
type = '\0';
fprintf(stderr, "output %s of type %s: unknown output type\n", name, tok);
return false;
}
break;
case 2:
switch(type) {
case 'b':
if(4 == sscanf(tok, "%lf,%lf,%lf,%lf", &minlon, &minlat, &maxlon, &maxlat)) {
info.addExtract(name, minlat, minlon, maxlat, maxlon);
} else {
fprintf(stderr, "error reading BBOX %s for %s\n", tok, name);
return false;
}
break;
case 'p':
if(1 == sscanf(tok, "%s", file)) {
geos::geom::Geometry *geom = OsmiumExtension::GeometryReader::fromPolyFile(file);
if(!geom) {
fprintf(stderr, "error creating geometry from poly-file %s for %s\n", file, name);
break;
}
info.addExtract(name, geom);
}
break;
case 'o':
if(1 == sscanf(tok, "%s", file)) {
geos::geom::Geometry *geom = OsmiumExtension::GeometryReader::fromOsmFile(file);
if(!geom) {
fprintf(stderr, "error creating geometry from poly-file %s for %s\n", file, name);
break;
}
info.addExtract(name, geom);
}
break;
}
break;
}
tok = strtok(NULL, "\t ");
n++;
}
}
fclose(fp);
return true;
}