My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
/*
* Copyright 2007 Tom Gibara
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0

* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.tomgibara.cluster.gvm.demo.city;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.tomgibara.cluster.gvm.dbl.DblClusters;
import com.tomgibara.cluster.gvm.dbl.DblListKeyer;
import com.tomgibara.cluster.gvm.dbl.DblResult;

/*
* Created on 06-Aug-2007
*/

public class CityDemo {

private static final int CITY_NAME_MAX = 20;

private static final Map<String, Color> CONTINENT_COLORS = new HashMap<String, Color>();

static {
CONTINENT_COLORS.put("Africa", new Color(128, 0, 0));
CONTINENT_COLORS.put("Europe", new Color(0, 180, 0));
CONTINENT_COLORS.put("America", new Color(255, 0, 0));
CONTINENT_COLORS.put("Asia", new Color(180, 128, 0));
CONTINENT_COLORS.put("Australia", new Color(0, 180, 255));
}

public static void insert(final Container c, final List<City> cities, Dimension d) {
c.setLayout(new BorderLayout());
final WorldMap map = new WorldMap();
if (d != null) map.setPreferredSize(d);
c.add(map, BorderLayout.CENTER);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JCheckBox checkbox = new JCheckBox("Display Cities", false);
panel.add(checkbox);
c.add(panel, BorderLayout.NORTH);
JSlider slider = new JSlider(1, 200);
slider.setValue(10);
Dictionary<Integer, Component> labels = new Hashtable<Integer, Component>();
labels.put(1, new JLabel("1"));
labels.put(50, new JLabel("50"));
labels.put(100, new JLabel("100"));
labels.put(150, new JLabel("150"));
labels.put(200, new JLabel("200"));
slider.setLabelTable(labels);
slider.setMajorTickSpacing(50);
slider.setMinorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
c.add(slider, BorderLayout.SOUTH);

List<DblResult<City>> results = clusterCities(cities, slider.getValue());
List<Pin> pins = pinsFromResults(results);
map.addAllPins(pins);
map.repaint();
map.setTitle(String.format("%,3d largest cities...", cities.size()));

final AtomicInteger currentValue = new AtomicInteger( 0 );
final AtomicInteger desiredValue = new AtomicInteger( slider.getValue() );
final AtomicBoolean currentDisplay = new AtomicBoolean(true);
final AtomicBoolean desiredDisplay = new AtomicBoolean(true);

new UpdateMap(map, cities, currentDisplay, desiredDisplay, currentValue, desiredValue).run();

final Timer timer = new Timer(true);

checkbox.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JCheckBox checbox = (JCheckBox) e.getSource();
desiredDisplay.set(!checbox.isSelected());
UpdateMap update = new UpdateMap(map, cities, currentDisplay, desiredDisplay, currentValue, desiredValue);
timer.schedule(update, new Date());
}
});

slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider) e.getSource();
desiredValue.set(slider.getValue());
UpdateMap update = new UpdateMap(map, cities, currentDisplay, desiredDisplay, currentValue, desiredValue);
timer.schedule(update, new Date());
}
});
}

// city methods

private static List<DblResult<List<City>>> clusterCities2(Collection<City> cities, int maxClusters) {
double[] xs = new double[2];
DblClusters<List<City>> clusters = new DblClusters<List<City>>(2, maxClusters);
clusters.setKeyer(new DblListKeyer<City>());
for (City city : cities) {
xs[0] = city.lng;
xs[1] = city.lat;
double m = city.pop;
ArrayList<City> list = new ArrayList<City>();
list.add(city);
clusters.add(m, xs, list);
}
List<DblResult<List<City>>> results = clusters.results();
return results;
}

private static List<Pin> pinsFromResults2(List<DblResult<List<City>>> results) {
ArrayList<Pin> pins = new ArrayList<Pin>();
int count = 0;
for (DblResult<List<City>> result : results) {
double lng = result.getCoords()[0];
double lat = result.getCoords()[1];
Color color = new Color((int) ((lng + 180.0) * 256.0 / 360.0), (int) ((lat + 90.0) * 256.0 / 180.0), count * 256 / results.size());
List<City> cities = result.getKey();
for (City city : cities) {
Pin pin = pinFromCity2(city, color);
pins.add(pin);
}
count++;
}
return pins;
}

private static Pin pinFromCity2(City city, Color color) {
double lng = city.lng;
double lat = city.lat;
int pop = city.pop;

int radius = radiusFromPop2(pop);
float x = (float) ((180.0 + lng) / 360.0);
float y = (float) ((90.0 - lat) / 180.0);

Pin pin = new Pin();
pin.setX(x);
pin.setY(y);
pin.setRadius(radius);
pin.setZ(-radius);
pin.setColor(color);
return pin;
}


private static int radiusFromPop2(double pop) {
double poplog = Math.log(pop);
return 1 + (int) (poplog*poplog*poplog/900.0);
}

//cluster methods

