View Javadoc

1   /**
2    * 
3    */
4   package de.tivsource.page.helper.osm;
5   
6   import java.awt.image.BufferedImage;
7   import java.io.File;
8   import java.io.IOException;
9   import java.net.URL;
10  import java.nio.file.Files;
11  import java.nio.file.Path;
12  import java.nio.file.Paths;
13  import java.util.HashMap;
14  import java.util.Iterator;
15  import java.util.Map;
16  
17  import javax.imageio.ImageIO;
18  
19  /**
20   * @author Marc Michele
21   *
22   */
23  public class CreateOpenStreetMapCache {
24  
25  	private static final String HTDOCS = "/var/www/html/osmcache/";
26  	
27  	private String uuid;
28  
29  	private String centerLatitude;
30  
31  	private String centerLongitude;
32  	
33  	// <struts:property value="latitude" />,<struts:property value="longitude" />
34  	// <struts:property value="latitude" />,<struts:property value="longitude" />
35  	private String url = "http://staticmap.openstreetmap.de/staticmap.php?center=";
36  	private String urlParameter1 = "&zoom=18&size=";
37  	private String urlParameter2 = "&maptype=mapnik&markers=";
38  	private String urlParameter3 = ",lightblue";
39  
40  	private Map<String, String> widthMap;
41  
42  	public CreateOpenStreetMapCache(String uuid, String latitude,
43  			String longitude) {
44  		super();
45  		widthMap = new HashMap<String, String>();
46  		initWidth();
47  		this.uuid = uuid;
48  		this.centerLatitude = latitude;
49  		this.centerLongitude = longitude;
50  	}
51  
52      public void generate() {
53      	Iterator<String> keys = widthMap.keySet().iterator();
54  		while(keys.hasNext()) {
55  			String key = keys.next();
56  			loadImages(
57  					constructUrl(widthMap.get(key)), 
58  					constructPath(key)
59  					);
60  		}
61  	}// Ende generate()
62  
63      private void initWidth() {
64      	widthMap.put("wdefault", "250x230");
65      	widthMap.put("w0201",    "250x250");
66      	widthMap.put("w0581",    "230x465");
67      	widthMap.put("w0619",    "232x450");
68      	widthMap.put("w0641",    "270x467");
69      	widthMap.put("w0701",    "300x450");
70      	widthMap.put("w0901",    "300x465");
71      	widthMap.put("w0951",    "310x450");
72      	widthMap.put("w1001",    "340x452");
73      	widthMap.put("w1101",    "440x455");
74      	widthMap.put("w1291",    "490x455");
75      	widthMap.put("w1321",    "540x455");
76      	widthMap.put("w1401",    "590x455");
77      	widthMap.put("w1501",    "640x458");
78      	widthMap.put("w1701",    "740x458");
79      }
80  
81      private void loadImages(String urlString, String filePath) {
82          BufferedImage image =null;
83          try {
84          	URL url = new URL(urlString);
85              // Lade das Bild
86              image = ImageIO.read(url);
87  
88              Path picturePath = Paths.get(filePath);
89  
90  			if (Files.exists(picturePath) && !Files.isDirectory(picturePath)
91  					&& Files.isRegularFile(picturePath)) {
92  				// Lösche die Datei
93              	Files.delete(picturePath);
94              	// Speichere die Datei
95              	ImageIO.write(image, "png",new File(filePath));
96              } else if(Files.notExists(picturePath)) {
97              	// Speichere die Datei
98              	ImageIO.write(image, "png",new File(filePath));
99              }
100 
101         } catch(IOException e) {
102 	            e.printStackTrace();
103 	    }
104     }
105     
106     private String constructUrl(String size) {
107     	StringBuffer stringBuffer = new StringBuffer();
108     	stringBuffer.append(url);
109     	stringBuffer.append(centerLatitude);
110     	stringBuffer.append(",");
111     	stringBuffer.append(centerLongitude);
112     	stringBuffer.append(urlParameter1);
113     	stringBuffer.append(size);
114     	stringBuffer.append(urlParameter2);
115     	stringBuffer.append(centerLatitude);
116     	stringBuffer.append(",");
117     	stringBuffer.append(centerLongitude);
118     	stringBuffer.append(urlParameter3);
119     	return stringBuffer.toString();
120     }
121 
122     private String constructPath(String width) {
123     	StringBuffer stringBuffer = new StringBuffer();
124     	stringBuffer.append(HTDOCS);
125     	stringBuffer.append(uuid);
126     	stringBuffer.append("_");
127     	stringBuffer.append(width);
128     	stringBuffer.append(".png");
129     	return stringBuffer.toString();
130     }
131 
132 }// Ende class