-
Notifications
You must be signed in to change notification settings - Fork 113
/
OsmCutter.java
332 lines (284 loc) · 9.65 KB
/
OsmCutter.java
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* This program
* - reads an *.osm from stdin
* - writes 45*30 degree node tiles + a way file + a rel file
*
* @author ab
*/
package btools.mapcreator;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import btools.expressions.BExpressionContextNode;
import btools.expressions.BExpressionContextWay;
import btools.expressions.BExpressionMetaData;
public class OsmCutter extends MapCreatorBase
{
private long recordCnt;
private long nodesParsed;
private long waysParsed;
private long relsParsed;
private long changesetsParsed;
private DataOutputStream wayDos;
private DataOutputStream cyclewayDos;
private DataOutputStream restrictionsDos;
public WayCutter wayCutter;
public RestrictionCutter restrictionCutter;
public NodeFilter nodeFilter;
public static void main(String[] args) throws Exception
{
System.out.println("*** OsmCutter: cut an osm map in node-tiles + a way file");
if (args.length != 6 && args.length != 7)
{
System.out.println("usage: bzip2 -dc <map> | java OsmCutter <lookup-file> <out-tile-dir> <out-way-file> <out-rel-file> <out-res-file> <filter-profile>");
System.out.println("or : java OsmCutter <lookup-file> <out-tile-dir> <out-way-file> <out-rel-file> <out-res-file> <filter-profile> <inputfile> ");
return;
}
new OsmCutter().process(
new File( args[0] )
, new File( args[1] )
, new File( args[2] )
, new File( args[3] )
, new File( args[4] )
, new File( args[5] )
, args.length > 6 ? new File( args[6] ) : null
);
}
private BExpressionContextWay _expctxWay;
private BExpressionContextNode _expctxNode;
// private BExpressionContextWay _expctxWayStat;
// private BExpressionContextNode _expctxNodeStat;
public void process (File lookupFile, File outTileDir, File wayFile, File relFile, File resFile, File profileFile, File mapFile ) throws Exception
{
if ( !lookupFile.exists() )
{
throw new IllegalArgumentException( "lookup-file: " + lookupFile + " does not exist" );
}
BExpressionMetaData meta = new BExpressionMetaData();
_expctxWay = new BExpressionContextWay( meta );
_expctxNode = new BExpressionContextNode( meta );
meta.readMetaData( lookupFile );
_expctxWay.parseFile( profileFile, "global" );
// _expctxWayStat = new BExpressionContextWay( null );
// _expctxNodeStat = new BExpressionContextNode( null );
this.outTileDir = outTileDir;
if ( !outTileDir.isDirectory() ) throw new RuntimeException( "out tile directory " + outTileDir + " does not exist" );
wayDos = wayFile == null ? null : new DataOutputStream( new BufferedOutputStream( new FileOutputStream( wayFile ) ) );
cyclewayDos = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( relFile ) ) );
if ( resFile != null )
{
restrictionsDos = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( resFile ) ) );
}
// read the osm map into memory
long t0 = System.currentTimeMillis();
new OsmParser().readMap( mapFile, this, this, this );
long t1 = System.currentTimeMillis();
System.out.println( "parsing time (ms) =" + (t1-t0) );
// close all files
closeTileOutStreams();
if ( wayDos != null )
{
wayDos.close();
}
cyclewayDos.close();
if ( restrictionsDos != null )
{
restrictionsDos.close();
}
// System.out.println( "-------- way-statistics -------- " );
// _expctxWayStat.dumpStatistics();
// System.out.println( "-------- node-statistics -------- " );
// _expctxNodeStat.dumpStatistics();
System.out.println( statsLine() );
}
private void checkStats()
{
if ( (++recordCnt % 100000) == 0 ) System.out.println( statsLine() );
}
private String statsLine()
{
return "records read: " + recordCnt + " nodes=" + nodesParsed + " ways=" + waysParsed + " rels=" + relsParsed + " changesets=" + changesetsParsed;
}
@Override
public void nextNode( NodeData n ) throws Exception
{
nodesParsed++;
checkStats();
if ( n.getTagsOrNull() != null )
{
int[] lookupData = _expctxNode.createNewLookupData();
for( Map.Entry<String,String> e : n.getTagsOrNull().entrySet() )
{
_expctxNode.addLookupValue( e.getKey(), e.getValue(), lookupData );
// _expctxNodeStat.addLookupValue( key, value, null );
}
n.description = _expctxNode.encode(lookupData);
}
// write node to file
int tileIndex = getTileIndex( n.ilon, n.ilat );
if ( tileIndex >= 0 )
{
n.writeTo( getOutStreamForTile( tileIndex ) );
if ( wayCutter != null )
{
wayCutter.nextNode( n );
}
}
}
private void generatePseudoTags( HashMap<String,String> map )
{
// add pseudo.tags for concrete:lanes and concrete:plates
String concrete = null;
for( Map.Entry<String,String> e : map.entrySet() )
{
String key = e.getKey();
if ( "concrete".equals( key ) )
{
return;
}
if ( "surface".equals( key ) )
{
String value = e.getValue();
if ( value.startsWith( "concrete:" ) )
{
concrete = value.substring( "concrete:".length() );
}
}
}
if ( concrete != null )
{
map.put( "concrete", concrete );
}
}
@Override
public void nextWay( WayData w ) throws Exception
{
waysParsed++;
checkStats();
// encode tags
if ( w.getTagsOrNull() == null ) return;
generatePseudoTags( w.getTagsOrNull() );
int[] lookupData = _expctxWay.createNewLookupData();
for( String key : w.getTagsOrNull().keySet() )
{
String value = w.getTag( key );
_expctxWay.addLookupValue( key, value.replace( ' ', '_' ), lookupData );
// _expctxWayStat.addLookupValue( key, value, null );
}
w.description = _expctxWay.encode(lookupData);
if ( w.description == null ) return;
// filter according to profile
_expctxWay.evaluate( false, w.description );
boolean ok = _expctxWay.getCostfactor() < 10000.;
_expctxWay.evaluate( true, w.description );
ok |= _expctxWay.getCostfactor() < 10000.;
if ( !ok ) return;
if ( wayDos != null )
{
w.writeTo( wayDos );
}
if ( wayCutter != null )
{
wayCutter.nextWay( w );
}
if ( nodeFilter != null )
{
nodeFilter.nextWay( w );
}
}
@Override
public void nextRelation( RelationData r ) throws Exception
{
relsParsed++;
checkStats();
String route = r.getTag( "route" );
// filter out non-cycle relations
if ( route == null )
{
return;
}
String network = r.getTag( "network" );
if ( network == null ) network = "";
String state = r.getTag( "state" );
if ( state == null ) state = "";
writeId( cyclewayDos, r.rid );
cyclewayDos.writeUTF( route );
cyclewayDos.writeUTF( network );
cyclewayDos.writeUTF( state );
for ( int i=0; i<r.ways.size();i++ )
{
long wid = r.ways.get(i);
writeId( cyclewayDos, wid );
}
writeId( cyclewayDos, -1 );
}
@Override
public void nextRestriction( RelationData r, long fromWid, long toWid, long viaNid ) throws Exception
{
String type = r.getTag( "type" );
if ( type == null || !"restriction".equals( type ) )
{
return;
}
short exceptions = 0;
String except = r.getTag( "except" );
if ( except != null )
{
exceptions |= toBit( "bicycle" , 0, except );
exceptions |= toBit( "motorcar" , 1, except );
exceptions |= toBit( "agricultural" , 2, except );
exceptions |= toBit( "forestry" , 2, except );
exceptions |= toBit( "psv" , 3, except );
exceptions |= toBit( "hgv" , 4, except );
}
for( String restrictionKey : r.getTagsOrNull().keySet() )
{
if ( !( restrictionKey.equals( "restriction" ) || restrictionKey.startsWith( "restriction:" ) ) )
{
continue;
}
String restriction = r.getTag( restrictionKey );
RestrictionData res = new RestrictionData();
res.restrictionKey = restrictionKey;
res.restriction = restriction;
res.exceptions = exceptions;
res.fromWid = fromWid;
res.toWid = toWid;
res.viaNid = viaNid;
if ( restrictionsDos != null )
{
res.writeTo( restrictionsDos );
}
if ( restrictionCutter != null )
{
restrictionCutter.nextRestriction( res );
}
}
}
private static short toBit( String tag, int bitpos, String s )
{
return (short) ( s.indexOf( tag ) < 0 ? 0 : 1 << bitpos );
}
private int getTileIndex( int ilon, int ilat )
{
int lon = ilon / 45000000;
int lat = ilat / 30000000;
if ( lon < 0 || lon > 7 || lat < 0 || lat > 5 )
{
System.out.println( "warning: ignoring illegal pos: " + ilon + "," + ilat );
return -1;
}
return lon*6 + lat;
}
protected String getNameForTile( int tileIndex )
{
int lon = (tileIndex / 6 ) * 45 - 180;
int lat = (tileIndex % 6 ) * 30 - 90;
String slon = lon < 0 ? "W" + (-lon) : "E" + lon;
String slat = lat < 0 ? "S" + (-lat) : "N" + lat;
return slon + "_" + slat + ".ntl";
}
}