comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/map/PrintMap.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents 4a76da133144
children
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
1 package de.intevation.flys.artifacts.map;
2
3 import java.awt.Color;
4 import java.awt.Rectangle;
5
6 import java.io.File;
7 import java.io.IOException;
8
9 import java.awt.Graphics2D;
10 import java.awt.image.BufferedImage;
11
12 import java.util.List;
13
14 import java.net.URL;
15 import java.net.MalformedURLException;
16
17 import javax.imageio.ImageIO;
18
19
20 import org.geotools.data.ows.Layer;
21 import org.geotools.data.ows.WMSCapabilities;
22 import org.geotools.data.wms.WebMapServer;
23 import org.geotools.geometry.jts.ReferencedEnvelope;
24 import org.geotools.map.MapContext;
25 import org.geotools.map.WMSMapLayer;
26 import org.geotools.ows.ServiceException;
27 import org.geotools.renderer.lite.StreamingRenderer;
28 import org.geotools.renderer.GTRenderer;
29
30
31 public class PrintMap {
32
33 public static final String DEFAULT_WMS = "http://map1.naturschutz.rlp.de/service_lanis/mod_wms/wms_getmap.php?mapfile=group_gdide&REQUEST=GetCapabilities&SERVICE=WMS";
34 public static final String DEFAULT_OUTFILE = "~/map.jpeg";
35
36 public static final String MAPSERVER = System.getProperty("wms", DEFAULT_WMS);
37 public static final String MAP_IMAGE = System.getProperty("outfile", DEFAULT_OUTFILE);
38
39
40 public static void main(String[] args) {
41 System.out.println("-> start PrintMap");
42 System.out.println(" -> Print layers of WMS: " + MAPSERVER);
43
44 try {
45 WebMapServer server = getMapserver();
46 WMSMapLayer[] wmsLayer = getWMSLayers(server);
47
48 MapContext mapcontent = new MapContext( wmsLayer );
49 mapcontent.setTitle(" NEW MAP CONTENT TITLE ");
50
51 printMap(mapcontent);
52 }
53 catch (Exception e) {
54 e.printStackTrace();
55 }
56
57 System.out.println("-> finished PrintMap");
58 }
59
60
61 public static void printMap(MapContext map) throws Exception {
62 int imageWidth = 600;
63
64 GTRenderer renderer = new StreamingRenderer();
65 renderer.setContext(map);
66
67 Rectangle imageBounds = null;
68 ReferencedEnvelope mapBounds = null;
69
70 try {
71 mapBounds = map.getLayerBounds();
72 double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
73 imageBounds = new Rectangle(
74 0, 0,
75 imageWidth,
76 (int) Math.round(imageWidth * heightToWidth));
77
78 }
79 catch (Exception e) {
80 // failed to access map layers
81 throw new RuntimeException(e);
82 }
83
84 BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
85 Graphics2D gr = image.createGraphics();
86 gr.setPaint(Color.WHITE);
87 gr.fill(imageBounds);
88
89 try {
90 renderer.paint(gr, imageBounds, mapBounds);
91 File fileToSave = new File(MAP_IMAGE);
92 ImageIO.write(image, "jpeg", fileToSave);
93 }
94 catch (IOException e) {
95 throw new RuntimeException(e);
96 }
97 }
98
99
100 public static WebMapServer getMapserver() throws MalformedURLException, IOException, ServiceException {
101 return new WebMapServer(getServerUrl());
102 }
103
104
105 public static URL getServerUrl() throws MalformedURLException {
106 return new URL(MAPSERVER);
107 }
108
109
110 public static WMSMapLayer[] getWMSLayers(WebMapServer server) {
111 if (server == null) {
112 System.out.println("WebMapServer == null");
113 throw new RuntimeException("WebMapServer == null");
114 }
115
116 WMSCapabilities capabilities = server.getCapabilities();
117
118 List<Layer> layers = capabilities.getLayerList();
119 WMSMapLayer[] wmslayers = new WMSMapLayer[layers.size()];
120
121 for (int i = 0, L = layers.size(); i < L; i++) {
122 Layer l = layers.get(i);
123
124 System.out.println(" -> add layer: " + l);
125 wmslayers[i] = new WMSMapLayer(server, l);
126 }
127
128 return wmslayers;
129 }
130 }
131 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org