使用Geotools JMapFrame显示图例

时间:2016-01-29 10:21:48

标签: legend geotools

有人可以提供一些关于如何在Geotools的JMapFrame中显示shapefile图例的技巧吗?我已经为shapefile创建了样式,我需要一种方法来告诉用户如何定义样式,这就需要传奇。

有一个包org.geotools.legend。但我不知道如何使用它。

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要遍历Style s FeatureTypeStyles s Rule s Symbolizer并为每个private void drawLegend(BufferedImage img, Rule r) { for (Symbolizer sym : r.symbolizers()) { SimpleFeature feature = null; if (sym instanceof LineSymbolizer) { LineString line = drawer.line(new int[] { 1, 1, 10, 20, 20, 20 }); feature = drawer.feature(line); } else if(sym instanceof PolygonSymbolizer) { Polygon p = drawer.polygon(new int[] { 1, 1, 1, 18, 18, 18, 18, 1, 1,1 }); feature = drawer.feature(p); } else if(sym instanceof PointSymbolizer || sym instanceof TextSymbolizer) { Point p = drawer.point(10, 10); feature = drawer.feature(p); } if(feature == null) continue; drawer.drawDirect(img, feature, r); Graphics2D gr = img.createGraphics(); gr.setColor(Color.BLACK); if (r.getDescription() != null && r.getDescription().getTitle() != null) { gr.drawString(r.getDescription().getTitle().toString(), 20, 18); } } } 绘制代表性功能。类似的东西:

def copyPicture(pict):
  tpict = makeEmptyPicture(getWidth(pict)*2, getHeight(pict)*2)
  for x in range(0,getWidth(pict)):
    for y in range(0,getHeight(pict)):
      srcPix=getPixel(pict, x,y)
      tgtPix=getPixel(tpict,x,y)
      setColor(tgtPix, getColor(srcPix))
  for x in range(0,getWidth(pict)):
    for y in range(0,getHeight(pict)):
      srcPix=getPixel(pict,getWidth(pict)-x-1,y)
      tgtPix=getPixel(tpict,x+getWidth(pict),y)
      setColor(tgtPix, getColor(srcPix))
  for x in range(0,getWidth(tpict)):
    for y in range(0,getHeight(tpict)/2):
      srcPix=getPixel(tpict,x,y)
      tgtPix=getPixel(tpict,x,getHeight(pict)+getHeight(pict)-y-1)
      setColor(tgtPix,getColor(srcPix))
  x1=getWidth(tpict)/2    
  for x in range(0,getWidth(pict),2):
    y1=getHeight(tpict)/2
    for y in range(0,getHeight(pict),2):
      srcPix=getPixel(pict,x,y)
      tgtPix=getPixelAt(tpict,x1,y1)
      setColor(tgtPix, getColor(srcPix))
      y1=y1+1
    x1=x1+1
  repaint(tpict)

然后你可以将这些图像绘制到JPanel或地图上。

有关完整工作的示例,请参阅GeoServer如何创建对getLegendGraphic request的响应。