View Javadoc
1   package au.gov.amsa.geo.distance;
2   
3   import static au.gov.amsa.geo.distance.DistanceTravelledCalculator.calculateTrafficDensity;
4   import static au.gov.amsa.geo.distance.Renderer.saveAsPng;
5   import static com.google.common.base.Optional.of;
6   
7   import java.io.File;
8   import java.util.List;
9   import java.util.concurrent.atomic.AtomicInteger;
10  
11  import org.apache.log4j.Logger;
12  import org.joda.time.DateTime;
13  
14  import rx.Observable;
15  import rx.Observer;
16  import rx.functions.Action1;
17  import rx.functions.Func1;
18  import au.gov.amsa.geo.Util;
19  import au.gov.amsa.geo.distance.DistanceTravelledCalculator.CalculationResult;
20  import au.gov.amsa.geo.model.Options;
21  
22  public class DistanceTravelledMovieMaker {
23  
24  	private static Logger log = Logger
25  			.getLogger(DistanceTravelledMovieMaker.class);
26  
27  	/**
28  	 * Saves a sequence of image files of Vessel Traffic Density plots to the
29  	 * <code>imageDirectory</code> with filenames map1.png, map2.png, etc.
30  	 * 
31  	 * @param options
32  	 * @param files
33  	 * @param times
34  	 * @param imageDirectory
35  	 */
36  	private static void saveImagesWithTimeRange(final Options options,
37  			final Observable<File> files, Observable<Long> times,
38  			final String imageDirectory) {
39  
40  		times.buffer(2, 1).doOnNext(new Action1<List<Long>>() {
41  			AtomicInteger i = new AtomicInteger();
42  
43  			@Override
44  			public void call(List<Long> pair) {
45  				if (pair.size() < 2)
46  					return;
47  				Long startTime = pair.get(0);
48  				Long finishTime = pair.get(1);
49  				saveImageWithTimeRange(options, files, startTime, finishTime,
50  						imageDirectory + "/map" + i.incrementAndGet() + ".png");
51  			}
52  		}).subscribe(reportErrors());
53  
54  	}
55  
56  	private static Observer<Object> reportErrors() {
57  		return new Observer<Object>() {
58  
59  			@Override
60  			public void onCompleted() {
61  				log.info("completed");
62  			}
63  
64  			@Override
65  			public void onError(Throwable e) {
66  				log.error(e.getMessage(), e);
67  				throw new RuntimeException(e);
68  			}
69  
70  			@Override
71  			public void onNext(Object t) {
72  				// do nothing
73  			}
74  
75  		};
76  	}
77  
78  	private static void saveImageWithTimeRange(Options options,
79  			final Observable<File> files, long startTime, long finishTime,
80  			String filename) {
81  		Options op = options.buildFrom()
82  		// set start time
83  				.startTime(of(startTime))
84  				// set finish time
85  				.finishTime(of(finishTime))
86  				// build
87  				.build();
88  		CalculationResult result = calculateTrafficDensity(op, files);
89  		saveAsPng(Renderer.createImage(op, 2, 1600, result), new File(filename));
90  	}
91  
92  	private static Options createOptions(double cellSizeDegrees) {
93  		return Options.builder()
94  		// set origin latitude
95  				.originLat(0)
96  				// set origin longitudue
97  				.originLon(0)
98  				// square cell size in degrees
99  				.cellSizeDegrees(cellSizeDegrees)
100 				// set bounds
101 				// sabine bounds:
102 				// .bounds(new Bounds(-10, 110, -45, 158))
103 				// .bounds(new Bounds(15, 90, -20, 125))
104 				// build options
105 				.build();
106 	}
107 
108 	public static void main(String[] args) {
109 
110 		double cellSizeDegrees = 0.2;
111 		Options options = createOptions(cellSizeDegrees);
112 
113 		String directory = System.getProperty("user.home")
114 				+ "/Downloads/positions-365-days";
115 
116 		final Observable<File> files = Util.getFiles(directory, "craft-");
117 
118 		Observable<Long> times = Observable.range(1, 13).map(
119 				new Func1<Integer, Long>() {
120 
121 					@Override
122 					public Long call(Integer n) {
123 						return DateTime.parse("2013-06-01").plusMonths(n - 1)
124 								.getMillis();
125 					}
126 				});
127 		saveImagesWithTimeRange(options, files, times, "target");
128 
129 	}
130 
131 }