29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.geotools; |
package skrueger.geotools; |
31 |
|
|
32 |
|
import java.awt.Color; |
33 |
|
import java.awt.Dimension; |
34 |
|
import java.awt.Graphics; |
35 |
|
import java.awt.image.BufferedImage; |
36 |
import java.io.File; |
import java.io.File; |
37 |
import java.io.FileNotFoundException; |
import java.io.FileNotFoundException; |
38 |
import java.io.FileWriter; |
import java.io.FileWriter; |
44 |
import java.util.SortedMap; |
import java.util.SortedMap; |
45 |
import java.util.TreeMap; |
import java.util.TreeMap; |
46 |
|
|
47 |
|
import javax.swing.Box; |
48 |
|
import javax.swing.BoxLayout; |
49 |
|
import javax.swing.ImageIcon; |
50 |
|
import javax.swing.JLabel; |
51 |
|
|
52 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
53 |
import org.geotools.coverage.grid.GridCoverage2D; |
import org.geotools.coverage.grid.GridCoverage2D; |
54 |
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader; |
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader; |
57 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
58 |
import org.geotools.styling.ColorMap; |
import org.geotools.styling.ColorMap; |
59 |
import org.geotools.styling.ColorMapEntry; |
import org.geotools.styling.ColorMapEntry; |
60 |
|
import org.geotools.styling.FeatureTypeStyle; |
61 |
import org.geotools.styling.RasterSymbolizer; |
import org.geotools.styling.RasterSymbolizer; |
62 |
|
import org.geotools.styling.Rule; |
63 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
64 |
import org.jdom.Document; |
import org.jdom.Document; |
65 |
import org.jdom.Element; |
import org.jdom.Element; |
66 |
import org.jdom.input.SAXBuilder; |
import org.jdom.input.SAXBuilder; |
67 |
import org.jdom.output.XMLOutputter; |
import org.jdom.output.XMLOutputter; |
68 |
|
import org.opengis.feature.simple.SimpleFeatureType; |
69 |
|
|
70 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
71 |
import schmitzm.io.IOUtil; |
import schmitzm.io.IOUtil; |
736 |
else |
else |
737 |
saveStyledLayerStyle(style,geoObjectURL, "sld", "amd"); |
saveStyledLayerStyle(style,geoObjectURL, "sld", "amd"); |
738 |
} |
} |
739 |
|
|
740 |
|
|
741 |
|
|
742 |
|
/** |
743 |
|
* Creates a {@link Box} that shows a legend for a list of |
744 |
|
* {@link FeatureTypeStyle}s and a targeted featureType |
745 |
|
* |
746 |
|
* @param featureType |
747 |
|
* If this a legend for Point, Polygon or Line? |
748 |
|
* @param list |
749 |
|
* The Styles to presented in this legend |
750 |
|
* |
751 |
|
* @author <a href="mailto:[email protected]">Stefan Alfons |
752 |
|
* Krüger</a> |
753 |
|
*/ |
754 |
|
public static Box createLegendPanel(List<FeatureTypeStyle> list, |
755 |
|
SimpleFeatureType featureType, int iconWidth, int iconHeight) { |
756 |
|
|
757 |
|
Box box = new Box(BoxLayout.Y_AXIS) { |
758 |
|
|
759 |
|
/** |
760 |
|
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot |
761 |
|
* gemacht wird) wird. Dann werden wird der Hintergrund auf WEISS |
762 |
|
* gesetzt. |
763 |
|
* |
764 |
|
* @author <a href="mailto:[email protected]">Stefan Alfons |
765 |
|
* Krüger</a> |
766 |
|
*/ |
767 |
|
@Override |
768 |
|
public void print(Graphics g) { |
769 |
|
final Color orig = getBackground(); |
770 |
|
setBackground(Color.WHITE); |
771 |
|
// wrap in try/finally so that we always restore the state |
772 |
|
try { |
773 |
|
super.print(g); |
774 |
|
} finally { |
775 |
|
setBackground(orig); |
776 |
|
} |
777 |
|
} |
778 |
|
}; |
779 |
|
|
780 |
|
for (FeatureTypeStyle ftStyle : list) { |
781 |
|
|
782 |
|
// One child-node for every rule |
783 |
|
List<Rule> rules = ftStyle.rules(); |
784 |
|
for (Rule rule : rules) { |
785 |
|
|
786 |
|
/** |
787 |
|
* Let's not create a hbox for Rules that only contain |
788 |
|
* TextSymbolizers |
789 |
|
*/ |
790 |
|
if (StylingUtil.getTextSymbolizers(rule.getSymbolizers()) |
791 |
|
.size() == rule.getSymbolizers().length) |
792 |
|
continue; |
793 |
|
|
794 |
|
Box hbox = new Box(BoxLayout.X_AXIS) { |
795 |
|
|
796 |
|
/** |
797 |
|
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein |
798 |
|
* Screenshot gemacht wird) wird. Dann werden wird der |
799 |
|
* Hintergrund auf WEISS gesetzt. |
800 |
|
* |
801 |
|
* @author <a href="mailto:[email protected]">Stefan |
802 |
|
* Alfons Krüger</a> |
803 |
|
*/ |
804 |
|
@Override |
805 |
|
public void print(Graphics g) { |
806 |
|
final Color orig = getBackground(); |
807 |
|
setBackground(Color.WHITE); |
808 |
|
// wrap in try/finally so that we always restore the |
809 |
|
// state |
810 |
|
try { |
811 |
|
super.print(g); |
812 |
|
} finally { |
813 |
|
setBackground(orig); |
814 |
|
} |
815 |
|
} |
816 |
|
}; |
817 |
|
|
818 |
|
/** |
819 |
|
* The size of the legend Symbol is dependent on the size of the |
820 |
|
* font. |
821 |
|
*/ |
822 |
|
final int fontHeight = new JLabel().getFontMetrics( |
823 |
|
new JLabel().getFont()).getHeight(); |
824 |
|
|
825 |
|
final Dimension ICON_SIZE = new Dimension(iconWidth, |
826 |
|
fontHeight > 5 ? fontHeight : iconHeight); |
827 |
|
|
828 |
|
// **************************************************************************** |
829 |
|
// Create the actual icon |
830 |
|
// **************************************************************************** |
831 |
|
final BufferedImage imageForRule = LegendIconFeatureRenderer |
832 |
|
.getInstance().createImageForRule(rule, featureType, |
833 |
|
ICON_SIZE); |
834 |
|
|
835 |
|
// LOGGER.debug("Creating a new Legend Image for RUle name = |
836 |
|
// "+rule.getName()); |
837 |
|
|
838 |
|
ImageIcon legendIcon = new ImageIcon(imageForRule); |
839 |
|
|
840 |
|
final JLabel iconLabel = new JLabel(legendIcon); |
841 |
|
hbox.setAlignmentX(0f); |
842 |
|
hbox.add(iconLabel); |
843 |
|
hbox.add(Box.createHorizontalStrut(3)); |
844 |
|
|
845 |
|
// **************************************************************************** |
846 |
|
// The labeltext is read from the SLD. Its either the title |
847 |
|
// directly, or interpreted as OneLine Code |
848 |
|
// **************************************************************************** |
849 |
|
// final String rawText = |
850 |
|
// rule.getDescription().getTitle().toString(); |
851 |
|
final String rawText = rule.getDescription().getTitle().toString(); |
852 |
|
|
853 |
|
Translation labelT = new Translation(); |
854 |
|
labelT.fromOneLine(rawText); |
855 |
|
|
856 |
|
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
857 |
|
hbox.add(classTitleLabel); |
858 |
|
classTitleLabel.setLabelFor(iconLabel); |
859 |
|
|
860 |
|
box.add(hbox); |
861 |
|
|
862 |
|
} |
863 |
|
} |
864 |
|
|
865 |
|
return box; |
866 |
|
} |
867 |
|
|
868 |
|
|
869 |
|
/** |
870 |
|
* Creates a |
871 |
|
* @param styledGrid |
872 |
|
* @param iconHeight |
873 |
|
* @param iconWidth |
874 |
|
* @return |
875 |
|
*/ |
876 |
|
public static Box createLegendPanel(StyledRasterInterface<?> styledGrid, int iconWidth, int iconHeight) { |
877 |
|
throw new RuntimeException("Not yet..."); |
878 |
|
} |
879 |
|
|
880 |
|
|
881 |
} |
} |