1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.session;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.Preferences;
|
---|
11 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
12 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
|
---|
13 | import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
|
---|
14 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
15 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
17 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
18 | import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
|
---|
19 | import org.w3c.dom.Element;
|
---|
20 | import org.w3c.dom.Node;
|
---|
21 | import org.w3c.dom.NodeList;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Session importer for both TMSLayer and WMSLayer.
|
---|
25 | */
|
---|
26 | public class ImagerySessionImporter implements SessionLayerImporter {
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | public Layer load(Element elem, ImportSupport support, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
30 | String version = elem.getAttribute("version");
|
---|
31 | if (!"0.1".equals(version)) {
|
---|
32 | throw new IllegalDataException(tr("Version ''{0}'' of meta data for imagery layer is not supported. Expected: 0.1", version));
|
---|
33 | }
|
---|
34 | Map<String, String> attributes = new HashMap<>();
|
---|
35 |
|
---|
36 | NodeList nodes = elem.getChildNodes();
|
---|
37 |
|
---|
38 | for (int i = 0; i < nodes.getLength(); ++i) {
|
---|
39 | Node node = nodes.item(i);
|
---|
40 | if (node.getNodeType() == Node.ELEMENT_NODE) {
|
---|
41 | Element e = (Element) node;
|
---|
42 | attributes.put(e.getTagName(), e.getTextContent());
|
---|
43 | }
|
---|
44 | }
|
---|
45 | ImageryPreferenceEntry prefEntry = Preferences.deserializeStruct(attributes, ImageryPreferenceEntry.class);
|
---|
46 | ImageryInfo i = new ImageryInfo(prefEntry);
|
---|
47 | ImageryLayer layer = ImageryLayer.create(i);
|
---|
48 | if (layer instanceof AbstractTileSourceLayer) {
|
---|
49 | AbstractTileSourceLayer tsLayer = (AbstractTileSourceLayer) layer;
|
---|
50 | if (attributes.containsKey("automatic-downloading")) {
|
---|
51 | tsLayer.autoLoad = Boolean.valueOf(attributes.get("automatic-downloading"));
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (attributes.containsKey("automatically-change-resolution")) {
|
---|
55 | tsLayer.autoZoom = Boolean.valueOf(attributes.get("automatically-change-resolution"));
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (attributes.containsKey("show-errors")) {
|
---|
59 | tsLayer.showErrors = Boolean.valueOf(attributes.get("show-errors"));
|
---|
60 | }
|
---|
61 | }
|
---|
62 | if (attributes.containsKey("dx") && attributes.containsKey("dy")) {
|
---|
63 | layer.setOffset(Double.parseDouble(attributes.get("dx")), Double.parseDouble(attributes.get("dy")));
|
---|
64 | }
|
---|
65 | return layer;
|
---|
66 | }
|
---|
67 | }
|
---|