📄 svgwriter.java
字号:
/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
* This code is licensed under the GPL 2.0 license, availible at the root
* application directory.
*/
package org.vfny.geoserver.wms.responses.map.svg;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.MultiPoint;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import org.geotools.data.DataSourceException;
import org.geotools.data.FeatureReader;
import org.geotools.feature.Feature;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.FeatureType;
import org.geotools.feature.IllegalAttributeException;
import org.vfny.geoserver.wms.WMSMapContext;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.logging.Logger;
/**
* DOCUMENT ME!
*
* @author Gabriel Rold?n
* @version $Id: SVGWriter.java 7746 2007-11-13 15:38:35Z aaime $
*/
public class SVGWriter extends OutputStreamWriter {
private static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger(SVGWriter.class.getPackage().getName());
/**
* a number formatter setted up to write SVG legible numbers ('.' as
* decimal separator, no group separator
*/
private static DecimalFormat formatter;
/**
* map of geometry class to writer
*/
private HashMap writers;
static {
Locale locale = new Locale("en", "US");
DecimalFormatSymbols decimalSymbols = new DecimalFormatSymbols(locale);
decimalSymbols.setDecimalSeparator('.');
formatter = new DecimalFormat();
formatter.setDecimalFormatSymbols(decimalSymbols);
//do not group
formatter.setGroupingSize(0);
//do not show decimal separator if it is not needed
formatter.setDecimalSeparatorAlwaysShown(false);
formatter.setDecimalFormatSymbols(null);
//set default number of fraction digits
formatter.setMaximumFractionDigits(5);
//minimun fraction digits to 0 so they get not rendered if not needed
formatter.setMinimumFractionDigits(0);
}
/** DOCUMENT ME! */
private double minY;
/** DOCUMENT ME! */
private double maxY;
/** DOCUMENT ME! */
private int coordsSkipCount;
/** DOCUMENT ME! */
private int coordsWriteCount;
/** DOCUMENT ME! */
private SVGFeatureWriterHandler writerHandler = new SVGFeatureWriterHandler();
/** DOCUMENT ME! */
private SVGFeatureWriter featureWriter = null;
/** DOCUMENT ME! */
private double minCoordDistance;
/** DOCUMENT ME! */
private String attributeStyle;
/** DOCUMENT ME! */
private boolean pointsAsCircles;
/** DOCUMENT ME! */
private WMSMapContext mapContext;
/**
* Creates a new SVGWriter object.
*
* @param out DOCUMENT ME!
* @param config DOCUMENT ME!
*/
public SVGWriter(OutputStream out, WMSMapContext mapContext) {
super(out);
this.mapContext = mapContext;
Envelope space = mapContext.getAreaOfInterest();
this.minY = space.getMinY();
this.maxY = space.getMaxY();
initWriters();
}
private void initWriters() {
writers = new HashMap();
writers.put(Point.class, new PointWriter());
writers.put(LineString.class, new LineStringWriter());
writers.put(LinearRing.class, new LineStringWriter());
writers.put(Polygon.class, new PolygonWriter());
writers.put(MultiPoint.class, new MultiPointWriter());
writers.put(MultiLineString.class, new MultiLineStringWriter());
writers.put(MultiPolygon.class, new MultiPolygonWriter());
}
/**
* DOCUMENT ME!
*
* @param attributeName DOCUMENT ME!
*/
public void setAttributeStyle(String attributeName) {
this.attributeStyle = attributeName;
}
/**
* DOCUMENT ME!
*
* @param asCircles DOCUMENT ME!
*/
public void setPointsAsCircles(boolean asCircles) {
this.pointsAsCircles = asCircles;
}
/**
* DOCUMENT ME!
*
* @param gtype DOCUMENT ME!
*
* @throws IllegalArgumentException DOCUMENT ME!
*/
public void setGeometryType(Class gtype) {
featureWriter = (SVGFeatureWriter) writers.get(gtype);
if (featureWriter == null) {
//check for abstract Geometry type
if (gtype == Geometry.class) {
featureWriter = new GeometryWriter();
} else {
throw new IllegalArgumentException("No SVG Feature writer defined for " + gtype);
}
}
// if (gtype == Point.class) {
// featureWriter = new PointWriter();
// } else if (gtype == MultiPoint.class) {
// featureWriter = new MultiPointWriter();
// } else if (gtype == LineString.class) {
// featureWriter = new LineStringWriter();
// } else if (gtype == MultiLineString.class) {
// featureWriter = new MultiLineStringWriter();
// } else if (gtype == Polygon.class) {
// featureWriter = new PolygonWriter();
// } else if (gtype == MultiPolygon.class) {
// featureWriter = new MultiPolygonWriter();
// } else {
// throw new IllegalArgumentException(
// "No SVG Feature writer defined for " + gtype);
// }
/*
if (config.isCollectGeometries()) {
this.writerHandler = new CollectSVGHandler(featureWriter);
} else {
this.writerHandler = new SVGFeatureWriterHandler();
this.writerHandler = new FIDSVGHandler(this.writerHandler);
this.writerHandler = new BoundsSVGHandler(this.writerHandler);
this.writerHandler = new AttributesSVGHandler(this.writerHandler);
}
*/
}
public void setWriterHandler(SVGFeatureWriterHandler handler) {
this.writerHandler = handler;
}
/**
* DOCUMENT ME!
*
* @param minCoordDistance DOCUMENT ME!
*/
public void setMinCoordDistance(double minCoordDistance) {
this.minCoordDistance = minCoordDistance;
}
/**
* if a reference space has been set, returns a translated Y coordinate
* wich is inverted based on the height of such a reference space,
* otherwise just returns <code>y</code>
*
* @param y DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
public double getY(double y) {
return (maxY - y) + minY;
}
/**
* DOCUMENT ME!
*
* @param x DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
public double getX(double x) {
return x;
}
/**
* DOCUMENT ME!
*
* @param numDigits DOCUMENT ME!
*/
public void setMaximunFractionDigits(int numDigits) {
formatter.setMaximumFractionDigits(numDigits);
}
/**
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
public int getMaximunFractionDigits() {
return formatter.getMaximumFractionDigits();
}
/**
* DOCUMENT ME!
*
* @param numDigits DOCUMENT ME!
*/
public void setMinimunFractionDigits(int numDigits) {
formatter.setMinimumFractionDigits(numDigits);
}
/**
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
public int getMinimunFractionDigits() {
return formatter.getMinimumFractionDigits();
}
/**
* DOCUMENT ME!
*
* @param d DOCUMENT ME!
*
* @throws IOException DOCUMENT ME!
*/
public void write(double d) throws IOException {
write(formatter.format(d));
}
/**
* DOCUMENT ME!
*
* @param c DOCUMENT ME!
*
* @throws IOException DOCUMENT ME!
*/
public void write(char c) throws IOException {
super.write(c);
}
/**
* DOCUMENT ME!
*
* @throws IOException DOCUMENT ME!
*/
public void newline() throws IOException {
super.write('\n');
}
public void writeFeatures(FeatureType featureType, FeatureIterator reader, String style)
throws IOException, AbortedException {
Feature ft;
try {
Class gtype = featureType.getDefaultGeometry().getType();
boolean doCollect = false;
/*
boolean doCollect = config.isCollectGeometries()
&& (gtype != Point.class) && (gtype != MultiPoint.class);
*/
setGeometryType(gtype);
setPointsAsCircles("#circle".equals(style));
if ((style != null) && !"#circle".equals(style) && style.startsWith("#")) {
style = style.substring(1);
} else {
style = null;
}
setAttributeStyle(style);
setUpWriterHandler(featureType, doCollect);
if (doCollect) {
write("<path ");
write("d=\"");
}
while (reader.hasNext()) {
ft = reader.next();
writeFeature(ft);
ft = null;
}
if (doCollect) {
write("\"/>\n");
}
LOGGER.fine("encoded " + featureType.getTypeName());
} catch (NoSuchElementException ex) {
throw new DataSourceException(ex.getMessage(), ex);
} catch (Exception ex) {
throw new DataSourceException(ex.getMessage(), ex);
}
}
private void setUpWriterHandler(FeatureType featureType, boolean doCollect)
throws IOException {
if (doCollect) {
this.writerHandler = new CollectSVGHandler(featureWriter);
LOGGER.finer("Established a collecting features writer handler");
} else {
this.writerHandler = new SVGFeatureWriterHandler();
String typeName = featureType.getTypeName();
/*
* REVISIT: get rid of all this attribute stuff, since if attributes are
* needed it fits better to have SVG with gml attributes as another output
* format for WFS's getFeature.
*/
List atts = new ArrayList(0); // config.getAttributes(typeName);
if (atts.contains("#FID")) {
this.writerHandler = new FIDSVGHandler(this.writerHandler);
atts.remove("#FID");
LOGGER.finer("Added FID handler decorator");
}
if (atts.contains("#BOUNDS")) {
this.writerHandler = new BoundsSVGHandler(this.writerHandler);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -