summaryrefslogtreecommitdiff
path: root/parsecalldata.pde
blob: 4b1b1b1886dc952081879226c1f09eeff427159b (plain)
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
//BT IRELAND MTM,Destination CID Name,VIA,Calls,MINS,ALOC,% User,ASR,NER 02
boolean DEBUG=true;

import proxml.*;

//java.io.NotSerializableException: geomerative.RShape

// ATTEMPT TO SERIALISE COUNTRY DATA.. unfinished
//
// use proxml manually
//


class country{
  String name;
  float calls,mins,aloc,user,asr,ner;
  int networks;
  RShape outline;
  country(String n,float c,float m,float al,float u,float as,float ne,RShape shp){
    name=n;
    calls=c;
    mins=m;
    aloc=al;
    user=u;
    asr=as;
    ner=ne;
    outline=shp;
    networks=1;
  }
  void addnetwork(float c,float m,float al,float u,float as,float ne) {
    calls+=c;
    mins+=m;
    aloc+=al;
    user+=u;
    asr+=as;
    ner+=ne;
    networks++;
  }
  void getavgs() {
    aloc/=networks;
    user/=networks;
    asr/=networks;
    ner/=networks;
  }
  void printOut() {
    println(name+" "+calls+" "+mins);
  }
  RPoint getpoint(int tries){
      RPoint tl=outline.getTopLeft();
      RPoint br=outline.getBottomRight();
      RPoint np= new RPoint(0,0);
      int searching =1;
      while (searching>0&&searching<tries) {
        np=new RPoint(random(tl.x,br.x),random(tl.y,br.y));
        if(outline.contains(np)) searching=0;
        //if (DEBUG) println(name+"; try  "+searching+": "+np.x+","+np.y+" bounds: "+tl.x+","+tl.y+" - "+br.x+","+br.y+": "+(searching>0));
        if (searching==10||searching==100||searching==1000||searching==10000||searching==100000) {
          println(name+"; try  "+searching+": MISS at "+np.x+","+np.y+" bounds: "+tl.x+","+tl.y+" - "+br.x+","+br.y);
        }
      }
      if (searching==0) return np;
      else return new RPoint(0,0);
  }
   RPoint getcentre(){
     RPoint tl=outline.getTopLeft();
      RPoint br=outline.getBottomRight();
      return  new RPoint((tl.x+br.x)*0.5,(tl.y+br.y)*0.5);
   }
}

class weightedpixel {
  float bright;
  RPoint pt;
  float index; //index weighted from 0-1
  weightedpixel(RPoint _p,float _b) {
    pt=_p;
    bright=_b;
  }
  RPoint randompt(float r) {
   return new RPoint(pt.x+random(r),pt.y+random(r)); 
  }
}

class bitmapcountry extends country {
  //examines a bitmap
  //builds a table of population desnsity
  
  //look at it from the POV of how its gonna be used
  //getpoints(int num) should be able to return an array of points to draw from
  //should be dithered
  //has to distribute the points around the brightest points first
  //distribute in proportion to their brightness - weighted
  
  //table of pixels: if within shape:
  //sample brightness, keep running total and store normalised weighted index for quick searching
  
