Search in sources :

Example 1 with AisPositionA

use of au.gov.amsa.ais.message.AisPositionA 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 AisPositionA

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

the class AisMessageParser method parse.

/**
 * Returns an {@link AisMessage} from the string representation of the
 * message as per 1371-4 IMO specification (as per the appropriate column in
 * the NMEA message).
 *
 * @param message
 * @param source
 * @return
 */
public AisMessage parse(String message, String source, int padBits) {
    AisExtractor extractor = factory.create(message, 0, padBits);
    int id = extractor.getMessageId();
    if (Util.isClassAPositionReport(id)) {
        return new AisPositionA(message, source, padBits);
    } else if (id == 4)
        return new AisBaseStation(message, source, padBits);
    else if (id == 5)
        return new AisShipStaticA(message, source, padBits);
    else if (id == 18)
        return new AisPositionB(message, source, padBits);
    else if (id == 19)
        return new AisPositionBExtended(message, source, padBits);
    else if (id == 21)
        return new AisAidToNavigation(message, source, padBits);
    else if (id == AisMessageType.STATIC_DATA_REPORT.getId()) {
        int partNumber = AbstractAisBStaticDataReport.extractPartNumber(factory, message, padBits);
        return parseStaticDataReport(partNumber, id, message, source, padBits);
    } else if (id == 27)
        return new AisPositionGPS(message, source, padBits);
    else
        return new AisMessageOther(id, source, padBits);
}
Also used : AisPositionA(au.gov.amsa.ais.message.AisPositionA) AisAidToNavigation(au.gov.amsa.ais.message.AisAidToNavigation) AisPositionGPS(au.gov.amsa.ais.message.AisPositionGPS) AisBaseStation(au.gov.amsa.ais.message.AisBaseStation) AisPositionB(au.gov.amsa.ais.message.AisPositionB) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) AisPositionBExtended(au.gov.amsa.ais.message.AisPositionBExtended) AisMessageOther(au.gov.amsa.ais.message.AisMessageOther)

Aggregations

AisPositionA (au.gov.amsa.ais.message.AisPositionA)2 AisShipStaticA (au.gov.amsa.ais.message.AisShipStaticA)2 AisMessage (au.gov.amsa.ais.AisMessage)1 ShipTypeDecoder (au.gov.amsa.ais.ShipTypeDecoder)1 Timestamped (au.gov.amsa.ais.Timestamped)1 AisAidToNavigation (au.gov.amsa.ais.message.AisAidToNavigation)1 AisBaseStation (au.gov.amsa.ais.message.AisBaseStation)1 AisMessageOther (au.gov.amsa.ais.message.AisMessageOther)1 AisPosition (au.gov.amsa.ais.message.AisPosition)1 AisPositionB (au.gov.amsa.ais.message.AisPositionB)1 AisPositionBExtended (au.gov.amsa.ais.message.AisPositionBExtended)1 AisPositionGPS (au.gov.amsa.ais.message.AisPositionGPS)1 AisShipStatic (au.gov.amsa.ais.message.AisShipStatic)1 Streams (au.gov.amsa.ais.rx.Streams)1 TimestampedAndLine (au.gov.amsa.ais.rx.Streams.TimestampedAndLine)1 Logging (com.github.davidmoten.rx.slf4j.Logging)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1