Changeset 15397 in josm for trunk


Ignore:
Timestamp:
2019-10-01T00:26:14+02:00 (5 years ago)
Author:
Don-vip
Message:

fix #18012 - create track segment names from gpx layer name if there is no 'name' attribute present in the actual gpx (patch by cmuelle8)

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/relation/ExportRelationToGpxAction.java

    r15121 r15397  
    166166                                .findFirst()
    167167                                .ifPresent(r -> {
    168                                     trkAttr.put("name", r.getName() != null ? r.getName() : r.getId());
     168                                    trkAttr.put("name", r.getName() != null ? r.getName() : Long.toString(r.getId()));
    169169                                    trkAttr.put("desc", tr("based on osm route relation data, timestamps are synthetic"));
    170170                                });
    171                         GpxData.ensureUniqueName(trkAttr, names);
     171                        GpxData.ensureUniqueName(trkAttr, names, (String) trkAttr.get("name"));
    172172                    }
    173173                    List<Node> ln = flat.get(i).getWay().getNodes();
  • trunk/src/org/openstreetmap/josm/data/gpx/GpxData.java

    r15121 r15397  
    438438     * @param attrs attributes of/for an gpx track, written to if the name appeared previously in {@code counts}.
    439439     * @param counts a {@code HashMap} of previously seen names, associated with their count.
     440     * @param srcLayerName Source layer name
    440441     * @return the unique name for the gpx track.
    441442     *
    442      * @since 13210
    443      */
    444     public static String ensureUniqueName(Map<String, Object> attrs, Map<String, Integer> counts) {
    445         String name = attrs.getOrDefault("name", "GPX split result").toString();
     443     * @since 15397
     444     */
     445    public static String ensureUniqueName(Map<String, Object> attrs, Map<String, Integer> counts, String srcLayerName) {
     446        String name = attrs.getOrDefault("name", srcLayerName).toString().replaceFirst(" #\\d+$", "");
    446447        Integer count = counts.getOrDefault(name, 0) + 1;
    447448        counts.put(name, count);
    448449
    449         attrs.put("name", MessageFormat.format("{0}{1}", name, (count > 1) ? " #"+count : ""));
     450        attrs.put("name", MessageFormat.format("{0}{1}", name, " #" + count));
    450451        return attrs.get("name").toString();
    451452    }
     
    455456     * Each segment will make up one individual track after this operation.
    456457     *
    457      * @since 13210
    458      */
    459     public synchronized void splitTrackSegmentsToTracks() {
     458     * @param srcLayerName Source layer name
     459     *
     460     * @since 15397
     461     */
     462    public synchronized void splitTrackSegmentsToTracks(String srcLayerName) {
    460463        final HashMap<String, Integer> counts = new HashMap<>();
    461464
     
    463466            .flatMap(trk -> trk.getSegments().stream().map(seg -> {
    464467                    HashMap<String, Object> attrs = new HashMap<>(trk.getAttributes());
    465                     ensureUniqueName(attrs, counts);
     468                    ensureUniqueName(attrs, counts, srcLayerName);
    466469                    return new ImmutableGpxTrack(Arrays.asList(seg), attrs);
    467470                }))
     
    479482     * is untouched as to preserve potential route or wpt parts.
    480483     *
    481      * @since 13210
    482      */
    483     public synchronized void splitTracksToLayers() {
     484     * @param srcLayerName Source layer name
     485     *
     486     * @since 15397
     487     */
     488    public synchronized void splitTracksToLayers(String srcLayerName) {
    484489        final HashMap<String, Integer> counts = new HashMap<>();
    485490
     
    490495                GpxData d = new GpxData();
    491496                d.addTrack(trk);
    492                 return new GpxLayer(d, ensureUniqueName(attrs, counts));
     497                return new GpxLayer(d, ensureUniqueName(attrs, counts, srcLayerName));
    493498            })
    494499            .forEachOrdered(layer -> MainApplication.getLayerManager().addLayer(layer));
     
    707712    * so we just ingore points from future and from year before 1970 in this method
    708713    * works correctly @since 5815
    709      * @return minimum and maximum dates in array of 2 elements
     714    * @return minimum and maximum dates in array of 2 elements
    710715    */
    711716    public synchronized Date[] getMinMaxTimeForAllTracks() {
  • trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java

    r15371 r15397  
    427427        @Override
    428428        public void actionPerformed(ActionEvent e) {
    429             layer.data.splitTrackSegmentsToTracks();
     429            layer.data.splitTrackSegmentsToTracks(!layer.getName().isEmpty() ? layer.getName() : "GPX split result");
    430430            layer.invalidate();
    431431        }
     
    460460        @Override
    461461        public void actionPerformed(ActionEvent e) {
    462             layer.data.splitTracksToLayers();
     462            layer.data.splitTracksToLayers(!layer.getName().isEmpty() ? layer.getName() : "GPX split result");
    463463            // layer is not modified by this action
    464464        }
Note: See TracChangeset for help on using the changeset viewer.