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