1 package org.opentrafficsim.importexport.osm.input;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.net.MalformedURLException;
7 import java.net.URISyntaxException;
8 import java.net.URL;
9 import java.util.List;
10
11 import org.openstreetmap.osmosis.core.task.v0_6.RunnableSource;
12 import org.openstreetmap.osmosis.xml.common.CompressionMethod;
13 import org.openstreetmap.osmosis.xml.v0_6.XmlReader;
14 import org.opentrafficsim.importexport.osm.Network;
15 import org.opentrafficsim.importexport.osm.Tag;
16
17 import crosby.binary.osmosis.OsmosisReader;
18
19
20
21
22
23
24
25
26
27
28
29 public class ReadOSMFile
30 {
31
32
33 private NewSink sinkImplementation = null;
34
35
36 private boolean pbf = false;
37
38
39 private URL url;
40
41
42 private File file;
43
44
45 private CompressionMethod compression;
46
47
48 private RunnableSource reader = null;
49
50
51 private Thread readerThread;
52
53
54 private boolean isReaderThreadDead = false;
55
56
57 private List<Tag> wantedTags;
58
59
60 private List<String> filterKeys;
61
62
63
64
65
66
67
68
69
70 public ReadOSMFile(final String location, final List<Tag> wt, final List<String> ft) throws URISyntaxException,
71 FileNotFoundException, MalformedURLException
72 {
73 this.wantedTags = wt;
74 this.filterKeys = ft;
75 this.url = new URL(location);
76 if (null == this.url)
77 {
78 throw new FileNotFoundException("Cannot construct url for location \"" + location + "\"");
79 }
80 this.file = new File(this.url.toURI());
81
82 this.sinkImplementation = new NewSink();
83 this.sinkImplementation.setWantedTags(this.wantedTags);
84 this.sinkImplementation.setFilterKeys(this.filterKeys);
85
86 this.compression = CompressionMethod.None;
87
88 if (this.file.getName().endsWith(".pbf"))
89 {
90 this.pbf = true;
91 }
92 else if (this.file.getName().endsWith(".gz"))
93 {
94 this.compression = CompressionMethod.GZip;
95 }
96 else if (this.file.getName().endsWith(".bz2"))
97 {
98 this.compression = CompressionMethod.BZip2;
99 }
100
101 if (this.pbf)
102 {
103 try
104 {
105 this.reader = new OsmosisReader(new FileInputStream(this.file));
106 }
107 catch (FileNotFoundException e)
108 {
109
110 e.printStackTrace();
111 }
112 }
113 else
114 {
115 this.reader = new XmlReader(this.file, false, this.compression);
116 }
117
118 this.reader.setSink(this.sinkImplementation);
119
120 this.readerThread = new Thread(this.reader);
121 this.readerThread.start();
122 while (this.readerThread.isAlive())
123 {
124 try
125 {
126 this.readerThread.join();
127 }
128 catch (InterruptedException e)
129 {
130 System.err.println("The map reader thread got problem!");
131 }
132 }
133
134 this.isReaderThreadDead = true;
135
136
137 }
138
139
140
141
142 public final boolean checkisReaderThreadDead()
143 {
144 return this.isReaderThreadDead;
145 }
146
147
148
149
150 public final Network getNetwork()
151 {
152 return this.sinkImplementation.getNetwork();
153 }
154 }