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");
}
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();
}
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");
}
Aggregations