/** * Creates a image of the WFS layer's data * * @return image */ private BufferedImage draw() { MapContent content = new MapContent(); MapViewport viewport = new MapViewport(); CoordinateReferenceSystem crs = location.getCrs(); ReferencedEnvelope bounds = location.getEnvelope(); Rectangle screenArea; if (isTile && bufferSize != 0.0d) { double width = (location.getRight() - location.getLeft()) / 2 * bufferSize; double height = (location.getTop() - location.getBottom()) / 2 * bufferSize; bounds = location.createEnlargedEnvelope(width, height); screenArea = new Rectangle(0, 0, bufferedImageWidth, bufferedImageHeight); } else { screenArea = new Rectangle(0, 0, imageWidth, imageHeight); // image size } viewport.setCoordinateReferenceSystem(crs); viewport.setScreenArea(screenArea); viewport.setBounds(bounds); viewport.setMatchingAspectRatio(true); if (features.size() > 0) { Layer featureLayer = new FeatureLayer(features, style); content.addLayer(featureLayer); } content.setViewport(viewport); return saveImage(content); }
@Override public void draw(Graphics2D g2d, MapContent map, MapViewport viewport) { if (map != null && this.map == null) { this.map = map; } if (viewport == null) { viewport = map.getViewport(); // use the map viewport if one has not been provided } if (viewport == null || viewport.getScreenArea() == null) { return; // renderer is not set up for use yet } if (collection != null && signsdb == null) { signsdb = new Hashtable(); for (Iterator it1 = collection.iterator(); it1.hasNext(); ) { SimpleFeature feature = (SimpleFeature) it1.next(); StructOffset offset = new StructOffset(0, 0, -1); signsdb.put(feature, offset); } } // 消除线条锯齿 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); DefaultFeatureCollection collection = this.getCollection(); // 遍历得到每个SimpleFeature for (Iterator it1 = collection.iterator(); it1.hasNext(); ) { SimpleFeature feature = (SimpleFeature) it1.next(); String id = feature.getID(); DirectPosition2D geoCoords = null; DirectPosition2D signCoords = null; Point point = (Point) feature.getAttribute("the_geom"); if (point != null) { // 保存前一次刷新所处的位置 double x = point.getX(); double y = point.getY(); geoCoords = new DirectPosition2D(x, y); } // 经纬度坐标转换为屏幕坐标 MathTransform transform_math; try { transform_math = CRS.findMathTransform( DefaultGeographicCRS.WGS84, viewport.getCoordinateReferenceSystem(), true); transform_math.transform(geoCoords, geoCoords); AffineTransform transform_affine = viewport.getWorldToScreen(); transform_affine.transform(geoCoords, geoCoords); g2d.setColor(Color.CYAN); g2d.drawOval((int) geoCoords.x, (int) geoCoords.y, 10, 10); // 如果被选中,填充空心成实体 if (id.equals(selectedId)) g2d.fillOval((int) geoCoords.x, (int) geoCoords.y, 10, 10); if (signsdb.get(feature) != null) { StructOffset offset = (StructOffset) (signsdb.get(feature)); double offsetX = offset.getX(); double offsetY = offset.getY(); signCoords = new DirectPosition2D(geoCoords.x + offsetX, geoCoords.y + offsetY); } if (signCoords != null) { // 坐标默认偏移量<-25,-60> int width = 60, height = 40; g2d.setColor(Color.CYAN); g2d.drawLine( (int) geoCoords.x + 5, (int) geoCoords.y + 5, (int) signCoords.x + offsetX + width / 2, (int) signCoords.y + offsetY + height / 2); g2d.setColor(Color.BLACK); g2d.drawRect((int) signCoords.x + offsetX, (int) signCoords.y + offsetY, width, height); g2d.setColor(Color.ORANGE); g2d.fillRect((int) signCoords.x + offsetX, (int) signCoords.y + offsetY, width, height); g2d.setColor(Color.RED); String signsText = (String) feature.getAttribute("name"); g2d.drawString( signsText, (int) signCoords.x + offsetX + 2, (int) signCoords.y + offsetY + 12); } } catch (Exception e) { e.printStackTrace(); } } // 绘制指南针 // int x[] = {20,10,20,30}; // int y[] = {10,20,30,20}; // int nPoint = 4; // g2d.drawPolygon(x, y, nPoint); String APP_PATH = ""; Bundle bundle = Platform.getBundle("MapControl"); try { APP_PATH = FileLocator.resolve(bundle.getEntry("")).getPath().substring(1) + "icons/map/compass.png"; } catch (Exception e) { System.out.println(e.getMessage()); System.exit(1); } Image src = Toolkit.getDefaultToolkit().getImage(APP_PATH); double dx = 0.72, dy = 0.72; double x = 10, y = 10; // 指南针左上角 g2d.rotate( -MainMapViewPart.rotateAngle + (Math.PI / 360) * 40, x + dx * src.getWidth(null) / 2, y + dy * src.getHeight(null) / 2); AffineTransform affine = new AffineTransform(); affine.setToTranslation(x, y); affine.scale(dx, dy); // 缩放图片 g2d.drawImage(src, affine, null); }