From: Ian T. <ian...@us...> - 2001-11-22 15:43:26
|
Update of /cvsroot/geotools/geotools/src/uk/ac/leeds/ccg/widgets In directory usw-pr-cvs1:/tmp/cvs-serv26683/uk/ac/leeds/ccg/widgets Modified Files: ScaleBar.java Log Message: added an option to "unproject" the scale bar if your map is in latlong - It may not be completely correct if you are along way north/south, since the measurement is made in the centre of the displayed map. It also assumes a equatorial mercator project - But as a rough idea of the distances involved its fine. Index: ScaleBar.java =================================================================== RCS file: /cvsroot/geotools/geotools/src/uk/ac/leeds/ccg/widgets/ScaleBar.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** ScaleBar.java 2001/04/02 07:47:33 1.6 --- ScaleBar.java 2001/11/22 15:43:22 1.7 *************** *** 4,17 **** import uk.ac.leeds.ccg.geotools.*; import uk.ac.leeds.ccg.geotools.misc.*; public class ScaleBar extends Canvas implements ScaleChangedListener{ private final static boolean DEBUG=false; Scaler s; String units = "m"; boolean isSI = true; int height = 50; int width = 150; public ScaleBar(Viewer v){ ! if(DEBUG)System.out.println("---->uk.ac.leeds.ccg.widgets.ScaleBar constructed. Will identify itself as ScB->"); s = v.getScale(); s.addScaleChangedListener(this); --- 4,27 ---- import uk.ac.leeds.ccg.geotools.*; import uk.ac.leeds.ccg.geotools.misc.*; + import uk.ac.leeds.ccg.geotools.projections.*; public class ScaleBar extends Canvas implements ScaleChangedListener{ private final static boolean DEBUG=false; + private final static String DBC="ScB:"; Scaler s; String units = "m"; boolean isSI = true; + boolean isProjected = false; int height = 50; int width = 150; + ElipsoidalMercator proj; + + public ScaleBar(Viewer v,boolean projf){ + this(v); + isProjected=projf; + proj = new ElipsoidalMercator(); + } public ScaleBar(Viewer v){ ! if(DEBUG)System.out.println("---->uk.ac.leeds.ccg.widgets.ScaleBar constructed. Will identify itself as "+DBC); s = v.getScale(); s.addScaleChangedListener(this); *************** *** 28,36 **** return isSI; } - /** a present we'll draw a line of a fixed length and say how long it is - * in future we should choose a nice length so as to give a round number - * etc - */ int ticklen = 5; // how long the ticks are int inset =5; // distance from either end to the bar; --- 38,48 ---- return isSI; } + public void setProjected(boolean f){ + isProjected=f; + } + public boolean getProjected(){ + return isProjected; + } int ticklen = 5; // how long the ticks are int inset =5; // distance from either end to the bar; *************** *** 42,53 **** int lh = 2*h/3; len = w - 2*inset; ! if(DEBUG)System.out.println("ScB->len = "+len+" w "+w); ! double scaledLen = s.toMap(len); if(Double.isNaN(scaledLen)) return; // no scale in map double fac = Math.log(scaledLen)/Math.log(10.0); ! if(DEBUG)System.out.println("ScB->len = "+scaledLen+" "+fac); // can't just truncate for lengths less than 1! double size = Math.pow(10.0,Math.floor(fac)); len=s.toGraphics(size); int ticks=0; while(len<w-2*inset){ --- 54,89 ---- int lh = 2*h/3; len = w - 2*inset; ! if(DEBUG)System.out.println(DBC+"->len = "+len+" w "+w); ! double scaledLen; ! if(!isProjected){ ! scaledLen = s.toMap(len); ! }else{ ! GeoRectangle r = s.getMapExtent(); ! double slen = s.toMap(len); ! double p1[] = proj.project(r.x,r.y+r.height/2.0); ! double p2[] = proj.project(r.x+slen,r.y+r.height/2.0); ! if(DEBUG){ ! System.out.println(DBC+" in "+r.x+","+(r.y+r.height/2.0)+" -> "+ ! (r.x+slen)+","+(r.y+r.height/2.0)); ! System.out.println(DBC+" out "+p1[0]+","+p1[1]+" "+p2[0]+","+p2[1]); ! } ! ! scaledLen = (p2[0]-p1[0])*1000; // proj is in km ! } ! if(Double.isNaN(scaledLen)) return; // no scale in map double fac = Math.log(scaledLen)/Math.log(10.0); ! if(DEBUG)System.out.println(DBC+"->len = "+scaledLen+" "+fac); // can't just truncate for lengths less than 1! double size = Math.pow(10.0,Math.floor(fac)); len=s.toGraphics(size); + if(DEBUG)System.out.println(DBC+" size "+size+" len "+len+" fac "+fac); + int fudge=0; + while(len>w){ + size = Math.pow(10.0,Math.floor(--fac)); + fudge++; + len=s.toGraphics(size); + if(DEBUG)System.out.println(DBC+" size "+size+" len "+len+" fac "+fac); + } int ticks=0; while(len<w-2*inset){ *************** *** 64,69 **** len=s.toGraphics(size*ticks); scaledLen = size*ticks; ! if(DEBUG)System.out.println("ScB->len = "+len+" slen "+scaledLen+" size "+size); String tunits=units; if(isSI&&scaledLen>1000.0){ size/=1000.0; --- 100,116 ---- len=s.toGraphics(size*ticks); scaledLen = size*ticks; ! if(DEBUG)System.out.println(DBC+"->len = "+len+" slen "+scaledLen+" size "+size); String tunits=units; + + if(isProjected&&fudge>0){ + if(DEBUG)System.out.println(DBC+"fudge = "+fudge+" size " + size + + "slen "+scaledLen); + while(fudge-->0){ + size*=10.0; + scaledLen*=10.0; + } + if(DEBUG)System.out.println(DBC+"fudge = "+fudge+" size " + size + + "slen "+scaledLen); + } if(isSI&&scaledLen>1000.0){ size/=1000.0; *************** *** 72,81 **** } - g.drawLine(inset,lh,inset+len,lh); double tlen = len/(double)ticks; ! if(DEBUG)System.out.println("ScB->ticks "+ticks+" "+tlen); int step =1; - if(ticks>=4) step=2; if(ticks>=6) step =3; if(ticks>=8) step = 4; --- 119,126 ---- } g.drawLine(inset,lh,inset+len,lh); double tlen = len/(double)ticks; ! if(DEBUG)System.out.println(DBC+"->ticks "+ticks+" "+tlen); int step =1; if(ticks>=6) step =3; if(ticks>=8) step = 4; *************** *** 84,87 **** --- 129,133 ---- g.drawLine(inset+(int)Math.round(tlen*i),lh,inset+(int)Math.round(tlen*i),lh+ticklen); if(i%step==0||i==ticks){ + // should work out length of string (size*i) and centre label g.drawString(""+size*i,(int)(tlen*i),h); g.drawLine((int)Math.round(inset+tlen*i),lh-ticklen,(int)Math.round(inset+tlen*i),lh); |