Source Code Cross Referenced for PrintListener.java in  » GIS » deegree » org » deegree » portal » standard » wms » control » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » deegree » org.deegree.portal.standard.wms.control 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/portal/standard/wms/control/PrintListener.java $
002:        /*----------------    FILE HEADER  ------------------------------------------
003:
004:         This file is part of deegree.
005:         Copyright (C) 2001-2008 by:
006:         EXSE, Department of Geography, University of Bonn
007:         http://www.giub.uni-bonn.de/deegree/
008:         lat/lon GmbH
009:         http://www.lat-lon.de
010:
011:         This library is free software; you can redistribute it and/or
012:         modify it under the terms of the GNU Lesser General Public
013:         License as published by the Free Software Foundation; either
014:         version 2.1 of the License, or (at your option) any later version.
015:
016:         This library is distributed in the hope that it will be useful,
017:         but WITHOUT ANY WARRANTY; without even the implied warranty of
018:         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019:         Lesser General Public License for more details.
020:
021:         You should have received a copy of the GNU Lesser General Public
022:         License along with this library; if not, write to the Free Software
023:         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024:
025:         Contact:
026:
027:         Andreas Poth
028:         lat/lon GmbH
029:         Aennchenstr. 19
030:         53177 Bonn
031:         Germany
032:         E-Mail: poth@lat-lon.de
033:
034:         Prof. Dr. Klaus Greve
035:         Department of Geography
036:         University of Bonn
037:         Meckenheimer Allee 166
038:         53115 Bonn
039:         Germany
040:         E-Mail: greve@giub.uni-bonn.de
041:
042:         ---------------------------------------------------------------------------*/
043:        package org.deegree.portal.standard.wms.control;
044:
045:        import java.awt.Color;
046:        import java.awt.Font;
047:        import java.awt.Graphics;
048:        import java.awt.Rectangle;
049:        import java.awt.geom.Rectangle2D;
050:        import java.awt.image.BufferedImage;
051:        import java.io.File;
052:        import java.io.FileOutputStream;
053:        import java.io.IOException;
054:        import java.io.InputStream;
055:        import java.io.UnsupportedEncodingException;
056:        import java.net.MalformedURLException;
057:        import java.net.URL;
058:        import java.net.URLDecoder;
059:        import java.util.ArrayList;
060:        import java.util.HashMap;
061:        import java.util.StringTokenizer;
062:
063:        import javax.imageio.ImageIO;
064:        import javax.servlet.http.HttpServletRequest;
065:        import javax.servlet.http.HttpSession;
066:
067:        import org.deegree.enterprise.control.FormEvent;
068:        import org.deegree.enterprise.control.RPCMember;
069:        import org.deegree.enterprise.control.RPCMethodCall;
070:        import org.deegree.enterprise.control.RPCParameter;
071:        import org.deegree.enterprise.control.RPCStruct;
072:        import org.deegree.enterprise.control.RPCWebEvent;
073:        import org.deegree.framework.log.ILogger;
074:        import org.deegree.framework.log.LoggerFactory;
075:        import org.deegree.framework.util.CharsetUtils;
076:        import org.deegree.framework.util.IDGenerator;
077:        import org.deegree.framework.util.ImageUtils;
078:        import org.deegree.framework.util.StringTools;
079:        import org.deegree.framework.xml.XMLParsingException;
080:        import org.deegree.model.crs.UnknownCRSException;
081:        import org.deegree.ogcwebservices.InconsistentRequestException;
082:        import org.deegree.ogcwebservices.OGCWebServiceException;
083:        import org.deegree.ogcwebservices.OWSUtils;
084:        import org.deegree.ogcwebservices.wms.operation.GetLegendGraphic;
085:        import org.deegree.ogcwebservices.wms.operation.GetMap;
086:        import org.deegree.portal.Constants;
087:        import org.deegree.portal.context.GeneralExtension;
088:        import org.deegree.portal.context.IOSettings;
089:        import org.deegree.portal.context.ViewContext;
090:
091:        /**
092:         * will be called if the client forces a print action. 
093:         * 
094:         * @deprecated use @see org.deegree.portal.common.control.AbstractSimplePrintListener
095:         *
096:         * @author <a href="mailto:lupp@lat-lon.de">Katharina Lupp</a>
097:         * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
098:         * @author last edited by: $Author: apoth $
099:         * 
100:         * @version $Revision: 9346 $ $Date: 2007-12-27 08:39:07 -0800 (Thu, 27 Dec 2007) $
101:         */
102:        @Deprecated
103:        public class PrintListener extends AbstractMapListener {
104:
105:            private static final ILogger LOG = LoggerFactory
106:                    .getLogger(PrintListener.class);
107:
108:            /*
109:             * the method will be called if a print action/event occurs.
110:             * 
111:             * (non-Javadoc)
112:             * @see org.deegree.enterprise.control.WebListener#actionPerformed(org.deegree.enterprise.control.FormEvent)
113:             */
114:            @Override
115:            public void actionPerformed(FormEvent event) {
116:
117:                super .actionPerformed(event);
118:
119:                RPCWebEvent rpc = (RPCWebEvent) event;
120:                RPCMethodCall mc = rpc.getRPCMethodCall();
121:                RPCParameter[] para = mc.getParameters();
122:                RPCStruct struct = (RPCStruct) para[0].getValue(); // paperFormat, resolution, orientation, format
123:                RPCStruct struct2 = (RPCStruct) para[1].getValue(); // wmsRequest_0 .. wmsRequest_i
124:
125:                HttpSession session = ((HttpServletRequest) this .getRequest())
126:                        .getSession(true);
127:                ViewContext vc = null;
128:                if (session.getAttribute(Constants.CURRENTMAPCONTEXT) != null) {
129:                    vc = (ViewContext) session
130:                            .getAttribute(Constants.CURRENTMAPCONTEXT);
131:                } else {
132:                    vc = (ViewContext) session
133:                            .getAttribute("DefaultMapContext");
134:                }
135:
136:                HashMap[] model = createWMSRequestModel(struct2);
137:                int[] imageSize = calcImageSize(struct, model[0]);
138:                int width = imageSize[0];
139:                //int height = imageSize[1];
140:                int xmap = imageSize[2];
141:                int ymap = imageSize[3];
142:                int mapStart = imageSize[4];
143:
144:                try {
145:                    GetLegendGraphic legendParam = getLegendRequestParameter();
146:                    //HashMap with legend
147:                    HashMap symbols = getLegend(struct, legendParam, model);
148:                    Rectangle rectLegend = calcLegendSize(struct, symbols);
149:                    Rectangle rectMap = calcMapSize(model, rectLegend, width);
150:                    //sets new size of map und creates the adequate BufferedImage
151:                    model = modifyModelSize(rectMap, model, xmap, ymap);
152:                    HashMap[] copy = createCopy(model);
153:                    BufferedImage biReq = getMap(copy);
154:
155:                    //creates BufferedImage with required size and creates graphic
156:                    BufferedImage bi = createBackgroundImage(rectMap,
157:                            rectLegend);
158:
159:                    bi = drawMapToBI(biReq, bi, mapStart);
160:                    bi = drawLegendToBI(symbols, bi, (mapStart + rectMap.width));
161:
162:                    saveImage(vc, struct, bi);
163:
164:                } catch (Exception e) {
165:                    e.printStackTrace();
166:                }
167:
168:            }
169:
170:            /**
171:             * creates a background BufferedImage for the attributes (map, legend, scalebar)
172:             *
173:             * @param rectMap
174:             * @param rectLegend
175:             * @return
176:             */
177:            private BufferedImage createBackgroundImage(Rectangle rectMap,
178:                    Rectangle rectLegend) {
179:                BufferedImage bi = null;
180:
181:                if (rectMap.height > rectLegend.height) {
182:                    bi = new BufferedImage(rectMap.width + rectLegend.width
183:                            + 40, rectMap.height + 100,
184:                            BufferedImage.TYPE_INT_RGB);
185:                } else {
186:                    bi = new BufferedImage(rectMap.width + rectLegend.width
187:                            + 40, rectLegend.height + 100,
188:                            BufferedImage.TYPE_INT_RGB);
189:                }
190:
191:                Graphics g = bi.getGraphics();
192:                g.setColor(Color.WHITE);
193:                g.fillRect(1, 1, bi.getWidth() - 2, bi.getHeight() - 2);
194:                g.dispose();
195:
196:                return bi;
197:            }
198:
199:            /**
200:             * creates copy of the model
201:             *
202:             * @param model
203:             * @return the copy of the model
204:             */
205:            private HashMap[] createCopy(HashMap[] model) {
206:                HashMap[] copy = new HashMap[model.length];
207:                synchronized (this ) {
208:                    for (int i = 0; i < model.length; i++) {
209:                        copy[i] = (HashMap) model[i].clone();
210:                    }
211:                }
212:
213:                return copy;
214:            }
215:
216:            /**
217:             * creates model with WMS request from the RPCStruct request parameter.
218:             *
219:             * @param struc
220:             * @return
221:             */
222:            private HashMap[] createWMSRequestModel(RPCStruct struc) {
223:
224:                RPCMember[] member = struc.getMembers();
225:                HashMap<String, String>[] getMR = new HashMap[member.length];
226:
227:                String request = "";
228:                //for ( int i = 0; i < member.length; i++ ) {
229:                for (int i = member.length - 1; i >= 0; i--) {
230:                    request = (String) member[i].getValue();
231:                    getMR[i] = toMap(request);
232:                    StringTokenizer st = new StringTokenizer(request, "?");
233:                    getMR[i].put("URL", st.nextToken());
234:                }
235:                return getMR;
236:            }
237:
238:            /**
239:             * calculates images size in dependency on chosen paperFormat, resolution 
240:             * and orientation.
241:             *
242:             * @param struct
243:             * @param model
244:             * @return
245:             */
246:            private int[] calcImageSize(RPCStruct struct, HashMap model) {
247:                double width = 0;
248:                double height = 0;
249:                double mapSize = 0;
250:
251:                double mapWI = Double.parseDouble((String) model.get("WIDTH"));
252:                double mapHE = Double.parseDouble((String) model.get("HEIGHT"));
253:
254:                String paperFormat = (String) struct.getMember("paperFormat")
255:                        .getValue();
256:                String resolution = (String) struct.getMember("resolution")
257:                        .getValue();
258:                String orientation = (String) struct.getMember("orientation")
259:                        .getValue();
260:
261:                if (paperFormat.equals("A4")) {
262:                    if (orientation.equals("hoch")) {
263:                        width = 8.2;
264:                        height = 11.6;
265:                        mapSize = 6.5; //legend beside
266:                    } else {
267:                        width = 11.6;
268:                        height = 8.2;
269:                        mapSize = 9.5;
270:                    }
271:                } else if (paperFormat.equals("A3")) {
272:                    if (orientation.equals("hoch")) {
273:                        width = 11.6;
274:                        height = 16.5;
275:                        mapSize = 9.8; //legend beside
276:                    } else {
277:                        width = 16.5;
278:                        height = 11.6;
279:                        mapSize = 14;
280:                    }
281:                } else if (paperFormat.equals("A5")) {
282:                    if (orientation.equals("hoch")) {
283:                        width = 5.8;
284:                        height = 8.2;
285:                        mapSize = 4.1; //legend beside
286:                    } else {
287:                        width = 8.2;
288:                        height = 5.8;
289:                        mapSize = 6.5;
290:                    }
291:                }
292:
293:                width = width * Double.parseDouble(resolution);
294:                height = height * Double.parseDouble(resolution);
295:                mapSize = mapSize * Double.parseDouble(resolution);
296:
297:                int xmap = (int) Math.round(mapSize);
298:                double fac = mapWI / mapHE;
299:                int ymap = (int) Math.round(mapSize / fac);
300:                int mapStart = 45;
301:                int[] imagesize = new int[] { (int) Math.round(width),
302:                        (int) Math.round(height), xmap, ymap, mapStart };
303:
304:                return imagesize;
305:            }
306:
307:            /**
308:             * calculates size (width and height) of legend depending on 
309:             * chosen paperFormat and resolution.
310:             *
311:             * @param struct
312:             * @param map
313:             * @return
314:             */
315:            private Rectangle calcLegendSize(RPCStruct struct, HashMap map) {
316:
317:                String paperFormat = (String) struct.getMember("paperFormat")
318:                        .getValue();
319:                String resolution = (String) struct.getMember("resolution")
320:                        .getValue();
321:
322:                String[] layers = (String[]) map.get("NAMES");
323:                BufferedImage[] legs = (BufferedImage[]) map.get("IMAGES");
324:
325:                int w = 0;
326:                int h = 0;
327:                int size = 12;
328:                double tmph1 = 0;
329:                double tmph2 = 0;
330:
331:                if (paperFormat.equals("A4")) {
332:                    tmph1 = size / 3d;
333:                } else if (paperFormat.equals("A3")) {
334:                    tmph1 += 2 * size / 3;
335:                }
336:
337:                double res = Double.parseDouble(resolution);
338:                tmph2 = ((res - 150) / 150d) * size;
339:                size += (int) Math.round(tmph2 + tmph1);
340:                for (int i = 0; i < layers.length; i++) {
341:                    h += (legs[i].getHeight() + 6);
342:
343:                    Graphics g = legs[i].getGraphics();
344:                    Font f = new Font(g.getFont().getFontName(), g.getFont()
345:                            .getStyle(), size);
346:                    g.setFont(f);
347:                    Rectangle2D rect = g.getFontMetrics().getStringBounds(
348:                            layers[i], g);
349:                    g.dispose();
350:
351:                    if (rect.getWidth() > w) {
352:                        w = (int) rect.getWidth();
353:                    }
354:                }
355:                w += 150;
356:
357:                return new Rectangle(w, h);
358:            }
359:
360:            /**
361:             * calculates map size in dependency of the chosen paperFormat and resolution. 
362:             *
363:             * @param model
364:             * @param rectLegend
365:             * @param width
366:             * @return
367:             */
368:            private Rectangle calcMapSize(HashMap[] model,
369:                    Rectangle rectLegend, int width) {
370:                int w = width - rectLegend.width - 20;
371:                double wi = Double.parseDouble((String) model[0].get("WIDTH"));
372:                double he = Double.parseDouble((String) model[0].get("HEIGHT"));
373:                double fac = he / wi;
374:                int h = (int) Math.round(fac * w);
375:
376:                return new Rectangle(w, h);
377:            }
378:
379:            /**
380:             * draws legend with symbols and name of the layers to the background BufferedImage.
381:             *
382:             * @param map
383:             * @param bi
384:             * @param start
385:             * @return
386:             */
387:            private BufferedImage drawLegendToBI(HashMap map, BufferedImage bi,
388:                    int start) {
389:
390:                int h = 5;
391:                Graphics g = bi.getGraphics();
392:                g.setColor(Color.WHITE);
393:                String[] layers = (String[]) map.get("NAMES");
394:                BufferedImage[] legs = (BufferedImage[]) map.get("IMAGES");
395:
396:                for (int i = layers.length - 1; i >= 0; i--) {
397:                    g.drawImage(legs[i], start + 10, h, null);
398:                    g.setColor(Color.BLACK);
399:
400:                    if (legs[i].getHeight() < 50) {
401:                        g.drawString(layers[i],
402:                                start + 25 + legs[i].getWidth(), h
403:                                        + (int) (legs[i].getHeight() / 1.2));
404:                    }
405:
406:                    h += (legs[i].getHeight() + 5);
407:                }
408:
409:                g.dispose();
410:                return bi;
411:            }
412:
413:            /**
414:             * modifies width and height in the WMSGetMapRequest
415:             * in dependency of the chosen paperFormat and resolution. 
416:             * New width and height are calculated in the method "calcMapSize".
417:             *
418:             * @param rectMap
419:             * @param model
420:             * @param width
421:             * @param height
422:             * @return
423:             */
424:            private HashMap<String, Object>[] modifyModelSize(
425:                    Rectangle rectMap, HashMap<String, Object>[] model,
426:                    int width, int height) {
427:                int w = rectMap.width;
428:                double fac = (double) height / (double) width;
429:                double h = fac * w;
430:
431:                for (int i = 0; i < model.length; i++) {
432:                    model[i].put("HEIGHT", "" + (int) Math.round(h));
433:                    model[i].put("WIDTH", "" + w);
434:                }
435:
436:                return model;
437:            }
438:
439:            /**
440:             * gets the map with corresponding mapsizes built-in the urls. 
441:             * @throws UnknownCRSException 
442:             *
443:             * @param model
444:             * @return
445:             * @throws MalformedURLException
446:             * @throws IOException
447:             * @throws XMLParsingException
448:             * @throws InconsistentRequestException
449:             * @throws OGCWebServiceException
450:             * @throws UnknownCRSException
451:             */
452:            private BufferedImage getMap(HashMap<String, String>[] model)
453:                    throws MalformedURLException, IOException,
454:                    XMLParsingException, InconsistentRequestException,
455:                    OGCWebServiceException {
456:                int w = Integer.parseInt(model[0].get("WIDTH"));
457:                int h = Integer.parseInt(model[0].get("HEIGHT"));
458:                BufferedImage bJ = new BufferedImage(w, h,
459:                        BufferedImage.TYPE_INT_RGB);
460:                Graphics g = bJ.getGraphics();
461:                g.setColor(Color.WHITE);
462:                g.fillRect(1, 1, bJ.getWidth() - 2, bJ.getHeight() - 2);
463:                g.dispose();
464:                GetMap gmr = null;
465:
466:                for (int i = model.length - 1; i >= 0; i--) {
467:                    String urls = OWSUtils.validateHTTPGetBaseURL(model[i]
468:                            .remove("URL"));
469:                    String s = URLDecoder.decode(model[i].get("FORMAT"),
470:                            CharsetUtils.getSystemCharset());
471:                    model[i].put("FORMAT", s);
472:                    model[i].put("ID", "1.1.1");
473:                    gmr = GetMap.create(model[i]);
474:                    /*
475:                     if ( sessionid != null ) {
476:                        urls = StringTools.concat( 1000, urls, "&sessionID=", sessionid.getValue(), '&' );
477:                     } 
478:                     */
479:                    URL url = new URL(urls + gmr.getRequestParameter());
480:                    InputStream is = url.openStream();
481:                    BufferedImage tmp = ImageIO.read(is);
482:                    is.close();
483:                    g = bJ.getGraphics();
484:                    g.drawImage(tmp, 0, 0, null);
485:                    g.dispose();
486:                }
487:
488:                return bJ;
489:            }
490:
491:            /**
492:             * draws the map to the background BufferedImage
493:             *
494:             * @param reqIm
495:             * @param bi
496:             * @param mapStart
497:             * @return
498:             */
499:            private BufferedImage drawMapToBI(BufferedImage reqIm,
500:                    BufferedImage bi, int mapStart) {
501:                Graphics g = bi.getGraphics();
502:                g.drawImage(reqIm, mapStart, mapStart, null);
503:                g.dispose();
504:
505:                return bi;
506:            }
507:
508:            /**
509:             * saves image with BufferedImage including map, legend and scalebar in 
510:             * chosen image format and directory set in mapcontext.xml file.
511:             * Via the attribute "ImageSource" the image can be called for.
512:             *
513:             * @param vc
514:             * @param struct
515:             * @param bg
516:             */
517:            private void saveImage(ViewContext vc, RPCStruct struct,
518:                    BufferedImage bg) {
519:                String format = (String) struct.getMember("format").getValue();
520:                format = format.substring(format.indexOf('/') + 1, format
521:                        .length());
522:                GeneralExtension ge = vc.getGeneral().getExtension();
523:                IOSettings ios = ge.getIOSettings();
524:                String dir = ios.getPrintDirectory().getDirectoryName();
525:
526:                long l = IDGenerator.getInstance().generateUniqueID() % 100;
527:                String file = "Map" + l + '.' + format;
528:
529:                try {
530:                    String outPut = new URL(dir).getFile() + '/' + file;
531:                    File f = new File(outPut);
532:                    f.deleteOnExit();
533:                    FileOutputStream fos = new FileOutputStream(f);
534:                    ImageUtils.saveImage(bg, fos, format, 1);
535:                } catch (Exception e) {
536:                    LOG.logError("Error occurred in saving image_: ", e);
537:                }
538:
539:                String access = OWSUtils.validateHTTPGetBaseURL(ios
540:                        .getPrintDirectory().getOnlineResource()
541:                        .toExternalForm())
542:                        + "request=download&filename=" + file;
543:
544:                this .getRequest().setAttribute("ImageSource", access);
545:                this .getRequest().setAttribute("ImageWidth",
546:                        new Integer(bg.getWidth()));
547:                this .getRequest().setAttribute("ImageHeight",
548:                        new Integer(bg.getHeight()));
549:
550:            }
551:
552:            /**
553:             * gets parameters of the LegendRequest
554:             *
555:             * @return
556:             * @throws InconsistentRequestException
557:             */
558:            private GetLegendGraphic getLegendRequestParameter()
559:                    throws InconsistentRequestException {
560:                HashMap<String, String> legend = toMap("VERSION=1.1.1&REQUEST=GetLegendGraphic&FORMAT=image/png&WIDTH=50&HEIGHT=50&"
561:                        + "EXCEPTIONS=application/vnd.ogc.se_inimage&LAYER=europe:major_rivers&STYLE=default&"
562:                        + "SLD=file:///C:/Projekte/UmweltInfo/deegreewms/WEB-INF/xml/styles.xml");
563:                return GetLegendGraphic.create(legend);
564:
565:            }
566:
567:            /**
568:             * gets legend with layers and styles. The size of the symbols is adjusted to the
569:             * chosen paperFomrat and resolution.
570:             *
571:             * @param struct
572:             * @param glr
573:             * @param model
574:             * @return
575:             */
576:            private HashMap<String, Object> getLegend(RPCStruct struct,
577:                    GetLegendGraphic glr, HashMap[] model) {
578:
579:                ArrayList<String> list1 = new ArrayList<String>();
580:                ArrayList<BufferedImage> list2 = new ArrayList<BufferedImage>();
581:
582:                String format = glr.getFormat();
583:                if (format.equals("image/jpg"))
584:                    format = "image/jpeg";
585:
586:                String legendURL = "";
587:
588:                String paperFormat = (String) struct.getMember("paperFormat")
589:                        .getValue();
590:                String resolution = (String) struct.getMember("resolution")
591:                        .getValue();
592:                int h = 15;
593:                int w = 15;
594:                double tmph1 = 0;
595:                double tmpw1 = 0;
596:                double tmph2 = 0;
597:                double tmpw2 = 0;
598:
599:                if (paperFormat.equals("A4")) {
600:                    tmph1 = h / 3d;
601:                    tmpw1 = w / 3d;
602:                } else if (paperFormat.equals("A3")) {
603:                    tmph1 = 2 * h / 3d;
604:                    tmpw1 = 2 * w / 3d;
605:                }
606:
607:                double res = Double.parseDouble(resolution);
608:                tmph2 = ((res - 150) / 150d) * h;
609:                tmpw2 = ((res - 150) / 150d) * w;
610:
611:                w += (int) Math.round(tmpw1 + tmpw2);
612:                h += (int) Math.round(tmph1 + tmph2);
613:
614:                for (int i = 0; i < model.length; i++) {
615:                    if (model[i].get("SLD") != null) {
616:                        continue;
617:                    }
618:                    String[] lays = StringTools.toArray((String) model[i]
619:                            .get("LAYERS"), ",", false);
620:
621:                    String style = (String) model[i].get("STYLES");
622:                    String[] styles = new String[lays.length];
623:                    for (int j = 0; j < styles.length; j++) {
624:                        styles[j] = "default";
625:                    }
626:
627:                    if (style != null && !style.trim().equals("")) {
628:                        //styles = StringTools.toArray(style,",",false);
629:                        if ((StringTools.toArray(style, ",", false)).length != 0) {
630:                            styles = StringTools.toArray(style, ",", false);
631:                        }
632:                    } else {
633:                        styles = new String[lays.length];
634:                        for (int j = 0; j < styles.length; j++) {
635:                            styles[j] = "default";
636:                        }
637:                    }
638:
639:                    if (styles == null) {
640:                        styles = new String[lays.length];
641:                        for (int j = 0; j < styles.length; j++) {
642:                            styles[j] = "default";
643:                        }
644:                    }
645:
646:                    for (int j = 0; j < lays.length; j++) {
647:                        String layer = lays[j];
648:
649:                        //added to prevent ArrayOutOfBoundsException
650:                        String stl = "default";
651:                        if (j < styles.length) {
652:                            stl = styles[j];
653:                        }
654:
655:                        legendURL = createLegendURL(w, h, layer, stl, format,
656:                                glr, model[i]);
657:                        BufferedImage legendGraphic = null;
658:                        try {
659:                            InputStream is = new URL(legendURL).openStream();
660:                            legendGraphic = ImageIO.read(is);
661:                            is.close();
662:                        } catch (Exception e) {
663:                            System.out.println("\n\nLegend graphic for layer '"
664:                                    + layer
665:                                    + "' is not available. Skipping.\n\n");
666:                            legendGraphic = null;
667:                            e.printStackTrace();
668:                        }
669:                        if (legendGraphic != null) {
670:                            list1.add(layer);
671:                            list2.add(legendGraphic);
672:                        }
673:                    }
674:                }
675:
676:                String[] layers = list1.toArray(new String[list1.size()]);
677:                BufferedImage[] legs = list2.toArray(new BufferedImage[list2
678:                        .size()]);
679:                HashMap<String, Object> map = new HashMap<String, Object>();
680:                map.put("NAMES", layers);
681:                map.put("IMAGES", legs);
682:
683:                return map;
684:            }
685:
686:            /**
687:             * creates legend URL with corresponding key values.
688:             * 
689:             * @param w
690:             * @param h
691:             * @param layer
692:             * @param style
693:             * @param format
694:             * @param glr
695:             * @param model
696:             * @return
697:             */
698:            private String createLegendURL(int w, int h, String layer,
699:                    String style, String format, GetLegendGraphic glr,
700:                    HashMap model) {
701:
702:                String s = null;
703:                try {
704:                    if ("default".equals(style)) {
705:                        style = "";
706:                    }
707:                    String url = OWSUtils.validateHTTPGetBaseURL((String) model
708:                            .get("URL"));
709:                    s = StringTools
710:                            .concat(
711:                                    500,
712:                                    url,
713:                                    "service=WMS",
714:                                    "&VERSION=",
715:                                    glr.getVersion(),
716:                                    "&REQUEST=GetLegendGraphic",
717:                                    "&FORMAT=",
718:                                    format,
719:                                    "&WIDTH=",
720:                                    w,
721:                                    "&HEIGHT=",
722:                                    h,
723:                                    "&EXCEPTIONS=application/vnd.ogc.se_inimage&LAYER=",
724:                                    URLDecoder.decode(layer, CharsetUtils
725:                                            .getSystemCharset()), "&STYLE=",
726:                                    style);
727:                    // insufficient information available for user restrictions
728:                    // TODO  
729:                    //HttpServletRequest req = (HttpServletRequest) getRequest();
730:                    //if ( req.getUserPrincipal() != null ) {
731:                    //    String user = req.getUserPrincipal().getName();
732:                    //    if ( user.indexOf( "\\" ) > 1 ) {
733:                    //        String[] us = StringTools.toArray( user, "\\", false );
734:                    //        user = us[us.length - 1];
735:                    //    }
736:                    //    s = StringTools.concat( 500, s, "&user=", user );
737:                    //}
738:
739:                } catch (UnsupportedEncodingException e) {
740:                    // should never happen
741:                    e.printStackTrace();
742:                }
743:                return s;
744:            }
745:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.