comparison src/java/de/intevation/mxd/writer/SymbolWriter.java @ 251:6b80e46b8f38

Added picture symbol support to the converter.
author raimund renkert <raimund.renkert@intevation.de>
date Fri, 12 Aug 2011 16:08:57 +0200
parents df4e0946ef02
children 8e5d67c45b64
comparison
equal deleted inserted replaced
250:d10fd4de02aa 251:6b80e46b8f38
33 import edu.umn.gis.mapscript.fontSetObj; 33 import edu.umn.gis.mapscript.fontSetObj;
34 import edu.umn.gis.mapscript.hashTableObj; 34 import edu.umn.gis.mapscript.hashTableObj;
35 import edu.umn.gis.mapscript.MS_SYMBOL_TYPE; 35 import edu.umn.gis.mapscript.MS_SYMBOL_TYPE;
36 36
37 import java.io.File; 37 import java.io.File;
38 import java.awt.Image;
39 import java.awt.image.BufferedImage;
40 import java.awt.image.DataBufferByte;
41 import java.awt.image.FilteredImageSource;
42 import java.awt.image.ImageFilter;
43 import java.awt.image.ImageProducer;
44 import java.awt.image.RGBImageFilter;
45 import java.awt.Toolkit;
46 import java.awt.Color;
47 import java.awt.Graphics2D;
48 import javax.imageio.ImageIO;
49 import java.io.ByteArrayInputStream;
50
51 import org.apache.commons.codec.binary.Base64;
38 52
39 /** 53 /**
40 * The interface to the mapfile writer. 54 * The interface to the mapfile writer.
41 * 55 *
42 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> 56 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
73 */ 87 */
74 public boolean write(Element symbolElement) { 88 public boolean write(Element symbolElement) {
75 logger.debug("write(Element)"); 89 logger.debug("write(Element)");
76 symbolSetObj symbolSet = map.getSymbolset(); 90 symbolSetObj symbolSet = map.getSymbolset();
77 91
78 String name = symbolElement.getAttribute("name");
79 symbolObj sym = symbolSet.getSymbolByName(name);
80 String symType = symbolElement.getAttribute("style"); 92 String symType = symbolElement.getAttribute("style");
81 String type = symbolElement.getAttribute("type"); 93 String type = symbolElement.getAttribute("type");
82 if(symType.equals("point")) { 94 if(symType.equals("point")) {
95 String name = symbolElement.getAttribute("name");
96 symbolObj sym = symbolSet.getSymbolByName(name);
83 writeSimple(sym); 97 writeSimple(sym);
84 } 98 }
85 else if (symType.equals("arrow")) { 99 else if (symType.equals("arrow")) {
100 String name = symbolElement.getAttribute("name");
101 symbolObj sym = symbolSet.getSymbolByName(name);
86 writeArrow(sym, symbolElement); 102 writeArrow(sym, symbolElement);
87 } 103 }
88 else if (symType.equals("char")) { 104 else if (symType.equals("char")) {
105 String name = symbolElement.getAttribute("name");
106 symbolObj sym = symbolSet.getSymbolByName(name);
107
89 int exists = symbolExists(symbolElement); 108 int exists = symbolExists(symbolElement);
90 if(exists == -1) { 109 if(exists == -1) {
91 String old = symbolElement.getAttribute("name"); 110 String old = symbolElement.getAttribute("name");
92 symbolElement.setAttribute( 111 symbolElement.setAttribute(
93 "name", 112 "name",
98 symbolElement.setAttribute( 117 symbolElement.setAttribute(
99 "name", 118 "name",
100 symbolSet.getSymbol(exists).getName()); 119 symbolSet.getSymbol(exists).getName());
101 } 120 }
102 } 121 }
103 else if (type.equals("line")) { 122 else if (type.equals("line") && !symType.equals("picture")) {
123 String name = symbolElement.getAttribute("name");
124 symbolObj sym = symbolSet.getSymbolByName(name);
104 writeHatch(sym); 125 writeHatch(sym);
126 }
127 else if(symType.equals("picture")) {
128 // Create the path to the picture.
129 int exists = symbolExists(symbolElement);
130 String n = symbolElement.getAttribute("name");
131 symbolElement.setAttribute(
132 "name",
133 n + symbolSet.getNumsymbols() + 1);
134 String path = this.map.getMappath();
135 if (path.endsWith(".map")) {
136 path = path.substring(0, path.lastIndexOf(File.separator) + 1);
137 path += symbolElement.getAttribute("name") + ".png";
138 }
139 else {
140 path += symbolElement.getAttribute("name") + ".png";
141 }
142 // Create a new symbol using the path as symbol name.
143 // This is a workarround to set the IMAGE attribute to the path
144 // since this attribute is immutable via mapscript.
145 symbolObj sym = symbolSet.getSymbolByName(path);
146 writePicture(sym, symbolElement, path);
105 } 147 }
106 else { 148 else {
107 return false; 149 return false;
108 } 150 }
109 151
191 symbol.setType(MS_SYMBOL_TYPE.MS_SYMBOL_TRUETYPE.swigValue()); 233 symbol.setType(MS_SYMBOL_TYPE.MS_SYMBOL_TRUETYPE.swigValue());
192 symbol.setAntialias(1); 234 symbol.setAntialias(1);
193 symbol.setCharacter("&#" + symbolElement.getAttribute("char") + ";"); 235 symbol.setCharacter("&#" + symbolElement.getAttribute("char") + ";");
194 } 236 }
195 237
196 /** 238
239 /**
240 * Create the image and a pixmap symbol.
241 *
242 * @param sym The symbol object.
243 * @param symElem The DOM element containing symbol informations.
244 */
245 private void writePicture(symbolObj sym, Element symElem, String path) {
246 logger.info("writePicture(symbolObj, Element)");
247 try {
248 // Create the image as png.
249 if(symElem.hasAttribute("picture")) {
250 Base64 decoder = new Base64();
251 // Decode the base64 string.
252 byte[] ba = decoder.decode(symElem.getAttribute("picture"));
253 ByteArrayInputStream ins = new ByteArrayInputStream(ba);
254 // Create temporary image from byte array.
255 BufferedImage bi = ImageIO.read(ins);
256 Color c = Color.decode(
257 symElem.getAttribute("transparent_color"));
258 // Make a color transparent.
259 Image im = colorToTransparent(bi, c);
260
261 // Save image as png.
262 BufferedImage dest = new BufferedImage(
263 bi.getWidth(),
264 bi.getHeight(),
265 BufferedImage.TYPE_INT_ARGB);
266 Graphics2D g2 = dest.createGraphics();
267 g2.drawImage(im, 0, 0, null);
268 g2.dispose();
269 File f = new File(path);
270 ImageIO.write(dest, "PNG", f);
271 }
272 // Set symbol attributes.
273 // Overwrite the symbol name conating the path to the image
274 // with the real name. This is part of the workarround(line 137).
275 sym.setName(symElem.getAttribute("name"));
276 sym.setType(MS_SYMBOL_TYPE.MS_SYMBOL_PIXMAP.swigValue());
277 }
278 catch(Exception e) {
279 logger.warn("Could not save image for pixmap symbol.");
280 }
281 }
282
283
284 /**
197 * Create a hatch symbol for polygon fill. 285 * Create a hatch symbol for polygon fill.
198 * 286 *
199 * @param symbol The symbol object. 287 * @param symbol The symbol object.
200 */ 288 */
201 private void writeHatch(symbolObj symbol) { 289 private void writeHatch(symbolObj symbol) {
210 */ 298 */
211 public boolean canWrite(String type) { 299 public boolean canWrite(String type) {
212 if (type.equals("point") || 300 if (type.equals("point") ||
213 type.equals("arrow") || 301 type.equals("arrow") ||
214 type.equals("char") || 302 type.equals("char") ||
215 type.equals("line")) { 303 type.equals("line") ||
304 type.equals("picture")) {
216 return true; 305 return true;
217 } 306 }
218 else { 307 else {
219 return false; 308 return false;
220 } 309 }
282 } 371 }
283 else if(stype == MS_SYMBOL_TYPE.MS_SYMBOL_HATCH.swigValue () && 372 else if(stype == MS_SYMBOL_TYPE.MS_SYMBOL_HATCH.swigValue () &&
284 etype.equals("hatch")) { 373 etype.equals("hatch")) {
285 return i; 374 return i;
286 } 375 }
376 else if(stype == MS_SYMBOL_TYPE.MS_SYMBOL_PIXMAP.swigValue() &&
377 etype.equals("picture")) {
378 return i;
379 }
287 } 380 }
288 return -1; 381 return -1;
289 } 382 }
383
384
385 /**
386 * Transform a color to transparency.
387 *
388 * @param bi The image.
389 * @param col the color.
390 *
391 * @return The image with transparent color.
392 */
393 private Image colorToTransparent(BufferedImage bi, Color col) {
394 final int markerRGB = col.getRGB() | 0xFF000000;
395 ImageFilter filter = new RGBImageFilter() {
396 public final int filterRGB(int x, int y, int rgb) {
397 if ((rgb | 0xFF000000) == markerRGB) {
398 // Mark the alpha bits as zero - transparent
399 return 0x00FFFFFF & rgb;
400 }
401 else {
402 // nothing to do
403 return rgb;
404 }
405 }
406 };
407 ImageProducer ip = new FilteredImageSource(bi.getSource(), filter);
408 return Toolkit.getDefaultToolkit().createImage(ip);
409 }
410
290 } 411 }
291 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 412 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)