diff options
| author | git@eclectronics.org <git@eclectronics.org@eclectronics.org> | 2012-02-17 13:56:30 +0000 |
|---|---|---|
| committer | git@eclectronics.org <git@eclectronics.org@eclectronics.org> | 2012-02-17 13:56:30 +0000 |
| commit | 6b47aa628182fc6dfa376113145d8f8b8bb047d1 (patch) | |
| tree | b281054b0a6e09273e85aa3d93ebdd6051216213 /parsecalldata.pde | |
| parent | fada66e0ff98ce98c50d6e9a5cc1f449a8a87296 (diff) | |
load/save data working
Diffstat (limited to 'parsecalldata.pde')
| -rw-r--r-- | parsecalldata.pde | 180 |
1 files changed, 127 insertions, 53 deletions
diff --git a/parsecalldata.pde b/parsecalldata.pde index 3c08b33..e3688c6 100644 --- a/parsecalldata.pde +++ b/parsecalldata.pde @@ -1,14 +1,19 @@ //BT IRELAND MTM,Destination CID Name,VIA,Calls,MINS,ALOC,% User,ASR,NER 02 boolean DEBUG=true; -import java.io.ObjectOutputStream; -import java.io.FileOutputStream; -import java.io.IOException; +import proxml.*; -class country { +//java.io.NotSerializableException: geomerative.RShape + +// ATTEMPT TO SERIALISE COUNTRY DATA.. unfinished +// +// use proxml manually +// + +class country{ String name; float calls,mins; - RShape outline; + transient RShape outline; country() { super(); } @@ -42,9 +47,9 @@ class country { else return new RPoint(0,0); } RPoint getcentre() { - RPoint tl=outline.getTopLeft(); - RPoint br=outline.getBottomRight(); - return new RPoint((tl.x+br.x)/2,(tl.y+br.y)/2); + RPoint tl=outline.getTopLeft(); + RPoint br=outline.getBottomRight(); + return new RPoint((tl.x+br.x)/2,(tl.y+br.y)/2); } } @@ -73,11 +78,12 @@ class bitmapcountry extends country { float xo,xs,yo,ys; Vector<weightedpixel> points = new Vector<weightedpixel>(); + bitmapcountry(){super();} bitmapcountry(String n,float c,float m,RShape shp) { super(n,c,m,shp); } void analyse(float _xo,float _xs,float _yo,float _ys) { - int step=1; //speedup + int step=10; //speedup xo=_xo; xs=_xs; yo=_yo; @@ -96,7 +102,7 @@ class bitmapcountry extends country { for (int j=0;j<brp.y-tlp.y;j+=step) { RPoint pn=new RPoint(tl.x+(i*pixstep),tl.y+(j*pixstep)); if(outline.contains(pn)) { - float bn=brightness(lightmap.get((int)brp.x+i,(int)brp.y+j)); + float bn=brightness(lightmap.get((int)brp.x+i,(int)brp.y+j))+1;//gives a 0.5% weighting to blank pixels points.add(new weightedpixel(pn,bn)); totalbright +=bn; if (bn>bmax) bmax=bn; @@ -125,60 +131,128 @@ class bitmapcountry extends country { return new RPoint(px,py); } - RPoint getpoint(){ - if (points.size()>0) { - float index=random(0.999); - int i=0; - while (points.get(i).index<index&&i<points.size()) { - i++; - } - return new RPoint(points.get(i).pt.x+random(1),points.get(i).pt.y+random(1)); - } - else return new RPoint(0,0); + RPoint getpoint(){ + float index=random(1.0); + int i=0; + while (points.get(i).index<index&&i<points.size()) { + i++; + } + return new RPoint(points.get(i).pt.x+random(1),points.get(i).pt.y+random(1)); } } class calldata { - - //serialise THIS - //take hard coded frame bounds out - Vector<bitmapcountry> countries = new Vector<bitmapcountry>(); HashMap<String, Integer> outlines = new HashMap<String, Integer>(); - calldata(String[][] data,RShape shp) { + proxml.XMLElement xmlcountries; + XMLInOut xmlInOut; + PApplet pApplet; + boolean loaded; + + 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; - for (int i=0;i<shp.children.length;i++) { - outlines.put(shp.children[i].name.substring(0, 3),i); + 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,(int)country.getFloatAttribute("calls"),(int)country.getFloatAttribute("mins"),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); + } + pts+=country.countChildren(); + countries.add(c); + } } + println("loaded "+countries.size()+" countries, "+pts+" data points"); + loaded=true; + } - println("parsing "+data.length+" entries"); - //detect number of countries - int num=0; - if (outlines.containsKey(data[0][1].substring(0, 3))) { - countries.add(new bitmapcountry(data[0][1],0.0,0.0,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))) { - countries.lastElement().addnetwork(Float.valueOf(data[i][3].trim()).floatValue(),Float.valueOf(data[i][4].trim()).floatValue()); + calldata(String[][] data,RShape shp,String filename,PApplet _pApplet) { + boolean parsedata=true; + pApplet=_pApplet; + loaded=false; + + /* + 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); } - else { - if (outlines.containsKey(data[i][1].substring(0, 3))) { - countries.add(new bitmapcountry(data[i][1],Float.valueOf(data[i][3].trim()).floatValue(),Float.valueOf(data[i][4].trim()).floatValue(),shp.children[outlines.get(data[i][1].substring(0, 3))])); + 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],0.0,0.0,shp.children[outlines.get(data[0][1].substring(0, 3))])); } - } - } - - for (int i=0; i < countries.size(); i++) { - //if (DEBUG) countries.get(i).printOut(); - countries.get(i).analyse(18.279,746.302,109,374.293); //129.314 - } - - } - country getcountry(int w) { - return countries.get(w); - } - void serialise(String file) { + for (int i=0; i < data.length; i++) { + if (data[i][1].substring(0,3).equals(countries.lastElement().name.substring(0, 3))) { + countries.lastElement().addnetwork(Float.valueOf(data[i][3].trim()).floatValue(),Float.valueOf(data[i][4].trim()).floatValue()); + } + else { + if (outlines.containsKey(data[i][1].substring(0, 3))) { + countries.add(new bitmapcountry(data[i][1],Float.valueOf(data[i][3].trim()).floatValue(),Float.valueOf(data[i][4].trim()).floatValue(),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).analyse(18.279,746.302,129.314,374.293); + 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); + 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); + loaded=true; + } } } + +void xmlEvent(proxml.XMLElement element){ + //reconstruct countries from xml data including attaching shapes + calls.loadxml(element); +} |
