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

📄 svgutils.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
字号:
/**
 * 
 */
package com.esri.solutions.jitk.data.svg;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.geom.GeneralPath;
import java.util.Iterator;

import com.esri.adf.web.data.geometry.WebExtent;
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.WebRing;
import com.esri.adf.web.data.geometry.WebSpatialReference;
import com.esri.adf.web.data.symbol.WebSimpleLineSymbol;
import com.esri.adf.web.data.symbol.WebTrueTypeMarkerSymbol;

/**
 * @author vlad2928
 *
 */
public class SVGUtils {
	
	private static int counter = 0;
	private static final WebSpatialReference _wgs84 = WebSpatialReference.getWebSpatialReference(4326);
	
	public static int toAWTFontStyle(String sADFFontStyle) {
		
		if(sADFFontStyle != null) {
			
			if(sADFFontStyle.equals(WebTrueTypeMarkerSymbol.REGULAR))
				return Font.PLAIN;
			
			else if(sADFFontStyle.equals(WebTrueTypeMarkerSymbol.BOLD))
				return Font.BOLD;
			
			else if(sADFFontStyle.equals(WebTrueTypeMarkerSymbol.ITALIC))
				return Font.ITALIC;
			
			else if(sADFFontStyle.equals(WebTrueTypeMarkerSymbol.UNDERLINE))
				return Font.PLAIN;
			
			else if(sADFFontStyle.equals(WebTrueTypeMarkerSymbol.OUTLINE))
				return Font.PLAIN;
			
			else if(sADFFontStyle.equals(WebTrueTypeMarkerSymbol.BOLDITALIC))
				return Font.BOLD|Font.ITALIC;
		}
		
		return Font.PLAIN;
	}
	
	public static float[] toAWTLineType(WebSimpleLineSymbol symbol) {
		
		float[] pattern = null;
		float w = symbol.getWidth();
		
		if(symbol != null) {
			if(symbol.getLineType().equals(WebSimpleLineSymbol.DASH)) {
				pattern = new float[] { 12.0f + w, 5.0f + w };
			}
			else if(symbol.getLineType().equals(WebSimpleLineSymbol.DOT))
				pattern = new float[] { 1.0f, 3.0f + w };
			
			else if(symbol.getLineType().equals(WebSimpleLineSymbol.DASH_DOT))
				pattern = new float[] { 12.0f + w, 5.0f + w, 1.0f, 3.0f + w };
			
			else if(symbol.getLineType().equals(WebSimpleLineSymbol.DASH_DOT_DOT))
				pattern = new float[] { 12.0f + w, 5.0f + w, 1.0f, 3.0f + w, 1.0f, 3.0f + w };
		}

		return pattern;
	}
	
	public static String toADFLineType(String sPattern) {
		
		String sLineType = WebSimpleLineSymbol.SOLID;
		
		if(sPattern != null && sPattern.length() > 0) {
			
			String[] pattern = sPattern.split(",");
			
			if(pattern.length == 2)
				sLineType = (pattern[0].equals("1")) ? WebSimpleLineSymbol.DOT : WebSimpleLineSymbol.DASH; 
			
			else if(pattern.length == 4)
				sLineType = WebSimpleLineSymbol.DASH_DOT;
			
			else if(pattern.length == 6)
				sLineType = WebSimpleLineSymbol.DASH_DOT_DOT;
		}

		return sLineType;
	}
	
	public static int toAWTLineCapType(String sADFLineCapType) {
		
		if(sADFLineCapType != null) {
			
			if(sADFLineCapType.equals(WebSimpleLineSymbol.ROUND))
				return BasicStroke.CAP_ROUND;
			
			else if(sADFLineCapType.equals(WebSimpleLineSymbol.SQUARE))
				return BasicStroke.CAP_SQUARE;
		}
		
		return BasicStroke.CAP_BUTT;
	}
	
	public static String toADFLineCapType(String sAWTLineCapType) {
		
		String sLineCapType = WebSimpleLineSymbol.BUTT;
		
		if(sAWTLineCapType != null) {
			
			if(sAWTLineCapType.equalsIgnoreCase(WebSimpleLineSymbol.ROUND))
				sLineCapType = WebSimpleLineSymbol.ROUND;
			
			else if(sAWTLineCapType.equalsIgnoreCase(WebSimpleLineSymbol.SQUARE))
				sLineCapType = WebSimpleLineSymbol.SQUARE;
		}
		
		return sLineCapType;
	}
	
	public static int toAWTLineJoinType(String sADFLineJoinType) {
		
		if(sADFLineJoinType != null) {
			
			if(sADFLineJoinType.equals(WebSimpleLineSymbol.MITER))
				return BasicStroke.JOIN_MITER;
			
			else if(sADFLineJoinType.equals(WebSimpleLineSymbol.BEVEL))
				return BasicStroke.JOIN_BEVEL;
		}
		
		return BasicStroke.JOIN_ROUND;
	}
	
