View Javadoc
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   * <p>
24   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * <p>
27   * $LastChangedDate: 2015-07-26 01:01:13 +0200 (Sun, 26 Jul 2015) $, @version $Revision: 1155 $, by $Author: averbraeck $,
28   * initial version 31 dec. 2014 <br>
29   * @author <a href="http://www.tbm.tudelft.nl/mzhang">Mingxin Zhang </a>
30   * @author <a>Moritz Bergmann</a>
31   */
32  public final class ReadOSMFile implements Serializable
33  {
34  
35      /** */
36      private static final long serialVersionUID = 20141231L;
37  
38      /** The parser/network builder. */
39      private OSMParser sinkImplementation = null;
40  
41      /**  */
42      private boolean isReaderThreadDead = false;
43  
44      /**
45       * @param location String; the location of the OSM file
46       * @param wantedTags List&lt;OSMTag&gt;; the list of wanted tags
47       * @param filteredKeys List&lt;String&gt;; the list of filtered keys
48       * @param progressListener ProgressListener; ProgressListener
49       * @throws URISyntaxException when <cite>location</cite> is not a valid URL
50       * @throws FileNotFoundException when the OSM file can not be found
51       * @throws MalformedURLException when <cite>location</cite> is not valid
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          // Reader has finished
98          this.isReaderThreadDead = true;
99      }
100 
101     /**
102      * @return is reader thread dead
103      */
104     public boolean checkisReaderThreadDead()
105     {
106         return this.isReaderThreadDead;
107     }
108 
109     /**
110      * @return get the whole Network
111      */
112     public OSMNetwork getNetwork()
113     {
114         return this.sinkImplementation.getNetwork();
115     }
116 
117     /**
118      * @return progressListener.
119      */
120     public ProgressListener getProgressListener()
121     {
122         return this.sinkImplementation.getProgressListener();
123     }
124 
125     /**
126      * @param progressListener ProgressListener; set progressListener.
127      */
128     public void setProgressListener(final ProgressListener progressListener)
129     {
130         this.sinkImplementation.setProgressListener(progressListener);
131     }
132 
133     /** {@inheritDoc} */
134     @Override
135     public final String toString()
136     {
137         return "ReadOSMFile [sinkImplementation=" + this.sinkImplementation + ", isReaderThreadDead=" + this.isReaderThreadDead
138                 + "]";
139     }
140 }