View Javadoc
1   package au.gov.amsa.ihs.reader;
2   
3   import java.io.File;
4   import java.io.FileNotFoundException;
5   import java.io.PrintStream;
6   
7   import org.joda.time.DateTime;
8   
9   import com.google.common.base.Optional;
10  
11  import au.gov.amsa.ihs.model.Ship;
12  import rx.functions.Action1;
13  
14  public class ExtractCsvMain {
15  
16      public static void main(String[] args) throws FileNotFoundException {
17          File file = new File("/media/analysis/ship-data/ihs/608750.zip");
18          final PrintStream out = new PrintStream("target/ships.csv");
19          out.println(
20                  "IMO, MMSI, DWT, GT, Type2, Type3, Type4, Type5, StatCode5, LOAMetres, BreadthMetres, BuildYear, BuildMonth, DisplacementTonnage, DraughtMetres, SpeedKnots, LastUpdateTime");
21          IhsReader.fromZip(file).map(IhsReader::toShip).forEach(new Action1<Ship>() {
22  
23              @Override
24              public void call(Ship s) {
25                  out.printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", s.getImo(),
26                          st(s.getMmsi()), str(s.getDeadweightTonnage()), str(s.getGrossTonnage()),
27                          st(s.getType2()), st(s.getType3()), st(s.getType4()), st(s.getType5()),
28                          st(s.getStatCode5()), str(s.getLengthOverallMetres()),
29                          str(s.getBreadthMetres()), str(s.getYearOfBuild()),
30                          str(s.getMonthOfBuild()), str(s.getDisplacementTonnage()),
31                          str(s.getDraughtMetres()), str(s.getSpeedKnots()),
32                          dst(s.getLastUpdateTime()));
33              }
34          });
35          out.close();
36      }
37  
38      private static String dst(Optional<DateTime> d) {
39  
40          if (d.isPresent())
41              return d.get().toString();
42          else
43              return "";
44      }
45  
46      private static String str(Optional<? extends Number> value) {
47          if (value.isPresent())
48              return value.get().toString();
49          else
50              return "";
51      }
52  
53      private static String st(Optional<String> value) {
54          return value.or("");
55      }
56  }