📄 kmlvectortransformer.java
字号:
}
}
for (int i = 0; i < symbolizers.length; i++) {
Symbolizer symbolizer = symbolizers[i];
LOGGER.finer(new StringBuffer("Applying symbolizer ").append(symbolizer).toString());
//create a 2-D style
Style2D style = styleFactory.createStyle(feature, symbolizer, scaleRange);
//split out each type of symbolizer
if (symbolizer instanceof TextSymbolizer) {
encodeTextStyle((TextStyle2D) style, (TextSymbolizer) symbolizer);
}
if (symbolizer instanceof PolygonSymbolizer) {
encodePolygonStyle((PolygonStyle2D) style, (PolygonSymbolizer) symbolizer, forceOutline);
}
if (symbolizer instanceof LineSymbolizer) {
encodeLineStyle((LineStyle2D) style, (LineSymbolizer) symbolizer);
}
if (symbolizer instanceof PointSymbolizer) {
encodePointStyle(style, (PointSymbolizer) symbolizer);
}
}
}
/**
* Encodes a KML IconStyle + PolyStyle from a polygon style and symbolizer.
*/
protected void encodePolygonStyle(PolygonStyle2D style, PolygonSymbolizer symbolizer, boolean forceOutline) {
//star the polygon style
start("PolyStyle");
//fill
if (symbolizer.getFill() != null) {
//get opacity
double opacity = SLD.opacity(symbolizer.getFill());
if (Double.isNaN(opacity)) {
//none specified, default to full opacity
opacity = 1.0;
}
encodeColor(SLD.color(symbolizer.getFill()), opacity);
} else {
//make it transparent
encodeColor("00aaaaaa");
}
//outline
if (symbolizer.getStroke() != null || forceOutline) {
element("outline", "1");
} else {
element("outline", "0");
}
end("PolyStyle");
//if stroke specified add line style as well
if (symbolizer.getStroke() != null) {
start("LineStyle");
//opacity
double opacity = SLD.opacity(symbolizer.getStroke());
if (Double.isNaN(opacity)) {
//none specified, default to full opacity
opacity = 1.0;
}
encodeColor(colorToHex((Color) style.getContour(), opacity));
//width
int width = SLD.width(symbolizer.getStroke());
if (width != SLD.NOTFOUND) {
element("width", Integer.toString(width));
}
end("LineStyle");
}
}
/**
* Encodes a KML IconStyle + LineStyle from a polygon style and symbolizer.
*/
protected void encodeLineStyle(LineStyle2D style, LineSymbolizer symbolizer) {
start("LineStyle");
//stroke
if (symbolizer.getStroke() != null) {
//opacity
double opacity = SLD.opacity(symbolizer.getStroke());
if (Double.isNaN(opacity)) {
//default to full opacity
opacity = 1.0;
}
encodeColor((Color) style.getContour(), opacity);
//width
int width = SLD.width(symbolizer.getStroke());
if (width != SLD.NOTFOUND) {
element("width", Integer.toString(width));
}
} else {
//default
encodeColor("ffaaaaaa");
element("width", "1");
}
end("LineStyle");
}
/**
* Encodes a KML IconStyle from a point style and symbolizer.
*/
protected void encodePointStyle(Style2D style, PointSymbolizer symbolizer) {
start("IconStyle");
if (style instanceof MarkStyle2D) {
Mark mark = SLD.mark(symbolizer);
if (mark != null) {
double opacity = SLD.opacity(mark.getFill());
if (Double.isNaN(opacity)) {
//default to full opacity
opacity = 1.0;
}
if(mark.getFill() != null) {
final Color color = SLD.color(mark.getFill());
encodeColor(color, opacity);
}
} else {
//default
encodeColor("ffaaaaaa");
}
} else {
//default
encodeColor("ffaaaaaa");
}
element("colorMode", "normal");
// placemark icon
String iconHref = null;
//if the point symbolizer uses an external graphic use it
if ((symbolizer.getGraphic() != null)
&& (symbolizer.getGraphic().getExternalGraphics() != null)
&& (symbolizer.getGraphic().getExternalGraphics().length > 0)) {
ExternalGraphic graphic = symbolizer.getGraphic().getExternalGraphics()[0];
try {
if ("file".equals(graphic.getLocation().getProtocol())) {
//it is a local file, reference locally from "styles" directory
File file = new File(graphic.getLocation().getFile());
iconHref = RequestUtils.baseURL(mapContext.getRequest()
.getHttpServletRequest())
+ "styles/" + file.getName();
} else if ( "http".equals(graphic.getLocation().getProtocol()) ) {
iconHref = graphic.getLocation().toString();
} else {
// TODO: should we check for http:// and use it directly?
//other protocols?
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error processing external graphic:" + graphic, e);
}
}
if (iconHref == null) {
iconHref = "http://maps.google.com/mapfiles/kml/pal4/icon25.png";
}
start("Icon");
element("href", iconHref);
end("Icon");
end("IconStyle");
}
/**
* Encodes a KML LabelStyle from a text style and symbolizer.
*/
protected void encodeTextStyle(TextStyle2D style, TextSymbolizer symbolizer) {
start("LabelStyle");
if (symbolizer.getFill() != null) {
double opacity = SLD.opacity(symbolizer.getFill());
if (Double.isNaN(opacity)) {
//default to full opacity
opacity = 1.0;
}
encodeColor(SLD.color(symbolizer.getFill()), opacity);
} else {
//default
encodeColor("ffffffff");
}
end("LabelStyle");
}
/**
* Encodes a KML Placemark from a feature and optional name.
*/
protected void encodePlacemark(Feature feature, FeatureTypeStyle[] styles) {
Geometry geometry = featureGeometry(feature);
Coordinate centroid = geometryCentroid(geometry);
start("Placemark", KMLUtils.attributes(new String[] { "id", feature.getID() }));
//encode name + description only if kmattr was specified
if (mapContext.getRequest().getKMattr()) {
//name
try {
encodePlacemarkName( feature, styles );
}
catch( Exception e ) {
String msg = "Error occured processing 'title' template.";
LOGGER.log( Level.WARNING, msg, e );
}
//description
try {
encodePlacemarkDescription(feature);
} catch (Exception e) {
String msg = "Error occured processing 'description' template.";
LOGGER.log( Level.WARNING, msg, e );
}
}
//look at
encodePlacemarkLookAt(centroid);
//time
try {
encodePlacemarkTime(feature);
} catch (Exception e) {
String msg = "Error occured processing 'time' template: " + e.getMessage();
LOGGER.log( Level.WARNING, msg );
LOGGER.log( Level.FINE, "", e );
}
//style reference
element("styleUrl", "#GeoServerStyle" + feature.getID());
// encode extended data (kml 2.2)
encodeExtendedData(feature);
//geometry
encodePlacemarkGeometry(geometry, centroid);
end("Placemark");
}
/**
* Encodes kml 2.2 extended data section
* @param feature
*/
protected void encodeExtendedData(Feature feature) {
// code at the moment is in KML3VectorTransfomer
}
/**
* Encodes a KML Placemark name from a feature by processing a
* template.
*/
protected void encodePlacemarkName(Feature feature, FeatureTypeStyle[] styles )
throws IOException {
//order to use when figuring out what the name / label of a
// placemark should be:
// 1. the title template for features
// 2. a text sym with a label from teh sld
// 3. nothing ( do not use fid )
String title = template.title( feature );
//ensure not empty and != fid
if ( title == null || "".equals( title ) || feature.getID().equals( title ) ) {
//try sld
StringBuffer label = new StringBuffer();
for ( int i = 0; i < styles.length; i++ ) {
Rule[] rules = filterRules(styles[i], feature );
for ( int j = 0; j < rules.length; j++ ) {
Symbolizer[] syms = rules[j].getSymbolizers();
for ( int k = 0; k < syms.length; k++) {
if ( syms[k] instanceof TextSymbolizer ) {
Expression e = SLD.textLabel((TextSymbolizer) syms[k]);
Object object = e.evaluate(feature);
String value = null;
if (object instanceof String) {
value = (String) object;
} else {
if (object != null) {
value = object.toString();
}
}
if ((value != null) && !"".equals(value.trim())) {
label.append(value);
}
}
}
}
}
if ( label.length() > 0 ) {
title = label.toString();
}
else {
title = null;
}
}
if ( title != null ) {
start("name");
cdata(title);
end("name");
}
}
/**
* Encodes a KML Placemark description from a feature by processing a
* template.
*/
protected void encodePlacemarkDescription(Feature feature)
throws IOException {
String description = template.description( feature );
if (description != null) {
start("description");
cdata(description);
end("description");
}
}
/**
* Encods a KML Placemark LookAt from a geometry + centroid.
*/
protected void encodePlacemarkLookAt(Coordinate centroid) {
start("LookAt");
element("longitude", Double.toString(centroid.x));
element("latitude", Double.toString(centroid.y));
element("range", "700");
element("tilt", "10.0");
element("heading", "10.0");
end("LookAt");
}
/**
* Encodes a KML TimePrimitive geometry from a feature.
*/
protected void encodePlacemarkTime(Feature feature) throws IOException {
try {
String[] time = new FeatureTimeTemplate(template).execute(feature);
if ( time.length == 0 ) {
return;
}
if ( time.length == 1 ) {
String datetime = encodeDateTime(time[0]);
if ( datetime != null ) {
//timestamp case
start("TimeStamp");
element("when", datetime );
end("TimeStamp");
}
}
else {
//timespan case
String begin = encodeDateTime(time[0]);
String end = encodeDateTime(time[1]);
if (!(begin == null && end == null)) {
start("TimeSpan");
if ( begin != null ) {
element("begin", begin);
}
if ( end != null ) {
element("end", end);
}
end("TimeSpan");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -