Ignore:
Timestamp:
2017-05-13T12:58:12+02:00 (3 years ago)
Author:
bastiK
Message:

see #14773 - improve session export/import for imagery layer offset data

Location:
trunk/src/org/openstreetmap/josm/io/session
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/session/ImagerySessionExporter.java

    r10578 r12134  
    1414import org.openstreetmap.josm.data.Preferences;
    1515import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
     16import org.openstreetmap.josm.data.imagery.OffsetBookmark;
    1617import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
    1718import org.openstreetmap.josm.gui.layer.ImageryLayer;
     
    8283        Map<String, String> data = new LinkedHashMap<>(Preferences.serializeStruct(e, ImageryPreferenceEntry.class));
    8384        if (layer instanceof AbstractTileSourceLayer) {
    84             AbstractTileSourceLayer<?> tsLayer = (AbstractTileSourceLayer<?>) layer;
    85             tsLayer.getDisplaySettings().storeTo(data);
     85            ((AbstractTileSourceLayer<?>) layer).getDisplaySettings().storeTo(data);
    8686        }
    87         for (Map.Entry<String, String> entry : data.entrySet()) {
    88             Element attrElem = support.createElement(entry.getKey());
    89             layerElem.appendChild(attrElem);
    90             attrElem.appendChild(support.createTextNode(entry.getValue()));
     87        addAttributes(layerElem, data, support);
     88        if (layer instanceof AbstractTileSourceLayer) {
     89            OffsetBookmark offset = ((AbstractTileSourceLayer<?>) layer).getDisplaySettings().getOffsetBookmark();
     90            if (offset != null) {
     91                Map<String, String> offsetProps = offset.toPropertiesMap();
     92                Element offsetEl = support.createElement("offset");
     93                layerElem.appendChild(offsetEl);
     94                addAttributes(offsetEl, offsetProps, support);
     95            }
    9196        }
    9297        return layerElem;
    9398    }
     99
     100    private void addAttributes(Element element, Map<String, String> props, ExportSupport support) {
     101        for (Map.Entry<String, String> entry : props.entrySet()) {
     102            Element attrElem = support.createElement(entry.getKey());
     103            element.appendChild(attrElem);
     104            attrElem.appendChild(support.createTextNode(entry.getValue()));
     105        }
     106    }
    94107}
  • trunk/src/org/openstreetmap/josm/io/session/ImagerySessionImporter.java

    r10571 r12134  
    1111import org.openstreetmap.josm.data.imagery.ImageryInfo;
    1212import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
     13import org.openstreetmap.josm.data.imagery.OffsetBookmark;
    1314import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
    1415import org.openstreetmap.josm.gui.layer.ImageryLayer;
     
    3637            throw new IllegalDataException(tr("Version ''{0}'' of meta data for imagery layer is not supported. Expected: 0.1", version));
    3738        }
     39        Map<String, String> attributes = readProperties(elem);
     40
     41        ImageryPreferenceEntry prefEntry = Preferences.deserializeStruct(attributes, ImageryPreferenceEntry.class);
     42        ImageryInfo info = new ImageryInfo(prefEntry);
     43        ImageryLayer layer = ImageryLayer.create(info);
     44        if (layer instanceof AbstractTileSourceLayer) {
     45            AbstractTileSourceLayer<?> tsLayer = (AbstractTileSourceLayer<?>) layer;
     46            tsLayer.getDisplaySettings().loadFrom(attributes);
     47            NodeList nodes = elem.getChildNodes();
     48            for (int i = 0; i < nodes.getLength(); ++i) {
     49                Node node = nodes.item(i);
     50                if (node.getNodeType() == Node.ELEMENT_NODE && "offset".equals(node.getLocalName())) {
     51                    Map<String, String> offsetAttributes = readProperties((Element) node);
     52                    OffsetBookmark offset = OffsetBookmark.fromPropertiesMap(offsetAttributes);
     53                    tsLayer.getDisplaySettings().setOffsetBookmark(offset);
     54                    break;
     55                }
     56            }
     57        }
     58        return layer;
     59    }
     60
     61    private Map<String, String> readProperties(Element elem) {
    3862        Map<String, String> attributes = new HashMap<>();
    39 
    4063        NodeList nodes = elem.getChildNodes();
    41 
    4264        for (int i = 0; i < nodes.getLength(); ++i) {
    4365            Node node = nodes.item(i);
    44             if (node.getNodeType() == Node.ELEMENT_NODE) {
     66            if (node.getNodeType() == Node.ELEMENT_NODE && node.getChildNodes().getLength() <= 1) {
    4567                Element e = (Element) node;
    4668                attributes.put(e.getTagName(), e.getTextContent());
    4769            }
    4870        }
    49         ImageryPreferenceEntry prefEntry = Preferences.deserializeStruct(attributes, ImageryPreferenceEntry.class);
    50         ImageryInfo i = new ImageryInfo(prefEntry);
    51         ImageryLayer layer = ImageryLayer.create(i);
    52         if (layer instanceof AbstractTileSourceLayer) {
    53             AbstractTileSourceLayer<?> tsLayer = (AbstractTileSourceLayer<?>) layer;
    54             tsLayer.getDisplaySettings().loadFrom(attributes);
    55         }
    56         return layer;
     71        return attributes;
    5772    }
    5873}
Note: See TracChangeset for help on using the changeset viewer.