我想在Java应用程序(基于Eclipse)上使用带有Geotools库的KML创建地图图层。从文件中读取KML数据是可行的,但是我不知道怎么做才能从中创建一层。
问题(我不确定)可能是将KML渲染为地图:
public void render(final Graphics2D g2d) {
if (map != null) {
if (log.isDebug() && Component.traceLevel == 1) {
log.debug("KmlLayer(" + getName() + ").render()");
}
final GTRenderer renderer = new StreamingRenderer();
Rectangle imageBounds = null;
renderer.setMapContent(map);
imageBounds = new Rectangle(0, 0, viewport.getWidth(), viewport.getHeight());
Point mapPos = viewport.getMapPosition();
Point endPos = viewport.getMapPosition();
endPos.x += imageBounds.width;
endPos.y += imageBounds.height;
Projection proj = viewport.getProjection();
Point2D p1 = proj.getLongitudeLatitude(mapPos, viewport.getZoom());
Point2D p2 = proj.getLongitudeLatitude(endPos, viewport.getZoom());
CoordinateReferenceSystem sourceCRS = DefaultGeographicCRS.WGS84;
CoordinateReferenceSystem destCRS = null;
ReferencedEnvelope mapBounds = null;
try {
// This is the WKT (Well Known Text) Definition for the Mercator
// Projection. Source: http://spatialreference.org/ref/
// You need this definition because geotools does not include it
destCRS = CRS.parseWKT("PROJCS[\"WGS 84 / World Mercator\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\"," +
"SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]," +
"PROJECTION[\"Mercator\"],PARAMETER[\"central_meridian\",0],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0]," + "UNIT[\"Meter\",1]]");
// At this point the Envelope for the map is created. In the same
// step it has to be transformed from WGS 84 to Mercator
mapBounds = new ReferencedEnvelope(p1.getX(), p2.getX(), p1.getY(), p2.getY(), sourceCRS).transform(destCRS, true);
renderer.paint(g2d, imageBounds, mapBounds);
} catch (FactoryException e) {
if (log.isDebug()) {
log.debug("KmlLayer(" + getName() + ").render(): FactoryException caught!", e);
}
}