Changeset 29826 in osm


Ignore:
Timestamp:
2013-08-05T11:15:02+02:00 (11 years ago)
Author:
malcolmh
Message:

save

Location:
applications/editors/josm/plugins/smed2/src/seamap
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/smed2/src/seamap/Renderer.java

    r29789 r29826  
    1111
    1212import java.awt.*;
     13import java.awt.font.*;
    1314import java.awt.geom.*;
    1415import java.util.*;
     
    2526
    2627public class Renderer {
    27        
     28
    2829        static MapHelper helper;
    2930        static SeaMap map;
     
    3233        static Graphics2D g2;
    3334        static int zoom;
    34        
     35
    3536        public static void reRender(Graphics2D g, int z, double factor, SeaMap m, MapHelper h) {
    3637                g2 = g;
     
    3839                helper = h;
    3940                map = m;
    40                 sScale = Symbols.symbolScale[zoom]*factor;
    41                 tScale = Symbols.textScale[zoom]*factor;
     41                sScale = Symbols.symbolScale[zoom] * factor;
     42                tScale = Symbols.textScale[zoom] * factor;
    4243                if (map != null) {
    4344                        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     
    4647                }
    4748        }
    48        
     49
    4950        public static AttMap getAtts(Feature feature, Obj obj, int idx) {
    5051                HashMap<Integer, AttMap> objs = feature.objs.get(obj);
    51                 if (objs == null) return null;
    52                 else return objs.get(idx);
    53         }
    54        
     52                if (objs == null)
     53                        return null;
     54                else
     55                        return objs.get(idx);
     56        }
     57
    5558        public static Object getAttVal(Feature feature, Obj obj, int idx, Att att) {
    5659                AttMap atts = getAtts(feature, obj, idx);
    57                 if (atts == null) return S57val.nullVal(att);
     60                if (atts == null)
     61                        return S57val.nullVal(att);
    5862                else {
    5963                        AttItem item = atts.get(att);
    60                         if (item == null) return S57val.nullVal(att);
     64                        if (item == null)
     65                                return S57val.nullVal(att);
    6166                        return item.val;
    6267                }
     
    7378                }
    7479        }
    75        
     80
    7681        private static Rectangle symbolSize(Symbol symbol) {
    7782                Symbol ssymb = symbol;
     
    8287                                }
    8388                                if (item.type == Prim.SYMB) {
    84                                         ssymb = ((SubSymbol)item.params).instr;
     89                                        ssymb = ((SubSymbol) item.params).instr;
    8590                                        break;
    8691                                }
     
    9095                }
    9196                return null;
     97        }
     98
     99        private double mile(Feature feature) {
     100                return Math.pow(2, zoom) * 256 / (21600 * Math.abs(Math.cos(feature.centre.lat)));
    92101        }
    93102       
     
    133142                                        if (first) {
    134143                                                curr = succ = next;
    135                                                 gap  = (space > 0);
    136                                                 scount  = ratio;
    137                                                 symbol  = prisymb;
     144                                                gap = (space > 0);
     145                                                scount = ratio;
     146                                                symbol = prisymb;
    138147                                                len = gap ? psize * space * 0.5 : psize;
    139148                                                first = false;
     
    156165                                                        }
    157166                                                        if (!gap) {
    158                                                                 Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(),
    159                                                                                 new Delta(Handle.BC, AffineTransform.getRotateInstance(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())) + Math.toRadians(90))), null);
    160                                                         }
    161                                                         if (space > 0) gap = !gap;
     167                                                                Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(), new Delta(Handle.BC, AffineTransform.getRotateInstance(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())) + Math.toRadians(90))), null);
     168                                                        }
     169                                                        if (space > 0)
     170                                                                gap = !gap;
    162171                                                        curr = succ;
    163172                                                        len = gap ? (psize * space) : (--scount == 0) ? ssize : psize;
     
    175184        }
    176185
    177         public static void lineVector (Feature feature, LineStyle style) {
     186        public static void lineVector(Feature feature, LineStyle style) {
    178187                Path2D.Double p = new Path2D.Double();
    179188                p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
     
    220229                }
    221230        }
    222        
    223         public static void labelText (Feature feature, String str, Font font, Color colour, Delta delta) {
     231
     232        public static void labelText(Feature feature, String str, Font font, Color colour, Delta delta) {
    224233                Symbol label = new Symbol();
    225234                label.add(new Instr(Prim.TEXT, new Caption(str, font, colour, (delta == null) ? new Delta(Handle.CC, null) : delta)));
     
    227236                Symbols.drawSymbol(g2, label, tScale, point.getX(), point.getY(), null, null);
    228237        }
    229        
    230         public static void lineText (Feature feature, String str, Font font, Color colour, double offset, double dy) {
    231                
     238
     239        public static void lineText(Feature feature, String str, Font font, Color colour, double offset, double dy) {
     240                Area area;
     241                switch (feature.flag) {
     242                case LINE:
     243                        Edge edge = map.edges.get(feature.refs);
     244                        area = map.new Area();
     245                        area.add(map.new Bound(map.new Side(edge, true), true));
     246                        break;
     247                case AREA:
     248                        area = map.areas.get(feature.refs);
     249                        break;
     250                default:
     251                        return;
     252                }
     253//              Rectangle prect = symbolSize(prisymb);
     254                if (!str.isEmpty()) {
     255                        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     256            FontRenderContext frc = g2.getFontRenderContext();
     257            GlyphVector gv = font.deriveFont((float)(font.getSize()*tScale)).createGlyphVector(frc, str);
     258//                      double psize = Math.abs(prect.getX());
     259                        Point2D prev = new Point2D.Double();
     260                        Point2D next = new Point2D.Double();
     261                        Point2D curr = new Point2D.Double();
     262                        Point2D succ = new Point2D.Double();
     263                        boolean gap = true;
     264                        boolean piv = false;
     265                        double len = 0;
     266                        double angle = 0;
     267                        for (Bound bound : area) {
     268                                BoundIterator bit = map.new BoundIterator(bound);
     269                                boolean first = true;
     270                                while (bit.hasNext()) {
     271                                        prev = next;
     272                                        next = helper.getPoint(bit.next());
     273                                        angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
     274                                        piv = true;
     275                                        if (first) {
     276                                                curr = succ = next;
     277//                                              gap = (space > 0);
     278//                                              len = gap ? psize * space * 0.5 : psize;
     279                                                first = false;
     280                                        } else {
     281                                                while (curr.distance(next) >= len) {
     282                                                        if (piv) {
     283                                                                double rem = len;
     284                                                                double s = prev.distance(next);
     285                                                                double p = curr.distance(prev);
     286                                                                if ((s > 0) && (p > 0)) {
     287                                                                        double n = curr.distance(next);
     288                                                                        double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
     289                                                                        double phi = Math.asin(p / len * Math.sin(theta));
     290                                                                        rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
     291                                                                }
     292                                                                succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
     293                                                                piv = false;
     294                                                        } else {
     295                                                                succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
     296                                                        }
     297                                                        if (!gap) {
     298//                                                              Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(), new Delta(Handle.BC, AffineTransform.getRotateInstance(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())) + Math.toRadians(90))), null);
     299                                                        }
     300                                                        gap = false;
     301                                                        curr = succ;
     302//                                                      len = psize;
     303                                                }
     304                                        }
     305                                }
     306                        }
     307                }
    232308        }
    233309}
  • applications/editors/josm/plugins/smed2/src/seamap/SeaMap.java

    r29286 r29826  
    163163                public AttMap atts;
    164164                public ObjMap objs;
    165                 public long area;
     165                public double area;
     166                public double length;
    166167                public Snode centre;
    167168
     
    173174                        objs = new ObjMap();
    174175                        area = 0;
     176                        length = 0;
    175177                        centre = new Snode();
    176178                }
     
    377379                                node.flg = Nflag.ISOL;
    378380                        }
     381                        feature.length = 0;
     382                        feature.area = 0;
    379383                        break;
    380384                case LINE:
     
    382386                        nodes.get(edge.first).flg = Nflag.CONN;
    383387                        nodes.get(edge.last).flg = Nflag.CONN;
     388                        Bound ebound = (new Bound(new Side(edge, edge.forward), true));
     389                        feature.length = calcLength(ebound);
    384390                        if (edge.first == edge.last) {
    385391                                feature.flag = Fflag.AREA;
    386392                                Area area = new Area();
    387                                 area.add(new Bound(new Side(edge, edge.forward), true));
     393                                area.add(ebound);
     394                                feature.area = calcArea(ebound);
    388395                                areas.put(id, area);
     396                        } else {
     397                                feature.area = 0;
    389398                        }
    390399                        break;
    391400                case AREA:
     401                        Bound bound = null;
    392402                        Area area = new Area();
    393403                        ArrayList<Long> role = outers;
     
    397407                                        long node1 = edge.first;
    398408                                        long node2 = edge.last;
    399                                         Bound bound = new Bound(new Side(edge, edge.forward), (role == outers));
     409                                        bound = new Bound(new Side(edge, edge.forward), (role == outers));
    400410                                        if (node1 != node2) {
    401411                                                for (ListIterator<Long> it = role.listIterator(0); it.hasNext();) {
     
    417427                                }
    418428                                if (role == outers) {
     429                                        feature.length = calcLength(bound);
     430                                        feature.area = calcArea(bound);
    419431                                        role = inners;
    420432                                } else {
     
    435447        }
    436448
    437         public double signedArea(Bound bound) {
     449        double signedArea(Bound bound) {
    438450                Snode node;
    439451                double lat, lon, llon, llat;
     
    449461                        sigma += (lon * Math.sin(llat)) - (llon * Math.sin(lat));
    450462                }
    451                 return sigma;
     463                return sigma / 2.0;
    452464        }
    453465
     
    457469
    458470        public double calcArea(Bound bound) {
    459                 return Math.abs(signedArea(bound)) * 3444 * 3444 / 2.0;
     471                return Math.abs(signedArea(bound)) * 3444 * 3444;
     472        }
     473
     474        public double calcLength(Bound bound) {
     475                Snode node;
     476                double lat, lon, llon, llat;
     477                lat = lon = llon = llat = 0;
     478                double sigma = 0;
     479                BoundIterator it = new BoundIterator(bound);
     480                if (it.hasNext()) {
     481                        node = it.next();
     482                        lat = node.lat;
     483                        lon = node.lon;
     484                        while (it.hasNext()) {
     485                                llon = lon;
     486                                llat = lat;
     487                                node = it.next();
     488                                lat = node.lat;
     489                                lon = node.lon;
     490                                sigma += Math.acos(Math.sin(lat) * Math.sin(llat) + Math.cos(lat) * Math.cos(llat) * Math.cos(llon - lon));
     491                        }
     492                }
     493                return sigma * 3444;
    460494        }
    461495
Note: See TracChangeset for help on using the changeset viewer.