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