summaryrefslogtreecommitdiffstats
path: root/DataStructure/GoogleOut.java
blob: 0d96e33c18dce37d6c4ed874cf5fe89a3b9b1cb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package DataStructure;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class GoogleOut {
	private BufferedWriter br;
	private LinkedList<MeasurementReport> MRlog;
	// private LinkedList<GPScoordinate> GPSlog;
	private double accuracy = 0.00004;
	private GSMMap map;
	private double[] Xcoords;
	private double[] Ycoords;
	private String fileName;

	public GoogleOut(GSMMap map, String fileName) {
		this.map = map;
		this.fileName = fileName;
	}

	// Constructor
	public void write() throws IOException {
		// this.map = map;
		Xcoords = map.Xcoords;
		Ycoords = map.Ycoords;
		br = new BufferedWriter(new FileWriter(new File(fileName)));
		writeHeader();

		// Downlink
		// write Folder and Polygons for each BTS in the database
		SingleBTS[] btsNames = map.btsnames; // change here to plot e-plus bts
		for (SingleBTS singleBTSDL : btsNames) {
			if (map.contains(singleBTSDL)) {
				// if the GSMmap contains at least one of this BTS, do it
				writeFolderDesc(singleBTSDL, false);
				writePlacemarks(singleBTSDL, false);
				br.write("</Folder> \n");
			}

		}

		// Uplink (RZ-GSM)
		for (SingleBTS singleBTSDL : btsNames) {
			if (map.contains(singleBTSDL)) {
				// if the GSMmap contains at least one of this BTS, do it
				writeFolderDesc(singleBTSDL, true);
				writePlacemarks(singleBTSDL, true);
				br.write("</Folder> \n");
			}

		}

		// Downlink (foreign BTSs). Actually one could just change BTSnames =
		// map.content() in the beginning
		SingleBTS[] foreign = map.contentForeignBTS();
		if (foreign != null) {
			for (SingleBTS foreignBTS : foreign) {
				writeFolderDesc(foreignBTS, false);
				writePlacemarks(foreignBTS, false);
				br.write("</Folder> \n");
			}
		}

		br.write("</Document>");
		br.write("</kml>");
		br.flush();
		br.close();
	}

	/**
	 * Writes Header for the folder: Foldername, Up/Downlink, LookAt,...
	 * 
	 * @param btsname
	 * @param uplink
	 * @throws IOException
	 */
	private void writeFolderDesc(SingleBTS btsname, boolean uplink)
			throws IOException {
		SingleBTS foundBTS = map.getFirstBTSmatch(btsname);
		String upDown;
		if (uplink)
			upDown = "Uplink: ";
		else
			upDown = "Downlink: ";

		// br.write("<open>1</open>\n");
		br.write("  <Folder>\n");
		br.write("<name>MR " + upDown + "" + btsname.ARFCN + ": "
				+ btsname.name + "</name>\n");
		br.write("        <visibility>1</visibility>\n");
		br.write("<description>MR from " + foundBTS.time.toString()
				+ "</description>\n");
		br.write("        <LookAt>\n");
		br.write("<longitude>" + (map.maxX + map.minX) / 2 + "</longitude>\n");
		br.write("<latitude>" + (map.maxY + map.minY) / 2 + "</latitude>\n");
		br.write("<altitude>0</altitude>\n");
		br.write("<heading>0.0</heading>\n");
		br.write("<tilt>0</tilt>\n");
		br.write("<range>276.0</range>\n");
		br.write("</LookAt>\n");

	}

	private void writePlacemarks(SingleBTS btsName, boolean uplink)
			throws IOException {
		// was a BTS written to file?
		// Polygon Description for every Measured BTS
		for (double gpsX : Xcoords) {
			for (double gpsY : Ycoords) {
				// because within GSMmap, every cell of the array is
				// initialized, no null pointer exception will occur. only a
				// empty list will be "thrown" but it doesn't hurt
				ArrayList<SingleBTS> currentBTSList = map
						.getBTSList(gpsX, gpsY);

				// traverse the ListBTS to find out if a BTS with the current
				// name
				// exists on this coordinate. Tell if a BTS was written to file

				writePlacemarkDetails(currentBTSList, btsName, gpsX, gpsY,
						uplink);

			}

		}

		// Polygon Coordinates
	}

	private void writePlacemarkDetails(List<SingleBTS> btsList,
			SingleBTS btsName, double coordX, double coordY, boolean uplink)
			throws IOException {
		// if the current field was empty, so is the btsList. But still it is
		// initilized, so there is no nullpointer exception!
		String name;
		for (SingleBTS singleBTS : btsList) {
			// maybe its null, then continue with next
			if (singleBTS == null)
				continue;

			// get correct output String for UL or DL
			if (uplink)
				name = singleBTS.ULtoString();
			else
				name = singleBTS.DLtoString();
			// you can only draw uplink if you've got a FullBTS!
			if ((singleBTS.ARFCN == btsName.ARFCN && (!uplink || singleBTS.fullBTS))) {
				if (uplink)
					name = singleBTS.ULtoString();
				else
					name = singleBTS.DLtoString();
				// draw it!
				br.write("<Placemark> \n");
				br.write("<visibility>1</visibility>\n");
				br.write("<name>" + name + "</name>\n");

				writeStyleIdentifier(singleBTS, uplink);

				br.write("<Polygon>\n");
				br.write("<tessellate>1</tessellate>\n");
				br.write("<outerBoundaryIs>\n");
				br.write("<LinearRing>\n");
				br.write("<coordinates>\n");

				writePolygonCoordinates(coordX, coordY, map.accuracy);

				br.write("</coordinates>\n");
				br.write("</LinearRing>\n");
				br.write("</outerBoundaryIs>\n");
				br.write("</Polygon>\n");
				br.write("</Placemark>\n");

				// if (singleBTS.name.equals("neuer Name")) {
				// System.out.print("hier! " + coordX + "/" + coordY);
				// System.out.println(" Xcoord results to array "
				// + map.searchBestX(coordX));
				// }

			}

		}
	}

	private void writeStyleIdentifier(SingleBTS bts, boolean uplink)
			throws IOException {
		int btsRXvalue;
		if (uplink)
			btsRXvalue = (int) bts.getUldB();
		else
			btsRXvalue = (int) bts.getDldB();

		if (bts.isInterpolated()) {
			// no FullBTS but interpolated: draw black line("#I")
			br.write("<styleUrl>#I" + btsRXvalue + "</styleUrl>\n");
		} else if (bts.fullBTS) {
			// show the nice FullBTS line around
			br.write("<styleUrl>#F" + btsRXvalue + "</styleUrl>\n");
		} else {
			// no line
			br.write("<styleUrl>#" + btsRXvalue + "</styleUrl>\n");
		}

	}

	private void writePolygonCoordinates(double coordX, double coordY,
			double accuracy) throws IOException {
		double accuracyX = accuracy / 2;
		double accuracyY = accuracy;
		// Point 1
		br.write(coordX - accuracyX + "," + coordY + accuracyY + ",0,\n");
		// Point 2
		br.write(coordX + accuracyX + "," + coordY + accuracyY + ",0,\n");
		// Point3
		br.write(coordX + accuracyX + "," + (coordY - accuracyY) + ",0,\n");
		// Point4
		br.write((coordX - accuracyX) + "," + (coordY - accuracyY) + ",0,\n");
		// End: Point 1
		br.write((coordX - accuracyX) + "," + coordY + accuracyY + ",0\n");

	}

	public GoogleOut(LinkedList<GPScoordinate> GPSlog,
			LinkedList<MeasurementReport> MRlog, String fileName)
			throws IOException {
		this.MRlog = MRlog;
		// this.GPSlog = GPSlog;

		br = new BufferedWriter(new FileWriter(new File(fileName)));
		// write header
		writeHeader();
		// write viewpoint
		br.write("<open>1</open>");
		br.write("  <Folder>");
		br.write("<name>Measurement Report " + map.IMSI + "</name>");
		br.write("        <visibility>1</visibility>");
		br.write("<description>MR from " + GPSlog.getFirst().time.toString()
				+ "</description>");
		br.write("        <LookAt>");
		br.write("<longitude>" + GPSlog.getFirst().coord2 + "</longitude>");
		br.write("<latitude>" + GPSlog.getFirst().coord1 + "</latitude>");
		br.write("<altitude>0</altitude>");
		br.write("<heading>-34.82469740081282</heading>");
		br.write("<tilt>0</tilt>");
		br.write("<range>276.7870053764046</range>");
		br.write("</LookAt>");

		// write content
		int i = 0;
		int discarded = 0;
		Iterator<GPScoordinate> GPSitr = GPSlog.iterator();
		while (GPSitr.hasNext()) {
			GPScoordinate GPS = GPSitr.next();
			MeasurementReport MR = MRlog.get(i);

			// do nothing if Time is not valid
			if (Math.abs(MR.time.getTime() - GPS.time.getTime()) < 3000) {

				br.write("<Placemark> \n");
				br.write("<visibility>1</visibility>");
				br.write("<name>" + MR.toString() + "</name>");
				// br.write("<PolyStyle>");
				// br.write("<color>bf00ff00</color>");
				// br.write("</PolyStyle>");

				// dl: Downlink - Network to phone
				// up: Uplink - Phone to Network (has better perception for some
				// reason)
				br.write("<styleUrl>#" + MRlog.get(i).getFirstBTSdl()
						+ "</styleUrl>");

				br.write("<Polygon>");
				br.write("<tessellate>1</tessellate>");
				br.write("<outerBoundaryIs>");
				br.write("<LinearRing>");
				br.write("<coordinates>");
				// Point 1
				System.out.println("this should not be started anymore");
				br.write(GPS.coord2 - accuracy + "," + GPS.coord1 + accuracy
						+ ",0,");
				// Point 2
				br.write(GPS.coord2 + accuracy + "," + GPS.coord1 + accuracy
						+ ",0,");
				// Point3
				br.write(GPS.coord2 + accuracy + "," + (GPS.coord1 - accuracy)
						+ ",0,");
				// Point4
				br.write((GPS.coord2 - accuracy) + ","
						+ (GPS.coord1 - accuracy) + ",0,");
				// End: Point 1
				br.write((GPS.coord2 - accuracy) + "," + GPS.coord1 + accuracy
						+ ",0");

				br.write("</coordinates>");
				br.write("</LinearRing>");
				br.write("</outerBoundaryIs>");
				br.write("</Polygon>");
				br.write("</Placemark>\n");
			} else {
				discarded++;

			}

			i++;
		}
		br.write("</Folder>");
		br.write("</Document>");
		br.write("</kml>");
		br.flush();
		br.close();
		System.out.println("Discarded Coordinates: " + discarded);

	}

	@SuppressWarnings("unused")
	private int getMin() {
		Iterator<MeasurementReport> itr = MRlog.iterator();
		MeasurementReport current;
		int min = 0;
		while (itr.hasNext()) {
			current = itr.next();
			if (current.getFirstBTSul() < min)
				min = current.getFirstBTSul();
		}
		return min;

	}

	@SuppressWarnings("unused")
	private int getMax() {
		Iterator<MeasurementReport> itr = MRlog.iterator();
		MeasurementReport current;
		int max = -999;
		while (itr.hasNext()) {
			current = itr.next();
			if (current.getFirstBTSul() > max)
				max = current.getFirstBTSul();
		}
		return max;
	}

	@SuppressWarnings("unused")
	private int getSteps() {
		Iterator<MeasurementReport> itr = MRlog.iterator();
		int i = 0;
		while (itr.hasNext()) {
			itr.next();
			i++;
		}
		return i;
	}

	@SuppressWarnings("unused")
	private String toHex(int i) {
		String out = Integer.toHexString(Math.abs(i));
		if (out.length() == 4)
			return (out + "000");
		if (out.length() == 5)
			return (out + "00");
		if (out.length() == 6)
			return (out + "0");
		if (out.length() == 7)
			return (out);
		return out;

	}

	// strength -30 to -120
	// yellow at -75
	private String getColor(int strength) {
		int green = 0;
		int red = 0;
		// yellow at -75
		if (strength > -75) {
			green = 255;
			red = 256 - Math.abs((256 / 45) * (-75 - strength));

		} else {
			// transient green from now on
			red = 255;
			green = Math.abs((256 / 45) * (-120 - strength));
		}
		// int green = 256 - Math.abs((256 / 90) * (-120 - strength));
		// int red = Math.abs((256 / 90) * (-120 - strength));
		// int result = red << 24 | green
		if ((green > 255) || (red > 255))
			System.out.println("Zu groß bei strength= " + strength + ",Rot: "
					+ red + "Grün: " + green);
		if (green > 255)
			green = 255;
		if (red > 255)
			red = 255;
		// return ("7f00" + Integer.toHexString(red) +
		// Integer.toHexString(green));
		// return ("ff00" + Integer.toHexString(red) +
		// Integer.toHexString(green));
		// return ("ff00" + Integer.toHexString(red) +
		// Integer.toHexString(green));
		return ("ff00" + Integer.toHexString(green) + Integer.toHexString(red));

	}

	private void writeHeader() throws IOException {
		br.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		br.write("<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n");
		br.write("  <Document>\n");
		br.write("<name>Measurement Report " + map.IMSI + "</name>\n");
		br.write("<open>1</open>\n");

		// style: color for signal strength from -30db to -120db
		// color red = #FF0000 (16711680)
		// color green = #00FF00 (65280)

		// styles for normal BTS
		for (int style = -30; style >= -120; style--) {

			// int step = (16711680 - 65280) / getSteps();
			// for (int i = getMin(); i <= getMax(); i++) {

			br.write("<Style id=\"" + style + "\"> \n");
			br.write("<PolyStyle> \n");
			br.write("<color>" + getColor(style) + "</color> \n");
			br.write("</PolyStyle> \n");
			br.write("<LineStyle>\n");
			// br.write("<color>" + getColor(style) + "</color>\n");
			br.write("<color>" + 00000000 + "</color>\n");
			br.write("</LineStyle>\n");
			br.write("</Style> \n");
		}

		// styles for Full BTS: full white ffffff, no transparency:ff
		for (int style = -30; style >= -120; style--) {
			br.write("<Style id=\"F" + style + "\"> \n");
			br.write("<PolyStyle> \n");
			br.write("<color>" + getColor(style) + "</color> \n");
			br.write("</PolyStyle> \n");
			br.write("<LineStyle>\n");
			// // br.write("<color>" + getColor(style) + "</color>\n");
			br.write("<color>" + "ffffffff" + "</color>\n");
			br.write("<width>2</width>\n");
			br.write("</LineStyle>\n");
			br.write("</Style> \n");
		}

		// styles for Interpolated BTS: full black 000000, no transparency:ff
		for (int style = -30; style >= -120; style--) {
			br.write("<Style id=\"I" + style + "\">\n");
			br.write("<PolyStyle>\n");
			br.write("<color>" + getColor(style) + "</color>\n");
			br.write("</PolyStyle>\n");
			br.write("<LineStyle>\n");
			// br.write("<color>" + getColor(style) + "</color>\n");
			br.write("<color>" + "ff000000" + "</color>\n");
			br.write("<width>2</width>\n");
			br.write("</LineStyle>\n");
			br.write("</Style>\n");
		}

	}

	/**
	 * Writes Probabilitymap to file.
	 * 
	 * @param scoremap
	 * @param file
	 * @throws IOException
	 */
	public void writeProbability(double[][] scoremap, String file)
			throws IOException {
		BufferedWriter br = new BufferedWriter(new FileWriter(new File(file)));
		writeProbHeader(br);
		for (int x = 0; x < map.Xcoords.length; x++) {
			for (int y = 0; y < map.Ycoords.length; y++) {
				if (scoremap[x][y] > 0) {
					writeProbabilityLandmark(x, y, scoremap, br);
				}
			}
		}
		br.write("</Folder> \n");
		br.write("</Document>");
		br.write("</kml>");
		br.flush();
		br.close();
	}

	private void writeProbabilityLandmark(int x, int y, double[][] scoremap,
			BufferedWriter br) throws IOException {
		int score = (int) (scoremap[x][y] * 100);

		br.write("<Placemark> \n");
		br.write("<visibility>1</visibility>\n");
		br.write("<name>" + score + "</name>\n");

		br.write("<styleUrl>#" + score + "</styleUrl>\n");

		br.write("<Polygon>\n");
		br.write("<tessellate>1</tessellate>\n");
		br.write("<outerBoundaryIs>\n");
		br.write("<LinearRing>\n");
		br.write("<coordinates>\n");

		// draw the square
		double accuracyX = map.accuracy;
		double accuracyY = map.accuracy;
		double coordX = map.Xcoords[x];
		double coordY = map.Ycoords[y];
		br.write(coordX - accuracyX + "," + coordY + accuracyY + ",0,\n");
		// Point 2
		br.write(coordX + accuracyX + "," + coordY + accuracyY + ",0,\n");
		// Point3
		br.write(coordX + accuracyX + "," + (coordY - accuracyY) + ",0,\n");
		// Point4
		br.write((coordX - accuracyX) + "," + (coordY - accuracyY) + ",0,\n");
		// End: Point 1
		br.write((coordX - accuracy) + "," + coordY + accuracy + ",0\n");

		br.write("</coordinates>\n");
		br.write("</LinearRing>\n");
		br.write("</outerBoundaryIs>\n");
		br.write("</Polygon>\n");
		br.write("</Placemark>\n");

	}

	private void writeProbHeader(BufferedWriter br) throws IOException {
		br.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		br.write("<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n");
		br.write("  <Document>\n");
		br.write("<name> Probability map </name> \n");
		br.flush();
		br.write("<open>1</open>\n");

		// style: color for probability from 0% to 100%
		// color red = #FF0000 (16711680)
		// color green = #00FF00 (65280)

		// styles for probability
		for (int style = 0; style <= 100; style++) {

			// int step = (16711680 - 65280) / getSteps();
			// for (int i = getMin(); i <= getMax(); i++) {

			br.write("<Style id=\"" + style + "\"> \n");
			br.write("<PolyStyle> \n");
			br.write("<color>" + getProbabilityColor(style) + "</color> \n");
			br.write("</PolyStyle> \n");
			br.write("<LineStyle>\n");
			// br.write("<color>" + getColor(style) + "</color>\n");
			br.write("<color>" + 00000000 + "</color>\n");
			br.write("</LineStyle>\n");
			br.write("</Style> \n");
		}
		br.write("  <Folder>\n");
		br.write("<name>Probability</name>");
		br.write("        <visibility>1</visibility>\n");
		br.write("<description>Probability Map</description>\n");
		br.write("        <LookAt>\n");
		br.write("<longitude>" + (map.maxX + map.minX) / 2 + "</longitude>\n");
		br.write("<latitude>" + (map.maxY + map.minY) / 2 + "</latitude>\n");
		br.write("<altitude>0</altitude>\n");
		br.write("<heading>0.0</heading>\n");
		br.write("<tilt>0</tilt>\n");
		br.write("<range>276.0</range>\n");
		br.write("</LookAt>\n");

	}

	private String getProbabilityColor(int probability) {

		int greenp = (int) (probability * 2.55);
		return ("ff00" + Integer.toHexString(greenp) + "00");

		/*
		 * // probability is from 0 - 100 int green = 0; int red = 0; // yellow
		 * at -75 if (probability > 70) { green = 255; red = 256 - Math.abs((256
		 * / 30) * (-70 - probability));
		 * 
		 * } else { // transient green from now on red = 255; green =
		 * Math.abs((256 / 70) * (-30 - probability)); } // int green = 256 -
		 * Math.abs((256 / 90) * (-120 - strength)); // int red = Math.abs((256
		 * / 90) * (-120 - strength)); // int result = red << 24 | green if
		 * ((green > 255) || (red > 255))
		 * System.out.println("Zu groß bei strength= " + probability + ",Rot: "
		 * + red + "Grün: " + green); if (green > 255) green = 255; if (red >
		 * 255) red = 255; // return ("7f00" + Integer.toHexString(red) + //
		 * Integer.toHexString(green)); // return ("ff00" +
		 * Integer.toHexString(red) + // Integer.toHexString(green)); // return
		 * ("ff00" + Integer.toHexString(red) + // Integer.toHexString(green));
		 * return ("ff00" + Integer.toHexString(green) +
		 * Integer.toHexString(red));
		 */
	}
}