	public static String toADFLineJoinType(String sAWTLineJoinType) {
		
		String sLineJoinType = WebSimpleLineSymbol.ROUND;
			
		if(sAWTLineJoinType != null) {
			
			if(sAWTLineJoinType.equalsIgnoreCase(WebSimpleLineSymbol.MITER))
				sLineJoinType = WebSimpleLineSymbol.MITER;
			
			else if(sAWTLineJoinType.equalsIgnoreCase(WebSimpleLineSymbol.BEVEL))
				sLineJoinType = WebSimpleLineSymbol.BEVEL;
		}
		
		return sLineJoinType;
	}

	public static BasicStroke toAWTLineRenderer(WebSimpleLineSymbol symbol) {
		
		if(symbol == null) 
			return(new BasicStroke());
		
		else if(symbol.getLineType().equals(WebSimpleLineSymbol.SOLID))
			return new BasicStroke(symbol.getWidth(), toAWTLineCapType(symbol.getCapType()), toAWTLineJoinType(symbol.getJoinType()));
		
		else return new BasicStroke(symbol.getWidth(),						// width of this BasicStroke - must be greater than or equal to 0.0f.
								   toAWTLineCapType(symbol.getCapType()),	// cap - decoration of the ends of a BasicStroke
								   toAWTLineJoinType(symbol.getJoinType()),	// join - decoration applied where path segments meet
								   1.0f, 									// miterlimit - limit to trim the miter join, must be greater than or equal to 1.0f.
								   toAWTLineType(symbol),					// dash - the array representing the dashing pattern
								   0.0f);									// dash_phase - the offset to start the dashing pattern				   
	}
	
	public static Color toAWTColor(String sADFColor, double dTransparency ) {
			String[] sRGB = ColorEncoder.parseColor(sADFColor).split(",");
			return new Color(toInt(sRGB[0], 0), toInt(sRGB[1], 0), toInt(sRGB[2], 0), toInt(dTransparency * 255));
	}
	
	public static Dimension toAWTDimension(WebExtent extent) {
		return (extent != null) ? new Dimension(toInt(extent.getWidth()), toInt(extent.getHeight())) : new Dimension();
	}
	
	public static double toDouble(String s, double dflt) {
		double d;
		try { d = new Double(s).doubleValue(); }
		catch(NumberFormatException e) { d = dflt; }
		return d;
	}
	
	public static int toInt(String s, int dflt) {
		int i;
		try { i = new Integer(s).intValue(); }
		catch(NumberFormatException e) { i = dflt; }
		return i;
	}
	
	public static float toFloat(String s, float dflt) {
		float i;
		try { i = new Float(s).floatValue(); }
		catch(NumberFormatException e) { i = dflt; }
		return i;
	}
	
	public static float toFloat(double val) {
		return new Double(val).floatValue();
	}
	
	public static int toInt(double val) {
		return new Double(val).intValue();
	}
	
	public static int[] toIntArray(double[] dArray) {
		int[] iArray = null;
		if(dArray != null) {
			iArray = new int[dArray.length];
			for(int i=0; i < dArray.length; i++)
				iArray[i] = toInt(dArray[i]);
		}		
		return iArray;
	}
	
	public static boolean isWGS84(WebSpatialReference sr) {
		return ((sr != null) && (sr.equals(_wgs84)));
	}
	
	public static WebGeometry ensureWGS84(WebGeometry geometry) {
		if(geometry != null) {
			WebSpatialReference sr = geometry.getSpatialReference(); 
			if(sr == null || !isWGS84(sr))
				geometry.setSpatialReference(_wgs84);
			else
				geometry.project(_wgs84);
		}
		
		return geometry; 
	}
	
	public static GeneralPath toGeneralPath(WebPath webPath) {
		GeneralPath gPath = new GeneralPath();
		if(webPath != null) {
			for(Iterator<WebPoint> i = webPath.getPoints().iterator(); i.hasNext();) {
				WebPoint webPoint = i.next();
				if(gPath.getCurrentPoint() == null)
					gPath.moveTo(toFloat(webPoint.getX()), toFloat(90 - webPoint.getY()));
				else
					gPath.lineTo(toFloat(webPoint.getX()), toFloat(90 - webPoint.getY())); 
			}	
			if(webPath instanceof WebRing) gPath.closePath();
		}

		return gPath;
	}

	// calcArcPoint represents conversion mechanism from 'center' to 'endpoint' parameterization of the Arc
	// It is used by SVGExporter to export ADF WebArcs objects into SVG Arcs 
	public static WebPoint calcArcPoint(WebPoint center, double rx, double ry, double phi, double theta) {
		double cosPhi = Math.cos(phi), sinPhi = Math.sin(phi);
		double cosTheta = Math.cos(theta), sinTheta = Math.sin(theta);
		double rxCosTheta = rx * cosTheta, rySinTheta = ry * sinTheta;
		
		return new WebPoint(cosPhi * rxCosTheta - sinPhi * rySinTheta + center.getX(), sinPhi * rxCosTheta + cosPhi * rySinTheta + center.getY());
	}
	
