| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.io.imagery;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.image.BufferedImage;
|
|---|
| 5 | import java.io.ByteArrayInputStream;
|
|---|
| 6 | import java.io.ByteArrayOutputStream;
|
|---|
| 7 | import java.io.IOException;
|
|---|
| 8 | import java.net.URL;
|
|---|
| 9 | import java.text.MessageFormat;
|
|---|
| 10 | import java.util.ArrayList;
|
|---|
| 11 | import java.util.List;
|
|---|
| 12 | import java.util.StringTokenizer;
|
|---|
| 13 |
|
|---|
| 14 | import javax.imageio.ImageIO;
|
|---|
| 15 |
|
|---|
| 16 | import org.openstreetmap.josm.Main;
|
|---|
| 17 | import org.openstreetmap.josm.data.preferences.StringProperty;
|
|---|
| 18 | import org.openstreetmap.josm.gui.MapView;
|
|---|
| 19 | import org.openstreetmap.josm.gui.layer.WMSLayer;
|
|---|
| 20 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 21 |
|
|---|
| 22 | public class HTMLGrabber extends WMSGrabber {
|
|---|
| 23 | public static final StringProperty PROP_BROWSER = new StringProperty("imagery.wms.browser", "webkit-image {0}");
|
|---|
| 24 |
|
|---|
| 25 | public HTMLGrabber(MapView mv, WMSLayer layer, boolean localOnly) {
|
|---|
| 26 | super(mv, layer, localOnly);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | @Override
|
|---|
| 30 | protected BufferedImage grab(WMSRequest request, URL url, int attempt) throws IOException {
|
|---|
| 31 | String urlstring = url.toExternalForm();
|
|---|
| 32 |
|
|---|
| 33 | Main.info("Grabbing HTML " + (attempt > 1? "(attempt " + attempt + ") ":"") + url);
|
|---|
| 34 |
|
|---|
| 35 | List<String> cmdParams = new ArrayList<>();
|
|---|
| 36 | StringTokenizer st = new StringTokenizer(MessageFormat.format(PROP_BROWSER.get(), urlstring));
|
|---|
| 37 | while (st.hasMoreTokens()) {
|
|---|
| 38 | cmdParams.add(st.nextToken());
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | ProcessBuilder builder = new ProcessBuilder( cmdParams);
|
|---|
| 42 |
|
|---|
| 43 | Process browser;
|
|---|
| 44 | try {
|
|---|
| 45 | browser = builder.start();
|
|---|
| 46 | } catch (IOException ioe) {
|
|---|
| 47 | throw new IOException("Could not start browser. Please check that the executable path is correct.\n" + ioe.getMessage(), ioe);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|---|
| 51 | Utils.copyStream(browser.getInputStream(), baos);
|
|---|
| 52 | ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
|---|
| 53 | BufferedImage img = layer.normalizeImage(ImageIO.read(bais));
|
|---|
| 54 | bais.reset();
|
|---|
| 55 | layer.cache.saveToCache(layer.isOverlapEnabled()?img:null, bais, Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth);
|
|---|
| 56 |
|
|---|
| 57 | return img;
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|