1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.FileInputStream;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.InvalidClassException;
|
---|
10 | import java.io.ObjectInputStream;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
15 | import org.openstreetmap.josm.data.Preferences;
|
---|
16 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
17 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
18 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
|
---|
19 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
22 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Import a WMS layer from a serialized binary file previously exported via {@link WMSLayerExporter}.
|
---|
26 | * @since 5457
|
---|
27 | */
|
---|
28 | public class WMSLayerImporter extends FileImporter {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * The file filter used in "open" and "save" dialogs for WMS layers.
|
---|
32 | */
|
---|
33 | public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
|
---|
34 | "wms", "wms", tr("WMS Files (*.wms)"));
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructs a new {@code WMSLayerImporter}.
|
---|
38 | */
|
---|
39 | public WMSLayerImporter() {
|
---|
40 | super(FILE_FILTER);
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
45 | CheckParameterUtil.ensureParameterNotNull(file, "file");
|
---|
46 | final EastNorth zoomTo;
|
---|
47 | ImageryInfo info = null;
|
---|
48 | final ImageryLayer layer;
|
---|
49 |
|
---|
50 | try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
|
---|
51 | int sfv = ois.readInt();
|
---|
52 | if (sfv < 5) {
|
---|
53 | throw new InvalidClassException(tr("Unsupported WMS file version; found {0}, expected {1}", sfv, 5));
|
---|
54 | } else if (sfv == 5) {
|
---|
55 | ois.readInt(); // dax - not needed
|
---|
56 | ois.readInt(); // day - not needed
|
---|
57 | zoomTo = null;
|
---|
58 |
|
---|
59 | int imageSize = ois.readInt();
|
---|
60 | double pixelPerDegree = ois.readDouble();
|
---|
61 |
|
---|
62 | String name = (String) ois.readObject();
|
---|
63 | String extendedUrl = (String) ois.readObject();
|
---|
64 |
|
---|
65 | info = new ImageryInfo(name);
|
---|
66 | info.setExtendedUrl(extendedUrl);
|
---|
67 | info.setPixelPerDegree(pixelPerDegree);
|
---|
68 | info.setTileSize(imageSize);
|
---|
69 | } else if (sfv == WMSLayerExporter.CURRENT_FILE_VERSION) {
|
---|
70 | zoomTo = (EastNorth) ois.readObject();
|
---|
71 |
|
---|
72 | @SuppressWarnings("unchecked")
|
---|
73 | ImageryPreferenceEntry entry = Preferences.deserializeStruct(
|
---|
74 | (Map<String, String>) ois.readObject(),
|
---|
75 | ImageryPreferenceEntry.class);
|
---|
76 | info = new ImageryInfo(entry);
|
---|
77 | } else {
|
---|
78 | throw new InvalidClassException(tr("Unsupported WMS file version; found {0}, expected {1}", sfv, 6));
|
---|
79 | }
|
---|
80 | } catch (ClassNotFoundException e) {
|
---|
81 | throw new IllegalDataException(e);
|
---|
82 | }
|
---|
83 | layer = ImageryLayer.create(info);
|
---|
84 |
|
---|
85 |
|
---|
86 | // FIXME: remove UI stuff from IO subsystem
|
---|
87 | GuiHelper.runInEDT(() -> {
|
---|
88 | Main.getLayerManager().addLayer(layer);
|
---|
89 | if (zoomTo != null) {
|
---|
90 | Main.map.mapView.zoomTo(zoomTo);
|
---|
91 | }
|
---|
92 | });
|
---|
93 | }
|
---|
94 | }
|
---|