use of au.gov.amsa.ais.message.AisBStaticDataReportPartA in project risky by amsa-code.
the class ExtractStaticDataAsCsvFromNmeaGz method extractStaticData.
public static void extractStaticData(File input, File output) throws IOException {
output.getParentFile().mkdirs();
try (PrintStream out = new PrintStream(output)) {
//
Streams.nmeaFromGzip(input).compose(//
o -> Streams.extractMessages(o)).filter(x -> //
(x.message() instanceof AisShipStatic) || //
(x.message() instanceof AisBStaticDataReportPartA) || //
(x.message() instanceof AisBStaticDataReportPartB)).groupBy(//
x -> ((HasMmsi) x.message()).getMmsi()).flatMap(o -> //
o.scan(//
new Timed<State>(new State(o.getKey(), null, null, null, null, null), 0L), (timed, x) -> {
State state = timed.object;
String name = state.name;
String callsign = state.callsign;
Integer imo = state.imo;
Integer shipType = state.shipType;
AisClass aisClass = state.aisClass;
if (x.message() instanceof AisShipStaticA) {
AisShipStaticA a = (AisShipStaticA) x.message();
callsign = a.getCallsign();
name = a.getName();
imo = a.getImo().orElse(null);
shipType = a.getShipType();
aisClass = AisClass.A;
} else if (x.message() instanceof AisPositionBExtended) {
AisPositionBExtended a = (AisPositionBExtended) x.message();
name = a.getName();
shipType = a.getShipType();
aisClass = AisClass.B;
} else if (x.message() instanceof AisBStaticDataReportPartA) {
AisBStaticDataReportPartA a = (AisBStaticDataReportPartA) x.message();
if (a.getName().isPresent()) {
name = a.getName().get();
}
aisClass = AisClass.B;
} else if (x.message() instanceof AisBStaticDataReportPartB) {
aisClass = AisClass.B;
}
return new Timed<State>(new State(o.getKey(), aisClass, name, callsign, imo, shipType), x.time());
}).skip(//
1).distinctUntilChanged(ts -> ts.object)).doOnNext(x -> {
State y = x.object;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String line = //
y.mmsi + "," + sdf.format(new Date(x.time)) + //
"," + y.aisClass + //
"," + integer(y.imo) + //
"," + integer(y.shipType) + //
"," + string(y.name) + ",";
out.println(line);
}).subscribe();
}
}
Aggregations