  float xo,xs,yo,ys;
  float btotal,bavg,bmin,bmax;
  Vector<weightedpixel> points = new Vector<weightedpixel>();
  bitmapcountry(String n,float c,float m,float al,float u,float as,float ne,RShape shp) {
    super(n,c,m,al,u,as,ne,shp);
  }
  void analyse(float _xo,float _xs,float _yo,float _ys,int step) {
      //int step=2; //speedup
        xo=_xo;
        xs=_xs;
        yo=_yo;
        ys=_ys;
        //scan country outline by pixel
        RPoint tl=outline.getTopLeft();
        RPoint br=outline.getBottomRight();
        RPoint tlp=normpix(tl);
        RPoint brp=normpix(br);
        float xstep=xs/lightmap.width;
        float ystep=ys/lightmap.height;
        btotal=0;
        bmin=255;
        bmax=0;
        println("searching "+tl.x+","+tl.y+" -> "+(tl.x+(xstep*(brp.x-tlp.x)))+","+(tl.y+(ystep*(brp.y-tlp.y)))+" by "+xstep+","+ystep);
        for (int i=0;i<brp.x-tlp.x;i+=step) {
          for (int j=0;j<brp.y-tlp.y;j+=step) {
            RPoint pn=new RPoint(tl.x+(i*xstep),tl.y+(j*ystep));
            if(outline.contains(pn)) {
              float bn=brightness(lightmap.get((int)tlp.x+i,(int)tlp.y+j))+.01;//gives a 0.039% weighting to blank pixels
              points.add(new weightedpixel(pn,bn));
              btotal +=bn;
              if (bn>bmax) bmax=bn;
              if (bn<bmin) bmin=bn;
            }
          }
        }
        float runbright=0;
        float bfactor=1.0/btotal;
        for (int i=0;i<points.size();i++) {
          runbright+=points.get(i).bright*bfactor;
          points.get(i).index=runbright;
        }
        bavg=(btotal/points.size());
        if (DEBUG) println(name+"; bounds: "+tl.x+","+tl.y+" : "+br.x+","+br.y+" - "+points.size()+" pixels, "+bmin+"-"+bmax+" avg: "+bavg);
        
  }
  RPoint normalise(RPoint p) {
    float px=(((p.x-xo)/xs)-0.5)*PI*2; //26)/736);
    float py=(((p.y-yo)/ys)-0.5)*PI; //90)/390);
        
    return new RPoint(px,py);
  }
  RPoint normpix(RPoint p) {
    float px=((p.x-xo)/xs)*lightmap.width;
    float py=((p.y-yo)/ys)*lightmap.height;
        
    return new RPoint(px,py);
  }
  RPoint getpoint(){
    float index=random(0.99999);
    int i=0;
    while (points.get(i).index<index) {
      i++;
    }
    return new RPoint(points.get(i).pt.x+random(1),points.get(i).pt.y+random(1));
  }
  weightedpixel getpixel(){
    float index=random(0.9999);
    int i=0;
    while (points.get(i).index<index) {
      i++;
    }
    return points.get(i);
  }
}

class calldata {
  float start;
  Vector<bitmapcountry> countries = new Vector<bitmapcountry>();
  HashMap<String, Integer> outlines = new HashMap<String, Integer>();
  proxml.XMLElement xmlcountries;
  XMLInOut xmlInOut;
  PApplet pApplet;
  boolean loaded;
  int step; //coarseness of bitmap analysis
  
  country getcountry(int w) {
    return countries.get(w);
  }
  
  void loadxml(proxml.XMLElement element) {
    xmlcountries = element;
    //reconstruct countries from xml data including attaching shapes
    proxml.XMLElement country;
    proxml.XMLElement pt;
    
    int pts=0;
    
    for(int i = 0; i < xmlcountries.countChildren();i++){
      country = xmlcountries.getChild(i);
      String name=country.getAttribute("name");
      if (outlines.containsKey(name.substring(0, 3))) {
        bitmapcountry c=new bitmapcountry(name,country.getFloatAttribute("calls"),country.getFloatAttribute("mins"),country.getFloatAttribute("aloc"),country.getFloatAttribute("user"),country.getFloatAttribute("asr"),country.getFloatAttribute("ner"),shp.children[outlines.get(name.substring(0, 3))]);
        for(int j = 0; j < country.countChildren();j++){
          pt = country.getChild(j);
          weightedpixel p=new weightedpixel(new RPoint(pt.getFloatAttribute("x"),pt.getFloatAttribute("y")),pt.getFloatAttribute("b"));
          p.index=pt.getFloatAttribute("i");
          c.points.add(p);
        }
        c.btotal=country.getFloatAttribute("btotal");
        c.bmax=country.getFloatAttribute("bmax");
        c.bmin=country.getFloatAttribute("bmin");
        c.bavg=c.btotal/c.points.size();
        pts+=country.countChildren();
        countries.add(c);
      }
    }
    println("loaded "+countries.size()+" countries, "+pts+" data points in "+((millis()-start)*.001)+"secs");
  loaded=true;  
}
 
