Search in sources :

Example 1 with AisPosition

use of au.gov.amsa.ais.message.AisPosition in project risky by amsa-code.

the class ShipTypeBreakdownMain method main.

public static void main(String[] args) throws FileNotFoundException, IOException {
    // load a shapefile
    final GeometryFactory gf = new GeometryFactory();
    File file = new File("/home/dxm/temp/srr.shp");
    Map<String, Serializable> map = new HashMap<>();
    map.put("url", file.toURI().toURL());
    DataStore datastore = DataStoreFinder.getDataStore(map);
    String typeName = datastore.getTypeNames()[0];
    System.out.println(typeName);
    SimpleFeatureSource source = datastore.getFeatureSource(typeName);
    final SimpleFeatureCollection features = source.getFeatures();
    final List<PreparedGeometry> geometries = new ArrayList<>();
    SimpleFeatureIterator it = features.features();
    while (it.hasNext()) {
        SimpleFeature feature = it.next();
        Geometry g = (Geometry) feature.getDefaultGeometry();
        geometries.add(PreparedGeometryFactory.prepare(g));
    }
    // System.exit(0);
    String filename = "/media/analysis/nmea/2014/NMEA_ITU_20140815.gz";
    final Set<Long> mmsi = new HashSet<Long>();
    final Set<Long> mmsiA = new HashSet<Long>();
    final Set<Long> mmsiB = new HashSet<Long>();
    Streams.extract(Streams.nmeaFromGzip(filename)).flatMap(aisPositionsOnly).lift(Logging.<TimestampedAndLine<AisPosition>>logger().showCount().every(100000).log()).doOnNext(m -> {
        AisPosition p = m.getMessage().get().message();
        if (p.getLatitude() != null && p.getLongitude() != null && contains(gf, geometries, p.getLatitude(), p.getLongitude())) {
            long mmsiNo = m.getMessage().get().message().getMmsi();
            mmsi.add(mmsiNo);
            if (m.getMessage().get().message() instanceof AisPositionA)
                mmsiA.add(mmsiNo);
            else
                mmsiB.add(mmsiNo);
        }
    }).subscribe();
    final Map<ShipTypeClass, Set<Integer>> countsByShipType = new ConcurrentHashMap<>();
    Streams.extract(Streams.nmeaFromGzip(filename)).flatMap(aisShipStaticOnly).doOnNext(m -> {
        AisShipStatic s = m.getMessage().get().message();
        if (mmsi.contains(Long.valueOf(s.getMmsi()))) {
            boolean isClassA = s instanceof AisShipStaticA;
            ShipTypeClass shipTypeClass = new ShipTypeClass(isClassA, s.getShipType());
            if (countsByShipType.get(shipTypeClass) == null)
                countsByShipType.put(shipTypeClass, new HashSet<Integer>());
            else
                countsByShipType.get(shipTypeClass).add(s.getMmsi());
        }
    }).subscribe();
    System.out.println(countsByShipType);
    Set<String> set = new TreeSet<String>();
    int sizeA = 0;
    int sizeB = 0;
    for (Entry<ShipTypeClass, Set<Integer>> s : countsByShipType.entrySet()) {
        set.add(ShipTypeDecoder.getShipType(s.getKey().shipType) + "\t" + (s.getKey().isClassA ? "A" : "B") + "\t" + s.getValue().size());
        if (s.getKey().isClassA)
            sizeA += s.getValue().size();
        else
            sizeB += s.getValue().size();
    }
    set.stream().forEach(System.out::println);
    System.out.println("Unknown\tA\t" + (mmsiA.size() - sizeA));
    System.out.println("Unknown\tB\t" + (mmsiB.size() - sizeB));
    log.info("finished");
}
Also used : ShipTypeDecoder(au.gov.amsa.ais.ShipTypeDecoder) Logging(com.github.davidmoten.rx.slf4j.Logging) AisPosition(au.gov.amsa.ais.message.AisPosition) AisShipStatic(au.gov.amsa.ais.message.AisShipStatic) LoggerFactory(org.slf4j.LoggerFactory) Coordinate(org.locationtech.jts.geom.Coordinate) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) Observable(rx.Observable) HashSet(java.util.HashSet) Func1(rx.functions.Func1) AisPositionA(au.gov.amsa.ais.message.AisPositionA) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Streams(au.gov.amsa.ais.rx.Streams) Map(java.util.Map) DataStoreFinder(org.geotools.data.DataStoreFinder) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) TimestampedAndLine(au.gov.amsa.ais.rx.Streams.TimestampedAndLine) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Logger(org.slf4j.Logger) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) IOException(java.io.IOException) AisMessage(au.gov.amsa.ais.AisMessage) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) DataStore(org.geotools.data.DataStore) Serializable(java.io.Serializable) List(java.util.List) Entry(java.util.Map.Entry) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) Timestamped(au.gov.amsa.ais.Timestamped) PreparedGeometryFactory(org.locationtech.jts.geom.prep.PreparedGeometryFactory) Serializable(java.io.Serializable) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) PreparedGeometryFactory(org.locationtech.jts.geom.prep.PreparedGeometryFactory) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) AisPositionA(au.gov.amsa.ais.message.AisPositionA) TreeSet(java.util.TreeSet) DataStore(org.geotools.data.DataStore) AisShipStatic(au.gov.amsa.ais.message.AisShipStatic) TimestampedAndLine(au.gov.amsa.ais.rx.Streams.TimestampedAndLine) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) HashSet(java.util.HashSet) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) AisPosition(au.gov.amsa.ais.message.AisPosition) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Geometry(org.locationtech.jts.geom.Geometry) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) File(java.io.File)