private static List<DblResult<City>> clusterCities(Collection<City> cities, int maxClusters) {
DblClusters<City> clusters = new DblClusters<City>(2, maxClusters);
clusters.setKeyer(new SingleCityKeyer());
double[] xs = new double[2];
for (City city : cities) {
xs[0] = city.lng;
xs[1] = city.lat;
double m = city.pop;
clusters.add(m, xs, city);
}

List<DblResult<City>> results = clusters.results();
return results;
}

private static List<Pin> pinsFromResults(List<DblResult<City>> results) {
ArrayList<Pin> pins = new ArrayList<Pin>();
Collections.sort(results, new ResultComp());
int pinCount = 0;
int hiddenLimit = results.size() - 20;
for (DblResult<City> result : results) {
Pin pin = pinFromResult(result);
if (pinCount >= hiddenLimit) pin.setChild(pinFromCity(result.getKey()));
pins.add(pin);
pinCount ++;
}
return pins;
}

private static Pin pinFromCity(City city) {
String continent = city.cont;
String name = city.name;
double lng = city.lng;
double lat = city.lat;
int pop = city.pop;

String label = name.length() > CITY_NAME_MAX ? name.substring(0, CITY_NAME_MAX - 3) + "..." : name;
Color color = CONTINENT_COLORS.get(continent);
int radius = radiusFromPop(pop);
float x = (float) ((180.0 + lng) / 360.0);
float y = (float) ((90.0 - lat) / 180.0);

Pin pin = new Pin();
pin.setX(x);
pin.setY(y);
pin.setLabel(label);
pin.setRadius(radius);
pin.setZ(-radius);
pin.setColor(color);
return pin;
}

private static Pin pinFromResult(DblResult<City> result) {
String continent = result.getKey().cont;
double lng = result.getCoords()[0];
double lat = result.getCoords()[1];
double pop = result.getMass();

String label = null;
Color color = CONTINENT_COLORS.get(continent);
int radius = (int) Math.sqrt( result.getVariance() / pop );
float x = (float) ((180.0 + lng) / 360.0);
float y = (float) ((90.0 - lat) / 180.0);

Pin pin = new Pin();
pin.setX(x);
pin.setY(y);
pin.setLabel(label);
pin.setRadius(radius);
pin.setZ(-radius);
pin.setColor(color);
return pin;
}

private static int radiusFromPop(double pop) {
double poplog = Math.log(pop);
return 3 + (int) (poplog*poplog*poplog/600.0);
}

private static class ResultComp implements Comparator<DblResult<City>> {
public int compare(DblResult<City> r1, DblResult<City> r2) {
double m1 = r1.getMass();
double m2 = r2.getMass();
if (m1 == m2) return 0;
return m1 < m2 ? -1 : 1;
}
}

private static class UpdateMap extends TimerTask {

private WorldMap map;
private List<City> cities;
private AtomicBoolean currentDisplay;
private AtomicBoolean desiredDisplay;
private AtomicInteger desiredValue;
private AtomicInteger currentValue;

public UpdateMap(WorldMap map, List<City> cities, AtomicBoolean currentDisplay, AtomicBoolean desiredDisplay, AtomicInteger currentValue, AtomicInteger desiredValue) {
this.map = map;
this.cities = cities;
this.currentDisplay = currentDisplay;
this.desiredDisplay = desiredDisplay;
this.desiredValue = desiredValue;
this.currentValue = currentValue;
}

public void run() {
int newValue = desiredValue.get();
int oldValue = currentValue.getAndSet(newValue);
boolean newDisplay = desiredDisplay.get();
boolean oldDisplay = currentDisplay.getAndSet(newDisplay);
if (oldDisplay == newDisplay && oldValue == newValue) return;
if (desiredDisplay.get()) {
doClusterUpdate(newValue);
} else {
doCityUpdate(newValue);
}
};

private void doCityUpdate(int count) {
long start = System.currentTimeMillis();
List<DblResult<List<City>>> results = clusterCities2(cities, count);
long finish = System.currentTimeMillis();
List<Pin> pins = pinsFromResults2(results);
map.clearPins();
map.addAllPins(pins);
map.setCaption(String.format("...into %d clusters (%,3d ms)", results.size(), finish - start));
map.repaint();
}

private void doClusterUpdate(int count) {
long start = System.currentTimeMillis();
List<DblResult<City>> results = clusterCities(cities, count);
long finish = System.currentTimeMillis();
List<Pin> pins = pinsFromResults(results);
map.clearPins();
map.addAllPins(pins);
map.setCaption(String.format("...into %d clusters (%,3d ms)", results.size(), finish - start));
map.repaint();
}

}

}

Change log

r176 by tomgibara on Apr 14, 2011   Diff
[maven-release-plugin]  copy for tag
cluster-all-1.0
Go to: 
Project members, sign in to write a code review

Older revisions

r171 by tomgibara on Apr 14, 2011   Diff
Removed some TODOs.
r167 by tomgibara on Apr 14, 2011   Diff
Added licence notices to source files.
r162 by tomgibara on Apr 12, 2011   Diff
Initial code commit for GVM.
All revisions of this file

File info

Size: 12275 bytes, 332 lines

File properties

svn:mime-type
text/plain
svn:executable
*
Powered by Google Project Hosting