Search in sources :

Example 1 with AisClass

use of au.gov.amsa.risky.format.AisClass in project risky by amsa-code.

the class ShipStaticData method from.

public static Observable<Info> from(Reader reader) {
    return Strings.lines(reader).filter(line -> !line.startsWith("#")).map(line -> line.trim()).filter(line -> line.length() > 0).map(line -> line.split("\t")).map(items -> {
        int i = 0;
        int mmsi = Integer.parseInt(items[i++]);
        String imoTemp = items[i++];
        Optional<String> imo;
        if (imoTemp.trim().length() == 0 || Integer.parseInt(imoTemp.trim()) == -1)
            imo = Optional.empty();
        else
            imo = Optional.of(imoTemp);
        AisClass cls = items[i++].equals("A") ? AisClass.A : AisClass.B;
        int type = Integer.parseInt(items[i++]);
        Optional<Integer> shipType = type == -1 ? empty() : of(type);
        float draft = Float.parseFloat(items[i++]);
        Optional<Float> maxDraftMetres = draft == -1 ? empty() : of(draft);
        int a = Integer.parseInt(items[i++]);
        int b = Integer.parseInt(items[i++]);
        int c = Integer.parseInt(items[i++]);
        int d = Integer.parseInt(items[i++]);
        Optional<Integer> dimensionAMetres = a == -1 ? empty() : of(a);
        Optional<Integer> dimensionBMetres = b == -1 ? empty() : of(b);
        Optional<Integer> dimensionCMetres = c == -1 ? empty() : of(c);
        Optional<Integer> dimensionDMetres = d == -1 ? empty() : of(d);
        // skip length and width
        i += 2;
        Optional<String> name;
        if (i >= items.length || items[i].trim().length() == 0)
            name = Optional.empty();
        else
            name = Optional.of(items[i].trim());
        return new Info(mmsi, imo, cls, shipType, maxDraftMetres, dimensionAMetres, dimensionBMetres, dimensionCMetres, dimensionDMetres, name);
    });
}
Also used : Optional.empty(java.util.Optional.empty) AisShipStaticUtil(au.gov.amsa.ais.message.AisShipStaticUtil) AisClass(au.gov.amsa.risky.format.AisClass) Optional.of(java.util.Optional.of) Map(java.util.Map) Optional(java.util.Optional) Reader(java.io.Reader) Checked(com.github.davidmoten.rx.Checked) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) Strings(au.gov.amsa.streams.Strings) Observable(rx.Observable) AisClass(au.gov.amsa.risky.format.AisClass)

Example 2 with AisClass

use of au.gov.amsa.risky.format.AisClass in project risky by amsa-code.

the class DriftCandidates method toDriftCandidate.

private static DriftCandidate toDriftCandidate(String line) {
    String[] items = line.split(",");
    int i = 0;
    int mmsi = Integer.parseInt(items[i++]);
    float lat = Float.parseFloat(items[i++]);
    float lon = Float.parseFloat(items[i++]);
    long time = Long.parseLong(items[i++]);
    String cls = items[i++];
    float course = Float.parseFloat(items[i++]);
    float heading = Float.parseFloat(items[i++]);
    float speedKnots = Float.parseFloat(items[i++]);
    String status = items[i++];
    long driftingSince = Long.parseLong(items[i++]);
    final Optional<NavigationalStatus> navigationalStatus;
    if (status.trim().length() == 0)
        navigationalStatus = Optional.empty();
    else
        navigationalStatus = Optional.of(NavigationalStatus.valueOf(status));
    final AisClass aisClass;
    if (cls.trim().length() == 0)
        throw new RuntimeException("cls should not be empty");
    else if (AisClass.A.name().equals(cls))
        aisClass = AisClass.A;
    else
        aisClass = AisClass.B;
    FixImpl fix = new FixImpl(mmsi, lat, lon, time, Optional.<Integer>empty(), Optional.<Short>empty(), navigationalStatus, Optional.of(speedKnots), Optional.of(course), Optional.of(heading), aisClass);
    return new DriftCandidate(fix, driftingSince);
}
Also used : AisClass(au.gov.amsa.risky.format.AisClass) FixImpl(au.gov.amsa.risky.format.FixImpl) NavigationalStatus(au.gov.amsa.risky.format.NavigationalStatus)

Example 3 with AisClass

use of au.gov.amsa.risky.format.AisClass 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();
    }
}
Also used : PrintStream(java.io.PrintStream) AisBStaticDataReportPartA(au.gov.amsa.ais.message.AisBStaticDataReportPartA) Date(java.util.Date) TimeZone(java.util.TimeZone) AisClass(au.gov.amsa.risky.format.AisClass) AisShipStatic(au.gov.amsa.ais.message.AisShipStatic) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) AisPositionBExtended(au.gov.amsa.ais.message.AisPositionBExtended) File(java.io.File) AisBStaticDataReportPartB(au.gov.amsa.ais.message.AisBStaticDataReportPartB) HasMmsi(au.gov.amsa.ais.HasMmsi) Streams(au.gov.amsa.ais.rx.Streams) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) PrintStream(java.io.PrintStream) AisPositionBExtended(au.gov.amsa.ais.message.AisPositionBExtended) Date(java.util.Date) HasMmsi(au.gov.amsa.ais.HasMmsi) AisShipStatic(au.gov.amsa.ais.message.AisShipStatic) AisClass(au.gov.amsa.risky.format.AisClass) AisBStaticDataReportPartA(au.gov.amsa.ais.message.AisBStaticDataReportPartA) AisBStaticDataReportPartB(au.gov.amsa.ais.message.AisBStaticDataReportPartB) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

AisClass (au.gov.amsa.risky.format.AisClass)3 HasMmsi (au.gov.amsa.ais.HasMmsi)1 AisBStaticDataReportPartA (au.gov.amsa.ais.message.AisBStaticDataReportPartA)1 AisBStaticDataReportPartB (au.gov.amsa.ais.message.AisBStaticDataReportPartB)1 AisPositionBExtended (au.gov.amsa.ais.message.AisPositionBExtended)1 AisShipStatic (au.gov.amsa.ais.message.AisShipStatic)1 AisShipStaticA (au.gov.amsa.ais.message.AisShipStaticA)1 AisShipStaticUtil (au.gov.amsa.ais.message.AisShipStaticUtil)1 Streams (au.gov.amsa.ais.rx.Streams)1 FixImpl (au.gov.amsa.risky.format.FixImpl)1 NavigationalStatus (au.gov.amsa.risky.format.NavigationalStatus)1 Strings (au.gov.amsa.streams.Strings)1 Checked (com.github.davidmoten.rx.Checked)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintStream (java.io.PrintStream)1 Reader (java.io.Reader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 SimpleDateFormat (java.text.SimpleDateFormat)1