Source Code Cross Referenced for MapMerger.java in  » GIS » geonetwork » org » wfp » vam » intermap » kernel » map » 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 » geonetwork » org.wfp.vam.intermap.kernel.map 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //=============================================================================
002:        //===	Copyright (C) 2001-2007 Food and Agriculture Organization of the
003:        //===	United Nations (FAO-UN), United Nations World Food Programme (WFP)
004:        //===	and United Nations Environment Programme (UNEP)
005:        //===
006:        //===	This program is free software; you can redistribute it and/or modify
007:        //===	it under the terms of the GNU General Public License as published by
008:        //===	the Free Software Foundation; either version 2 of the License, or (at
009:        //===	your option) any later version.
010:        //===
011:        //===	This program is distributed in the hope that it will be useful, but
012:        //===	WITHOUT ANY WARRANTY; without even the implied warranty of
013:        //===	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014:        //===	General Public License for more details.
015:        //===
016:        //===	You should have received a copy of the GNU General Public License
017:        //===	along with this program; if not, write to the Free Software
018:        //===	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019:        //===
020:        //===	Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021:        //===	Rome - Italy. email: geonetwork@osgeo.org
022:        //==============================================================================
023:
024:        /**
025:         * A class representing a map, as a group of services. It provides methods
026:         * to add / remove services, and to set and change the bounding box.
027:         *
028:         * @author Stefano Giaccio, Emanuele Tajariol
029:         *
030:         */package org.wfp.vam.intermap.kernel.map;
031:
032:        import java.util.*;
033:
034:        import java.io.BufferedInputStream;
035:        import java.io.BufferedOutputStream;
036:        import java.io.File;
037:        import java.io.FileInputStream;
038:        import java.io.FileOutputStream;
039:        import java.text.DecimalFormat;
040:        import org.jdom.Element;
041:        import org.wfp.vam.intermap.http.ConcurrentHTTPTransactionHandler;
042:        import org.wfp.vam.intermap.http.cache.HttpGetFileCache;
043:        import org.wfp.vam.intermap.kernel.GlobalTempFiles;
044:        import org.wfp.vam.intermap.kernel.map.images.ImageMerger;
045:        import org.wfp.vam.intermap.kernel.map.mapServices.BoundingBox;
046:        import org.wfp.vam.intermap.kernel.map.mapServices.MapService;
047:        import org.wfp.vam.intermap.kernel.map.mapServices.wms.WmsService;
048:        import org.wfp.vam.intermap.kernel.map.mapServices.wms.schema.type.WMSLayer;
049:
050:        public class MapMerger {
051:            private static HttpGetFileCache cache;
052:            private static float defaultDPI = 96f;
053:            private float dpi = -1f;
054:
055:            private BoundingBox bBox = new BoundingBox(); // Image extent
056:
057:            private Map<Integer, Layer> _layers = new HashMap<Integer, Layer>();
058:
059:            private List<Integer> vRank = new ArrayList<Integer>(); // Service rank
060:            private int nextId = 1;
061:
062:            private int activeServiceId;
063:            private String imageName = null; // The name of the merge image file
064:            private String imagePath = null; // The path of the merge image file
065:            private float degScale; // The map scale
066:            private float distScale; // The map scale
067:            private Map<Integer, String> htErrors = new Hashtable<Integer, String>();
068:            private boolean reaspectWms = true;
069:
070:            public static void setCache(HttpGetFileCache cache) {
071:                MapMerger.cache = cache;
072:            }
073:
074:            public static void setDefaultDPI(float dpi) {
075:                defaultDPI = dpi;
076:            }
077:
078:            public void setDPI(float dpi) {
079:                this .dpi = dpi;
080:            }
081:
082:            public float getDPI() {
083:                return dpi == -1f ? defaultDPI : dpi;
084:            }
085:
086:            public void reaspectWms(boolean reaspect) {
087:                this .reaspectWms = reaspect;
088:            };
089:
090:            /** Sets the Map BoundingBox */
091:            public void setBoundingBox(BoundingBox bb) {
092:                bBox = bb;
093:            }
094:
095:            /** Returns the Map BoudingBox */
096:            public BoundingBox getBoundingBox() {
097:                return bBox;
098:            }
099:
100:            /** Sets the active layer inside a given servce */
101:            public void setActiveLayer(int service, int layer) throws Exception {
102:                //		if (! htServices.containsKey(new Integer(service)))
103:                if (!_layers.containsKey(service))
104:                    throw new Exception("");
105:                activeServiceId = service;
106:                getService(service).setActiveLayer(layer);
107:            }
108:
109:            /** Returns the active service id */
110:            public int getActiveServiceId() {
111:                return activeServiceId;
112:            }
113:
114:            /** Returns the active Layer id */
115:            public int getActiveLayerId() {
116:                //		System.out.println("this: " +  this);
117:                //		System.out.println("getActiveServiceId: " + getActiveServiceId());
118:                //		System.out.println("getActiveLayer: " + getService(getActiveServiceId()).getActiveLayer());
119:
120:                return getService(getActiveServiceId()).getActiveLayer();
121:            }
122:
123:            /** Adds a service on the top
124:             *
125:             * @return the id given to the service
126:             * */
127:            public int addService(MapService service) {
128:                //		System.out.println("service: " + service); // DEBUG
129:                if (_layers.isEmpty())
130:                    activeServiceId = nextId;
131:
132:                Integer id = new Integer(nextId++);
133:
134:                Layer layer = new Layer(service);
135:                _layers.put(id, layer);
136:
137:                vRank.add(0, id);
138:
139:                return id.intValue();
140:                //		System.out.println("htServices: " + htServices); // DEBUG
141:            }
142:
143:            /** Removes the service with the given id */
144:            public void delService(int id) throws Exception {
145:                _layers.remove(id);
146:                vRank.remove(new Integer(id)); // be careful: we're removing the id object (Integer); an int would be used as index
147:
148:                if (activeServiceId == id)
149:                    if (vRank.size() > 0) {
150:                        activeServiceId = vRank.get(0).intValue();
151:                        getService(activeServiceId).setActiveLayer(1);
152:                    } else {
153:                        activeServiceId = -1;
154:                    }
155:            }
156:
157:            /** Moves a service up */
158:            public void moveServiceUp(int id) {
159:                Integer t = new Integer(id);
160:                int pos = vRank.indexOf(t);
161:                if (pos > 0) {
162:                    vRank.remove(t);
163:                    vRank.add(pos - 1, t);
164:                }
165:            }
166:
167:            /** Moves a service down */
168:            public void moveServiceDown(int id) {
169:                Integer t = new Integer(id);
170:                int pos = vRank.indexOf(t);
171:                if (pos < vRank.size() - 1) {
172:                    vRank.remove(t);
173:                    vRank.add(pos + 1, t);
174:                }
175:            }
176:
177:            public void setServicesOrder(int[] order) {
178:                vRank.clear();
179:                for (int i = 0; i < order.length; i++)
180:                    vRank.add(new Integer(order[i]));
181:            }
182:
183:            public Element toElementSimple() {
184:                Element elServices = new Element("services");
185:
186:                // Add each contained service to the elServices, ordered by vRank
187:                for (int idx : vRank) {
188:                    Layer layer = _layers.get(idx);
189:                    MapService s = layer.getService();
190:
191:                    String legend = null;
192:                    try {
193:                        legend = s.getLegendUrl();
194:                    } catch (Exception e) {
195:                    }
196:
197:                    Element eLayer = new Element("layer").setAttribute("id",
198:                            "" + idx).setAttribute("title", s.getTitle())
199:                            .setAttribute("type", "" + s.getType())
200:                            .setAttribute("transparency",
201:                                    "" + layer.getIntTransparency())
202:                            .setAttribute("visible",
203:                                    layer.isVisible() ? "true" : "false");
204:
205:                    if (legend != null)
206:                        eLayer.setAttribute("legend", legend);
207:
208:                    if (s instanceof  WmsService) {
209:                        WmsService wm = (WmsService) s;
210:                        WMSLayer wlayer = wm.getWmsLayer();
211:
212:                        if (wlayer.getStyleSize() > 0)
213:                            eLayer.setAttribute("style", "true");
214:                    }
215:
216:                    elServices.addContent(eLayer);
217:                }
218:
219:                return elServices;
220:            }
221:
222:            /** Converts this object to a JDOM Element */
223:            public Element toElement() {
224:                Element elServices = new Element("services");
225:
226:                // Add each contained service to the elServices, ordered by vRank
227:                //		for (int i = 0; i < vRank.size(); i++) {
228:                //			MapService s = (MapService)htServices.get(vRank.get(i));
229:                //
230:                //			elServices.addContent(s.toElement()
231:                //									  .setAttribute("id", vRank.get(i) + ""));
232:                //		}
233:                for (int id : vRank) {
234:                    MapService s = _layers.get(id).getService();
235:                    elServices.addContent(s.toElement().setAttribute("id",
236:                            id + ""));
237:                }
238:
239:                DecimalFormat df = new DecimalFormat("0.0000000000");
240:                elServices.addContent(
241:                        new Element("activeLayer").setAttribute("service",
242:                                "" + activeServiceId).setAttribute("layer",
243:                                "" + getActiveLayerId())).addContent(
244:                        new Element("degScale").setText(df.format(degScale)))
245:                        .addContent(
246:                                new Element("distScale").setText(df
247:                                        .format(distScale)
248:                                        + "")).addContent(bBox.toElement())
249:                        .addContent(getStructTransparencies()).addContent(
250:                                getStructExpanded()).addContent(getErrors());
251:
252:                return elServices;
253:            }
254:
255:            public Iterable<Layer> getLayers() {
256:                return new Iterable<Layer>() {
257:                    public Iterator<Layer> iterator() {
258:                        return new Iterator<Layer>() {
259:                            private int i = 0;
260:                            private int last = vRank.size();
261:
262:                            public boolean hasNext() {
263:                                return i < last;
264:                            }
265:
266:                            public Layer next() {
267:                                return _layers.get(vRank.get(i++));
268:                            }
269:
270:                            public void remove() {
271:                                throw new UnsupportedOperationException();
272:                            }
273:                        };
274:                    }
275:                };
276:            }
277:
278:            /** Returns an Element containing the transparency value for each service */
279:            public Element getStructTransparencies() {
280:                Element elTransparency = new Element("transparency");
281:
282:                for (Integer id : _layers.keySet()) {
283:                    Layer layer = _layers.get(id);
284:
285:                    elTransparency
286:                            .addContent(new Element("service").setAttribute(
287:                                    "id", id.toString()).setAttribute(
288:                                    "transparency",
289:                                    "" + layer.getIntTransparency())
290:                                    .setAttribute("name",
291:                                            layer.getService().getName()));
292:                }
293:                return elTransparency;
294:            }
295:
296:            /** Returns the transparency of a given layer */
297:            public int getLayerTransparency(int id) {
298:                return _layers.get(id).getIntTransparency();
299:            }
300:
301:            public int getLayerTransparencyRanked(int id) {
302:                return _layers.get(vRank.get(id)).getIntTransparency();
303:            }
304:
305:            /** Sets the transparency value for a given service */
306:            public void setTransparency(int id, float transparency)
307:                    throws Exception {
308:                if (!_layers.containsKey(id))
309:                    throw new Exception("Illegal service id");
310:
311:                _layers.get(id).setTransparency(transparency);
312:            }
313:
314:            /** Expands a service in the layer frame (used for ArcIMS services only) */
315:            public void expandService(int id) throws Exception {
316:                if (!_layers.containsKey(id))
317:                    throw new Exception("Illegal service id");
318:
319:                _layers.get(id).setExpanded(true);
320:            }
321:
322:            /** Collapses a service in the layer frame (used for ArcIMS services only) */
323:            public void collapseService(int id) throws Exception {
324:                if (!_layers.containsKey(id))
325:                    throw new Exception("Illegal service id");
326:
327:                _layers.get(id).setExpanded(false);
328:            }
329:
330:            /** ??? Expands a service in the layer frame (used for ArcIMS services only) */
331:            public void showService(int id) throws Exception {
332:                if (!_layers.containsKey(id))
333:                    throw new Exception("Illegal service id");
334:
335:                _layers.get(id).setVisible(true);
336:            }
337:
338:            /** ??? Collapses a service in the layer frame (used for ArcIMS services only) */
339:            public void hideService(int id) throws Exception {
340:                if (!_layers.containsKey(id))
341:                    throw new Exception("Illegal service id");
342:
343:                _layers.get(id).setVisible(false);
344:            }
345:
346:            public boolean toggleVisibility(int id) throws Exception {
347:                if (!_layers.containsKey(id))
348:                    throw new Exception("Illegal service id");
349:
350:                boolean newVis = !_layers.get(id).isVisible();
351:                _layers.get(id).setVisible(newVis);
352:                return newVis;
353:            }
354:
355:            /** Get error messages from the remote servers */
356:            public Element getErrors() {
357:                Element errors = new Element("errors");
358:
359:                for (Integer id : htErrors.keySet()) {
360:                    errors.addContent(new Element("layer").setAttribute("id",
361:                            id.toString()).setAttribute("message",
362:                            htErrors.get(id)));
363:                }
364:
365:                return errors;
366:            }
367:
368:            /** Returns number of map services contained */
369:            public int size() {
370:                return _layers.size();
371:            }
372:
373:            /** Returns an Enumeration containing all the services */
374:            public Iterable<MapService> getServices() {
375:                return new Iterable<MapService>() {
376:                    public Iterator<MapService> iterator() {
377:                        return new Iterator<MapService>() {
378:                            private Iterator<Layer> _layerIterator = _layers
379:                                    .values().iterator();
380:
381:                            public boolean hasNext() {
382:                                return _layerIterator.hasNext();
383:                            }
384:
385:                            public MapService next() {
386:                                return _layerIterator.next().getService();
387:                            }
388:
389:                            public void remove() {
390:                                throw new UnsupportedOperationException();
391:                            }
392:                        };
393:                    }
394:                };
395:            }
396:
397:            /** Returns the MapService element with the given id*/
398:            public MapService getService(int id) {
399:                return _layers.get(id).getService();
400:            }
401:
402:            public MapService getServiceRanked(int rank) {
403:                return _layers.get(vRank.get(rank)).getService();
404:            }
405:
406:            //	public MapService getService(int id) { return (MapService)htServices.get(new Integer(id)); }
407:            //	public boolean isVisible(int id) { return _layers.get(id).isVisible(); }
408:            public boolean isVisibleRanked(int rank) {
409:                return _layers.get(vRank.get(rank)).isVisible();
410:            }
411:
412:            /** Returns an element containing informations about the expanded or collapsed
413:             *  services
414:             */
415:            private Element getStructExpanded() {
416:                Element expanded = new Element("expandedServices");
417:
418:                for (Integer id : _layers.keySet()) {
419:                    Layer layer = _layers.get(id);
420:
421:                    expanded.addContent(new Element("service").setAttribute(
422:                            "id", id.toString()).setAttribute("expanded",
423:                            layer.isExpanded() ? "true" : "false"));
424:                }
425:
426:                return expanded;
427:            }
428:
429:            private void buildWmsRequests(int width, int height) {
430:                //		if (reaspectWms) bBox = reaspect(bBox, width, height); // reaspect the bounding box if no ArcIMS service did it
431:                //
432:                ////		MapService prevService = (MapService)htServices.get(vRank.get(0));
433:                //		MapService prevService = _layers.get(vRank.get(0)).getService();
434:                //		if (vRank.size() == 1)
435:                //		{
436:                //			try { vImageUrls.add(prevService.getImageUrl(bBox, width, height)); }
437:                //			catch (Exception e) { e.printStackTrace(); /* DEBUG */ }
438:                //			return;
439:                //		}
440:                //
441:                //		MapService service;
442:                //		Vector imageNames = new Vector();
443:                //		String serverURL = null;
444:                //
445:                //		int i = 0;
446:                //		boolean flag = false;
447:                //		while (!flag)
448:                //		{
449:                ////			prevService = (MapService)htServices.get(vRank.get(i));
450:                //			Layer layer = _layers.get(vRank.get(i));
451:                //			prevService = layer.getService();
452:                ////			if (  htShow.get(vRank.get(i)).equals(new Boolean(true)) ) {
453:                //			if (  layer.isVisible() ) {
454:                //				imageNames.add(prevService.getName());
455:                //				flag = true;
456:                //			}
457:                //			i++;
458:                //		}
459:                //
460:                //		while (i < vRank.size())
461:                //		{
462:                //			System.out.println(i + " - imageNames = " + imageNames);
463:                //
464:                //
465:                //			// get the map service
466:                ////			service = (MapService)htServices.get(vRank.get(i));
467:                //			Layer layer = _layers.get(vRank.get(i));
468:                //			service = layer.getService();
469:                //
470:                //			//System.out.println("i = " + i + " - vRank.size() = " + vRank.size());
471:                //			serverURL = service.getServerURL();
472:                //
473:                ////			if (htShow.get(vRank.get(i)).equals(new Boolean(true)))
474:                //			if ( layer.isVisible() )
475:                //			{
476:                //				if (serverURL.equals(prevService.getServerURL()))
477:                //				{
478:                //					//System.out.println(ms.getName());
479:                //					vImageUrls.add(null);
480:                //				}
481:                //				else
482:                //				{
483:                //					vImageUrls.add(prevService.getGroupImageUrl(bBox, width, height, imageNames));
484:                //					imageNames.clear();
485:                //				}
486:                //			}
487:                //			else
488:                //				vImageUrls.add(null);
489:                //
490:                //			imageNames.add(service.getName());
491:                //
492:                //
493:                //			if ((i == vRank.size() - 1) && (prevService.getServerURL().equals(serverURL)))
494:                //				vImageUrls.add(service.getGroupImageUrl(bBox, width, height, imageNames));
495:                //
496:                //			prevService = service;
497:                //
498:                //			i++;
499:                //		}
500:                //
501:                //		System.out.println("- imageNames = " + imageNames);
502:                //		System.out.println("- vImageUrls = " + vImageUrls);
503:                //
504:                //		i++;
505:            }
506:
507:            private List<String> sendGetImageRequests(int width, int height) {
508:                List<String> vImageUrls = new ArrayList<String>();
509:
510:                //		vImageUrls.clear();
511:                //buildWmsRequests(width, height); // TODO: fix that function and uncomment these two lines
512:                //if (false)
513:
514:                {
515:                    // Retrieve WMS services urls
516:                    if (reaspectWms)
517:                        bBox = reaspect(bBox, width, height); // Reaspect the bounding box if no ArcIMS service did it
518:
519:                    for (int id : vRank) // takes ids sorted by their ranks
520:                    {
521:                        Layer layer = _layers.get(id);
522:                        MapService ms = layer.getService();
523:                        if (layer.isVisible()) {
524:                            try {
525:                                vImageUrls.add(ms.getImageUrl(bBox, width,
526:                                        height));
527:                            } catch (Exception e) {
528:                                e.printStackTrace(); /* DEBUG */
529:                            }
530:                        } else
531:                            vImageUrls.add(null);
532:                    }
533:                }
534:
535:                // Set the scale
536:                degScale = Math.abs(getBoundingBox().getEast()
537:                        - getBoundingBox().getWest())
538:                        / width;
539:                distScale = (long) (423307109.727 * degScale / 96.0 * getDPI());
540:
541:                return vImageUrls;
542:            }
543:
544:            /**
545:             * <UL>
546:             *    <LI>Fetch images from remote servers</LI>
547:             *    <LI>Merge them together</LI>
548:             *    <LI>Create a temp file containing the merged image</LI>
549:             * </UL>
550:             *
551:             * @return the image file name, or an empty string when
552:             */
553:            public String merge(int width, int height) throws Exception {
554:                // Clear the error list
555:                htErrors.clear();
556:
557:                List<String> vImageUrls = sendGetImageRequests(width, height);
558:
559:                //		/*DEBUG*/System.err.println("MERGE");
560:                //		/*DEBUG*/System.err.println("  requrls:");
561:                //		/*DEBUG*/for(String url : vImageUrls)
562:                //		/*DEBUG*/	System.err.println("      " + url);
563:
564:                List<String> files = new Vector<String>();
565:
566:                ConcurrentHTTPTransactionHandler c = new ConcurrentHTTPTransactionHandler();// DEBUG
567:                c.setCache(cache); // DEBUG
568:                c.checkIfModified(false); //DEBUG
569:
570:                for (String url : vImageUrls) {
571:                    if (url != null) { // null if some error encountered (never with WMS servers)
572:                        c.register(url);
573:                    } else // Only for ArcIMS services
574:                    {// Tell the user
575:                    }
576:                }
577:                c.doTransactions();
578:
579:                //		System.out.println("vImageUrls.size() = " + vImageUrls.size()); // DEBUG
580:                List<Float> vTransparency = new ArrayList<Float>(); // Image transparency
581:
582:                for (int i = 0; i < vImageUrls.size(); i++) {
583:                    if (vImageUrls.get(i) != null) {
584:                        String path = c.getResponseFilePath(vImageUrls.get(i));
585:                        //			System.out.println("vImageUrls.get(i) = " + (String)vImageUrls.get(i));
586:                        //			System.out.println("path = " + path);
587:                        if (path != null) {
588:                            String contentType = c.getHeaderValue(vImageUrls
589:                                    .get(i), "content-type");
590:                            //				System.out.println("contentType = " + contentType); // DEBUG
591:                            if (contentType.startsWith("image")) {
592:                                files.add(path);
593:                                vTransparency.add(_layers.get(vRank.get(i))
594:                                        .getTransparency());
595:                                //System.out.println("file: " + path + "; transparency: " + htTransparency.get(vRank.get(i))); // DEBUG
596:                            }
597:                        }
598:                    }
599:                }
600:
601:                //		System.out.println("files.size() = " + files.size()); // DEBUG
602:
603:                //		imageName="";
604:
605:                if (files.size() > 1) {
606:                    // Merge the images
607:                    File output = GlobalTempFiles.getInstance().getFile();
608:                    String path = output.getPath();
609:                    //			System.out.println("vTransparency" + vTransparency); // DEBUG
610:                    Collections.reverse(files);
611:                    Collections.reverse(vTransparency);
612:                    ImageMerger.mergeAndSave(files, vTransparency, path,
613:                            ImageMerger.GIF);
614:                    imageName = output.getName();
615:                    imagePath = output.getPath();
616:                    //			System.out.println("\n\n\nimagePath: " + imagePath + "\n\n\n");
617:                    //			return(imageName);
618:                } else if (files.size() == 1) {
619:                    //			System.out.println("files.get(0) = " + files.get(0)); // DEBUG
620:                    File f = new File(files.get(0));
621:                    File out = GlobalTempFiles.getInstance().getFile();
622:                    //			System.out.println("out.getPath() = " + out.getPath()); // DEBUG
623:
624:                    BufferedInputStream is = new BufferedInputStream(
625:                            new FileInputStream(f));
626:                    BufferedOutputStream os = new BufferedOutputStream(
627:                            new FileOutputStream(out));
628:
629:                    byte buf[] = new byte[1024];
630:                    for (int nRead; (nRead = is.read(buf, 0, 1024)) > 0; os
631:                            .write(buf, 0, nRead))
632:                        ;
633:                    os.flush();
634:                    os.close();
635:                    is.close();
636:
637:                    imageName = out.getName();
638:                    imagePath = out.getPath();
639:                    //			return imageName;
640:                } else {
641:                    System.out.println("\n\n\nno change to imagename"); // DEBUG
642:                    //			return ""; // no change to imagename
643:                }
644:
645:                System.out.println("\n\n\nimagePath: " + imagePath + "\n\n\n"); // DEBUG
646:                return imageName;
647:            }
648:
649:            public String getImageName() {
650:                return imageName;
651:            }
652:
653:            public File getImageLocalPath() {
654:                return GlobalTempFiles.getInstance().getDir();
655:            }
656:
657:            //	public String getImagePath() { return imagePath; }
658:
659:            public String getDegScale() {
660:                DecimalFormat df = new DecimalFormat("###,###");
661:                return df.format(degScale);
662:            }
663:
664:            public String getDistScale() {
665:                DecimalFormat df = new DecimalFormat("###,###");
666:                return df.format(distScale);
667:            }
668:
669:            private static BoundingBox reaspect(BoundingBox bb, int w, int h) {
670:                // Get boundaries
671:                float north = bb.getNorth();
672:                float south = bb.getSouth();
673:                float east = bb.getEast();
674:                float west = bb.getWest();
675:
676:                float dx = Math.abs(east - west);
677:                float dy = Math.abs(north - south);
678:
679:                // Reaspect
680:                if ((w / dx) > (h / dy)) {
681:                    float d = dy * w / h - dx;
682:                    west -= d / 2;
683:                    east += d / 2;
684:                    System.out.println("REASPECTING - changing ratio WE += "
685:                            + d);
686:                } else if ((h / dy) > (w / dx)) {
687:                    float d = dx * h / w - dy;
688:                    south -= d / 2;
689:                    north += d / 2;
690:                    System.out.println("REASPECTING - changing ratio NS += "
691:                            + d);
692:                }
693:
694:                // Check for pan overflows: shift the map up or down if it can
695:                // N-S: limit navigation
696:                if (north > 90 && south > -90) {
697:                    float off = north - 90;
698:                    north = 90;
699:                    south -= off;
700:                    System.out.println("REASPECTING - shifting NS -= " + off);
701:                }
702:
703:                if (south < -90 && north < 90) {
704:                    float off = -90 - south;
705:                    south = -90;
706:                    north += off;
707:                    System.out.println("REASPECTING - shifting NS += " + off);
708:                }
709:
710:                // If the map has scrolled enough sideways, then roll the view
711:                // W-E: wrap navigation
712:                if (west > 180) {
713:                    east -= 180;
714:                    west -= 180;
715:                    System.out.println("REASPECTING - wrapping WE -= 180");
716:                }
717:
718:                if (east < -180) {
719:                    east += 180;
720:                    west += 180;
721:                    System.out.println("REASPECTING - wrapping WE += 180");
722:                }
723:
724:                // If the map is too much reduced, zoom it to fit the view
725:                if ((Math.abs(east - west)) > 360
726:                        && (Math.abs(north - south)) > 180) {
727:                    // Which side can be fully extended?
728:                    if (w / 360f > h / 180f) {
729:                        north = 90;
730:                        south = -90;
731:
732:                        float we = 180.0f / h * w;
733:                        west = -we / 2;
734:                        east = we / 2;
735:                        System.out.println("REASPECTING - NS fit, WE = " + we);
736:                    } else {
737:                        west = -180;
738:                        east = 180;
739:
740:                        float ns = 360.0f / w * h;
741:                        north = ns / 2;
742:                        south = -ns / 2;
743:                        System.out.println("REASPECTING - WE fit, NS = " + ns);
744:                    }
745:                }
746:                //		System.out.println("north = " + north + "; south = " + south + "; east = " + east + "; west = " + west);
747:
748:                return new BoundingBox(north, south, east, west);
749:            }
750:
751:            //	private class GetImageUrlThread extends Thread {
752:            //		private MapService service;
753:            //		private BoundingBox bb;
754:            //		private int width, height;
755:            //		private String url;
756:            //		private boolean serviceError = false; // Flag used to detect if a serviceException was thrown by getImageUrl
757:            //		private Element error;
758:            //
759:            //		public void run() { sendRequest(); }
760:            //
761:            //		private void sendRequest() {
762:            //			try {
763:            //				url = service.getImageUrl(bb, width, height);
764:            //			}
765:            //			catch (ServiceException e) {
766:            //				// The service returned an error message (ArcIMS services only)
767:            //				serviceError = true;
768:            //			}
769:            //			catch (Exception e) {
770:            //				// Generic error
771:            ////				e.printStackTrace(); // DEBUG
772:            //				serviceError = true;
773:            //				url = null;
774:            //			}
775:            //		}
776:            //
777:            //		public MapService getService() { return service; }
778:            //
779:            //		public void setParameters(MapService service, BoundingBox bb, int width, int height) {
780:            //			this.service = service;
781:            //			this.bb = bb;
782:            //			this.width = width;
783:            //			this.height = height;
784:            //		}
785:            //
786:            //		public String getUrl() throws ServiceException {
787:            //			if (serviceError) {
788:            //				throw new ServiceException();
789:            //			}
790:            //			return url;
791:            //		}
792:            //
793:            //		public Element getResponse() { return service.getLastResponse(); }
794:            //
795:            //	}
796:            //
797:            //	private class HttpThread extends Thread {
798:            //		private static final int BUF_LEN = 1024;
799:            //
800:            //		private String stUrl;
801:            //		private String path;
802:            //		private HttpClient c;
803:            //
804:            //		public void run() { connect(); }
805:            //
806:            //		public void setParameters(String url) { stUrl = url; }
807:            //
808:            //		public String getPath() { return path; }
809:            //
810:            //		public HttpClient getHttpClient() { return c; }
811:            //
812:            //		private void connect() {
813:            //			BufferedInputStream is = null;
814:            //			BufferedOutputStream os = null;
815:            //
816:            //			try {
817:            //				c = new HttpClient(stUrl);
818:            //				File tf = GlobalTempFiles.getInstance().getFile();
819:            //				c.getFile(tf);
820:            //				path = tf.getPath();
821:            //			}
822:            //			catch (Exception e) {
823:            //				path = null;
824:            //				e.printStackTrace(); // DEBUG
825:            //			}
826:            //			finally {
827:            //				// Close the streams
828:            //				try { is.close(); } catch (Exception e) {}
829:            //				try { os.close(); } catch (Exception e) {}
830:            //			}
831:            //		}
832:            //	}
833:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.