⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 svgexporter.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package com.esri.solutions.jitk.data.svg.exporter;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.batik.svggen.SVGSyntax;
import org.apache.commons.codec.binary.Base64;

import com.esri.adf.web.data.GraphicElement;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebContextInitialize;
import com.esri.adf.web.data.geometry.WebGeometry;
import com.esri.adf.web.data.geometry.WebPath;
import com.esri.adf.web.data.geometry.WebPoint;
import com.esri.adf.web.data.geometry.WebPolygon;
import com.esri.adf.web.data.geometry.WebPolyline;
import com.esri.adf.web.data.geometry.WebRing;
import com.esri.adf.web.data.symbol.WebPictureMarkerSymbol;
import com.esri.adf.web.data.symbol.WebSimpleLineSymbol;
import com.esri.adf.web.data.symbol.WebSimpleMarkerSymbol;
import com.esri.adf.web.data.symbol.WebSimplePolygonSymbol;
import com.esri.adf.web.data.symbol.WebSymbol;
import com.esri.adf.web.data.symbol.WebTrueTypeMarkerSymbol;
import com.esri.solutions.jitk.data.svg.ColorEncoder;
import com.esri.solutions.jitk.data.svg.SVGUtils;
import com.esri.solutions.jitk.web.data.geometry.WebArc;

/**
 * @author vlad2928
 *
 */
public class SVGExporter implements WebContextInitialize {

private WebContext webContext;

	// Simple Marker Symbol definitions in SVG format - will add to <defs> section...
	private static final String[] _svgSymbol = {
		"<symbol id=\"symbol_" + WebSimpleMarkerSymbol.CIRCLE + "\" viewBox=\"0 0 100 100\"><circle cx=\"50\" cy=\"50\" r=\"48\"/></symbol>",
		"<symbol id=\"symbol_" + WebSimpleMarkerSymbol.SQUARE + "\" viewBox=\"0 0 100 100\"><rect x=\"0\" y=\"0\" width=\"100\" height=\"100\"/></symbol>",
		"<symbol id=\"symbol_" + WebSimpleMarkerSymbol.TRIANGLE + "\" viewBox=\"0 0 100 100\"><polygon points=\"50,0 100,100 0,100\"/></symbol>",
		"<symbol id=\"symbol_" + WebSimpleMarkerSymbol.CROSS + "\" viewBox=\"0 0 100 100\"><polygon points=\"1,47 47,47 47,1 53,1 53,47 99,47 99,53 53,53 53,99 47,99 47,53 1,53 1,47\"/></symbol>",
		"<symbol id=\"symbol_" + WebSimpleMarkerSymbol.STAR + "\" viewBox=\"22 36 22 30\"><polygon points=\"35,37.5 37.9,46.1 46.9,46.1 39.7,51.5 42.3,60.1 35,55 27.7,60.1 30.3,51.5 23.1,46.1 32.1,46.1\"/></symbol>"
	};
	
	private Hashtable<Integer,String> _svgSymbols;
	private ArrayList<String> _svgPatterns;
	private final String nl = "\n";
	
	// Inner helper class to generate svg tags
	private class Tag {
		
		private String _name;
		private String[][] _atts;
		private String _value;
		private boolean _hasChild = false;
		
		private Tag(String name, String[][] atts, String value, boolean hasChild) {
			this._name = name;
			this._atts = atts;
			this._value = value;
			this._hasChild = hasChild;
		}
		
		private boolean isValid() {
			return (this._name != null && !this._name.equals(""));
		}
		
		private StringBuffer open() {
			StringBuffer buf = new StringBuffer();
			
			if(isValid()) {
				// Open tag
				buf.append("<").append(_name);
			
				// Add tag attributes if any
				if(_atts != null)
				for(int i=0; i < _atts.length; i++) {
					buf.append(" ").append(_atts[i][0]).append("=\"").append(_atts[i][1]).append("\"");
				}
			
				// Add tag value if any
				if(_value != null && _value.length() > 0)
					buf.append(">").append(_value).append("</").append(_name).append(">");
				else
					buf.append(_hasChild ? ">" : "/>");
			}
			
			return buf.append(nl);
		}
		