	// The following functions calcMidPoint, calcMidCenter, calcCenter, calcTheta and calcDelta 
	// represent 5 step conversion procedure from 'endpoint' to 'center' parameterization of the Arc
	// It is used by SVGImporter to import SVG Arcs into ADF WebArcs
	
	// Step 1
	public static WebPoint calcMidPoint(WebPoint startPoint, WebPoint endPoint, double phi) {
		double x11 = Math.cos(phi) * (startPoint.getX() - endPoint.getX()) / 2 + Math.sin(phi) * (startPoint.getY() - endPoint.getY()) / 2;
		double y11 = -Math.sin(phi) * (startPoint.getX() - endPoint.getX()) / 2 + Math.cos(phi) * (startPoint.getY() - endPoint.getY()) / 2;
		
		return new WebPoint(x11, y11);
	}
	
	// Step 2
	public static WebPoint calcMidCenter(WebPoint midPoint, double rx, double ry, int fa, int fs) {
		double rx2 = Math.pow(rx, 2);
		double ry2 = Math.pow(ry, 2);
		double midx2 = Math.pow(midPoint.getX(), 2);
		double midy2 = Math.pow(midPoint.getY(), 2);
		
		double k = Math.sqrt((rx2 * ry2 - rx2 * midy2 - ry2 * midx2) / (rx2 * midy2 + ry2 * midx2)) * ((fa == fs) ? -1 : 1);
		double cx1 = k * rx * midPoint.getY() / ry;
		double cy1 = k * -(ry * midPoint.getX() / rx);
		
		return new WebPoint(cx1, cy1);
	}
	
	// Step 3
	public static WebPoint calcCenter(WebPoint startPoint, WebPoint endPoint, WebPoint midCenter, double phi) {
		double cosPhi = Math.cos(phi);
		double sinPhi = Math.sin(phi);
		
		double a1 = cosPhi * midCenter.getX() - sinPhi * midCenter.getY();
		double a2 = sinPhi * midCenter.getX() + cosPhi * midCenter.getY();
		
		double cx = a1 + (startPoint.getX() + endPoint.getX()) / 2;
		double cy = a2 + (startPoint.getY() + endPoint.getY()) / 2;
		
		return new WebPoint(cx, cy);
	}
	
	// Step 4
	public static double calcTheta(WebPoint midPoint, WebPoint midCenter, double rx, double ry) {
		
		double a1 = 1;
		double a2 = 0;
		double b1 = (midPoint.getX() - midCenter.getX()) / rx;
		double b2 = (midPoint.getY() - midCenter.getY()) / ry;
		
		double scalar = a1 * b1 + a2 * b2;
		double module_a = Math.sqrt(1);
		double module_b = Math.sqrt((Math.pow(b1, 2) + Math.pow(b2, 2)));
		
		return Math.acos(scalar / (module_a * module_b));
	}
	
	// Step 5
	public static double calcDelta(WebPoint midPoint, WebPoint midCenter, double rx, double ry) {
		
		double a1 = (midPoint.getX() - midCenter.getX()) / rx;
		double a2 = (midPoint.getY() - midCenter.getY()) / ry;
		
		double b1 = (-midPoint.getX() - midCenter.getX()) / rx;
		double b2 = (-midPoint.getY() - midCenter.getY()) / ry;
		
		double scalar = a1 * b1 + a2 * b2;
		double module_a = Math.sqrt((Math.pow(a1, 2) + Math.pow(a2, 2)));
		double module_b = Math.sqrt((Math.pow(b1, 2) + Math.pow(b2, 2)));
		
		return Math.acos(scalar / (module_a * module_b));
	}
	
	public synchronized static String getUniqueModifier() {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("ddHHmmssSSS");
		return formatter.format(new java.util.Date()) + counter++;
	}
	
	public synchronized static String getUniqueFileName() throws java.net.UnknownHostException {
		return java.net.InetAddress.getLocalHost().getHostName() + "_" + getUniqueModifier();
	}
	
	public static void main(String[] params) {
		// Arcs test
		WebPoint startPoint = new WebPoint(-108.08594627886013,87.6054334172079);
		WebPoint endPoint = new WebPoint(-171.40885312678614,98.77097039138609);
		double rx = 32.14988237767308, ry = 32.14988237767308;
		int phi = 0;
		int fa = 0;
		int fs = 0;
		
		WebPoint midPoint = calcMidPoint(startPoint, endPoint, phi);
		WebPoint midCenter = calcMidCenter(midPoint, rx, ry, fa, fs);
		WebPoint center = calcCenter(startPoint, endPoint, midCenter, phi);
		long theta = Math.round(Math.toDegrees(calcTheta(midPoint, midCenter, rx, ry)));
		long dtheta = Math.round(Math.toDegrees(calcDelta(midPoint, midCenter, rx, ry)));
		
		calcArcPoint(center, rx, ry, 0, theta);
		calcArcPoint(center, rx, ry, 0, dtheta);
		
		System.out.println(theta);
	}
}

⌨️ 快捷键说明

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