use of au.gov.amsa.ais.message.AisShipStaticA 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");
}
use of au.gov.amsa.ais.message.AisShipStaticA in project risky by amsa-code.
the class ShipStaticDataCreator method writeStaticDataToFileWithTimestamps.
public static Observable<Timestamped<AisShipStatic>> writeStaticDataToFileWithTimestamps(List<File> files, File outputFile, Scheduler scheduler) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Func0<PrintStream> resourceFactory = Checked.f0(() -> new PrintStream(outputFile));
Func1<PrintStream, Observable<Timestamped<AisShipStatic>>> observableFactory = out -> Observable.from(files).buffer(Math.max(1, files.size() / Runtime.getRuntime().availableProcessors() - 1)).flatMap(list -> //
Observable.from(list).lift(//
Logging.<File>logger().showValue().showMemory().log()).concatMap(//
file -> timestampedShipStatics(file))).groupBy(//
m -> m.message().getMmsi()).flatMap(//
g -> collect(g).subscribeOn(scheduler)).compose(Transformers.doOnFirst(x -> {
out.println("# MMSI, Time, IMO, AisClass, AisShipType, MaxPresentStaticDraughtMetres, DimAMetres, DimBMetres, DimCMetres, DimDMetres, LengthMetres, WidthMetres, Name");
out.println("# columns are tab delimited");
out.println("# -1 = not present");
})).filter(//
set -> set.size() <= 10).flatMapIterable(//
set -> set).doOnNext(k -> {
AisShipStatic m = k.message();
out.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", m.getMmsi(), sdf.format(new Date(k.time())), getImo(m).orElse(-1), m instanceof AisShipStaticA ? "A" : "B", m.getShipType(), getMaximumPresentStaticDraughtMetres(m).orElse(-1F), m.getDimensionA().orElse(-1), m.getDimensionB().orElse(-1), m.getDimensionC().orElse(-1), m.getDimensionD().orElse(-1), AisShipStaticUtil.lengthMetres(m).orElse(-1), AisShipStaticUtil.widthMetres(m).orElse(-1), prepareName(m.getName()));
out.flush();
});
Action1<PrintStream> disposeAction = out -> out.close();
return Observable.using(resourceFactory, observableFactory, disposeAction);
}
use of au.gov.amsa.ais.message.AisShipStaticA in project risky by amsa-code.
the class BenchmarksAis method parseShipStaticNmeaMessageAndExtractBitsOfInterest.
@Benchmark
public void parseShipStaticNmeaMessageAndExtractBitsOfInterest() {
AisNmeaMessage n = new AisNmeaMessage(shipStaticA);
AisShipStaticA m = (AisShipStaticA) n.getMessage();
m.getName();
m.getShipType();
m.getImo();
m.getLengthMetres();
m.getWidthMetres();
m.getCallsign();
m.getMmsi();
}
use of au.gov.amsa.ais.message.AisShipStaticA in project risky by amsa-code.
the class OperatorCraftProperty method call.
@Override
public Subscriber<? super Timestamped<? extends AisMessage>> call(final Subscriber<? super CraftProperty> child) {
return new Subscriber<Timestamped<? extends AisMessage>>(child) {
@Override
public void onCompleted() {
if (!isUnsubscribed())
child.onCompleted();
}
@Override
public void onError(Throwable e) {
if (!isUnsubscribed())
child.onError(e);
}
@SuppressWarnings("unchecked")
@Override
public void onNext(Timestamped<? extends AisMessage> m) {
if (m.message() instanceof AisShipStaticA) {
handleShipStatic((Timestamped<AisShipStaticA>) m, child);
} else if (m.message() instanceof AisPositionBExtended)
handleAisPositionBExtended((Timestamped<AisPositionBExtended>) m, child);
}
private void handleShipStatic(Timestamped<AisShipStaticA> m, Subscriber<? super CraftProperty> child) {
handleProperty(child, m, CraftPropertyName.CALLSIGN, m.message().getCallsign());
handleProperty(child, m, CraftPropertyName.DESTINATION, m.message().getDestination());
handleProperty(child, m, CraftPropertyName.DIMENSION_A, m.message().getDimensionA());
handleProperty(child, m, CraftPropertyName.DIMENSION_B, m.message().getDimensionB());
handleProperty(child, m, CraftPropertyName.DIMENSION_C, m.message().getDimensionC());
handleProperty(child, m, CraftPropertyName.DIMENSION_D, m.message().getDimensionD());
handleProperty(child, m, CraftPropertyName.IMO_NUMBER, m.message().getImo());
handleProperty(child, m, CraftPropertyName.LENGTH_METRES, m.message().getLengthMetres());
handleProperty(child, m, CraftPropertyName.DRAUGHT_METRES, m.message().getMaximumPresentStaticDraughtMetres());
handleProperty(child, m, CraftPropertyName.NAME, m.message().getName());
handleProperty(child, m, CraftPropertyName.SHIP_TYPE, m.message().getShipType());
handleProperty(child, m, CraftPropertyName.WIDTH_METRES, m.message().getWidthMetres());
}
private void handleAisPositionBExtended(Timestamped<AisPositionBExtended> m, Subscriber<? super CraftProperty> child) {
handleProperty(child, m, CraftPropertyName.DIMENSION_A, m.message().getDimensionA());
handleProperty(child, m, CraftPropertyName.DIMENSION_B, m.message().getDimensionB());
handleProperty(child, m, CraftPropertyName.DIMENSION_C, m.message().getDimensionC());
handleProperty(child, m, CraftPropertyName.DIMENSION_D, m.message().getDimensionD());
handleProperty(child, m, CraftPropertyName.LENGTH_METRES, m.message().getLengthMetres());
handleProperty(child, m, CraftPropertyName.NAME, m.message().getName());
handleProperty(child, m, CraftPropertyName.SHIP_TYPE, m.message().getShipType());
handleProperty(child, m, CraftPropertyName.WIDTH_METRES, m.message().getWidthMetres());
}
private <R extends AisMessage & HasMmsi> void handleProperty(Subscriber<? super CraftProperty> child, Timestamped<R> m, CraftPropertyName name, Object value) {
if (!isUnsubscribed() && value != null)
child.onNext(new CraftProperty(new Mmsi(m.message().getMmsi()), name, value.toString(), m.time()));
}
};
}
use of au.gov.amsa.ais.message.AisShipStaticA 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);
}
Aggregations