source: josm/trunk/src/org/openstreetmap/josm/gui/layer/ImageryLayer.java@ 3715

Last change on this file since 3715 was 3715, checked in by Upliner, 14 years ago

Added imagery plugin to josm core. Imagery plugin is union of wmsplugin and slippymap plugins. It includes code by Tim Waters, Petr Dlouhý, Frederik Ramm and others. Also enables the remotecontol which was integrated in [3707].

File size: 6.5 KB
Line 
1package org.openstreetmap.josm.gui.layer;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4import static org.openstreetmap.josm.tools.I18n.trc;
5
6import java.awt.Color;
7import java.awt.Component;
8import java.awt.Container;
9import java.awt.Font;
10import java.awt.Graphics;
11import java.awt.event.ActionEvent;
12import java.awt.image.BufferedImage;
13import java.awt.image.BufferedImageOp;
14import java.awt.image.ConvolveOp;
15import java.awt.image.Kernel;
16import java.util.ArrayList;
17import java.util.List;
18
19import javax.swing.AbstractAction;
20import javax.swing.Icon;
21import javax.swing.JCheckBoxMenuItem;
22import javax.swing.JMenu;
23import javax.swing.JMenuItem;
24import javax.swing.JSeparator;
25
26import org.openstreetmap.josm.Main;
27import org.openstreetmap.josm.actions.ImageryAdjustAction;
28import org.openstreetmap.josm.data.ProjectionBounds;
29import org.openstreetmap.josm.data.imagery.ImageryInfo;
30import org.openstreetmap.josm.data.imagery.OffsetBookmark;
31import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
32import org.openstreetmap.josm.data.preferences.IntegerProperty;
33import org.openstreetmap.josm.gui.MapView;
34import org.openstreetmap.josm.tools.ImageProvider;
35
36public abstract class ImageryLayer extends Layer {
37 protected static final Icon icon = ImageProvider.get("imagery_small");
38
39 public static final IntegerProperty PROP_FADE_AMOUNT = new IntegerProperty("imagery.fade_amount", 0);
40 public static final IntegerProperty PROP_SHARPEN_LEVEL = new IntegerProperty("imagery.sharpen_level", 0);
41
42 public static Color getFadeColor() {
43 return Main.pref.getColor("imagery.fade", Color.white);
44 }
45
46 public static Color getFadeColorWithAlpha() {
47 Color c = getFadeColor();
48 return new Color(c.getRed(),c.getGreen(),c.getBlue(),PROP_FADE_AMOUNT.get()*255/100);
49 }
50
51 public static void setFadeColor(Color color) {
52 Main.pref.putColor("imagery.fade", color);
53 }
54
55 protected final ImageryInfo info;
56 protected MapView mv;
57
58 protected double dx = 0.0;
59 protected double dy = 0.0;
60
61 protected int sharpenLevel;
62
63 public ImageryLayer(ImageryInfo info) {
64 super(info.getName());
65 this.info = info;
66 this.mv = Main.map.mapView;
67 this.sharpenLevel = PROP_SHARPEN_LEVEL.get();
68 }
69
70 public double getPPD(){
71 ProjectionBounds bounds = mv.getProjectionBounds();
72 return mv.getWidth() / (bounds.max.east() - bounds.min.east());
73 }
74
75 public double getDx() {
76 return dx;
77 }
78
79 public double getDy() {
80 return dy;
81 }
82
83 public void setOffset(double dx, double dy) {
84 this.dx = dx;
85 this.dy = dy;
86 }
87
88 public void displace(double dx, double dy) {
89 setOffset(this.dx += dx, this.dy += dy);
90 }
91
92 public ImageryInfo getInfo() {
93 return info;
94 }
95
96 @Override
97 public Icon getIcon() {
98 return icon;
99 }
100
101 @Override
102 public boolean isMergable(Layer other) {
103 return false;
104 }
105
106 @Override
107 public void mergeFrom(Layer from) {
108 }
109
110 @Override
111 public Object getInfoComponent() {
112 return getToolTipText();
113 }
114
115 public static ImageryLayer create(ImageryInfo info) {
116 if (info.getImageryType() == ImageryType.WMS || info.getImageryType() == ImageryType.HTML)
117 return new WMSLayer(info);
118 else if (info.getImageryType() == ImageryType.TMS || info.getImageryType() == ImageryType.BING)
119 return new TMSLayer(info);
120 else throw new AssertionError();
121 }
122
123 class ApplyOffsetAction extends AbstractAction {
124 private OffsetBookmark b;
125 ApplyOffsetAction(OffsetBookmark b) {
126 super(b.name);
127 this.b = b;
128 }
129
130 @Override
131 public void actionPerformed(ActionEvent ev) {
132 setOffset(b.dx, b.dy);
133 Main.map.repaint();
134 if (!(ev.getSource() instanceof Component)) return;
135 Component source = (Component)ev.getSource();
136 if (source.getParent() == null) return;
137 Container m = source.getParent();
138 for (Component c : m.getComponents()) {
139 if (c instanceof JCheckBoxMenuItem && c != source) {
140 ((JCheckBoxMenuItem)c).setSelected(false);
141 }
142 }
143 }
144 }
145
146 public class OffsetAction extends AbstractAction implements LayerAction {
147 @Override
148 public void actionPerformed(ActionEvent e) {
149 }
150 @Override
151 public Component createMenuComponent() {
152 JMenu menu = new JMenu(trc("layer", "Offset"));
153 menu.setIcon(ImageProvider.get("mapmode", "adjustimg"));
154 for (Component item : getOffsetMenu()) {
155 menu.add(item);
156 }
157 return menu;
158 }
159 @Override
160 public boolean supportLayers(List<Layer> layers) {
161 return false;
162 }
163 }
164
165 public List<Component> getOffsetMenu() {
166 List<Component> result = new ArrayList<Component>();
167 result.add(new JMenuItem(new ImageryAdjustAction(this)));
168 if (OffsetBookmark.allBookmarks.isEmpty()) return result;
169
170 result.add(new JSeparator(JSeparator.HORIZONTAL));
171 for (OffsetBookmark b : OffsetBookmark.allBookmarks) {
172 if (!b.isUsable(this)) {
173 continue;
174 }
175 JCheckBoxMenuItem item = new JCheckBoxMenuItem(new ApplyOffsetAction(b));
176 if (b.dx == dx && b.dy == dy) {
177 item.setSelected(true);
178 }
179 result.add(item);
180 }
181 return result;
182 }
183
184 public BufferedImage sharpenImage(BufferedImage img) {
185 if (sharpenLevel <= 0) return img;
186 int width = img.getWidth(null);
187 int height = img.getHeight(null);
188 BufferedImage tmp = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
189 tmp.getGraphics().drawImage(img, 0, 0, null);
190 Kernel kernel;
191 if (sharpenLevel == 1) {
192 kernel = new Kernel(2, 2, new float[] { 4, -1, -1, -1});
193 } else {
194 kernel = new Kernel(3, 3, new float[] { -1, -1, -1, -1, 9, -1, -1, -1, -1});
195 }
196 BufferedImageOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
197 return op.filter(tmp, null);
198 }
199
200 public void drawErrorTile(BufferedImage img) {
201 Graphics g = img.getGraphics();
202 g.setColor(Color.RED);
203 g.fillRect(0, 0, img.getWidth(), img.getHeight());
204 g.setFont(g.getFont().deriveFont(Font.PLAIN).deriveFont(36.0f));
205 g.setColor(Color.BLACK);
206 g.drawString(tr("ERROR"), 30, img.getHeight()/2);
207 }
208}
Note: See TracBrowser for help on using the repository browser.