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

📄 kmlwriter.java

📁 电子地图服务器,搭建自己的地图服务
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

				styleString.append("</LineStyle>");
			}

			write(styleString.toString());
		} else if (style instanceof LineStyle2D
				&& sym instanceof LineSymbolizer) {
			if (((LineStyle2D) style).getStroke() == null) {
				LOGGER.info("Empty LineSymbolizer, using default stroke.");
			}

			LineSymbolizer lineSym = (LineSymbolizer) sym;

			// ** LABEL **
			final StringBuffer styleString = new StringBuffer();
			styleString.append("<IconStyle>");

			if (!mapContext.getRequest().getKMattr()) { // if they don't want
														// attributes
				styleString.append("<color>#00ffffff</color>"); // fully
																// transparent
			}

			styleString.append("</IconStyle>");

			// ** LINE **
			styleString.append("<LineStyle><color>");

			if (lineSym.getStroke() != null) {
				int opacity = 255;

				if (lineSym.getStroke().getOpacity() != null) {
					float op = getOpacity(lineSym.getStroke().getOpacity());
					opacity = (new Float(255 * op)).intValue();
				}

				Paint p = ((LineStyle2D) style).getContour();

				if (p instanceof Color) {
					styleString.append("#").append(intToHex(opacity)).append(
							colorToHex((Color) p)); // transparancy needs to
													// come from the opacity
													// value.
				} else {
					styleString.append("#ffaaaaaa"); // should not occure in
														// normal parsing
				}

				styleString.append("</color>");

				// stroke width
				if (lineSym.getStroke().getWidth() != null) {
					int width = getWidth(lineSym.getStroke().getWidth());
					styleString.append("<width>").append(width).append(
							"</width>");
				}
			} else // no style defined, so use default
			{
				styleString.append("#ffaaaaaa");
				styleString.append("</color><width>1</width>");
			}

			styleString.append("</LineStyle>");

			write(styleString.toString());
		} else if (style instanceof TextStyle2D
				&& sym instanceof TextSymbolizer) {
			final StringBuffer styleString = new StringBuffer();
			TextSymbolizer textSym = (TextSymbolizer) sym;

			styleString.append("<LabelStyle><color>");

			if (textSym.getFill() != null) {
				int opacity = 255;

				if (textSym.getFill().getOpacity() != null) {
					float op = getOpacity(textSym.getFill().getOpacity());
					opacity = (new Float(255 * op)).intValue();
				}

				Paint p = ((TextStyle2D) style).getFill();

				if (p instanceof Color) {
					styleString.append("#").append(intToHex(opacity)).append(
							colorToHex((Color) p)); // transparancy needs to
													// come from the opacity
													// value.
				} else {
					styleString.append("#ffaaaaaa"); // should not occure in
														// normal parsing
				}

				styleString.append("</color></LabelStyle>");
			} else {
				styleString.append("#ffaaaaaa");
				styleString.append("</color></LabelStyle>");
			}

			write(styleString.toString());
		} else if (style instanceof MarkStyle2D
				&& sym instanceof PointSymbolizer) {
			// we can sorta style points. Just with color however.
			final StringBuffer styleString = new StringBuffer();
			PointSymbolizer pointSym = (PointSymbolizer) sym;

			styleString.append("<IconStyle><color>");

			if ((pointSym.getGraphic() != null)
					&& (pointSym.getGraphic().getMarks() != null)) {
				Mark[] marks = pointSym.getGraphic().getMarks();

				if ((marks.length > 0) && (marks[0] != null)) {
					Mark mark = marks[0];

					int opacity = 255;

					if (mark.getFill().getOpacity() != null) {
						float op = getOpacity(mark.getFill().getOpacity());
						opacity = (new Float(255 * op)).intValue();
					}

					Paint p = ((MarkStyle2D) style).getFill();

					if (p instanceof Color) {
						styleString.append("#").append(intToHex(opacity))
								.append(colorToHex((Color) p)); // transparancy
																// needs to come
																// from the
																// opacity
																// value.
					} else {
						styleString.append("#ffaaaaaa"); // should not occure
															// in normal parsing
					}
				} else {
					styleString.append("#ffaaaaaa");
				}
			} else {
				styleString.append("#ffaaaaaa");
			}

			styleString.append("</color>");
			styleString.append("<colorMode>normal</colorMode>");
			styleString.append("<Icon><href>root://icons/palette-4.png</href>");
			styleString.append("<x>32</x><y>128</y><w>32</w><h>32</h></Icon>");

			styleString.append("</IconStyle>");

			write(styleString.toString());
		}
	}

	/**
	 * @param href
	 * @param id
	 * @throws IOException
	 */
	private void writeRasterStyle(final String href, final String id)
			throws IOException {
		final StringBuffer styleString = new StringBuffer();
		styleString.append("<Style id=\"GeoServerStyle").append(id).append(
				"\">");
		styleString.append("<IconStyle><Icon><href>").append(href).append(
				"</href><viewRefreshMode>never</viewRefreshMode>").append(
				"<viewBoundScale>0.75</viewBoundScale><w>").append(
				this.mapContext.getMapWidth()).append("</w><h>").append(
				this.mapContext.getMapHeight()).append(
				"</h></Icon></IconStyle>");
		styleString
				.append("<PolyStyle><fill>0</fill><outline>0</outline></PolyStyle>");
		styleString.append("</Style>");

		write(styleString.toString());
	}

	private boolean isWithinScale(Rule r) {
		double min = r.getMinScaleDenominator();
		double max = r.getMaxScaleDenominator();

		if (((min - TOLERANCE) <= scaleDenominator)
				&& ((max + TOLERANCE) >= scaleDenominator)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Finds the geometric attribute requested by the symbolizer
	 * 
	 * @param f
	 *            The feature
	 * @param s
	 *            The symbolizer
	 * @return The geometry requested in the symbolizer, or the default geometry
	 *         if none is specified
	 */
	private com.vividsolutions.jts.geom.Geometry findGeometry(Feature f,
			Symbolizer s) {
		String geomName = getGeometryPropertyName(s);

		// get the geometry
		Geometry geom;

		if (geomName == null) {
			geom = f.getDefaultGeometry();
		} else {
			geom = (com.vividsolutions.jts.geom.Geometry) f
					.getAttribute(geomName);
		}

		// if the symbolizer is a point symbolizer generate a suitable location
		// to place the
		// point in order to avoid recomputing that location at each rendering
		// step
		if (s instanceof PointSymbolizer) {
			geom = getCentroid(geom); // djb: major simpificatioN
		}

		return geom;
	}

	/**
	 * Returns the default geometry in the feature.
	 * 
	 * @param f
	 *            feature to find the geometry in
	 * @return
	 */
	private com.vividsolutions.jts.geom.Geometry findGeometry(Feature f) {
		// get the geometry
		Geometry geom = f.getDefaultGeometry();

		// CoordinateReferenceSystem sourceCRS =
		// f.getFeatureType().getDefaultGeometry().getCoordinateSystem();
		if (!CRS.equalsIgnoreMetadata(sourceCrs, this.mapContext
				.getCoordinateReferenceSystem())) {
			try {
				MathTransform transform = CRS.findMathTransform(sourceCrs,
						this.mapContext.getCoordinateReferenceSystem(), true);
				geom = JTS.transform(geom, transform);
			} catch (MismatchedDimensionException e) {
				LOGGER.severe(e.getLocalizedMessage());
			} catch (TransformException e) {
				LOGGER.severe(e.getLocalizedMessage());
			} catch (FactoryException e) {
				LOGGER.severe(e.getLocalizedMessage());
			}
		}

		return geom;
	}

	/**
	 * Finds the centroid of the input geometry if input = point, line, polygon
	 * --> return a point that represents the centroid of that geom if input =
	 * geometry collection --> return a multipoint that represents the centoid
	 * of each sub-geom
	 * 
	 * @param g
	 * @return
	 */
	public Geometry getCentroid(Geometry g) {
		if (g instanceof GeometryCollection) {
			GeometryCollection gc = (GeometryCollection) g;
			Coordinate[] pts = new Coordinate[gc.getNumGeometries()];

			for (int t = 0; t < gc.getNumGeometries(); t++) {
				pts[t] = gc.getGeometryN(t).getCentroid().getCoordinate();
			}

			return g.getFactory().createMultiPoint(pts);
		} else {
			return g.getCentroid();
		}
	}

	/**
	 * Finds the geometric attribute coordinate reference system
	 * 
	 * @param f
	 *            The feature
	 * @param s
	 *            The symbolizer
	 * @return The geometry requested in the symbolizer, or the default geometry
	 *         if none is specified
	 */
	private org.opengis.referencing.crs.CoordinateReferenceSystem findGeometryCS(
			Feature f, Symbolizer s) {
		String geomName = getGeometryPropertyName(s);

		if (geomName != null) {
			return ((GeometryAttributeType) f.getFeatureType()
					.getAttributeType(geomName)).getCoordinateSystem();
		} else {
			return ((GeometryAttributeType) f.getFeatureType()
					.getDefaultGeometry()).getCoordinateSystem();
		}
	}

	/**
	 * Utility method to find which geometry property is referenced by a given
	 * symbolizer.
	 * 
	 * @param s
	 *            The symbolizer
	 * @TODO: this is c&p from lite renderer code as the method was private
	 *        consider moving to a public unility class.
	 */
	private String getGeometryPropertyName(Symbolizer s) {
		String geomName = null;

		// TODO: fix the styles, the getGeometryPropertyName should probably be
		// moved into an
		// interface...
		if (s instanceof PolygonSymbolizer) {
			geomName = ((PolygonSymbolizer) s).getGeometryPropertyName();
		} else if (s instanceof PointSymbolizer) {
			geomName = ((PointSymbolizer) s).getGeometryPropertyName();
		} else if (s instanceof LineSymbolizer) {
			geomName = ((LineSymbolizer) s).getGeometryPropertyName();
		} else if (s instanceof TextSymbolizer) {
			geomName = ((TextSymbolizer) s).getGeometryPropertyName();
		}

		return geomName;
	}

	/**
	 * Utility method to convert an int into hex, padded to two characters.
	 * handy for generating colour strings.
	 * 
	 * @param i
	 *            Int to convert
	 * @return String a two character hex representation of i NOTE: this is a
	 *         utility method and should be put somewhere more useful.
	 */
	protected static String intToHex(int i) {
		String prelim = Integer.toHexString(i);

		if (prelim.length() < 2) {
			prelim = "0" + prelim;
		}

		return prelim;
	}

	/**
	 * Utility method to convert a Color into a KML color ref
	 * 
	 * @param c
	 *            The color to convert
	 * @return A string in BBGGRR format - note alpha must be prefixed seperatly
	 *         before use.
	 */
	private String colorToHex(Color c) {
		return intToHex(c.getBlue()) + intToHex(c.getGreen())
				+ intToHex(c.getRed());
	}

	/**
	 * Borrowed from StreamingRenderer
	 * 
	 * @param sym
	 * @return
	 */
	private float getOpacity(final Symbolizer sym) {
		float alpha = 1.0f;
		Expression exp = null;

		if (sym instanceof PolygonSymbolizer) {
			exp = ((PolygonSymbolizer) sym).getFill().getOpacity();
		} else if (sym instanceof LineSymbolizer) {
			exp = ((LineSymbolizer) sym).getStroke().getOpacity();
		} else if (sym instanceof PointSymbolizer) {
			exp = ((PointSymbolizer) sym).getGraphic().getOpacity();
		} else if (sym instanceof TextSymbolizer) {
			exp = ((TextSymbolizer) sym).getFill().getOpacity();
		} else {
			LOGGER.info("Symbolizer not matched; was of class: " + sym);
		}

		if (exp == null) {
			LOGGER.info("Could not determine proper symbolizer opacity.");

			return alpha;
		}

		Float number = (Float) exp.evaluate(null, Float.class);

		if (number == null) {
			return alpha;
		}

		return number.floatValue();
	}

	private float getOpacity(final Expression exp) {
		float alpha = 1.0f;

		Float number = (Float) exp.evaluate(null, Float.class);

		if (number == null) {
			return alpha;
		}

		return number.floatValue();
	}

	private int getWidth(final Expression exp) {
		int defaultWidth = 1;

		Integer number = (Integer) exp.evaluate(null, Integer.class);

		if (number == null) {
			return defaultWidth;
		}

		return number.intValue();
	}
}

⌨️ 快捷键说明

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