Ticket #14833: josm_keep_ids.patch

File josm_keep_ids.patch, 9.0 KB (added by Don-vip, 9 years ago)
  • src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java

     
    4848
    4949    private static final AtomicLong idCounter = new AtomicLong(0);
    5050
     51    /**
     52     * Generates a new primitive unique id.
     53     * @return new primitive unique (negative) id
     54     */
    5155    static long generateUniqueId() {
    5256        return idCounter.decrementAndGet();
    5357    }
    5458
    5559    /**
     60     * Returns the current primitive unique id.
     61     * @return the current primitive unique (negative) id (last generated)
     62     * @since xxx
     63     */
     64    public static long currentUniqueId() {
     65        return idCounter.get();
     66    }
     67
     68    /**
     69     * Advances the current primitive unique id to skip a range of values.
     70     * @param newId new unique id
     71     * @throws IllegalArgumentException if newId is greater than current unique id
     72     * @since xxx
     73     */
     74    public static void advanceUniqueId(long newId) {
     75        if (newId > currentUniqueId()) {
     76            throw new IllegalArgumentException("Cannot modify the id counter backwards");
     77        }
     78        idCounter.set(newId);
     79    }
     80
     81    /**
    5682     * This flag shows, that the properties have been changed by the user
    5783     * and on upload the object will be send to the server.
    5884     */
  • src/org/openstreetmap/josm/data/osm/DataSet.java

     
    233233            // and then get the cloned members
    234234            Collection<Relation> relations = copyFrom.getRelations();
    235235            for (Relation r : relations) {
    236                 Relation newRelation = new Relation(r, r.isNew());
     236                Relation newRelation = new Relation(r);
    237237                newRelation.setMembers(null);
    238238                primMap.put(r, newRelation);
    239239                addPrimitive(newRelation);
     
    250250                dataSources.add(new DataSource(source));
    251251            }
    252252            version = copyFrom.version;
     253            uploadPolicy = copyFrom.uploadPolicy;
    253254        } finally {
    254255            copyFrom.getReadLock().unlock();
    255256        }
  • src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java

     
    4242import org.openstreetmap.josm.data.preferences.Setting;
    4343import org.openstreetmap.josm.data.preferences.StringSetting;
    4444import org.openstreetmap.josm.gui.dialogs.LogShowDialog;
     45import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    4546import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting;
    4647import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
    4748import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
     
    5051import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    5152import org.openstreetmap.josm.gui.widgets.JosmTextField;
    5253import org.openstreetmap.josm.tools.GBC;
     54import org.openstreetmap.josm.tools.Territories;
    5355import org.openstreetmap.josm.tools.Utils;
    5456
    5557/**
     
    322324        }
    323325
    324326        menu.addSeparator();
     327        menu.add(new AbstractAction(tr("Edit boundaries database")) {
     328            @Override
     329            public void actionPerformed(ActionEvent ae) {
     330                Main.getLayerManager().addLayer(
     331                    new OsmDataLayer(Territories.getDataSet(), Territories.FILENAME, null));
     332            }
     333        });
     334        menu.addSeparator();
    325335        menu.add(getProfileMenu());
    326336        menu.addSeparator();
    327337        menu.add(new AbstractAction(tr("Reset preferences")) {
  • src/org/openstreetmap/josm/io/OsmReader.java

     
    1111import java.util.Collection;
    1212import java.util.List;
    1313import java.util.Objects;
     14import java.util.OptionalLong;
    1415import java.util.regex.Matcher;
    1516import java.util.regex.Pattern;
    1617
     
    3031import org.openstreetmap.josm.data.osm.DataSet.UploadPolicy;
    3132import org.openstreetmap.josm.data.osm.Node;
    3233import org.openstreetmap.josm.data.osm.NodeData;
     34import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3335import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    3436import org.openstreetmap.josm.data.osm.PrimitiveData;
    3537import org.openstreetmap.josm.data.osm.Relation;
     
    213215        jumpToEnd();
    214216    }
    215217
     218    @SuppressWarnings("unchecked")
     219    protected <T extends OsmPrimitive> T buildPrimitive(PrimitiveData pd) throws XMLStreamException {
     220        readCommon(pd);
     221        OsmPrimitive p;
     222        if (pd.getUniqueId() < AbstractPrimitive.currentUniqueId()) {
     223            p = pd.getType().newInstance(pd.getUniqueId(), true);
     224        } else {
     225            p = pd.getType().newVersionedInstance(pd.getId(), pd.getVersion());
     226        }
     227        p.setVisible(pd.isVisible());
     228        p.load(pd);
     229        externalIdMap.put(pd.getPrimitiveId(), p);
     230        return (T) p;
     231    }
     232
    216233    protected Node parseNode() throws XMLStreamException {
    217         NodeData nd = new NodeData();
     234        NodeData nd = new NodeData(0);
    218235        String lat = parser.getAttributeValue(null, "lat");
    219236        String lon = parser.getAttributeValue(null, "lon");
    220237        LatLon ll = null;
     
    226243                Main.trace(e);
    227244            }
    228245        }
    229         readCommon(nd);
     246        Node n = buildPrimitive(nd);
    230247        if (lat != null && lon != null && (ll == null || !ll.isValid())) {
    231248            throwException(tr("Illegal value for attributes ''lat'', ''lon'' on node with ID {0}. Got ''{1}'', ''{2}''.",
    232249                    Long.toString(nd.getId()), lat, lon));
    233250        }
    234         Node n = new Node(nd.getId(), nd.getVersion());
    235         n.setVisible(nd.isVisible());
    236         n.load(nd);
    237         externalIdMap.put(nd.getPrimitiveId(), n);
    238251        while (true) {
    239252            int event = parser.next();
    240253            if (event == XMLStreamConstants.START_ELEMENT) {
     
    249262    }
    250263
    251264    protected Way parseWay() throws XMLStreamException {
    252         WayData wd = new WayData();
    253         readCommon(wd);
    254         Way w = new Way(wd.getId(), wd.getVersion());
    255         w.setVisible(wd.isVisible());
    256         w.load(wd);
    257         externalIdMap.put(wd.getPrimitiveId(), w);
    258 
     265        WayData wd = new WayData(0);
     266        Way w = buildPrimitive(wd);
    259267        Collection<Long> nodeIds = new ArrayList<>();
    260268        while (true) {
    261269            int event = parser.next();
     
    299307    }
    300308
    301309    protected Relation parseRelation() throws XMLStreamException {
    302         RelationData rd = new RelationData();
    303         readCommon(rd);
    304         Relation r = new Relation(rd.getId(), rd.getVersion());
    305         r.setVisible(rd.isVisible());
    306         r.load(rd);
    307         externalIdMap.put(rd.getPrimitiveId(), r);
    308 
     310        RelationData rd = new RelationData(0);
     311        Relation r = buildPrimitive(rd);
    309312        Collection<RelationMemberData> members = new ArrayList<>();
    310313        while (true) {
    311314            int event = parser.next();
     
    647650        } catch (IOException e) {
    648651            throw new IllegalDataException(e);
    649652        } finally {
     653            OptionalLong minId = externalIdMap.values().stream().mapToLong(AbstractPrimitive::getUniqueId).min();
     654            if (minId.isPresent() && minId.getAsLong() < AbstractPrimitive.currentUniqueId()) {
     655                AbstractPrimitive.advanceUniqueId(minId.getAsLong());
     656            }
    650657            progressMonitor.finishTask();
    651658            progressMonitor.removeCancelListener(cancelListener);
    652659        }
  • src/org/openstreetmap/josm/tools/Territories.java

     
    2727 */
    2828public final class Territories {
    2929
     30    /** Internal OSM filename */
     31    public static final String FILENAME = "boundaries.osm";
     32
    3033    private static final String ISO3166_1 = "ISO3166-1:alpha2";
    3134    private static final String ISO3166_2 = "ISO3166-2";
    3235
     
    7881     */
    7982    public static synchronized void initialize() {
    8083        iso3166Cache = new HashMap<>();
    81         try (CachedFile cf = new CachedFile("resource://data/boundaries.osm");
     84        try (CachedFile cf = new CachedFile("resource://data/" + FILENAME);
    8285                InputStream is = cf.getInputStream()) {
    8386            dataSet = OsmReader.parseDataSet(is, null);
    8487            Collection<OsmPrimitive> candidates = new ArrayList<>(dataSet.getWays());