1   package org.wcb.resources;
2   
3   import org.wcb.model.vo.hibernate.AirportBO;
4   
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.io.InputStreamReader;
8   import java.io.BufferedReader;
9   import java.util.StringTokenizer;
10  import java.util.List;
11  import java.util.ArrayList;
12  
13  /**
14   * <small>
15   * <p/>
16   * Copyright (C)  2006  wbogaardt.
17   * This library is free software; you can redistribute it and/or
18   * modify it under the terms of the GNU Lesser General Public
19   * License as published by the Free Software Foundation; either
20   * version 2.1 of the License, or (at your option) any later version.
21   * <p/>
22   * This library is distributed in the hope that it will be useful,
23   * but WITHOUT ANY WARRANTY; without even the implied warranty of
24   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25   * Lesser General Public License for more details.
26   * <p/>
27   * You should have received a copy of the GNU Lesser General Public
28   * License along with this library; if not, write to the Free Software
29   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
30   * <p/>
31   * $File:  $ <br>
32   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Dec 7, 2006 8:22:57 AM $ <br>
33   * </small>
34   *
35   * @author wbogaardt
36   *         This utility takes the file from https://164.214.2.62/products/usfif/editionhttp.cfm?cycle=0003&ed=8
37   *   called ARPRT.TXT and parses it for use in this program.
38   */
39  public class ImportAirportDataFile {
40  
41      public ImportAirportDataFile() {
42      }
43  
44      /**
45       * This file comes from an old format.
46       * @param filename
47       * @throws IOException
48       * @deprecated
49       */
50      public List<AirportBO> loadFile(String filename) throws IOException {
51          InputStream ips;
52          ClassLoader cl = Thread.currentThread().getContextClassLoader();
53          String cvsline;
54          List<AirportBO> returnVal = new ArrayList<AirportBO>();
55          ips = cl.getResourceAsStream(filename);
56          BufferedReader br = new BufferedReader(new InputStreamReader(ips));
57          boolean lineOneSkip = false;
58          while((cvsline = br.readLine()) != null) {
59              if (lineOneSkip) {
60                  returnVal.add(parseLine(cvsline));
61              }
62              lineOneSkip = true;
63          }
64          return returnVal;
65      }
66  
67       public List<AirportBO> marshall(String[][] result) {
68          String[] lineItem;
69          List<AirportBO> airports = new ArrayList<AirportBO>();
70          for (int i = 0; i < result.length; i++) {
71              lineItem = result[i];
72                  airports.add(processLine(lineItem));
73          }
74           return airports;
75      }
76  
77      /**
78       * This process the line items in a Bureau of Transportation data file format
79       * The following is a sample line item:
80       * XWS,"Oliktok, AK: Oliktok Lrrs","Oliktok, AK",Alaska,United States of America,70,30,0,N,149,52,35,W
81       * Downloaded this file format from http://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=288&DB_Short_Name=Aviation%20Support%20Tables
82       *
83       * @param line an array of the above line item
84       * @return an airport object.
85       */
86      public AirportBO processLine(String[] line) {
87          AirportBO returnVal = new AirportBO();
88          returnVal.setIata("K" + line[0]);
89          returnVal.setFaa(line[0]);
90          returnVal.setIcao("K" + line[0]);
91          returnVal.setName(this.parseAirportName(line[1]));
92          returnVal.setMunicipal(this.parseCityName(line[2]));
93          returnVal.setState(line[3]);
94          returnVal.setCountry(line[4]);
95          //34-12-02.9000N
96          returnVal.setLatitude(line[5] + "-" + line[6] + "-" + line[7] + ".0000" + line[8]);
97          returnVal.setLongitude(line[9] + "-" + line[10] + "-" + line[11] + ".0000" + line[12]);
98          return returnVal;
99      }
100 
101     /**
102      * This is old.
103      * @param fileline file line to parse.
104      * @return AirportBO
105      * @deprecated
106      */
107     public AirportBO parseLine(String fileline) {
108         StringTokenizer st = new StringTokenizer(fileline, "\t");
109         AirportBO returnVal = new AirportBO();
110         st.nextToken(); //airport identifier
111         returnVal.setName(st.nextToken());//name
112         st.nextToken(); //state in numeric format
113         st.nextToken();//ICAO
114         String airportCode = st.nextToken();
115         returnVal.setIcao(airportCode);//FAA airport code
116         returnVal.setIata(airportCode);
117         returnVal.setFaa(airportCode);
118         st.nextToken();//LOC HDATUM
119         st.nextToken();//WGS DATUM
120         returnVal.setLatitude(parseLatitude(st.nextToken()));//WGS_LAT
121         st.nextToken();//WGS_DLAT
122         returnVal.setLongitude(parseLongitude(st.nextToken()));//WGS_LONG
123         st.nextToken();//WGS_DLONG
124         returnVal.setAltitude(st.nextToken());//Elevation
125         return returnVal;
126     }
127 
128     /**
129      * Based on Bureau of Transportation data file format the string is in this
130      * format: Burbank, CA: Burbank Bob Hope
131      * We just need Burbank Bob Hope as the airport name
132      * @param preName Burbank, CA: Burbank Bob Hope
133      * @return Burbank Bob Hope
134      */
135     public String parseAirportName(String preName) {
136         return preName.substring(preName.indexOf(":") + 2, preName.length());
137     }
138 
139     /**
140      * Based on Bureau of Transportation data file format the string is in this
141      * Format: Pauloff Harbor, AK
142      * We just need the name
143      * @param preCity Pauloff Harbor, AK
144      * @return Pauloff Harbor
145      */
146     public String parseCityName(String preCity) {
147         return preCity.substring(0, preCity.indexOf(","));
148     }
149     /**
150      * Parses the latitude string that looks like N34120290
151      * Has to come out as 34-12-02.9000N
152      * @param valLat Latitude in N34120290 format
153      * @return modified to 34-12-02.9000N format
154      */
155     public String parseLatitude(String valLat) {
156         StringBuilder builder = new StringBuilder();
157         builder.append(valLat.substring(1,3));//the latitude degrees
158         builder.append("-");
159         builder.append(valLat.substring(3,5));//the Latitude minutes
160         builder.append("-");
161         builder.append(valLat.substring(5,7));// the seconds.
162         builder.append(".");
163         builder.append(valLat.substring(7,valLat.length()));
164         builder.append("00");//add last two positions of zero
165         builder.append(valLat.substring(0,1));
166         return builder.toString();
167     }
168 
169     /**
170      * Parses the latitude string that looks like W119122600
171      * Has to come out as 119-12-26.0000W
172      * @param valLat Latitude in W119122600 format
173      * @return modified to 119-12-26.0000W format
174      */
175     public String parseLongitude(String valLat) {
176         StringBuilder builder = new StringBuilder();
177         builder.append(valLat.substring(1,4));//the latitude degrees
178         builder.append("-");
179         builder.append(valLat.substring(4,6));//the Latitude minutes
180         builder.append("-");
181         builder.append(valLat.substring(6,8));// the seconds.
182         builder.append(".");
183         builder.append(valLat.substring(8,valLat.length()));
184         builder.append("00");//add last two positions of zero
185         builder.append(valLat.substring(0,1));
186         return builder.toString();
187     }
188 
189 }