Ticket #6629: coloring.patch

File coloring.patch, 15.5 KB (added by akks, 15 years ago)

customize gpx track coloring

  • org/openstreetmap/josm/gui/layer/CustomizeColor.java

     
    6262
    6363    @Override
    6464    public void actionPerformed(ActionEvent e) {
    65         JColorChooser c = new JColorChooser(layers.get(0).getColor(false));
     65        Color cl=layers.get(0).getColor(false); if (cl==null) cl=Color.gray;
     66        JColorChooser c = new JColorChooser(cl);
    6667        Object[] options = new Object[]{tr("OK"), tr("Cancel"), tr("Default")};
    6768        int answer = JOptionPane.showOptionDialog(
    6869                Main.parent,
  • org/openstreetmap/josm/gui/layer/GpxLayer.java

     
    2525import java.util.Collection;
    2626import java.util.Collections;
    2727import java.util.Comparator;
     28import java.util.Date;
     29import java.util.HashMap;
    2830import java.util.LinkedList;
    2931import java.util.List;
     32import java.util.Map;
    3033import java.util.concurrent.Future;
    3134
    3235import javax.swing.AbstractAction;
    3336import javax.swing.Action;
    3437import javax.swing.Box;
    3538import javax.swing.ButtonGroup;
     39import javax.swing.ButtonModel;
    3640import javax.swing.Icon;
    3741import javax.swing.JFileChooser;
    3842import javax.swing.JLabel;
     
    229233
    230234    public colorModes getColorMode() {
    231235        try {
    232             return colorModes.values()[Main.pref.getInteger("draw.rawgps.colors", "layer "+getName(), 0)];
     236            int i;
     237            if (isLocalFile)
     238                i=Main.pref.getInteger("draw.rawgps.colors.local", "layer "+getName(), 0);
     239            else
     240                i=Main.pref.getInteger("draw.rawgps.colors", "layer "+getName(), 0);
     241            return colorModes.values()[i];
    233242        } catch (Exception e) {
    234243        }
    235244        return colorModes.none;
     
    249258                SeparatorLayerAction.INSTANCE,
    250259                new CustomizeColor(this),
    251260                new CustomizeLineDrawing(this),
     261                new CustomizeColorMode(this),
    252262                new ConvertToDataLayerAction(),
    253263                SeparatorLayerAction.INSTANCE,
    254264                new RenameLayerAction(getAssociatedFile(), this),
     
    262272                new LayerSaveAsAction(this),
    263273                new CustomizeColor(this),
    264274                new CustomizeLineDrawing(this),
     275                new CustomizeColorMode(this),
    265276                new ImportImages(),
    266277                new ImportAudio(),
    267278                new MarkersFromNamedPoins(),
     
    375386
    376387    // the different color modes
    377388    enum colorModes {
    378         none, velocity, dilution, direction
     389        none, velocity, dilution, direction, time
    379390    }
    380391
    381392    @Override
     
    447458         ********** STEP 2b - RE-COMPUTE CACHE DATA *********************
    448459         ****************************************************************/
    449460        if (!computeCacheInSync) { // don't compute if the cache is good
    450             Float minval = null;
    451             Float maxval = null;
     461            double minval = +1e10;
     462            double maxval = -1e10;
    452463            WayPoint oldWp = null;
    453464            if (colorModeDynamic) {
    454465                if (colored == colorModes.velocity) {
     
    461472                                    continue;
    462473                                }
    463474                                if (oldWp != null && trkPnt.time > oldWp.time) {
    464                                     Float vel = new Float(c.greatCircleDistance(oldWp.getCoor()) / (trkPnt.time - oldWp.time));
    465                                     if(maxval == null || vel > maxval) maxval = vel;
    466                                     if(minval == null || vel < minval) minval = vel;
     475                                    double vel = c.greatCircleDistance(oldWp.getCoor())
     476                                            / (trkPnt.time - oldWp.time);
     477                                    if(vel > maxval) maxval = vel;
     478                                    if(vel < minval) minval = vel;
    467479                                }
    468480                                oldWp = trkPnt;
    469481                            }
     
    475487                            for (WayPoint trkPnt : segment.getWayPoints()) {
    476488                                Object val = trkPnt.attr.get("hdop");
    477489                                if (val != null) {
    478                                     Float hdop = (Float) val;
    479                                     if(maxval == null || hdop > maxval) maxval = hdop;
    480                                     if(minval == null || hdop < minval) minval = hdop;
     490                                    double hdop = ((Float) val).doubleValue();
     491                                    if(hdop > maxval) maxval = hdop;
     492                                    if(hdop < minval) minval = hdop;
    481493                                }
    482494                            }
    483495                        }
     
    485497                }
    486498                oldWp = null;
    487499            }
     500            if (colored == colorModes.time) {
     501                    for (GpxTrack trk : data.tracks) {
     502                        for (GpxTrackSegment segment : trk.getSegments()) {
     503                            for (WayPoint trkPnt : segment.getWayPoints()) {
     504                               double t=trkPnt.time;
     505                               if (t==0) continue; // skip non-dated trackpoints
     506                               if(t > maxval) maxval = t;
     507                               if(t < minval) minval = t;
     508                            }
     509                        }
     510                    }
     511                }
     512           
    488513            for (GpxTrack trk : data.tracks) {
    489514                for (GpxTrackSegment segment : trk.getSegments()) {
    490515                    if (!forceLines) { // don't draw lines between segments, unless forced to
     
    498523                        trkPnt.customColoring = neutralColor;
    499524                        if(colored == colorModes.dilution && trkPnt.attr.get("hdop") != null) {
    500525                            float hdop = ((Float) trkPnt.attr.get("hdop")).floatValue();
    501                             int hdoplvl = Math.round(colorModeDynamic ? ((hdop-minval)*255/(maxval-minval))
     526                            int hdoplvl =(int) Math.round(colorModeDynamic ? ((hdop-minval)*255/(maxval-minval))
    502527                            : (hdop <= 0 ? 0 : hdop * hdopfactor));
    503528                            // High hdop is bad, but high values in colors are green.
    504529                            // Therefore inverse the logic
     
    507532                        }
    508533                        if (oldWp != null) {
    509534                            double dist = c.greatCircleDistance(oldWp.getCoor());
    510 
     535                            boolean noDraw=false;
    511536                            switch (colored) {
    512537                            case velocity:
    513538                                double dtime = trkPnt.time - oldWp.time;
    514539                                if(dtime > 0) {
    515540                                    float vel = (float) (dist / dtime);
    516                                     int velColor = Math.round(colorModeDynamic ? ((vel-minval)*255/(maxval-minval))
     541                                    int velColor =(int) Math.round(colorModeDynamic ? ((vel-minval)*255/(maxval-minval))
    517542                                    : (vel <= 0 ? 0 : vel / colorTracksTune * 255));
    518543                                    trkPnt.customColoring = colors[velColor > 255 ? 255 : velColor];
    519544                                } else {
     
    529554                                    trkPnt.customColoring = colors_cyclic[(int) (dirColor)];
    530555                                }
    531556                                break;
     557                            case time:
     558                                if (trkPnt.time>0){
     559                                    int tColor = (int) Math.round((trkPnt.time-minval)*255/(maxval-minval));
     560                                    trkPnt.customColoring = colors[tColor];
     561                                } else {
     562                                    trkPnt.customColoring = neutralColor;
     563                                }
     564                                break;
    532565                            }
    533 
    534                             if (maxLineLength == -1 || dist <= maxLineLength) {
     566                           
     567                            if (!noDraw && (maxLineLength == -1 || dist <= maxLineLength)) {
    535568                                trkPnt.drawLine = true;
    536569                                trkPnt.dir = (int) oldWp.getCoor().heading(trkPnt.getCoor());
    537570                            } else {
     
    14171450        }
    14181451    }
    14191452
     1453        private class CustomizeColorMode extends AbstractAction implements LayerAction, MultiLayerAction {
     1454        List<Layer> layers;
     1455
     1456        public CustomizeColorMode(List<Layer> l) {
     1457            this();
     1458            layers = l;
     1459        }
     1460
     1461        public CustomizeColorMode(Layer l) {
     1462            this();
     1463            layers = new LinkedList<Layer>();
     1464            layers.add(l);
     1465        }
     1466
     1467        private CustomizeColorMode() {
     1468            super(tr("Customize color mode"), ImageProvider.get("mapmode/addsegment"));
     1469        }
     1470
     1471        @Override
     1472        public boolean supportLayers(List<Layer> layers) {
     1473            for(Layer layer: layers) {
     1474                if(!(layer instanceof GpxLayer))
     1475                    return false;
     1476            }
     1477            return true;
     1478        }
     1479
     1480        @Override
     1481        public Component createMenuComponent() {
     1482            return new JMenuItem(this);
     1483        }
     1484
     1485        @Override
     1486        public Action getMultiLayerAction(List<Layer> layers) {
     1487            return new CustomizeLineDrawing(layers);
     1488        }
     1489
     1490        @Override
     1491        public void actionPerformed(ActionEvent e) {
     1492
     1493            JRadioButton[] r = new JRadioButton[6];
     1494            Map<ButtonModel,Integer> propVals = new HashMap<ButtonModel,Integer> ();;
     1495           
     1496            r[0] = new JRadioButton(tr("Use global settings."));
     1497            r[1] = new JRadioButton(tr("Single Color (can be customized for named layers)"));
     1498            r[2] = new JRadioButton(tr("Velocity (red = slow, green = fast)"));
     1499            r[3] = new JRadioButton(tr("Direction (red = west, yellow = north, green = east, blue = south)"));
     1500            r[4] = new JRadioButton(tr("Dilution of Position (red = high, green = low, if available)"));
     1501            r[5] = new JRadioButton(tr("Track date"));
     1502            propVals.put(r[1].getModel(), 0);
     1503            propVals.put(r[2].getModel(), 1);
     1504            propVals.put(r[4].getModel(), 2);
     1505            propVals.put(r[3].getModel(), 3);
     1506            propVals.put(r[5].getModel(), 4);
     1507            ButtonGroup group = new ButtonGroup();
     1508            Box panel = Box.createVerticalBox();
     1509            for (JRadioButton b : r) {
     1510                group.add(b);
     1511                panel.add(b);
     1512            }
     1513            String propbase = isLocalFile ? "draw.rawgps.colors.local" : "draw.rawgps.colors";
     1514            String propName = propbase + ".layer " + layers.get(0).getName();
     1515            if (Main.pref.hasKey(propName)) {
     1516                int v = Main.pref.getInteger(propName,-1);
     1517                for (int i=1;i<6;i++) {
     1518                    if (propVals.get(r[i].getModel()).intValue()==v)
     1519                            group.setSelected(r[i].getModel(), true);
     1520                }
     1521            } else {
     1522                group.setSelected(r[0].getModel(), true);
     1523            }
     1524            int answer = JOptionPane.showConfirmDialog(Main.parent, panel,
     1525                    tr("Select color mode"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
     1526            if (answer == JOptionPane.CANCEL_OPTION || answer == JOptionPane.CLOSED_OPTION) return;
     1527            for(Layer layer : layers) {
     1528                propName = propbase + ".layer " + layer.getName();
     1529                if (group.getSelection() == r[0].getModel()) {
     1530                    Main.pref.put(propName, null);
     1531                } else {
     1532                    Main.pref.putInteger(propName, propVals.get(group.getSelection()));
     1533                }
     1534            }
     1535            Main.map.repaint();
     1536        }
     1537    }
     1538
    14201539    private class MarkersFromNamedPoins extends AbstractAction {
    14211540
    14221541        public MarkersFromNamedPoins() {
  • org/openstreetmap/josm/gui/preferences/DrawingPreference.java

     
    4646    private JRadioButton colorTypeVelocity = new JRadioButton(tr("Velocity (red = slow, green = fast)"));
    4747    private JRadioButton colorTypeDirection = new JRadioButton(tr("Direction (red = west, yellow = north, green = east, blue = south)"));
    4848    private JRadioButton colorTypeDilution = new JRadioButton(tr("Dilution of Position (red = high, green = low, if available)"));
     49    private JRadioButton colorTypeTime = new JRadioButton(tr("Track date"));
    4950    private JRadioButton colorTypeNone = new JRadioButton(tr("Single Color (can be customized for named layers)"));
    5051    private JComboBox colorTypeVelocityTune = new JComboBox(new String[] {tr("Car"), tr("Bicycle"), tr("Foot")});
    5152    private JCheckBox directionHint = new JCheckBox(tr("Draw Direction Arrows"));
     
    173174        colorGroup.add(colorTypeVelocity);
    174175        colorGroup.add(colorTypeDirection);
    175176        colorGroup.add(colorTypeDilution);
     177        colorGroup.add(colorTypeTime);
    176178
    177179        colorTypeVelocity.addChangeListener(new ChangeListener(){
    178180            public void stateChanged(ChangeEvent e) {
     
    199201        case 3:
    200202            colorTypeDirection.setSelected(true);
    201203            break;
     204        case 4:
     205            colorTypeTime.setSelected(true);
     206            break;
    202207        }
    203208
    204209        colorTypeNone.setToolTipText(tr("All points and track segments will have the same color. Can be customized in Layer Manager."));
    205210        colorTypeVelocity.setToolTipText(tr("Colors points and track segments by velocity."));
    206211        colorTypeDirection.setToolTipText(tr("Colors points and track segments by direction."));
    207212        colorTypeDilution.setToolTipText(tr("Colors points and track segments by dilution of position (HDOP). Your capture device needs to log that information."));
     213        colorTypeTime.setToolTipText(tr("Colors points and track segments by its timestamp."));
    208214
    209215        // color Tracks by Velocity Tune
    210216        int ccts = Main.pref.getInteger("draw.rawgps.colorTracksTune", 45);
     
    220226        panel.add(colorTypeVelocityTune, GBC.eop().insets(5,0,0,5));
    221227        panel.add(colorTypeDirection, GBC.eol().insets(40,0,0,0));
    222228        panel.add(colorTypeDilution, GBC.eol().insets(40,0,0,0));
     229        panel.add(colorTypeTime, GBC.eol().insets(40,0,0,0));
     230       
    223231        colorDynamic.setToolTipText(tr("Draw direction arrows for lines, connecting GPS points."));
    224232        colorDynamic.setSelected(Main.pref.getBoolean("draw.rawgps.colors.dynamic", false));
    225233        colorDynamic.setEnabled(colorTypeVelocity.isSelected() || colorTypeDilution.isSelected());
     
    319327            Main.pref.putInteger("draw.rawgps.colors", 2);
    320328        } else if(colorTypeDirection.isSelected()) {
    321329            Main.pref.putInteger("draw.rawgps.colors", 3);
     330        } else if(colorTypeTime.isSelected()) {
     331            Main.pref.putInteger("draw.rawgps.colors", 4);
    322332        } else {
    323333            Main.pref.putInteger("draw.rawgps.colors", 0);
    324334        }