0001: //$HeadURL: https://sushibar/svn/deegree/base/trunk/resources/eclipse/svn_classfile_header_template.xml $
0002: /*---------------- FILE HEADER ------------------------------------------
0003: This file is part of deegree.
0004: Copyright (C) 2001-2007 by:
0005: Department of Geography, University of Bonn
0006: http://www.giub.uni-bonn.de/deegree/
0007: lat/lon GmbH
0008: http://www.lat-lon.de
0009:
0010: This library is free software; you can redistribute it and/or
0011: modify it under the terms of the GNU Lesser General Public
0012: License as published by the Free Software Foundation; either
0013: version 2.1 of the License, or (at your option) any later version.
0014: This library is distributed in the hope that it will be useful,
0015: but WITHOUT ANY WARRANTY; without even the implied warranty of
0016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0017: Lesser General Public License for more details.
0018: You should have received a copy of the GNU Lesser General Public
0019: License along with this library; if not, write to the Free Software
0020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0021: Contact:
0022:
0023: Andreas Poth
0024: lat/lon GmbH
0025: Aennchenstr. 19
0026: 53177 Bonn
0027: Germany
0028: E-Mail: poth@lat-lon.de
0029:
0030: Prof. Dr. Klaus Greve
0031: Department of Geography
0032: University of Bonn
0033: Meckenheimer Allee 166
0034: 53115 Bonn
0035: Germany
0036: E-Mail: greve@giub.uni-bonn.de
0037: ---------------------------------------------------------------------------*/
0038:
0039: package org.deegree.io.mapinfoapi;
0040:
0041: import static java.awt.Color.black;
0042: import static java.awt.Color.decode;
0043: import static java.awt.Color.white;
0044: import static java.awt.Font.TRUETYPE_FONT;
0045: import static java.awt.Font.createFont;
0046: import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
0047: import static java.io.File.createTempFile;
0048: import static java.lang.Float.MAX_VALUE;
0049: import static java.lang.Integer.parseInt;
0050: import static java.lang.Integer.toHexString;
0051: import static javax.imageio.ImageIO.write;
0052: import static javax.media.jai.JAI.create;
0053: import static org.deegree.framework.log.LoggerFactory.getLogger;
0054: import static org.deegree.framework.xml.XMLTools.appendElement;
0055: import static org.deegree.framework.xml.XMLTools.getNodes;
0056: import static org.deegree.framework.xml.XMLTools.getRequiredElement;
0057: import static org.deegree.ogcbase.CommonNamespaces.GMLNS;
0058: import static org.deegree.ogcbase.CommonNamespaces.GML_PREFIX;
0059: import static org.deegree.ogcbase.CommonNamespaces.OGCNS;
0060: import static org.deegree.ogcbase.CommonNamespaces.OGC_PREFIX;
0061: import static org.deegree.ogcbase.CommonNamespaces.SLDNS;
0062: import static org.deegree.ogcbase.CommonNamespaces.SLD_PREFIX;
0063: import static org.deegree.ogcbase.CommonNamespaces.XLINK_PREFIX;
0064: import static org.deegree.ogcbase.CommonNamespaces.XLNNS;
0065: import static org.deegree.ogcbase.CommonNamespaces.getNamespaceContext;
0066:
0067: import java.awt.Color;
0068: import java.awt.Font;
0069: import java.awt.FontFormatException;
0070: import java.awt.Graphics2D;
0071: import java.awt.image.BufferedImage;
0072: import java.io.ByteArrayInputStream;
0073: import java.io.ByteArrayOutputStream;
0074: import java.io.File;
0075: import java.io.FileOutputStream;
0076: import java.io.IOException;
0077: import java.io.InputStream;
0078: import java.io.InputStreamReader;
0079: import java.net.MalformedURLException;
0080: import java.net.URL;
0081: import java.text.DecimalFormat;
0082: import java.util.HashMap;
0083: import java.util.HashSet;
0084: import java.util.List;
0085: import java.util.Map;
0086: import java.util.zip.ZipEntry;
0087: import java.util.zip.ZipFile;
0088:
0089: import javax.media.jai.RenderedOp;
0090:
0091: import org.apache.batik.transcoder.TranscoderException;
0092: import org.apache.batik.transcoder.TranscoderInput;
0093: import org.apache.batik.transcoder.TranscoderOutput;
0094: import org.apache.batik.transcoder.image.PNGTranscoder;
0095: import org.deegree.datatypes.QualifiedName;
0096: import org.deegree.framework.log.ILogger;
0097: import org.deegree.framework.xml.NamespaceContext;
0098: import org.deegree.framework.xml.XMLFragment;
0099: import org.deegree.framework.xml.XMLParsingException;
0100: import org.w3c.dom.DOMException;
0101: import org.w3c.dom.Element;
0102: import org.w3c.dom.Node;
0103: import org.xml.sax.SAXException;
0104:
0105: import com.sun.media.jai.codec.MemoryCacheSeekableStream;
0106:
0107: /**
0108: * <code>MIFStyle2SLD</code>
0109: *
0110: * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
0111: * @author last edited by: $Author:$
0112: *
0113: * @version $Revision:$, $Date:$
0114: */
0115: public class MIFStyle2SLD {
0116:
0117: private static final NamespaceContext nsContext = getNamespaceContext();
0118:
0119: private static final ILogger LOG = getLogger(MIFStyle2SLD.class);
0120:
0121: private Font symbolFont;
0122:
0123: private static File BRUSHES, POINTS;
0124:
0125: static {
0126: // copy the brushes.zip to temp dir
0127: // necessary, because we cannot directly extract a singe entry from jar-enclosed zip
0128: try {
0129: BRUSHES = File.createTempFile("brushes", ".zip");
0130: BRUSHES.deleteOnExit();
0131:
0132: InputStream in = MIFStyle2SLD.class
0133: .getResourceAsStream("brushes.zip");
0134: FileOutputStream out = new FileOutputStream(BRUSHES);
0135:
0136: byte[] buf = new byte[16384];
0137:
0138: int read;
0139: while ((read = in.read(buf)) != -1) {
0140: out.write(buf, 0, read);
0141: }
0142:
0143: in.close();
0144: out.close();
0145: } catch (IOException e) {
0146: LOG.logError("Could not find the brushes zip file", e);
0147: }
0148: // same for more complicated points
0149: try {
0150: POINTS = File.createTempFile("points", ".zip");
0151: POINTS.deleteOnExit();
0152:
0153: InputStream in = MIFStyle2SLD.class
0154: .getResourceAsStream("points.zip");
0155: FileOutputStream out = new FileOutputStream(POINTS);
0156:
0157: byte[] buf = new byte[16384];
0158:
0159: int read;
0160: while ((read = in.read(buf)) != -1) {
0161: out.write(buf, 0, read);
0162: }
0163:
0164: in.close();
0165: out.close();
0166: } catch (IOException e) {
0167: LOG.logError("Could not find the points zip file", e);
0168: }
0169: }
0170:
0171: /**
0172: * @param symbolFont
0173: * @throws FontFormatException
0174: * @throws IOException
0175: */
0176: public MIFStyle2SLD(String symbolFont) throws FontFormatException,
0177: IOException {
0178: this .symbolFont = createFont(TRUETYPE_FONT,
0179: new File(symbolFont));
0180: }
0181:
0182: /**
0183: * @param symbolFont
0184: * @throws FontFormatException
0185: * @throws IOException
0186: */
0187: public MIFStyle2SLD(URL symbolFont) throws FontFormatException,
0188: IOException {
0189: this .symbolFont = createFont(TRUETYPE_FONT, symbolFont
0190: .openStream());
0191: }
0192:
0193: /**
0194: * @param name
0195: * the layer name
0196: * @return an empty SLD document
0197: */
0198: public static XMLFragment getSLDTemplate(String name) {
0199: XMLFragment doc = new XMLFragment(new QualifiedName(SLD_PREFIX,
0200: "StyledLayerDescriptor", SLDNS));
0201: Element root = doc.getRootElement();
0202: root.setAttribute("version", "1.0.0");
0203: root.setAttribute("xmlns:app", "http://www.deegree.org/app");
0204: root.setAttribute("xmlns:" + XLINK_PREFIX, XLNNS
0205: .toASCIIString());
0206: root.setAttribute("xmlns:" + OGC_PREFIX, OGCNS.toASCIIString());
0207: root.setAttribute("xmlns:" + GML_PREFIX, GMLNS.toASCIIString());
0208:
0209: Element e = appendElement(root, SLDNS, SLD_PREFIX
0210: + ":NamedLayer");
0211: appendElement(e, SLDNS, SLD_PREFIX + ":Name", "default:" + name);
0212: e = appendElement(e, SLDNS, SLD_PREFIX + ":UserStyle");
0213: appendElement(e, SLDNS, SLD_PREFIX + ":Name", "default:" + name);
0214: appendElement(e, SLDNS, SLD_PREFIX + ":Title", "default:"
0215: + name);
0216: appendElement(e, SLDNS, SLD_PREFIX + ":IsDefault", "1");
0217: e = appendElement(e, SLDNS, SLD_PREFIX + ":FeatureTypeStyle");
0218: appendElement(e, SLDNS, SLD_PREFIX + ":Name", "default:" + name);
0219:
0220: return doc;
0221: }
0222:
0223: /**
0224: * @param id
0225: * @param rule
0226: */
0227: public static void appendIDFilter(String id, Element rule) {
0228: Element e = appendElement(rule, OGCNS, OGC_PREFIX + ":Filter");
0229: Element or = appendElement(e, OGCNS, OGC_PREFIX + ":Or");
0230: e = appendElement(or, OGCNS, OGC_PREFIX + ":PropertyIsLike");
0231: e.setAttribute("wildCard", "*");
0232: e.setAttribute("escapeChar", "\\");
0233: e.setAttribute("singleChar", "?");
0234: appendElement(e, OGCNS, OGC_PREFIX + ":PropertyName",
0235: "app:styleid");
0236: appendElement(e, OGCNS, OGC_PREFIX + ":Literal", id);
0237:
0238: // it could be part of a combined (polygon) style
0239: e = appendElement(or, OGCNS, OGC_PREFIX + ":PropertyIsLike");
0240: e.setAttribute("wildCard", "*");
0241: e.setAttribute("escapeChar", "\\");
0242: e.setAttribute("singleChar", "?");
0243: appendElement(e, OGCNS, OGC_PREFIX + ":PropertyName",
0244: "app:styleid");
0245: appendElement(e, OGCNS, OGC_PREFIX + ":Literal", "*_" + id);
0246:
0247: e = appendElement(or, OGCNS, OGC_PREFIX + ":PropertyIsLike");
0248: e.setAttribute("wildCard", "*");
0249: e.setAttribute("escapeChar", "\\");
0250: e.setAttribute("singleChar", "?");
0251: appendElement(e, OGCNS, OGC_PREFIX + ":PropertyName",
0252: "app:styleid");
0253: appendElement(e, OGCNS, OGC_PREFIX + ":Literal", id + "_*");
0254: }
0255:
0256: /**
0257: * @param map
0258: * @param doc
0259: * @param name
0260: * the layer name
0261: * @throws DOMException
0262: * @throws IOException
0263: * @throws XMLParsingException
0264: * @throws SAXException
0265: */
0266: public void insertSymbolStyle(Map<String, String> map,
0267: XMLFragment doc, String name) throws DOMException,
0268: IOException, XMLParsingException, SAXException {
0269:
0270: if (map.size() != 4) {
0271: LOG.logWarning("Symbol style not supported yet: " + map);
0272: return;
0273: }
0274:
0275: Color c = decode(map.get("color"));
0276:
0277: int size = parseInt(map.get("size"));
0278: int symbol = parseInt(map.get("shape"));
0279:
0280: BufferedImage img = null;
0281:
0282: switch (symbol) {
0283: case 31:
0284: // don't do anything
0285: return;
0286: case 32:
0287: img = symbolFromTwoChars(symbolFont, (char) 61473,
0288: (char) 61479, size, c, black);
0289: break;
0290: case 33:
0291: img = symbolFromTwoChars(symbolFont, (char) 61474,
0292: (char) 61480, size, c, black);
0293: break;
0294: case 34:
0295: img = symbolFromTwoChars(symbolFont, (char) 61475,
0296: (char) 61481, size, c, black);
0297: break;
0298: case 35:
0299: img = symbolFromTwoChars(symbolFont, (char) 61476,
0300: (char) 61482, size, c, black);
0301: break;
0302: case 36:
0303: img = symbolFromTwoChars(symbolFont, (char) 61477,
0304: (char) 61483, size, c, black);
0305: break;
0306: case 37:
0307: img = symbolFromTwoChars(symbolFont, (char) 61478,
0308: (char) 61484, size, c, black);
0309: break;
0310: case 38:
0311: case 39:
0312: case 40:
0313: case 41:
0314: case 42:
0315: case 43:
0316: case 49:
0317: case 50:
0318: case 51:
0319: case 52:
0320: case 53:
0321: case 54:
0322: case 55:
0323: case 56:
0324: case 57:
0325: case 58:
0326: case 60:
0327: case 61:
0328: case 62:
0329: case 63:
0330: case 64:
0331: img = symbolFromFont(symbolFont,
0332: (char) (61473 + symbol - 32), size, c);
0333: break;
0334: case 44:
0335: case 45:
0336: case 46:
0337: case 47:
0338: case 48:
0339: case 65:
0340: case 66:
0341: case 67: {
0342: ZipFile zip = new ZipFile(POINTS);
0343: ZipEntry entry = zip.getEntry(symbol + ".svg");
0344: XMLFragment svg = new XMLFragment(new InputStreamReader(zip
0345: .getInputStream(entry)), "http://www.systemid.org");
0346: zip.close();
0347: updateSVGColors(svg, toHexColor(c), toHexColor(c));
0348: img = renderSVGImage(svg, size);
0349: break;
0350: }
0351: case 59: {
0352: ZipFile zip = new ZipFile(POINTS);
0353: ZipEntry entry = zip.getEntry(symbol + ".svg");
0354: XMLFragment svg = new XMLFragment(new InputStreamReader(zip
0355: .getInputStream(entry)), "http://www.systemid.org");
0356: zip.close();
0357: updateSVGColors(svg, toHexColor(c), toHexColor(c));
0358: img = renderSVGImage(svg, size);
0359: break;
0360: }
0361: }
0362:
0363: if (img == null) {
0364: img = new BufferedImage(1, 1, TYPE_INT_ARGB);
0365: }
0366:
0367: File symbolFile = createTempFile("mmsvg", ".png");
0368: write(img, "png", symbolFile);
0369: symbolFile.deleteOnExit();
0370:
0371: Element e = getRequiredElement(doc.getRootElement(), ".//"
0372: + SLD_PREFIX + ":FeatureTypeStyle", nsContext);
0373: e = appendElement(e, SLDNS, SLD_PREFIX + ":Rule");
0374: appendIDFilter(map.get("styleid"), e);
0375: appendElement(e, SLDNS, SLD_PREFIX + ":Name", "default:" + name);
0376: appendElement(e, SLDNS, SLD_PREFIX + ":MinScaleDenominator",
0377: "0");
0378: appendElement(e, SLDNS, SLD_PREFIX + ":MaxScaleDenominator", ""
0379: + MAX_VALUE);
0380: e = appendElement(e, SLDNS, SLD_PREFIX + ":PointSymbolizer");
0381: e = appendElement(e, SLDNS, SLD_PREFIX + ":Graphic");
0382: e = appendElement(e, SLDNS, SLD_PREFIX + ":ExternalGraphic");
0383: Element o = appendElement(e, SLDNS, SLD_PREFIX
0384: + ":OnlineResource");
0385: o.setAttributeNS(XLNNS.toASCIIString(), XLINK_PREFIX + ":href",
0386: symbolFile.toURI().toURL().toExternalForm());
0387: o.setAttributeNS(XLNNS.toASCIIString(), XLINK_PREFIX + ":type",
0388: "simple");
0389: appendElement(e, SLDNS, SLD_PREFIX + ":Format", "image/png");
0390: }
0391:
0392: private static void appendSimpleLine(Element rule,
0393: String cssPattern, int width, Color c) {
0394: Element e = appendElement(rule, SLDNS, SLD_PREFIX
0395: + ":LineSymbolizer");
0396: e = appendElement(e, SLDNS, SLD_PREFIX + ":Stroke");
0397: appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter",
0398: toHexColor(c)).setAttribute("name", "stroke");
0399: appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter",
0400: "" + width).setAttribute("name", "stroke-width");
0401: if (cssPattern != null) {
0402: appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter",
0403: cssPattern)
0404: .setAttribute("name", "stroke-dasharray");
0405: }
0406: }
0407:
0408: private static void appendImageLine(Element rule, String image,
0409: int width, Color c) throws MalformedURLException,
0410: IOException, SAXException, XMLParsingException {
0411: XMLFragment svg = new XMLFragment(MIFStyle2SLD.class
0412: .getResource("lines/" + image));
0413: updateSVGColors(svg, toHexColor(c), toHexColor(c));
0414: BufferedImage img = renderSVGImage(svg, 0);
0415: File svgFile = createTempFile("mmsvg", ".png");
0416: write(img, "png", svgFile);
0417: svgFile.deleteOnExit();
0418:
0419: Element e = appendElement(rule, SLDNS, SLD_PREFIX
0420: + ":LineSymbolizer");
0421: e = appendElement(e, SLDNS, SLD_PREFIX + ":Stroke");
0422: appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter", "0")
0423: .setAttribute("name", "stroke-width");
0424: appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter", "0")
0425: .setAttribute("name", "stroke-opacity");
0426: e = appendElement(e, SLDNS, SLD_PREFIX + ":GraphicStroke");
0427: e = appendElement(e, SLDNS, SLD_PREFIX + ":Graphic");
0428: appendElement(e, SLDNS, SLD_PREFIX + ":Size", "" + width);
0429: e = appendElement(e, SLDNS, SLD_PREFIX + ":ExternalGraphic");
0430: Element o = appendElement(e, SLDNS, SLD_PREFIX
0431: + ":OnlineResource");
0432: o.setAttributeNS(XLNNS.toASCIIString(), XLINK_PREFIX + ":href",
0433: svgFile.toURI().toURL().toExternalForm());
0434: o.setAttributeNS(XLNNS.toASCIIString(), XLINK_PREFIX + ":type",
0435: "simple");
0436: appendElement(e, SLDNS, SLD_PREFIX + ":Format", "image/png");
0437: }
0438:
0439: /**
0440: * @param pattern
0441: * a pattern like "1 2 1 2"
0442: * @param mult
0443: * @return the new pattern with each value multiplied by mult
0444: */
0445: public static String multiplyPattern(String pattern, int mult) {
0446: String[] elems = pattern.split("[ ]");
0447: StringBuffer sb = new StringBuffer(pattern.length());
0448:
0449: for (int i = 0; i < elems.length; ++i) {
0450: int k = mult * parseInt(elems[i]);
0451: sb.append(k);
0452: if (i < elems.length - 1) {
0453: sb.append(" ");
0454: }
0455: }
0456:
0457: return sb.toString();
0458: }
0459:
0460: /**
0461: * @param map
0462: * @param doc
0463: * @param name
0464: * the layer name
0465: * @throws XMLParsingException
0466: * @throws SAXException
0467: * @throws IOException
0468: * @throws MalformedURLException
0469: */
0470: public static void insertPenStyle(Map<String, String> map,
0471: XMLFragment doc, String name) throws MalformedURLException,
0472: IOException, SAXException, XMLParsingException {
0473: int pattern = parseInt(map.get("pattern"));
0474: Color c = decode(map.get("color"));
0475: int width = parseInt(map.get("width"));
0476:
0477: Element e = getRequiredElement(doc.getRootElement(), ".//"
0478: + SLD_PREFIX + ":FeatureTypeStyle", nsContext);
0479: Element rule = appendElement(e, SLDNS, SLD_PREFIX + ":Rule");
0480: appendIDFilter(map.get("styleid"), rule);
0481: appendElement(rule, SLDNS, SLD_PREFIX + ":Name", "default:"
0482: + name);
0483: appendElement(rule, SLDNS, SLD_PREFIX + ":MinScaleDenominator",
0484: "0");
0485: appendElement(rule, SLDNS, SLD_PREFIX + ":MaxScaleDenominator",
0486: "" + MAX_VALUE);
0487:
0488: switch (pattern) {
0489: case 1:
0490: break;
0491: case 2:
0492: appendSimpleLine(rule, null, width, c);
0493: break;
0494: case 3:
0495: appendSimpleLine(rule, multiplyPattern("1 1", width),
0496: width, c);
0497: break;
0498: case 4:
0499: appendSimpleLine(rule, multiplyPattern("2 2", width),
0500: width, c);
0501: break;
0502: case 5:
0503: appendSimpleLine(rule, multiplyPattern("3 1", width),
0504: width, c);
0505: break;
0506: case 6:
0507: appendSimpleLine(rule, multiplyPattern("5 1", width),
0508: width, c);
0509: break;
0510: case 7:
0511: appendSimpleLine(rule, multiplyPattern("10 3", width),
0512: width, c);
0513: break;
0514: case 8:
0515: appendSimpleLine(rule, multiplyPattern("20 5", width),
0516: width, c);
0517: break;
0518: case 9:
0519: appendSimpleLine(rule, multiplyPattern("5 5", width),
0520: width, c);
0521: break;
0522: case 10:
0523: appendSimpleLine(rule, multiplyPattern("1 5", width),
0524: width, c);
0525: break;
0526: case 11:
0527: appendSimpleLine(rule, multiplyPattern("3 5", width),
0528: width, c);
0529: break;
0530: case 12:
0531: appendSimpleLine(rule, multiplyPattern("7 7", width),
0532: width, c);
0533: break;
0534: case 13:
0535: appendSimpleLine(rule, multiplyPattern("10 10", width),
0536: width, c);
0537: break;
0538: case 14:
0539: appendSimpleLine(rule, multiplyPattern("9 3 1 3", width),
0540: width, c);
0541: break;
0542: case 15:
0543: appendSimpleLine(rule, multiplyPattern("12 2 1 2", width),
0544: width, c);
0545: break;
0546: case 16:
0547: appendSimpleLine(rule, multiplyPattern("12 2 2 2", width),
0548: width, c);
0549: break;
0550: case 17:
0551: appendSimpleLine(rule,
0552: multiplyPattern("20 10 5 10", width), width, c);
0553: break;
0554: case 18:
0555: appendSimpleLine(rule, multiplyPattern("20 4 4 4 4 4",
0556: width), width, c);
0557: break;
0558: case 19:
0559: appendSimpleLine(rule, multiplyPattern("20 4 4 4 4 4 4 4",
0560: width), width, c);
0561: break;
0562: case 20:
0563: appendSimpleLine(rule,
0564: multiplyPattern("9 3 1 3 1 3", width), width, c);
0565: break;
0566: case 21:
0567: appendSimpleLine(rule, multiplyPattern("12 3 1 3 1 3",
0568: width), width, c);
0569: break;
0570: case 22:
0571: appendSimpleLine(rule, multiplyPattern("12 3 1 3 1 3 1 3",
0572: width), width, c);
0573: break;
0574: case 23:
0575: appendSimpleLine(rule, multiplyPattern("5 1 1 1", width),
0576: width, c);
0577: break;
0578: case 24:
0579: appendSimpleLine(rule,
0580: multiplyPattern("5 1 1 1 1 1", width), width, c);
0581: break;
0582: case 25:
0583: appendSimpleLine(rule, multiplyPattern("9 1 1 1 3 1 1 1",
0584: width), width, c);
0585: break;
0586: case 26: {
0587: appendSimpleLine(rule, null, width, c);
0588: appendSimpleLine(rule, multiplyPattern("1 8", width),
0589: width * 5, c);
0590: break;
0591: }
0592: case 27: {
0593: appendSimpleLine(rule, null, width, c);
0594: appendSimpleLine(rule, multiplyPattern("1 10", width),
0595: width * 5, c);
0596: break;
0597: }
0598: case 28: {
0599: appendSimpleLine(rule, null, width, c);
0600: appendSimpleLine(rule, multiplyPattern("1 15", width),
0601: width * 5, c);
0602: break;
0603: }
0604: case 29: {
0605: appendSimpleLine(rule, null, width, c);
0606: appendImageLine(rule, "29.svg", width * 16, c);
0607: break;
0608: }
0609: case 30: {
0610: appendSimpleLine(rule, null, width, c);
0611: appendImageLine(rule, "30.svg", width * 16, c);
0612: break;
0613: }
0614: case 31: {
0615: appendSimpleLine(rule, null, width, c);
0616: appendSimpleLine(rule, multiplyPattern("1 1 12", width),
0617: width * 5, c);
0618: break;
0619: }
0620: case 32: {
0621: appendSimpleLine(rule, multiplyPattern("6 3 5 0", width),
0622: width, c);
0623: appendSimpleLine(rule, multiplyPattern("1 13", width),
0624: width * 5, c);
0625: break;
0626: }
0627: case 33: {
0628: appendSimpleLine(rule, multiplyPattern("7 3 4 0", width),
0629: width, c);
0630: appendSimpleLine(rule, multiplyPattern("1 1 1 11", width),
0631: width * 5, c);
0632: break;
0633: }
0634: case 34: {
0635: appendSimpleLine(rule, multiplyPattern("0 1 10 2", width),
0636: width, c);
0637: appendImageLine(rule, "34.svg", width * 26, c);
0638: break;
0639: }
0640: case 35: {
0641: appendSimpleLine(rule, multiplyPattern("0 1 10 2", width),
0642: width, c);
0643: appendImageLine(rule, "35.svg", width * 26, c);
0644: break;
0645: }
0646: case 36: {
0647: appendImageLine(rule, "36.svg", width * 11, c);
0648: break;
0649: }
0650: case 37: {
0651: appendSimpleLine(rule, multiplyPattern("10 3", width),
0652: width, c);
0653: appendSimpleLine(rule, multiplyPattern("1 8 1 3", width),
0654: width * 5, c);
0655: break;
0656: }
0657: case 38: {
0658: appendSimpleLine(rule, null, width, c);
0659: appendImageLine(rule, "38-39.svg", width * 20, c);
0660: break;
0661: }
0662: case 39: {
0663: appendSimpleLine(rule, multiplyPattern("0 3 10 7", width),
0664: width, c);
0665: appendImageLine(rule, "38-39.svg", width * 20, c);
0666: break;
0667: }
0668: case 40: {
0669: appendSimpleLine(rule, multiplyPattern("10 3 3 3", width),
0670: width, c);
0671: appendSimpleLine(rule, multiplyPattern("0 14 1 4", width),
0672: width * 5, c);
0673: break;
0674: }
0675: case 41: {
0676: appendSimpleLine(rule, multiplyPattern("0 5 4 1", width),
0677: width, c);
0678: appendSimpleLine(rule, multiplyPattern("4 1 0 5", width),
0679: width * 3, c);
0680: break;
0681: }
0682: case 42: {
0683: appendSimpleLine(rule,
0684: multiplyPattern("0 5 4 1 4 1", width), width, c);
0685: appendSimpleLine(rule, multiplyPattern("5 10", width),
0686: width * 3, c);
0687: break;
0688: }
0689: case 43: {
0690: appendSimpleLine(rule, multiplyPattern("0 5 4 1 4 1 4 1",
0691: width), width, c);
0692: appendSimpleLine(rule, multiplyPattern("5 15", width),
0693: width * 3, c);
0694: break;
0695: }
0696: case 44: {
0697: appendSimpleLine(rule, multiplyPattern(
0698: "0 5 4 1 4 1 4 1 4 1", width), width, c);
0699: appendSimpleLine(rule, multiplyPattern("5 20", width),
0700: width * 3, c);
0701: break;
0702: }
0703: case 45: {
0704: appendSimpleLine(rule, null, width, c);
0705: appendSimpleLine(rule, multiplyPattern("4 15", width),
0706: width * 3, c);
0707: break;
0708: }
0709: case 46: {
0710: appendSimpleLine(rule, multiplyPattern("1 3", width),
0711: width * 4, c);
0712: break;
0713: }
0714: case 47: {
0715: appendImageLine(rule, "47.svg", width * 7, c);
0716: break;
0717: }
0718: case 48: {
0719: appendSimpleLine(rule, null, width, c);
0720: appendImageLine(rule, "48.svg", width * 5, c);
0721: break;
0722: }
0723: case 49: {
0724: appendSimpleLine(rule, null, width, c);
0725: appendImageLine(rule, "49.svg", width * 5, c);
0726: break;
0727: }
0728: case 50: {
0729: appendSimpleLine(rule, null, width, c);
0730: appendImageLine(rule, "50.svg", width * 6, c);
0731: break;
0732: }
0733: case 51: {
0734: appendSimpleLine(rule, null, width, c);
0735: appendImageLine(rule, "51.svg", width * 6, c);
0736: break;
0737: }
0738: case 52: {
0739: appendSimpleLine(rule, multiplyPattern("0 3 10 1", width),
0740: width, c);
0741: appendImageLine(rule, "52.svg", width * 14, c);
0742: break;
0743: }
0744: case 53: {
0745: appendSimpleLine(rule, multiplyPattern("0 3 10 1", width),
0746: width, c);
0747: appendImageLine(rule, "53.svg", width * 14, c);
0748: break;
0749: }
0750: case 54: {
0751: appendSimpleLine(rule, null, width, c);
0752: appendImageLine(rule, "54.svg", width * 11, c);
0753: break;
0754: }
0755: case 55: {
0756: appendSimpleLine(rule, null, width, c);
0757: appendImageLine(rule, "55.svg", width * 11, c);
0758: break;
0759: }
0760: case 56: {
0761: appendSimpleLine(rule, null, width, c);
0762: appendImageLine(rule, "56.svg", width * 16, c);
0763: break;
0764: }
0765: case 57:
0766: case 58: {
0767: appendImageLine(rule, pattern + ".svg", width * 11, c);
0768: break;
0769: }
0770: case 63: {
0771: appendSimpleLine(rule, null, width + 1, c);
0772: appendSimpleLine(rule, null, width, white);
0773: break;
0774: }
0775: case 65: {
0776: appendSimpleLine(rule, null, width + 1, black);
0777: appendSimpleLine(rule, null, width, c);
0778: break;
0779: }
0780: case 67: {
0781: appendSimpleLine(rule, null, width + 1, c);
0782: appendSimpleLine(rule, null, width, black);
0783: break;
0784: }
0785: case 68: {
0786: appendSimpleLine(rule, multiplyPattern("12 3", width),
0787: width + 1, c);
0788: appendSimpleLine(rule, null, width, white);
0789: break;
0790: }
0791: case 69: {
0792: appendSimpleLine(rule, null, width + 1, c);
0793: appendSimpleLine(rule, null, width, white);
0794: appendSimpleLine(rule, "1 " + 20 * width, width * 5, c);
0795: break;
0796: }
0797: case 70: {
0798: appendSimpleLine(rule, multiplyPattern("15 15", width),
0799: width, c);
0800: appendSimpleLine(rule, multiplyPattern("0 15 15 0", width),
0801: width, black);
0802: break;
0803: }
0804: case 71: {
0805: appendSimpleLine(rule, multiplyPattern("17 23", width),
0806: width, c);
0807: appendSimpleLine(rule, multiplyPattern("0 20 17 3", width),
0808: width, black);
0809: break;
0810: }
0811: case 72: {
0812: appendSimpleLine(rule, multiplyPattern("25 5", width),
0813: width + 1, black);
0814: appendSimpleLine(rule, null, width, c);
0815: break;
0816: }
0817: case 73: {
0818: appendSimpleLine(rule, null, width + 1, c);
0819: appendSimpleLine(rule, multiplyPattern("12 12", width),
0820: width, white);
0821: break;
0822: }
0823: case 74: {
0824: appendSimpleLine(rule, null, width + 1, black);
0825: appendSimpleLine(rule, multiplyPattern("12 12", width),
0826: width, c);
0827: break;
0828: }
0829: case 75: {
0830: appendSimpleLine(rule, null, width + 1, black);
0831: appendSimpleLine(rule, multiplyPattern("12 12", width),
0832: width, c);
0833: appendSimpleLine(rule, multiplyPattern("0 12 12 0", width),
0834: width, white);
0835: break;
0836: }
0837: case 76: {
0838: appendSimpleLine(rule, null, width + 1, black);
0839: appendSimpleLine(rule, multiplyPattern("25 25", width),
0840: width, c);
0841: appendSimpleLine(rule, multiplyPattern("0 25 25 0", width),
0842: width, white);
0843: break;
0844: }
0845: case 77: {
0846: appendSimpleLine(rule, null, width + 1, c);
0847: appendSimpleLine(rule, multiplyPattern("12 12", width),
0848: width, black);
0849: appendSimpleLine(rule, multiplyPattern("0 12 12 0", width),
0850: width, white);
0851: break;
0852: }
0853: case 81: {
0854: appendSimpleLine(rule, null, width, black);
0855: appendImageLine(rule, "81-85.svg", width * 10, c);
0856: break;
0857: }
0858: case 82: {
0859: appendImageLine(rule, "81-85.svg", width * 10, c);
0860: break;
0861: }
0862: case 83: {
0863: appendSimpleLine(rule, multiplyPattern("0 2 1 7", width),
0864: width, black);
0865: appendImageLine(rule, "81-85.svg", width * 10, c);
0866: break;
0867: }
0868: case 84: {
0869: appendSimpleLine(rule, null, width * 5, black);
0870: appendSimpleLine(rule, null, width * 5 - 1, white);
0871: appendImageLine(rule, "81-85.svg", width * 10, c);
0872: break;
0873: }
0874: case 85: {
0875: appendSimpleLine(rule, null, width * 6 + 1, black);
0876: appendSimpleLine(rule, null, width * 6, white);
0877: appendImageLine(rule, "81-85.svg", width * 10, c);
0878: break;
0879: }
0880: case 89: {
0881: appendSimpleLine(rule, null, width, black);
0882: appendSimpleLine(rule, multiplyPattern("5 5", width),
0883: width * 5, c);
0884: break;
0885: }
0886: case 90: {
0887: appendSimpleLine(rule, multiplyPattern("5 5", width),
0888: width * 5, c);
0889: break;
0890: }
0891: case 91: {
0892: appendSimpleLine(rule, multiplyPattern("0 7 1 2", width),
0893: width, black);
0894: appendSimpleLine(rule, multiplyPattern("5 5", width),
0895: width * 5, c);
0896: break;
0897: }
0898: case 92: {
0899: appendSimpleLine(rule, null, width * 5, black);
0900: appendSimpleLine(rule, null, width * 5 - 1, white);
0901: appendSimpleLine(rule, multiplyPattern("5 5", width),
0902: width * 5, c);
0903: break;
0904: }
0905: case 93: {
0906: appendSimpleLine(rule, null, width * 6 + 1, black);
0907: appendSimpleLine(rule, null, width * 6, white);
0908: appendSimpleLine(rule, multiplyPattern("5 5", width),
0909: width * 5, c);
0910: break;
0911: }
0912: case 97: {
0913: appendSimpleLine(rule, null, width, black);
0914: appendImageLine(rule, "97-101.svg", width * 10, c);
0915: break;
0916: }
0917: case 98: {
0918: appendImageLine(rule, "97-101.svg", width * 10, c);
0919: break;
0920: }
0921: case 99: {
0922: appendSimpleLine(rule, multiplyPattern("0 2 1 7", width),
0923: width, black);
0924: appendImageLine(rule, "97-101.svg", width * 10, c);
0925: break;
0926: }
0927: case 100: {
0928: appendSimpleLine(rule, null, width * 5, black);
0929: appendSimpleLine(rule, null, width * 5 - 1, white);
0930: appendImageLine(rule, "97-101.svg", width * 10, c);
0931: break;
0932: }
0933: case 101: {
0934: appendSimpleLine(rule, null, width * 6 + 1, black);
0935: appendSimpleLine(rule, null, width * 6, white);
0936: appendImageLine(rule, "97-101.svg", width * 10, c);
0937: break;
0938: }
0939: case 105: {
0940: appendSimpleLine(rule, null, width, black);
0941: appendImageLine(rule, "105-109.svg", width * 10, c);
0942: break;
0943: }
0944: case 106: {
0945: appendImageLine(rule, "105-109.svg", width * 10, c);
0946: break;
0947: }
0948: case 107: {
0949: appendSimpleLine(rule, multiplyPattern("0 2 1 7", width),
0950: width, black);
0951: appendImageLine(rule, "105-109.svg", width * 10, c);
0952: break;
0953: }
0954: case 108: {
0955: appendSimpleLine(rule, null, width * 5, black);
0956: appendSimpleLine(rule, null, width * 5 - 1, white);
0957: appendImageLine(rule, "105-109.svg", width * 10, c);
0958: break;
0959: }
0960: case 109: {
0961: appendSimpleLine(rule, null, width * 6 + 1, black);
0962: appendSimpleLine(rule, null, width * 6, white);
0963: appendImageLine(rule, "105-109.svg", width * 10, c);
0964: break;
0965: }
0966: case 114: {
0967: appendSimpleLine(rule, null, width * 3, c);
0968: appendImageLine(rule, "114.svg", width * 20, c);
0969: break;
0970: }
0971: case 115: {
0972: appendSimpleLine(rule, null, width * 3, c);
0973: appendImageLine(rule, "115.svg", width * 20, c);
0974: break;
0975: }
0976: case 116: {
0977: appendSimpleLine(rule, null, width * 3, c);
0978: appendImageLine(rule, "116.svg", width * 20, c);
0979: break;
0980: }
0981: case 117: {
0982: appendSimpleLine(rule, null, width * 2, c);
0983: appendImageLine(rule, "117.svg", width * 10, c);
0984: break;
0985: }
0986: case 118: {
0987: appendSimpleLine(rule, null, width * 5 + 1, black);
0988: appendSimpleLine(rule, null, width * 5, white);
0989: appendSimpleLine(rule, multiplyPattern("1 5", width),
0990: width * 10, c);
0991: break;
0992: }
0993: default:
0994: LOG
0995: .logWarning("Ignoring fancy pen style, as OpenJUMP cannot display it.");
0996: break;
0997: }
0998:
0999: }
1000:
1001: private static final DecimalFormat formatter = new DecimalFormat(
1002: "000");
1003:
1004: /**
1005: * @param map
1006: * @param doc
1007: * @param name
1008: * @throws SAXException
1009: * @throws IOException
1010: * @throws MalformedURLException
1011: * @throws XMLParsingException
1012: */
1013: public static void insertBrushStyle(Map<String, String> map,
1014: XMLFragment doc, String name) throws MalformedURLException,
1015: IOException, SAXException, XMLParsingException {
1016: int pattern = parseInt(map.get("pattern"));
1017: Color fore = decode(map.get("forecolor"));
1018: Color back = null;
1019: if (map.get("backcolor") != null) {
1020: back = decode(map.get("backcolor"));
1021: }
1022:
1023: if (pattern == 1) {
1024: return;
1025: }
1026:
1027: if (pattern == 2) {
1028: Element e = getRequiredElement(doc.getRootElement(), ".//"
1029: + SLD_PREFIX + ":FeatureTypeStyle", nsContext);
1030: e = appendElement(e, SLDNS, SLD_PREFIX + ":Rule");
1031: appendIDFilter(map.get("styleid"), e);
1032: appendElement(e, SLDNS, SLD_PREFIX + ":Name", "default:"
1033: + name);
1034: appendElement(e, SLDNS,
1035: SLD_PREFIX + ":MinScaleDenominator", "0");
1036: appendElement(e, SLDNS,
1037: SLD_PREFIX + ":MaxScaleDenominator", "" + MAX_VALUE);
1038: e = appendElement(e, SLDNS, SLD_PREFIX
1039: + ":PolygonSymbolizer");
1040: e = appendElement(e, SLDNS, SLD_PREFIX + ":Fill");
1041: appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter",
1042: toHexColor(fore)).setAttribute("name", "fill");
1043: return;
1044: }
1045:
1046: ZipFile zip = new ZipFile(MIFStyle2SLD.BRUSHES);
1047: ZipEntry entry = zip.getEntry(formatter.format(pattern)
1048: + ".svg");
1049:
1050: XMLFragment svg = new XMLFragment(new InputStreamReader(zip
1051: .getInputStream(entry), "UTF-8"),
1052: "http://www.systemid.org");
1053: zip.close();
1054:
1055: updateFillPatternSVG(svg, toHexColor(fore), back == null ? null
1056: : toHexColor(back));
1057:
1058: BufferedImage img = renderSVGImage(svg, 0);
1059: File svgFile = createTempFile("mmsvg", ".png");
1060: write(img, "png", svgFile);
1061: svgFile.deleteOnExit();
1062:
1063: Element e = getRequiredElement(doc.getRootElement(), ".//"
1064: + SLD_PREFIX + ":FeatureTypeStyle", nsContext);
1065: e = appendElement(e, SLDNS, SLD_PREFIX + ":Rule");
1066: appendIDFilter(map.get("styleid"), e);
1067: appendElement(e, SLDNS, SLD_PREFIX + ":Name", "default:" + name);
1068: appendElement(e, SLDNS, SLD_PREFIX + ":MinScaleDenominator",
1069: "0");
1070: appendElement(e, SLDNS, SLD_PREFIX + ":MaxScaleDenominator", ""
1071: + MAX_VALUE);
1072: e = appendElement(e, SLDNS, SLD_PREFIX + ":PolygonSymbolizer");
1073: e = appendElement(e, SLDNS, SLD_PREFIX + ":Fill");
1074: e = appendElement(e, SLDNS, SLD_PREFIX + ":GraphicFill");
1075: e = appendElement(e, SLDNS, SLD_PREFIX + ":Graphic");
1076: e = appendElement(e, SLDNS, SLD_PREFIX + ":ExternalGraphic");
1077: Element o = appendElement(e, SLDNS, SLD_PREFIX
1078: + ":OnlineResource");
1079: o.setAttributeNS(XLNNS.toASCIIString(), XLINK_PREFIX + ":href",
1080: svgFile.toURI().toURL().toExternalForm());
1081: o.setAttributeNS(XLNNS.toASCIIString(), XLINK_PREFIX + ":type",
1082: "simple");
1083: appendElement(e, SLDNS, SLD_PREFIX + ":Format", "image/png");
1084: }
1085:
1086: /**
1087: * @param map
1088: * @param doc
1089: * @param name
1090: * @throws XMLParsingException
1091: */
1092: public static void insertTextStyle(Map<String, String> map,
1093: XMLFragment doc, String name) throws XMLParsingException {
1094: String fontName = map.get("fontname");
1095: int styles = parseInt(map.get("style"));
1096: String style = (styles & 2) == 2 ? "italic" : "normal";
1097: String weight = (styles & 1) == 1 ? "bold" : "normal";
1098: boolean halo = (styles & 256) == 256;
1099: Color fore = decode(map.get("forecolor"));
1100: Color back = null;
1101: if (map.get("backcolor") != null) {
1102: back = decode(map.get("backcolor"));
1103: }
1104:
1105: Element e = getRequiredElement(doc.getRootElement(), ".//"
1106: + SLD_PREFIX + ":FeatureTypeStyle", nsContext);
1107: e = appendElement(e, SLDNS, SLD_PREFIX + ":Rule");
1108: appendIDFilter(map.get("styleid"), e);
1109: appendElement(e, SLDNS, SLD_PREFIX + ":Name", "default:" + name);
1110: appendElement(e, SLDNS, SLD_PREFIX + ":MinScaleDenominator",
1111: "0");
1112: appendElement(e, SLDNS, SLD_PREFIX + ":MaxScaleDenominator", ""
1113: + MAX_VALUE);
1114: Element symbolizer = appendElement(e, SLDNS, SLD_PREFIX
1115: + ":TextSymbolizer");
1116: e = appendElement(symbolizer, SLDNS, SLD_PREFIX + ":Label");
1117: appendElement(e, OGCNS, OGC_PREFIX + ":PropertyName",
1118: "app:text_geometry");
1119: e = appendElement(symbolizer, SLDNS, SLD_PREFIX + ":Font");
1120: appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter", fontName)
1121: .setAttribute("name", "font-family");
1122: appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter", style)
1123: .setAttribute("name", "font-style");
1124: appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter", weight)
1125: .setAttribute("name", "font-weight");
1126: appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter",
1127: toHexColor(fore)).setAttribute("name", "font-color");
1128: e = appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter", "92"); // TODO max font size?
1129: e.setAttribute("name", "font-size");
1130: // Element div = appendElement( e, OGCNS, OGC_PREFIX + ":Div" );
1131: // appendElement( div, OGCNS, OGC_PREFIX + ":Literal", map.get( "ratio" ) );
1132: // appendElement( div, OGCNS, OGC_PREFIX + ":PropertyName", "app:$SCALE" );
1133: if (halo) {
1134: e = appendElement(symbolizer, SLDNS, SLD_PREFIX + ":Halo");
1135: appendElement(e, SLDNS, SLD_PREFIX + ":Radius", "2");
1136: e = appendElement(e, SLDNS, SLD_PREFIX + ":Fill");
1137: e = appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter",
1138: back == null ? "#ffffff" : toHexColor(back));
1139: e.setAttribute("name", "fill");
1140: }
1141: e = appendElement(symbolizer, SLDNS, SLD_PREFIX + ":Fill");
1142: appendElement(e, SLDNS, SLD_PREFIX + ":CssParameter",
1143: toHexColor(fore)).setAttribute("name", "fill");
1144:
1145: e = appendElement(symbolizer, SLDNS, SLD_PREFIX
1146: + ":BoundingBox");
1147: Element m = appendElement(e, SLDNS, SLD_PREFIX + ":Minx");
1148: appendElement(m, OGCNS, OGC_PREFIX + ":PropertyName",
1149: "app:text_minx");
1150: m = appendElement(e, SLDNS, SLD_PREFIX + ":Miny");
1151: appendElement(m, OGCNS, OGC_PREFIX + ":PropertyName",
1152: "app:text_miny");
1153: m = appendElement(e, SLDNS, SLD_PREFIX + ":Maxx");
1154: appendElement(m, OGCNS, OGC_PREFIX + ":PropertyName",
1155: "app:text_maxx");
1156: m = appendElement(e, SLDNS, SLD_PREFIX + ":Maxy");
1157: appendElement(m, OGCNS, OGC_PREFIX + ":PropertyName",
1158: "app:text_maxy");
1159: }
1160:
1161: /**
1162: * @param styles
1163: * @param name
1164: * the layer name
1165: * @return a SLD document with temporary file references for point symbols
1166: */
1167: public XMLFragment getStyle(
1168: Map<String, HashSet<HashMap<String, String>>> styles,
1169: String name) {
1170: XMLFragment doc = getSLDTemplate(name);
1171:
1172: HashSet<HashMap<String, String>> symbols = styles.get("symbol");
1173: if (LOG.isDebug()) {
1174: LOG.logDebug("Found "
1175: + (symbols == null ? "no" : symbols.size())
1176: + " symbol styles.");
1177: }
1178: if (symbols != null) {
1179: for (Map<String, String> map : symbols) {
1180: try {
1181: insertSymbolStyle(map, doc, name);
1182: } catch (DOMException e) {
1183: LOG.logError("Unknown error", e);
1184: } catch (IOException e) {
1185: LOG.logError("Unknown error", e);
1186: } catch (XMLParsingException e) {
1187: LOG.logError("Unknown error", e);
1188: } catch (SAXException e) {
1189: LOG.logError("Unknown error", e);
1190: }
1191: }
1192: }
1193:
1194: HashSet<HashMap<String, String>> pens = styles.get("pen");
1195: if (LOG.isDebug()) {
1196: LOG.logDebug("Found " + (pens == null ? "no" : pens.size())
1197: + " pen styles.");
1198: }
1199: if (pens != null) {
1200: for (Map<String, String> map : pens) {
1201: try {
1202: insertPenStyle(map, doc, name);
1203: } catch (MalformedURLException e) {
1204: LOG.logError("Unknown error", e);
1205: } catch (IOException e) {
1206: LOG.logError("Unknown error", e);
1207: } catch (SAXException e) {
1208: LOG.logError("Unknown error", e);
1209: } catch (XMLParsingException e) {
1210: LOG.logError("Unknown error", e);
1211: }
1212: }
1213: }
1214:
1215: HashSet<HashMap<String, String>> brushes = styles.get("brush");
1216: if (LOG.isDebug()) {
1217: LOG.logDebug("Found "
1218: + (brushes == null ? "no" : brushes.size())
1219: + " brushes.");
1220: }
1221: if (brushes != null) {
1222: for (Map<String, String> map : brushes) {
1223: try {
1224: insertBrushStyle(map, doc, name);
1225: } catch (MalformedURLException e) {
1226: LOG.logError("Unknown error", e);
1227: } catch (IOException e) {
1228: LOG.logError("Unknown error", e);
1229: } catch (SAXException e) {
1230: LOG.logError("Unknown error", e);
1231: } catch (XMLParsingException e) {
1232: LOG.logError("Unknown error", e);
1233: }
1234: }
1235: }
1236:
1237: HashSet<HashMap<String, String>> texts = styles.get("text");
1238: if (LOG.isDebug()) {
1239: LOG
1240: .logDebug("Found "
1241: + (texts == null ? "no" : texts.size())
1242: + " texts.");
1243: }
1244: if (texts != null) {
1245: for (Map<String, String> map : texts) {
1246: try {
1247: insertTextStyle(map, doc, name);
1248: } catch (XMLParsingException e) {
1249: LOG.logError("Unknown error", e);
1250: }
1251: }
1252: }
1253:
1254: if (LOG.isDebug()) {
1255: LOG.logDebug("Generated SLD document", doc
1256: .getAsPrettyString());
1257: }
1258:
1259: return doc;
1260: }
1261:
1262: /**
1263: * @param doc
1264: * @param size
1265: * @return an SVG image with black colors overwritten with the given colors
1266: */
1267: public static BufferedImage renderSVGImage(XMLFragment doc, int size) {
1268: ByteArrayOutputStream bos = new ByteArrayOutputStream(size
1269: * size * 4);
1270: TranscoderOutput output = new TranscoderOutput(bos);
1271:
1272: PNGTranscoder trc = new PNGTranscoder();
1273: try {
1274: Element root = doc.getRootElement();
1275: TranscoderInput input = new TranscoderInput(root
1276: .getOwnerDocument());
1277: if (size > 0) {
1278: trc.addTranscodingHint(PNGTranscoder.KEY_HEIGHT,
1279: new Float(size));
1280: trc.addTranscodingHint(PNGTranscoder.KEY_WIDTH,
1281: new Float(size));
1282: }
1283: trc.transcode(input, output);
1284: bos.close();
1285: ByteArrayInputStream is = new ByteArrayInputStream(bos
1286: .toByteArray());
1287: MemoryCacheSeekableStream mcss = new MemoryCacheSeekableStream(
1288: is);
1289: RenderedOp rop = create("stream", mcss);
1290: return rop.getAsBufferedImage();
1291: } catch (TranscoderException e) {
1292: LOG.logError("Unknown error", e);
1293: } catch (MalformedURLException e) {
1294: LOG.logError("Unknown error", e);
1295: } catch (IOException e) {
1296: LOG.logError("Unknown error", e);
1297: }
1298:
1299: return null;
1300: }
1301:
1302: /**
1303: * @param col
1304: * @return a #rrggbb string
1305: */
1306: public static String toHexColor(Color col) {
1307: if (col == null) {
1308: col = black;
1309: }
1310: String scol = toHexString(col.getRGB() & 0xffffff);
1311: while (scol.length() < 6) {
1312: scol = "0" + scol;
1313: }
1314:
1315: return "#" + scol;
1316: }
1317:
1318: /**
1319: * @param doc
1320: * @param stroke
1321: * @param fill
1322: * @throws XMLParsingException
1323: */
1324: public static void updateSVGColors(XMLFragment doc, String fill,
1325: String stroke) throws XMLParsingException {
1326: List<Node> ns = getNodes(doc.getRootElement(), ".//@style",
1327: nsContext);
1328: for (Node n : ns) {
1329: String v = n.getTextContent();
1330: v = v.replace("fill:#000000", "fill:" + fill);
1331: v = v.replace("fill:black", "fill:" + fill);
1332: v = v.replace("stroke:#000000", "stroke:" + stroke);
1333: v = v.replace("stroke:black", "stroke:" + stroke);
1334: n.setTextContent(v);
1335: }
1336: }
1337:
1338: /**
1339: * @param doc
1340: * @param foreground
1341: * @param background
1342: * @throws XMLParsingException
1343: */
1344: public static void updateFillPatternSVG(XMLFragment doc,
1345: String foreground, String background)
1346: throws XMLParsingException {
1347: List<Node> ns = getNodes(doc.getRootElement(), ".//@style",
1348: nsContext);
1349: for (Node n : ns) {
1350: String v = n.getTextContent();
1351: v = v.replace("fill:#000000", "fill:" + foreground);
1352: v = v.replace("fill:black", "fill:" + foreground);
1353: v = v.replace("stroke:#000000", "stroke:" + foreground);
1354: v = v.replace("stroke:black", "stroke:" + foreground);
1355: if (background != null) {
1356: v = v.replace("fill:none", "fill:" + background);
1357: }
1358: n.setTextContent(v);
1359: }
1360: }
1361:
1362: /**
1363: * @param font
1364: * @param theChar
1365: * @param size
1366: * @param color
1367: * @return an image the char has been written onto
1368: */
1369: public static BufferedImage symbolFromFont(Font font, char theChar,
1370: int size, Color color) {
1371: if (font.canDisplay(theChar)) {
1372: BufferedImage img = new BufferedImage(size, size,
1373: TYPE_INT_ARGB);
1374: Graphics2D g = img.createGraphics();
1375: g.setFont(font.deriveFont((float) size));
1376: g.setColor(color);
1377: g.drawString(theChar + "", 0, size);
1378: g.dispose();
1379: return img;
1380: }
1381:
1382: return null;
1383: }
1384:
1385: /**
1386: * @param font
1387: * @param theChar1
1388: * @param theChar2
1389: * @param size
1390: * @param color1
1391: * @param color2
1392: * @return an image with the second char written over the first one
1393: */
1394: public static BufferedImage symbolFromTwoChars(Font font,
1395: char theChar1, char theChar2, int size, Color color1,
1396: Color color2) {
1397: if (font.canDisplay(theChar1) && font.canDisplay(theChar2)) {
1398: BufferedImage img = new BufferedImage(size, size,
1399: TYPE_INT_ARGB);
1400: Graphics2D g = img.createGraphics();
1401: g.setFont(font.deriveFont((float) size));
1402: g.setColor(color1);
1403: g.drawString(theChar1 + "", 0, size);
1404: g.setColor(color2);
1405: g.drawString(theChar2 + "", 0, size);
1406: g.dispose();
1407: return img;
1408: }
1409:
1410: return null;
1411: }
1412:
1413: }
|