		private StringBuffer close() {
			StringBuffer buf = new StringBuffer();	
			if(isValid()) buf.append("</").append(_name).append(">");
			
			return buf.append(nl);
		}
	}
	
	public SVGExporter(WebContext webContext) { init(webContext); }
	public void init(WebContext webContext) { if(webContext != null) setContext(webContext); }
	public void setContext(WebContext webContext) { this.webContext = webContext; }
	public void destroy() { this.webContext = null; }
	public boolean isInitialized() { return (webContext != null); }
	
	private StringBuffer svgHeader() {
		return new StringBuffer().append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'\n").append("'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>").append(nl);
	}
	
	private StringBuffer svgComment(String comment) {
		StringBuffer buf = new StringBuffer();
		buf.append("<!--").append(comment).append("-->");
		
		return buf.append(nl);
	}
	
	private StringBuffer svgDefs() {
		StringBuffer buf = new StringBuffer();
		
		Tag tagDefs = new Tag(SVGSyntax.SVG_DEFS_TAG, new String[][] {{SVGSyntax.SVG_ID_ATTRIBUTE, "genericDefs"}}, null, true);
		buf.append(tagDefs.open());
		
		if(_svgSymbols != null && _svgSymbols.size() > 0)
			for(Iterator<String> i = _svgSymbols.values().iterator(); i.hasNext();) {
				buf.append(i.next()).append(nl);
			}
		
		if(_svgPatterns != null && _svgPatterns.size() > 0)
			for(Iterator<String> i = _svgPatterns.iterator(); i.hasNext();) {
				buf.append(i.next()).append(nl);
			}
		
		buf.append(tagDefs.close());
		
		return buf;
	}
	
	private StringBuffer svgMetadata() {
		StringBuffer buf = new StringBuffer();

		Tag tagMetadata = new Tag(SVGSyntax.SVG_METADATA_TAG, null, null, true);
		buf.append(tagMetadata.open());
		
			Tag tagRDF = new Tag("rdf:RDF", new String[][] 	{
															{ "xmlns:crs", "http://www.ogc.org/crs" },
															{ "xmlns:rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" },
															{ "xmlns:svg", "http://www.w3.org/2000/svg" }}, null, true);

			buf.append(tagRDF.open());
		
				Tag tagDescription = new Tag("rdf:Description", null, null, true);
				buf.append(tagDescription.open());	
		
					Tag tagCRS = new Tag("crs:CoordinateReferenceSystem", new String[][] {{ "svg:transform", "translate(0,90) scale(1,-1)"}}, null, true);
					buf.append(tagCRS.open());
		
						Tag tagNameSet = new Tag("crs:NameSet", null, null, true);
						buf.append(tagNameSet.open());
							buf.append(new Tag("crs:Name", null, "WGS 84", false).open());
						buf.append(tagNameSet.close());
		
						Tag tagIdentifier = new Tag("crs:Identifier", null, null, true);
						buf.append(tagIdentifier.open());
							buf.append(new Tag("crs:code", null, "4326", false).open());
							buf.append(new Tag("crs:codeSpace", null, "EPSG", false).open());
							buf.append(new Tag("crs:edition", null, "5.2", false).open());
						buf.append(tagIdentifier.close());		
		
					buf.append(tagCRS.close());
		
				buf.append(tagDescription.close());
		
			buf.append(tagRDF.close());
			
		buf.append(tagMetadata.close());
		
		return buf;
	}
	
	private StringBuffer svgG(String tagName, String[][] tagAtts, String tagValue, String[][] renderingAtts) {
		
		StringBuffer buf = new StringBuffer();
		
		Tag g = new Tag(SVGSyntax.SVG_G_TAG, renderingAtts, null, true);
		buf.append(g.open());
		buf.append(new Tag(tagName, tagAtts, tagValue, false).open());
		buf.append(g.close());
		
		return buf;
	}
	