  calldata(String[][] data,RShape shp,String filename,int _step,PApplet _pApplet) {
    start=millis();
    boolean parsedata=true;
    pApplet=_pApplet;
    loaded=false;
    step=_step;
    /*
    try { //to deserialise
      File file = new File("countries.dat");
      ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
      Vector<bitmapcountry> countries = (Vector<bitmapcountry>) in.readObject();
      in.close();
      parsedata=false;
    } catch (ClassNotFoundException e) {
      println("problem parsing countries.dat");
      e.printStackTrace();
    } catch (IOException e) {
      println("problem loading countries.dat");
    } 
    if (parsedata) {  
      
      */ 
      
      for (int i=0;i<shp.children.length;i++) {
        outlines.put(shp.children[i].name.substring(0, 3),i);
      }
      
      xmlInOut = new XMLInOut(pApplet);
      try{
        xmlInOut.loadElement(filename);
      }catch(Exception e){
        //if the xml file could not be loaded it has to be created
        proxml.XMLElement xmlcountries = new proxml.XMLElement("xmlcountries");

        println("parsing "+data.length+" svg entities");
        //detect number of countries
        int num=0;
        if (outlines.containsKey(data[0][1].substring(0, 3))) {
          countries.add(new bitmapcountry(data[0][1],Float.valueOf(data[0][3].trim()),Float.valueOf(data[0][4].trim()),Float.valueOf(data[0][5].trim()),Float.valueOf(data[0][6].trim()),Float.valueOf(data[0][7].trim()),Float.valueOf(data[0][8].trim()),shp.children[outlines.get(data[0][1].substring(0, 3))]));
        }
        for (int i=0; i < data.length; i++) { 
          if (data[i][1].substring(0,3).equals(countries.lastElement().name.substring(0, 3))) {       
            //2x try-catch to catch empty fields in csv. fix data with search-replace ,, to ,0, and ,/n to ,0/n
            try {
              countries.lastElement().addnetwork(Float.valueOf(data[i][3].trim()),Float.valueOf(data[i][4].trim()),Float.valueOf(data[i][5].trim()),Float.valueOf(data[i][6].trim()),Float.valueOf(data[i][7].trim()),Float.valueOf(data[i][8].trim()));
            }catch(Exception ee){
            println("empty string in "+data[i][0]+","+data[i][1]+","+data[i][2]+","+data[i][3]+","+data[i][4]+","+data[i][5]+","+data[i][6]+","+data[i][7]+","+data[i][8]+",");
            }  
     
    }
          else {
            if (outlines.containsKey(data[i][1].substring(0, 3))) {
                countries.add(new bitmapcountry(data[i][1],Float.valueOf(data[i][3].trim()),Float.valueOf(data[i][4].trim()),Float.valueOf(data[i][5].trim()),Float.valueOf(data[i][6].trim()),Float.valueOf(data[i][7].trim()),Float.valueOf(data[i][8].trim()),shp.children[outlines.get(data[i][1].substring(0, 3))]));
            }
          }      
        }
        
        for (int i=0; i < countries.size(); i++) {  
        //if (DEBUG) countries.get(i).printOut();      
            countries.get(i).getavgs();
            countries.get(i).analyse(18.279,746.302,109,374.293,step);
            proxml.XMLElement country = new proxml.XMLElement("country");
            country.addAttribute("name",countries.get(i).name);
            country.addAttribute("calls",countries.get(i).calls);
            country.addAttribute("mins",countries.get(i).mins);
            country.addAttribute("aloc",countries.get(i).aloc);
            country.addAttribute("user",countries.get(i).user);
            country.addAttribute("asr",countries.get(i).asr);
            country.addAttribute("ner",countries.get(i).ner);
            country.addAttribute("btotal",countries.get(i).btotal);
            country.addAttribute("bmin",countries.get(i).bmin);
            country.addAttribute("bmax",countries.get(i).bmax);
            for (int j=0; j < countries.get(i).points.size(); j++) {
              proxml.XMLElement pt = new proxml.XMLElement("pt");
              pt.addAttribute("b",countries.get(i).points.get(j).bright);
              pt.addAttribute("x",countries.get(i).points.get(j).pt.x);
              pt.addAttribute("y",countries.get(i).points.get(j).pt.y);
              pt.addAttribute("i",countries.get(i).points.get(j).index);
              country.addChild(pt);
            }
            xmlcountries.addChild(country);
          }
         xmlInOut.saveElement(xmlcountries,filename);
         println("generated "+filename+": "+countries.size()+" countries in "+((millis()-start)*.001)+"secs");
        loaded=true;  
    }
  }
}

void xmlEvent(proxml.XMLElement element){
    //reconstruct countries from xml data including attaching shapes
    calls.loadxml(element);
}