View Javadoc
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   * <p>
21   * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights
22   * reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
24   * <p>
25   * @version 31 dec. 2014 <br>
26   * @author <a href="http://www.tbm.tudelft.nl/mzhang">Mingxin Zhang </a>
27   * @author <a>Moritz Bergmann</a>
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       * @param location String; the location of the OSM file
64       * @param wt List&lt;Tag&gt;; the list of wanted tags
65       * @param ft List&lt;String&gt;; the list of filtered keys
66       * @throws URISyntaxException when <cite>location</cite> is not a valid URL
67       * @throws FileNotFoundException when the OSM file can not be found
68       * @throws MalformedURLException when <cite>location</cite> is not valid
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                 // TODO Auto-generated catch block
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         // System.out.println("reader thread is dead here/////");
137     }
138 
139     /**
140      * @return is reader thread dead
141      */
142     public final boolean checkisReaderThreadDead()
143     {
144         return this.isReaderThreadDead;
145     }
146 
147     /**
148      * @return get the whole Network
149      */
150     public final Network getNetwork()
151     {
152         return this.sinkImplementation.getNetwork();
153     }
154 }