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

📄 svgimporter.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			String[] href = getAttributeValue(atts, SVGSyntax.XLINK_HREF_QNAME, "").split(",");
			if(href.length == 2 && href[0].equals("data:image/png;base64") && Base64.isArrayByteBase64(href[1].getBytes())) {
				
				WebPictureMarkerSymbol symbol = new WebPictureMarkerSymbol(); 
				symbol.setPicture(Base64.decodeBase64(href[1].getBytes()));
				symbol.setAntialiasing(true);
				symbol.setTransparency(1.0);
				
				newGraphicElement(point, symbol);
			}
		}
	}
	
	private WebSimpleLineSymbol readWebSimpleLineSymbol(Attributes atts) {
		
		WebSimpleLineSymbol symbol = new WebSimpleLineSymbol();
		symbol.setLineType(SVGUtils.toADFLineType(getAttributeValue(atts, SVGSyntax.SVG_STROKE_DASHARRAY_ATTRIBUTE, _defaultLineType))); 
		symbol.setColor(ColorEncoder.parseColor(getAttributeValue(atts, SVGSyntax.SVG_STROKE_ATTRIBUTE, _defaultLineColor)));
		symbol.setWidth(SVGUtils.toInt(getAttributeValue(atts, SVGSyntax.SVG_STROKE_WIDTH_ATTRIBUTE, _defaultLineWidth), SVGUtils.toInt(_defaultLineWidth, 1)));
		symbol.setCapType(SVGUtils.toADFLineCapType(getAttributeValue(atts, SVGSyntax.SVG_STROKE_LINECAP_ATTRIBUTE, _defaultLineCapType)));
		symbol.setJoinType(SVGUtils.toADFLineJoinType(getAttributeValue(atts, SVGSyntax.SVG_STROKE_LINEJOIN_ATTRIBUTE, _defaultLineJoinType)));
		symbol.setTransparency(SVGUtils.toDouble(getAttributeValue(atts, SVGSyntax.SVG_STROKE_OPACITY_ATTRIBUTE, _defaultLineColorOpacity), SVGUtils.toDouble(_defaultLineColorOpacity, 1)));
		symbol.setAntialiasing(true);
		
		return symbol;
	}
	
	private void readPath(Attributes atts) {
		WebGeometry geometry = null;
			
		if(atts.getIndex(SVGSyntax.SVG_D_ATTRIBUTE) != -1) {
			geometry = SVGParser.parsePath(atts.getValue(SVGSyntax.SVG_D_ATTRIBUTE));
			
			// Read new WebArc
			if(geometry instanceof WebArc)
				newGraphicElement(geometry, readWebSimpleLineSymbol(atts));
			
			// Read new WebPolygon
			else if(geometry instanceof WebRing) {	
				WebPolygon polygon = new WebPolygon();
				polygon.addRing((WebRing)geometry);
				
				WebSimplePolygonSymbol symbol = new WebSimplePolygonSymbol();
				symbol.setLineType(SVGUtils.toADFLineType(getAttributeValue(atts, SVGSyntax.SVG_STROKE_DASHARRAY_ATTRIBUTE, _defaultLineType))); 
				symbol.setColor(ColorEncoder.parseColor(getAttributeValue(atts, SVGSyntax.SVG_STROKE_ATTRIBUTE, _defaultLineColor)));
				symbol.setWidth(SVGUtils.toInt(getAttributeValue(atts, SVGSyntax.SVG_STROKE_WIDTH_ATTRIBUTE, _defaultLineWidth), SVGUtils.toInt(_defaultLineWidth, 1)));
				symbol.setCapType(SVGUtils.toADFLineCapType(getAttributeValue(atts, SVGSyntax.SVG_STROKE_LINECAP_ATTRIBUTE, _defaultLineCapType)));
				symbol.setJoinType(SVGUtils.toADFLineJoinType(getAttributeValue(atts, SVGSyntax.SVG_STROKE_LINEJOIN_ATTRIBUTE, _defaultLineJoinType)));
				symbol.setTransparency(SVGUtils.toDouble(getAttributeValue(atts, SVGSyntax.SVG_STROKE_OPACITY_ATTRIBUTE, _defaultLineColorOpacity), SVGUtils.toDouble(_defaultLineColorOpacity, 1)));				
				
				String svgFillColor = getAttributeValue(atts, SVGSyntax.SVG_FILL_ATTRIBUTE, _defaultFillColor);
				String adfFillColor = ColorEncoder.parseColor(svgFillColor);
				
				if(ColorEncoder.isSVGRGBColorFormat(svgFillColor) || ColorEncoder.isSVGStandardColor(svgFillColor)) { // This is a solid color fill 
					
					symbol.setFillType(WebSimplePolygonSymbol.SOLID);
					symbol.setFillColor(adfFillColor);
					
				} else if(ColorEncoder.isPatternLink(svgFillColor)) { // This is a pattern fill
					SVGPattern pattern = _svgPatterns.get(adfFillColor);
					
					if(pattern != null) {
						symbol.setFillType(pattern.getType());
						symbol.setFillColor(pattern.getFillColor());
						
					} else // pattern was not found
						symbol.setFillType(WebSimplePolygonSymbol.TRANSPARENT);
					
				} else // This is a transparent fill
					symbol.setFillType(WebSimplePolygonSymbol.TRANSPARENT);
					
				symbol.setFillTransparency(SVGUtils.toDouble(getAttributeValue(atts, SVGSyntax.SVG_FILL_OPACITY_ATTRIBUTE, _defaultFillColorOpacity), SVGUtils.toDouble(_defaultFillColorOpacity, 1)));				
				symbol.setAntialiasing(true);
				
				newGraphicElement(polygon, symbol);
			
			// Read new WebPolyline
			} else {
				WebPolyline polyline = new WebPolyline();
				polyline.addPath((WebPath)geometry);
				newGraphicElement(polyline, readWebSimpleLineSymbol(atts));
			}
		}
	}
	
	private void readCircle(Attributes atts) {
		// Read new WebArc
		double cx = SVGUtils.toDouble(getAttributeValue(atts, SVGSyntax.SVG_CX_ATTRIBUTE, ""), SVGUtils.toDouble("", Double.NaN));
		double cy = SVGUtils.toDouble(getAttributeValue(atts, SVGSyntax.SVG_CY_ATTRIBUTE, ""), SVGUtils.toDouble("", Double.NaN));
		double radius = SVGUtils.toDouble(getAttributeValue(atts, SVGSyntax.SVG_R_ATTRIBUTE, ""), SVGUtils.toDouble("", Double.NaN));
		String fillAtt = getAttributeValue(atts, SVGSyntax.SVG_FILL_ATTRIBUTE, "");
			
		if(cx != Double.NaN && cy != Double.NaN && radius != Double.NaN && fillAtt.equals(SVGSyntax.SVG_NONE_VALUE)) {
			WebArc arc = new WebArc(new WebCircle(new WebPoint(cx, cy), radius), 0, 360);
			newGraphicElement(arc, readWebSimpleLineSymbol(atts));
		}
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		super.characters(ch, start, length);
		if(_characters != null) {
			try {
				_characters.append(String.valueOf(ch, start, length));	
			} catch(IndexOutOfBoundsException ex) {}
		}
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		super.endElement(uri, localName, qName);
		
		// End reading <defs> tag section
		if(qName.equalsIgnoreCase(SVGSyntax.SVG_DEFS_TAG))
			_readDefs = false;
		
		if(_readMetadata) {
			
			if(qName.equalsIgnoreCase("crs:Name"))
				_crsName = _characters.toString();
			
			if(qName.equalsIgnoreCase("crs:code"))
				_crsCode = _characters.toString();
			
			if(qName.equalsIgnoreCase("crs:codeSpace"))
				_crsCodeSpace = _characters.toString();
		}
		
		if(_readG && qName.equals(SVGSyntax.SVG_TEXT_TAG)) {
			((WebTrueTypeMarkerSymbol)_ge.getSymbol()).addTextValue(_characters.toString());
			_gList.add(_ge);
		}
		
		// End reading <g> tag section
		if(qName.equalsIgnoreCase(SVGSyntax.SVG_G_TAG)) {
			_gAtts = null;
			_readG = false;
		}
		
		// End reading <metadata> tag section
		if (qName.equalsIgnoreCase(SVGSyntax.SVG_METADATA_TAG)) {
			
			if(_crsName.equals("WGS 84") || (_crsCode.equals("4326") && _crsCodeSpace.equals("EPSG")))
				spatialReference = WebSpatialReference.getWebSpatialReference(4326);
			
			_readMetadata = false;
		}
		
		_characters = null;
	}
	
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#endDocument()
	 */
	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
		
		if(_gList != null && spatialReference != null) {
			
			for(Iterator<GraphicElement> i = _gList.iterator(); i.hasNext();) {
				GraphicElement ge = i.next();
				ge.setGeometry(fixGeometry(ge.getGeometry()));
				
				if(ge.getSymbol() instanceof WebSimpleMarkerSymbol)
					points.add(ge);
			
				else if(ge.getSymbol() instanceof WebTrueTypeMarkerSymbol)
					labels.add(ge);
				
				else if(ge.getSymbol() instanceof WebPictureMarkerSymbol)
					graphics.add(ge);
			
				else if(ge.getSymbol() instanceof WebSimplePolygonSymbol)
					polygons.add(ge);
				
				else if(ge.getSymbol() instanceof WebSimpleLineSymbol) {
					if(ge.getGeometry() instanceof WebArc)
						arcs.add(ge);
					else if(ge.getGeometry() instanceof WebPolyline)
						lines.add(ge);
				}
			}
			_gList = null;
		}
	}
	
	// This method fixes geometries that were read from SVG file by
	// 1) applying transformation and
	// 2) assigning Spatial Reference
	// as it is described in svg <metadata> section
	private WebGeometry fixGeometry(WebGeometry inGeometry) {
		WebGeometry outGeometry = applyTransformation(inGeometry, _tx);
		outGeometry.setSpatialReference(spatialReference);
		
		return outGeometry;
	}
	
	public WebGeometry applyTransformation(WebGeometry inGeometry, AffineTransform tx) {
		
		if(tx != null && inGeometry != null) {
			
			if(inGeometry instanceof WebPoint) {
				
				WebPoint inPoint = (WebPoint) inGeometry;
				Point2D.Double dstPoint = (Point2D.Double)_tx.transform(new Point2D.Double(inPoint.getX(), inPoint.getY()), null);
				
				return new WebPoint(dstPoint.x, dstPoint.y);
			
			} else if(inGeometry instanceof WebArc) {
				
				WebArc arc = (WebArc) inGeometry;
				WebPoint center = arc.getCenter();
				Point2D.Double dstPoint = (Point2D.Double)_tx.transform(new Point2D.Double(center.getX(), center.getY()), null);
				arc.setCenter(new WebPoint(dstPoint.x, dstPoint.y));
				if(!arc.isDensified()) arc.densify();
				
				return arc;
				
			} else if(inGeometry instanceof WebPolygon) {
				
				WebPolygon polygon = (WebPolygon) inGeometry;
				WebRing inRing = polygon.getRing(0);
				WebRing outRing = new WebRing();
				
				for(Iterator<WebPoint> i=inRing.getPoints().iterator(); i.hasNext();) {
					WebPoint srcPoint = i.next();
					Point2D.Double dstPoint = (Point2D.Double)_tx.transform(new Point2D.Double(srcPoint.getX(), srcPoint.getY()), null);
					outRing.addPoint(new WebPoint(dstPoint.x, dstPoint.y));
				}
				
				WebPolygon outPolygon = new WebPolygon();
				outPolygon.addRing(outRing);
				
				return outPolygon;
				
			} else if(inGeometry instanceof WebPolyline) {
				
				WebPolyline polyline = (WebPolyline) inGeometry;
				WebPath inPath = polyline.getPath(0);
				WebPath outPath = new WebPath();
				
				for(Iterator<WebPoint> i=inPath.getPoints().iterator(); i.hasNext();) {
					WebPoint srcPoint = i.next();
					Point2D.Double dstPoint = (Point2D.Double)_tx.transform(new Point2D.Double(srcPoint.getX(), srcPoint.getY()), null);
					outPath.addPoint(new WebPoint(dstPoint.x, dstPoint.y));
				}
				
				WebPolyline outPolyline = new WebPolyline();
				outPolyline.addPath(outPath);
				
				return outPolyline;
			}
		}
		
		return inGeometry;
	}

	private String getAttributeValue(Attributes atts, String aName, String dfltValue) {
		if(atts != null && atts.getIndex(aName) != -1)
			return atts.getValue(aName);
		
		else if(_gAtts != null && _gAtts.containsKey(aName))
			return _gAtts.get(aName);
		
		else return dfltValue;
	}	
	
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#endPrefixMapping(java.lang.String)
	 */
	@Override
	public void endPrefixMapping(String arg0) throws SAXException {
		super.endPrefixMapping(arg0);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#error(org.xml.sax.SAXParseException)
	 */
	@Override
	public void error(SAXParseException arg0) throws SAXException {
		super.error(arg0);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#fatalError(org.xml.sax.SAXParseException)
	 */
	@Override
	public void fatalError(SAXParseException arg0) throws SAXException {
		super.fatalError(arg0);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#ignorableWhitespace(char[], int, int)
	 */
	@Override
	public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
		super.ignorableWhitespace(arg0, arg1, arg2);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#notationDecl(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void notationDecl(String arg0, String arg1, String arg2) throws SAXException {
		super.notationDecl(arg0, arg1, arg2);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#processingInstruction(java.lang.String, java.lang.String)
	 */
	@Override
	public void processingInstruction(String arg0, String arg1) throws SAXException {
		super.processingInstruction(arg0, arg1);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#resolveEntity(java.lang.String, java.lang.String)
	 */
	@Override
	public InputSource resolveEntity(String arg0, String arg1) throws IOException, SAXException {
		return super.resolveEntity(arg0, arg1);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#setDocumentLocator(org.xml.sax.Locator)
	 */
	@Override
	public void setDocumentLocator(Locator arg0) {
		super.setDocumentLocator(arg0);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#skippedEntity(java.lang.String)
	 */
	@Override
	public void skippedEntity(String arg0) throws SAXException {
		super.skippedEntity(arg0);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startPrefixMapping(java.lang.String, java.lang.String)
	 */
	@Override
	public void startPrefixMapping(String arg0, String arg1) throws SAXException {
		super.startPrefixMapping(arg0, arg1);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void unparsedEntityDecl(String arg0, String arg1, String arg2, String arg3) throws SAXException {
		super.unparsedEntityDecl(arg0, arg1, arg2, arg3);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#warning(org.xml.sax.SAXParseException)
	 */
	@Override
	public void warning(SAXParseException arg0) throws SAXException {
		super.warning(arg0);
	}
	
	/**
	 * @return the points
	 */
	public List<GraphicElement> getPoints() {
		return this.points;
	}

	/**
	 * @return the labels
	 */
	public List<GraphicElement> getLabels() {
		return this.labels;
	}

	/**
	 * @return the graphics
	 */
	public List<GraphicElement> getGraphics() {
		return this.graphics;
	}

	/**
	 * @return the lines
	 */
	public List<GraphicElement> getLines() {
		return this.lines;
	}

	/**
	 * @return the polygons
	 */
	public List<GraphicElement> getPolygons() {
		return this.polygons;
	}
	
	public List<GraphicElement> getArcs() {
		return this.arcs;
	}
	
	/**
	 * @return the spatialReference
	 */
	public WebSpatialReference getSpatialReference() {
		return this.spatialReference;
	}
}

⌨️ 快捷键说明

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