36 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
37 |
import java.awt.geom.AffineTransform; |
import java.awt.geom.AffineTransform; |
38 |
import java.awt.image.BufferedImage; |
import java.awt.image.BufferedImage; |
39 |
|
import java.awt.image.ColorModel; |
40 |
|
import java.awt.image.ComponentColorModel; |
41 |
|
import java.awt.image.DataBuffer; |
42 |
import java.io.File; |
import java.io.File; |
43 |
import java.io.FileNotFoundException; |
import java.io.FileNotFoundException; |
44 |
import java.io.FileWriter; |
import java.io.FileWriter; |
47 |
import java.util.HashMap; |
import java.util.HashMap; |
48 |
import java.util.List; |
import java.util.List; |
49 |
import java.util.Map; |
import java.util.Map; |
|
import java.util.Set; |
|
50 |
import java.util.SortedMap; |
import java.util.SortedMap; |
51 |
import java.util.TreeMap; |
import java.util.TreeMap; |
52 |
|
|
53 |
|
import javax.swing.BorderFactory; |
54 |
import javax.swing.Box; |
import javax.swing.Box; |
55 |
import javax.swing.BoxLayout; |
import javax.swing.BoxLayout; |
56 |
import javax.swing.ImageIcon; |
import javax.swing.ImageIcon; |
57 |
import javax.swing.JLabel; |
import javax.swing.JLabel; |
58 |
|
|
59 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
60 |
|
import org.geotools.coverage.grid.GeneralGridEnvelope; |
61 |
import org.geotools.coverage.grid.GridCoverage2D; |
import org.geotools.coverage.grid.GridCoverage2D; |
62 |
|
import org.geotools.coverage.grid.GridGeometry2D; |
63 |
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader; |
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader; |
64 |
|
import org.geotools.coverage.grid.io.AbstractGridFormat; |
65 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
66 |
|
import org.geotools.gce.imagepyramid.ImagePyramidReader; |
67 |
|
import org.geotools.geometry.jts.ReferencedEnvelope; |
68 |
import org.geotools.map.DefaultMapLayer; |
import org.geotools.map.DefaultMapLayer; |
69 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
70 |
|
import org.geotools.parameter.Parameter; |
71 |
import org.geotools.renderer.lite.gridcoverage2d.GridCoverageRenderer; |
import org.geotools.renderer.lite.gridcoverage2d.GridCoverageRenderer; |
72 |
import org.geotools.styling.ColorMap; |
import org.geotools.styling.ColorMap; |
73 |
import org.geotools.styling.ColorMapEntry; |
import org.geotools.styling.ColorMapEntry; |
80 |
import org.jdom.input.SAXBuilder; |
import org.jdom.input.SAXBuilder; |
81 |
import org.jdom.output.XMLOutputter; |
import org.jdom.output.XMLOutputter; |
82 |
import org.opengis.feature.simple.SimpleFeatureType; |
import org.opengis.feature.simple.SimpleFeatureType; |
83 |
|
import org.opengis.parameter.GeneralParameterValue; |
84 |
|
|
85 |
import schmitzm.geotools.JTSUtil; |
import schmitzm.geotools.JTSUtil; |
86 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
980 |
hbox.add(iconLabel); |
hbox.add(iconLabel); |
981 |
hbox.add(Box.createHorizontalStrut(3)); |
hbox.add(Box.createHorizontalStrut(3)); |
982 |
|
|
|
// **************************************************************************** |
|
|
// The labeltext is read from the SLD. Its either the title |
|
|
// directly, or interpreted as OneLine Code |
|
|
// **************************************************************************** |
|
|
// final String rawText = |
|
|
// rule.getDescription().getTitle().toString(); |
|
|
final String rawText = rule.getDescription().getTitle() |
|
|
.toString(); |
|
|
|
|
983 |
Translation labelT = new Translation(); |
Translation labelT = new Translation(); |
984 |
labelT.fromOneLine(rawText); |
labelT.fromOneLine(rule.getDescription().getTitle()); |
985 |
|
|
986 |
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
987 |
hbox.add(classTitleLabel); |
hbox.add(classTitleLabel); |
998 |
/** |
/** |
999 |
* Creates a |
* Creates a |
1000 |
* |
* |
1001 |
* @param styledGrid |
* @param styledRaster |
1002 |
* @param iconHeight |
* @param iconHeight |
1003 |
* @param iconWidth |
* @param iconWidth |
1004 |
* @return |
* @return |
1005 |
*/ |
*/ |
1006 |
public static Box createLegendPanel(StyledRasterInterface<?> styledGrid, |
public static Box createLegendPanel(StyledRasterInterface<?> styledRaster, |
1007 |
int iconWidth, int iconHeight) { |
int iconWidth, int iconHeight) { |
1008 |
RasterLegendData rasterLegendData = styledGrid.getLegendMetaData(); |
|
1009 |
Set<Double> legendRasterValues = rasterLegendData.keySet(); |
/** |
1010 |
|
* Determine whether a Style is responsible for the coloring |
1011 |
|
*/ |
1012 |
|
ColorModel colorModel = null; |
1013 |
|
if (!isStyleable(styledRaster) |
1014 |
|
|| (isStyleable(styledRaster) && styledRaster.getStyle() == null)) { |
1015 |
|
colorModel = getColorModel(styledRaster); |
1016 |
|
} |
1017 |
|
|
1018 |
|
RasterLegendData rasterLegendData = styledRaster.getLegendMetaData(); |
1019 |
|
List<Double> legendRasterValues = rasterLegendData.getSortedKeys(); |
1020 |
Map<Double, GridCoverage2D> sampleRasters = rasterLegendData |
Map<Double, GridCoverage2D> sampleRasters = rasterLegendData |
1021 |
.createSampleRasters(); |
.createSampleRasters(); |
1022 |
|
|
1043 |
for (Double rValue : legendRasterValues) { |
for (Double rValue : legendRasterValues) { |
1044 |
|
|
1045 |
/** |
/** |
|
* The coverage contains only one value of value rValue |
|
|
*/ |
|
|
GridCoverage2D sampleCov = sampleRasters.get(rValue); |
|
|
GridCoverageRenderer renderer; |
|
|
try { |
|
|
renderer = new GridCoverageRenderer(sampleCov |
|
|
.getCoordinateReferenceSystem(), JTSUtil |
|
|
.createEnvelope(sampleCov.getEnvelope()), |
|
|
new Rectangle(iconWidth, iconHeight), |
|
|
(AffineTransform) null); |
|
|
} catch (Exception e1) { |
|
|
throw new RuntimeException( |
|
|
"Creating the GridCoverageRenderer:", e1); |
|
|
} |
|
|
|
|
|
/** |
|
1046 |
* |
* |
1047 |
*/ |
*/ |
1048 |
Box hbox = new Box(BoxLayout.X_AXIS) { |
Box hbox = new Box(BoxLayout.X_AXIS) { |
1078 |
ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB); |
ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB); |
1079 |
Graphics2D graphics = buffImage.createGraphics(); |
Graphics2D graphics = buffImage.createGraphics(); |
1080 |
|
|
1081 |
/** |
if (colorModel != null) { |
|
* Iterate over all FeatureTypeStyles. |
|
|
*/ |
|
|
// for (FeatureTypeStyle ftStyle : styledGrid.getStyle() |
|
|
// .featureTypeStyles()) { |
|
|
// One child-node for every rule |
|
|
// List<Rule> rules = ftStyle.rules(); |
|
|
|
|
|
// for (Rule rule : rules) { |
|
|
List<RasterSymbolizer> rSymbols = StylingUtil |
|
|
.getRasterSymbolizers(styledGrid.getStyle()); |
|
1082 |
|
|
|
for (RasterSymbolizer symbolizer : rSymbols) { |
|
|
// LOGGER.debug("Creating a new Legend Image for RUle name = |
|
|
// "+rule.getName()); |
|
1083 |
try { |
try { |
1084 |
renderer.paint(graphics, sampleCov, symbolizer); |
Object inData = null; |
1085 |
|
switch (colorModel.getTransferType()) { |
1086 |
|
case DataBuffer.TYPE_BYTE: |
1087 |
|
inData = new byte[] { rValue.byteValue() }; |
1088 |
|
break; |
1089 |
|
case DataBuffer.TYPE_USHORT: |
1090 |
|
inData = new short[] { rValue.shortValue() }; |
1091 |
|
break; |
1092 |
|
case DataBuffer.TYPE_INT: |
1093 |
|
inData = new int[] { rValue.intValue() }; |
1094 |
|
break; |
1095 |
|
case DataBuffer.TYPE_SHORT: |
1096 |
|
inData = new short[] { rValue.shortValue() }; |
1097 |
|
break; |
1098 |
|
case DataBuffer.TYPE_FLOAT: |
1099 |
|
inData = new float[] { rValue.floatValue() }; |
1100 |
|
break; |
1101 |
|
case DataBuffer.TYPE_DOUBLE: |
1102 |
|
inData = new double[] { rValue.doubleValue() }; |
1103 |
|
break; |
1104 |
|
default: |
1105 |
|
inData = rValue.intValue(); |
1106 |
|
} |
1107 |
|
final Color color = new Color(colorModel.getRGB(inData)); |
1108 |
|
graphics.setBackground(color); |
1109 |
|
graphics.setColor(color); |
1110 |
|
graphics.fillRect(0, 0, ICON_SIZE.width, ICON_SIZE.height); |
1111 |
} catch (Exception e) { |
} catch (Exception e) { |
1112 |
LOGGER.error("Unable to paint " + symbolizer |
LOGGER.debug("Dann nehmen wir halt den GridCoverageRenderer", e); |
1113 |
+ " into the legend image", e); |
colorModel = null; |
1114 |
|
} |
1115 |
|
} |
1116 |
|
|
1117 |
|
if (colorModel == null) { |
1118 |
|
|
1119 |
|
/** |
1120 |
|
* The coverage contains only one value of value rValue |
1121 |
|
*/ |
1122 |
|
GridCoverage2D sampleCov = sampleRasters.get(rValue); |
1123 |
|
GridCoverageRenderer renderer; |
1124 |
|
try { |
1125 |
|
renderer = new GridCoverageRenderer(sampleCov |
1126 |
|
.getCoordinateReferenceSystem(), JTSUtil |
1127 |
|
.createEnvelope(sampleCov.getEnvelope()), |
1128 |
|
new Rectangle(iconWidth, iconHeight), |
1129 |
|
(AffineTransform) null); |
1130 |
|
} catch (Exception e1) { |
1131 |
|
throw new RuntimeException( |
1132 |
|
"Creating the GridCoverageRenderer:", e1); |
1133 |
|
} |
1134 |
|
|
1135 |
|
/** |
1136 |
|
* Iterate over all FeatureTypeStyles. |
1137 |
|
*/ |
1138 |
|
// for (FeatureTypeStyle ftStyle : styledGrid.getStyle() |
1139 |
|
// .featureTypeStyles()) { |
1140 |
|
// One child-node for every rule |
1141 |
|
// List<Rule> rules = ftStyle.rules(); |
1142 |
|
// for (Rule rule : rules) { |
1143 |
|
final Style style = styledRaster.getStyle(); |
1144 |
|
List<RasterSymbolizer> rSymbols = StylingUtil |
1145 |
|
.getRasterSymbolizers(style); |
1146 |
|
|
1147 |
|
for (RasterSymbolizer symbolizer : rSymbols) { |
1148 |
|
// LOGGER.debug("Creating a new Legend Image for RUle |
1149 |
|
// name = |
1150 |
|
// "+rule.getName()); |
1151 |
|
try { |
1152 |
|
renderer.paint(graphics, sampleCov, symbolizer); |
1153 |
|
} catch (Exception ee) { |
1154 |
|
LOGGER.error("Unable to paint " + symbolizer |
1155 |
|
+ " into the legend image", ee); |
1156 |
|
} |
1157 |
|
// } |
1158 |
|
// } |
1159 |
} |
} |
|
// } |
|
|
// } |
|
1160 |
} |
} |
1161 |
|
|
1162 |
ImageIcon legendIcon = new ImageIcon(buffImage); |
ImageIcon legendIcon = new ImageIcon(buffImage); |
1172 |
classTitleLabel.setLabelFor(iconLabel); |
classTitleLabel.setLabelFor(iconLabel); |
1173 |
|
|
1174 |
box.add(hbox); |
box.add(hbox); |
1175 |
|
|
1176 |
|
if (rasterLegendData.getPaintGaps()) { |
1177 |
|
iconLabel |
1178 |
|
.setBorder(BorderFactory.createLineBorder(Color.black)); |
1179 |
|
box.add(Box.createVerticalStrut(3)); |
1180 |
|
} |
1181 |
|
|
1182 |
} |
} |
1183 |
|
|
1184 |
return box; |
return box; |
1185 |
} |
} |
1186 |
|
|
1187 |
|
/** |
1188 |
|
* Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May |
1189 |
|
* return <code>null</code> if the geoobject can not be accessed. |
1190 |
|
*/ |
1191 |
|
public static ColorModel getColorModel(StyledRasterInterface<?> styledGrid) { |
1192 |
|
ColorModel colorModel = null; |
1193 |
|
try { |
1194 |
|
Object geoObject = styledGrid.getGeoObject(); |
1195 |
|
if (geoObject instanceof GridCoverage2D) { |
1196 |
|
GridCoverage2D cov = (GridCoverage2D) geoObject; |
1197 |
|
colorModel = cov.getRenderedImage().getColorModel(); |
1198 |
|
} else if (geoObject instanceof ImagePyramidReader) { |
1199 |
|
|
1200 |
|
Parameter readGG = new Parameter( |
1201 |
|
AbstractGridFormat.READ_GRIDGEOMETRY2D); |
1202 |
|
|
1203 |
|
ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope( |
1204 |
|
styledGrid.getEnvelope(), styledGrid.getCrs()); |
1205 |
|
|
1206 |
|
readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope( |
1207 |
|
new Rectangle(0, 0, 10, 10)), mapExtend)); |
1208 |
|
|
1209 |
|
final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) geoObject; |
1210 |
|
GridCoverage2D cov = (GridCoverage2D) aReader |
1211 |
|
.read(new GeneralParameterValue[] { readGG }); |
1212 |
|
colorModel = cov.getRenderedImage().getColorModel(); |
1213 |
|
} |
1214 |
|
} catch (Exception e) { |
1215 |
|
LOGGER.error("Error reading the colormodel from " + styledGrid); |
1216 |
|
return null; |
1217 |
|
} |
1218 |
|
return colorModel; |
1219 |
|
} |
1220 |
|
|
1221 |
|
/** |
1222 |
|
* @return <code>true</code> if a {@link RasterSymbolizer} can be applied |
1223 |
|
* and will have an effect. Some rasters (e.g. GeoTIFF) can come |
1224 |
|
* with their own {@link ColorModel} and will ignore any |
1225 |
|
* {@link RasterSymbolizer} = SLD. |
1226 |
|
*/ |
1227 |
|
public static boolean isStyleable(StyledRasterInterface<?> styledRaster) { |
1228 |
|
ColorModel colorModel = getColorModel(styledRaster); |
1229 |
|
|
1230 |
|
LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is " |
1231 |
|
+ colorModel.getClass().getSimpleName()); |
1232 |
|
|
1233 |
|
if (colorModel == null) |
1234 |
|
return true; |
1235 |
|
if (colorModel instanceof ComponentColorModel) |
1236 |
|
return true; |
1237 |
|
return false; |
1238 |
|
} |
1239 |
} |
} |