	private StringBuffer svgG(WebPoint webPoint, WebSimpleMarkerSymbol symbol) {	
		// <use> tag attributes
		String[][] useAtts = {
				{ SVGSyntax.XLINK_HREF_QNAME, "#symbol_" + symbol.getMarkerType() },
				{ SVGSyntax.SVG_X_ATTRIBUTE, String.valueOf(webPoint.getX()) },
				{ SVGSyntax.SVG_Y_ATTRIBUTE, String.valueOf(90 - webPoint.getY()) },
				{ SVGSyntax.SVG_WIDTH_ATTRIBUTE, String.valueOf(symbol.getWidth()) },
				{ SVGSyntax.SVG_HEIGHT_ATTRIBUTE, String.valueOf(symbol.getWidth()) }
		};
		
		return svgG(SVGSyntax.SVG_USE_TAG, useAtts, null, getRenderingAttributes(symbol));
	}
	
	private StringBuffer svgG(WebPoint webPoint, WebPictureMarkerSymbol symbol) {
		
		byte[] pic = symbol.getPicture();
		if(pic != null)
			try {
				BufferedImage img = ImageIO.read(new ByteArrayInputStream(pic));
				// <image> tag attributes
				String[][] imageAtts = {
						{ SVGSyntax.SVG_X_ATTRIBUTE, String.valueOf(webPoint.getX()) },
						{ SVGSyntax.SVG_Y_ATTRIBUTE, String.valueOf(90 - webPoint.getY()) },
						{ SVGSyntax.SVG_WIDTH_ATTRIBUTE, String.valueOf(img.getWidth()) },
						{ SVGSyntax.SVG_HEIGHT_ATTRIBUTE, String.valueOf(img.getHeight()) },
						{ SVGSyntax.XLINK_HREF_QNAME, "data:image/png;base64," + new String(Base64.encodeBase64Chunked(symbol.getPicture())) },
						{ "xlink:type", "simple" },
						{ "xlink:actuate", "onLoad" },
						{ "xlink:show", "embed" },
						{ "xmlns:xlink", "http://www.w3.org/1999/xlink" },
						{ SVGSyntax.SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE, SVGSyntax.SVG_NONE_VALUE }
				};
				
				return svgG(SVGSyntax.SVG_IMAGE_TAG, imageAtts, null, getRenderingAttributes(symbol));
			
			} catch(IOException ex) {}
		
		return new StringBuffer();
	}
	
	private StringBuffer svgG(WebPoint webPoint, WebTrueTypeMarkerSymbol symbol) {	
		// <text> tag attributes
		String[][] textAtts = {
				{ SVGSyntax.SVG_X_ATTRIBUTE, String.valueOf(webPoint.getX()) },
				{ SVGSyntax.SVG_Y_ATTRIBUTE, String.valueOf(90 - webPoint.getY()) },
				{ SVGSyntax.XML_SPACE_QNAME, "preserve" }
		};
		
		String tagValue = symbol.getTextValues().get(0);
		if(tagValue != null && tagValue.length() > 0)
			return svgG(SVGSyntax.SVG_TEXT_TAG, textAtts, tagValue, getRenderingAttributes(symbol));
		
		return new StringBuffer();
	}

	private StringBuffer svgG(WebPolyline webPolyline, WebSimpleLineSymbol symbol) {
		// <path> tag attributes
		String[][] pathAtts = {
				{ SVGSyntax.SVG_FILL_ATTRIBUTE, SVGSyntax.SVG_NONE_VALUE },
				{ SVGSyntax.SVG_D_ATTRIBUTE, svgPath(webPolyline.getPath(0)) }
		};
		
		return svgG(SVGSyntax.SVG_PATH_TAG, pathAtts, null, getRenderingAttributes(symbol));
	}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -