summaryrefslogtreecommitdiffstats
path: root/DataStructure/GSMMapInterpolatorOld.java
blob: eff8057c4f65763893ab1d650125415fd268dc11 (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
package DataStructure;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import Parse.sqlreader;

@SuppressWarnings("serial")
public class GSMMapInterpolatorOld extends GSMMap {

	public GSMMapInterpolatorOld(sqlreader SQL, double accuracy) {
		super(SQL, accuracy);
		// TODO Auto-generated constructor stub
	}

	public void interpolate() {
		for (SingleBTS currentBTS : this.btsnames) {
			// go through every known BTS
			interpolateBTS(currentBTS);

		}
	}

	private void interpolateBTS(SingleBTS currentBTS) {
		// add security bounds
		for (int x = 2; x < this.Xcoords.length - 2; x++) {
			for (int y = 2; y < this.Ycoords.length - 2; y++) {
				// check if interpolation is needed
				if (!InterpolateHelper.listContains(currentBTS, this.map[x][y])) {
					// try to interpolate
					SingleBTS interpolated = tryInterpolation(currentBTS, x, y);
					if (interpolated != null)
						// maybe the list is empty. then create list
						if (map[x][y] == null) {
							ArrayList<SingleBTS> list = new ArrayList<SingleBTS>();
							list.add(interpolated);
							map[x][y] = list;
						} else {
							map[x][y].add(interpolated);
						}

				}

			}
		}

	}

	private SingleBTS tryInterpolation(SingleBTS btsName, int x, int y) {
		int interpolated = 0;
		// do interpolation for left,right,up,down
		SingleBTS left = interpolate1to2(btsName, x - 2, x - 1, y, y);
		SingleBTS right = interpolate1to2(btsName, x + 2, x + 1, y, y);
		SingleBTS down = interpolate1to2(btsName, x, x, y + 2, y + 1);
		SingleBTS up = interpolate1to2(btsName, x, x, y - 2, y - 1);
		SingleBTS between = interpolateInBetween(btsName, x, y);
		if (left != null)
			interpolated++;
		if (right != null)
			interpolated++;
		if (down != null)
			interpolated++;
		if (up != null)
			interpolated++;
		if (between != null)
			interpolated++;
		if (interpolated > 1) {
			// interpolation successfull!
			SingleBTS element = new SingleBTS(btsName.ARFCN, 0, 0, true,
					new Date(), btsName.name);
			// prepare element for averaging
			element.clear();
			// nothing bad will happen if one of these BTSs are null!
			element.addBTSMeasure(left);
			element.addBTSMeasure(right);
			element.addBTSMeasure(down);
			element.addBTSMeasure(up);
			element.addBTSMeasure(between);
			if (element.getDldB() > -121 && element.getUldB() > -121)
				if (element.getDldB() < -29 && element.getUldB() < -29)
					return element;

		}
		return null;

	}

	private SingleBTS interpolate1to2(SingleBTS btsName, int x1, int x2,
			int y1, int y2) {

		SingleBTS first = InterpolateHelper.getFromList(btsName, map[x1][y1]);
		SingleBTS second = InterpolateHelper.getFromList(btsName, map[x2][y2]);
		// prepare returning BTS

		if (first != null && second != null) {
			// interpolation for these two elements possible
			double deltaDL = second.getDldB() - first.getDldB();
			double deltaUL = second.getUldB() - first.getUldB();
			// add the value of log10(0.005). one Array cell is 5*5m big! See
			// the
			// Youngs Model!

			// TODO: careful! double gets castet to int. Do correct
			// interpolation here!

			double DL = deltaDL + second.getDldB();
			double UL = deltaUL + second.getUldB();
			SingleBTS element = new SingleBTS(first.ARFCN, (int) UL, (int) DL,
					true, new Date(), first.name);
			// first.interpolated = true;
			return element;
		}

		return null;
	}

	private SingleBTS interpolateInBetween(SingleBTS btsName, int x, int y) {
		// take one from the left, one from the right
		// one from the top, one from the bottom
		// one from the diagonal and one from the other diagonal
		// prepare interpolated element!
		SingleBTS element = new SingleBTS(btsName.ARFCN, 0, 0, true,
				new Date(), btsName.name);
		element.clear();

		// to check if a correct interpolation was done
		boolean[] done = { false, false, false, false };

		// diagonal leftAndRight
		SingleBTS first = InterpolateHelper.getFromList(btsName,
				map[x - 1][y + 1]);
		SingleBTS third = InterpolateHelper.getFromList(btsName,
				map[x + 1][y - 1]);
		if (first != null && third != null) {
			element.addBTSMeasure(first);
			element.addBTSMeasure(third);
			done[0] = true;
		}

		// diagonal rightAndLeft
		first = InterpolateHelper.getFromList(btsName, map[x + 1][y + 1]);
		third = InterpolateHelper.getFromList(btsName, map[x - 1][y - 1]);
		if (first != null && third != null) {
			element.addBTSMeasure(first);
			element.addBTSMeasure(third);
			done[1] = true;
		}

		// leftAndRight
		first = InterpolateHelper.getFromList(btsName, map[x - 1][y]);
		third = InterpolateHelper.getFromList(btsName, map[x + 1][y]);
		if (first != null && third != null) {
			element.addBTSMeasure(first);
			element.addBTSMeasure(third);
			done[2] = true;
		}

		// bottomAndTop
		first = InterpolateHelper.getFromList(btsName, map[x][y + 1]);
		third = InterpolateHelper.getFromList(btsName, map[x][y - 1]);
		if (first != null && third != null) {
			element.addBTSMeasure(first);
			element.addBTSMeasure(third);
			done[3] = true;
		}

		// return the BTS only if at least one Interpolation worked out
		for (int i = 0; i < done.length; i++) {
			if (done[i])
				return element;
		}
		return null;

	}
}

final class InterpolateHelper {

	public static boolean listContains(SingleBTS bts, List<SingleBTS> btsList) {
		for (SingleBTS listBTS : btsList) {
			if (bts.ARFCN == listBTS.ARFCN)
				return true;
		}
		return false;
	}

	// get the Element containing the ARFCN of btsName. Returns null, if no
	// Element found!
	public static SingleBTS getFromList(SingleBTS btsName,
			List<SingleBTS> btslist) {
		for (SingleBTS listBTS : btslist) {
			if (listBTS.ARFCN == btsName.ARFCN)
				return listBTS;

		}
		return null;
	}

}