Example 2 with AisPosition

use of au.gov.amsa.ais.message.AisPosition in project risky by amsa-code.

the class SatelliteReportingIntervalsMain method main.

/**
 * Used this oracle query to get time diffs by mmsi for satellite reports
 * from cts.position.
 *
 * <pre>
 * select mmsi, timeMs from
 * (
 * select mmsi, EXTRACT( DAY    FROM diff ) * 24*60*60000
 *      + EXTRACT( HOUR   FROM diff ) *  60*60000
 *      + EXTRACT( MINUTE FROM diff ) *  60000
 *      + EXTRACT( SECOND FROM diff ) * 1000 timeMs from
 * (
 * select mmsi, position_time - to_timestamp('1970-01-01', 'yyyy-mm-dd') diff from cts.position
 * where position_time >= to_date('2015/06/28','yyyy/mm/dd') and position_time < to_date('2015/06/30','yyyy/mm/dd')
 * and (source_detail like 'AISSat%' or source_detail like 'NORAIS%' or source_detail like 'rEV%')
 * )
 * ) order by mmsi, timeMs
 * </pre>
 *
 * @param args
 */
public static void main(String[] args) {
    Observable<Double> splits = Strings.from(new File("/home/dxm/times.txt")).compose(o -> Strings.split(o, "\n")).filter(line -> line.trim().length() > 0).map(line -> line.split("\t")).map(items -> new Record(Long.parseLong(items[0]), Long.parseLong(items[1]) / ((double) TimeUnit.HOURS.toMillis(1)))).groupBy(record -> record.mmsi).flatMap(g -> g.buffer(2, 1).filter(list -> list.size() == 2).map(list -> list.get(1).timeHrs - list.get(0).timeHrs)).toSortedList().flatMap(list -> Observable.from(list)).cast(Double.class).cache();
    splits.reduce(Pair.create(0, 0.0), (p1, x) -> Pair.<Integer, Double>create(p1.a() + 1, x + p1.b())).map(pair -> pair.b() / pair.a()).doOnNext(value -> System.out.println("average interval hours=" + value)).subscribe();
    Observable<BucketCount> buckets = splits.map(diff -> Math.floor(diff * 10) / 10.0).collect(() -> new HashMap<Double, Integer>(), (map, x) -> {
        if (map.get(x) == null)
            map.put(x, 1);
        else
            map.put(x, map.get(x) + 1);
    }).map(map -> new TreeMap<Double, Integer>(map)).flatMap(map -> Observable.from(map.entrySet())).map(entry -> new BucketCount(entry.getKey(), entry.getValue())).scan(new BucketCount(0.0, 0), (b1, b2) -> new BucketCount(b2.bucket, b1.count + b2.count)).cache();
    double max = buckets.last().map(b -> b.count).toBlocking().single();
    buckets.doOnNext(b -> System.out.println(b.bucket + "\t" + b.count / max * 100)).count().toBlocking().single();
    System.exit(0);
    File file = new File("/home/dxm/2015-06-29.txt.gz");
    Streams.nmeaFromGzip(file).compose(o -> Streams.extractMessages(o)).filter(m -> m.message() instanceof AisPosition).filter(m -> isNorwaySatellite(m.message().getSource())).groupBy(m -> ((AisPosition) m.message()).getMmsi()).flatMap(g -> g.toSortedList()).doOnNext(System.out::println).count().toBlocking().single();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Pair(au.gov.amsa.util.Pair) TreeMap(java.util.TreeMap) Streams(au.gov.amsa.ais.rx.Streams) AisPosition(au.gov.amsa.ais.message.AisPosition) HashMap(java.util.HashMap) File(java.io.File) Strings(au.gov.amsa.streams.Strings) Observable(rx.Observable) HashMap(java.util.HashMap) AisPosition(au.gov.amsa.ais.message.AisPosition) File(java.io.File)

Example 3 with AisPosition

use of au.gov.amsa.ais.message.AisPosition in project risky by amsa-code.

the class NmeaGzToCsvMain method main.

public static void main(String[] args) throws IOException {
    long t = System.currentTimeMillis();
    File input = new File(System.getProperty("user.home") + "/Downloads/2018-11-27.txt.gz");
    long[] count = new long[1];
    Observable<String> nmea = Streams.nmeaFromGzip(input);
    long n = 100000;
    try (PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("target/2018-11-27-positions.csv")))) {
        // 
        Streams.extract(nmea).doOnNext(x -> {
            count[0]++;
            if (count[0] % n == 0) {
                System.out.println(count[0] / 1000000.0 + "m records read");
            }
        }).filter(x -> {
            if (x.getMessage().isPresent()) {
                if (x.getMessage().get().message() instanceof AisPosition) {
                    AisPosition p = (AisPosition) x.getMessage().get().message();
                    if (p.getLatitude() != null && p.getLongitude() != null) {
                        return true;
                    }
                }
            }
            return false;
        }).map(// 
        x -> csv(x.getMessage().get())).filter(// 
        x -> !x.isEmpty()).doOnNext(// 
        x -> out.println(x)).subscribe();
    }
    System.out.println("finished in " + (System.currentTimeMillis() - t) / 1000.0 + "s");
}
Also used : PrintStream(java.io.PrintStream) AisPositionA(au.gov.amsa.ais.message.AisPositionA) AisPosition(au.gov.amsa.ais.message.AisPosition) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Timestamped(au.gov.amsa.ais.Timestamped) AisPositionBExtended(au.gov.amsa.ais.message.AisPositionBExtended) AisMessage(au.gov.amsa.ais.AisMessage) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Observable(rx.Observable) PrintStream(java.io.PrintStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) AisPosition(au.gov.amsa.ais.message.AisPosition)

Aggregations

AisPosition (au.gov.amsa.ais.message.AisPosition)3 File (java.io.File)3 Observable (rx.Observable)3 AisMessage (au.gov.amsa.ais.AisMessage)2 Timestamped (au.gov.amsa.ais.Timestamped)2 AisPositionA (au.gov.amsa.ais.message.AisPositionA)2 Streams (au.gov.amsa.ais.rx.Streams)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ShipTypeDecoder (au.gov.amsa.ais.ShipTypeDecoder)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 TimestampedAndLine (au.gov.amsa.ais.rx.Streams.TimestampedAndLine)1 Strings (au.gov.amsa.streams.Strings)1 Pair (au.gov.amsa.util.Pair)1 Logging (com.github.davidmoten.rx.slf4j.Logging)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1