1 package org.opentrafficsim.road.network.factory.osm.input;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.Serializable;
7 import java.net.MalformedURLException;
8 import java.net.URISyntaxException;
9 import java.net.URL;
10 import java.util.List;
11
12 import org.openstreetmap.osmosis.core.task.v0_6.RunnableSource;
13 import org.openstreetmap.osmosis.xml.common.CompressionMethod;
14 import org.openstreetmap.osmosis.xml.v0_6.XmlReader;
15 import org.opentrafficsim.road.network.factory.osm.OSMNetwork;
16 import org.opentrafficsim.road.network.factory.osm.OSMTag;
17 import org.opentrafficsim.road.network.factory.osm.events.ProgressEvent;
18 import org.opentrafficsim.road.network.factory.osm.events.ProgressListener;
19
20 import crosby.binary.osmosis.OsmosisReader;
21
22
23
24
25
26
27
28
29
30
31
32 public final class ReadOSMFile implements Serializable
33 {
34
35
36 private static final long serialVersionUID = 20141231L;
37
38
39 private OSMParser sinkImplementation = null;
40
41
42 private boolean isReaderThreadDead = false;
43
44
45
46
47
48
49
50
51
52
53 public ReadOSMFile(final String location, final List<OSMTag> wantedTags, final List<String> filteredKeys,
54 final ProgressListener progressListener) throws URISyntaxException, FileNotFoundException, MalformedURLException
55 {
56 URL url = new URL(location);
57 File file = new File(url.toURI());
58
59 this.sinkImplementation = new OSMParser(wantedTags, filteredKeys);
60 this.sinkImplementation.setProgressListener(progressListener);
61
62 CompressionMethod compression = CompressionMethod.None;
63 boolean protocolBufferBinaryFormat = false;
64
65 if (file.getName().endsWith(".pbf"))
66 {
67 protocolBufferBinaryFormat = true;
68 }
69 else if (file.getName().endsWith(".gz"))
70 {
71 compression = CompressionMethod.GZip;
72 }
73 else if (file.getName().endsWith(".bz2"))
74 {
75 compression = CompressionMethod.BZip2;
76 }
77
78 RunnableSource reader = protocolBufferBinaryFormat ? new OsmosisReader(new FileInputStream(file))
79 : new XmlReader(file, false, compression);
80
81 reader.setSink(this.sinkImplementation);
82
83 Thread readerThread = new Thread(reader);
84 this.sinkImplementation.getProgressListener().progress(new ProgressEvent(this, "Starting Import."));
85 readerThread.start();
86 while (readerThread.isAlive())
87 {
88 try
89 {
90 readerThread.join();
91 }
92 catch (InterruptedException e)
93 {
94 System.err.println("Failed to join with the map reader thread: " + e.getMessage());
95 }
96 }
97
98 this.isReaderThreadDead = true;
99 }
100
101
102
103
104 public boolean checkisReaderThreadDead()
105 {
106 return this.isReaderThreadDead;
107 }
108
109
110
111
112 public OSMNetwork getNetwork()
113 {
114 return this.sinkImplementation.getNetwork();
115 }
116
117
118
119
120 public ProgressListener getProgressListener()
121 {
122 return this.sinkImplementation.getProgressListener();
123 }
124
125
126
127
128 public void setProgressListener(final ProgressListener progressListener)
129 {
130 this.sinkImplementation.setProgressListener(progressListener);
131 }
132
133
134 @Override
135 public final String toString()
136 {
137 return "ReadOSMFile [sinkImplementation=" + this.sinkImplementation + ", isReaderThreadDead=" + this.isReaderThreadDead
138 + "]";
139 }
140 }