source: osm/applications/editors/josm/plugins/wms-turbo-challenge2/src/wmsturbochallenge/FakeMapView.java@ 19990

Last change on this file since 19990 was 19990, checked in by balrog-kun, 15 years ago

Initial commit of the wms-turbo-challenge2 plugin.

This may not be an extremely useful plugin but it will still be great to
have it in the source tree so that it updated when upstream interfaces
change, and get nice versioning.

File size: 3.5 KB
Line 
1/*
2 * GPLv2 or 3, Copyright (c) 2010 Andrzej Zaborowski
3 *
4 * Implements a fake MapView that we can pass to WMSLayer's .paint,
5 * this will give us two things:
6 * # We'll be able to tell WMSLayer.paint() what area we want it
7 * to download (it ignores the "bounds" parameter) and override
8 * isVisible and friends as needed, and
9 * # We'll receive notifications from Grabber when we need to
10 * repaint (and call WMSLayer's .paint again) because the
11 * Grabber downloaded some or all of the tiles that we asked
12 * WMSLayer for and WMSLayer created the Grabber passing it
13 * our MapView. Otherwise we wouldn't be able to tell when
14 * this happened and could only guess.
15 */
16package wmsturbochallenge;
17
18import org.openstreetmap.josm.gui.MapView;
19import org.openstreetmap.josm.data.ProjectionBounds;
20import org.openstreetmap.josm.data.coor.EastNorth;
21
22import java.awt.Point;
23import java.awt.Graphics;
24import java.awt.Graphics2D;
25import java.awt.image.BufferedImage;
26
27class fake_map_view extends MapView {
28 public ProjectionBounds view_bounds;
29 public MapView parent;
30
31 public Graphics2D graphics;
32 public BufferedImage ground_image;
33 public int ground_width = -1;
34 public int ground_height = -1;
35 public double scale;
36 public double max_east_west;
37
38 public fake_map_view(MapView parent, double scale) {
39 this.parent = parent;
40 this.scale = scale;
41
42 ProjectionBounds parent_bounds = parent.getProjectionBounds();
43 max_east_west =
44 parent_bounds.max.east() - parent_bounds.min.east();
45 }
46
47 public void setProjectionBounds(ProjectionBounds bounds) {
48 view_bounds = bounds;
49
50 if (bounds.max.east() - bounds.min.east() > max_east_west) {
51 max_east_west = bounds.max.east() - bounds.min.east();
52
53 /* We need to set the parent MapView's bounds (i.e.
54 * zoom level) to the same as ours max possible
55 * bounds to avoid WMSLayer thinking we're zoomed
56 * out more than we are or it'll pop up an annoying
57 * "requested area is too large" popup.
58 */
59 EastNorth parent_center = parent.getCenter();
60 parent.zoomTo(new ProjectionBounds(
61 new EastNorth(
62 parent_center.east() -
63 max_east_west / 2,
64 parent_center.north()),
65 new EastNorth(
66 parent_center.east() +
67 max_east_west / 2,
68 parent_center.north())));
69
70 /* Request again because NavigatableContent adds
71 * a border just to be sure.
72 */
73 ProjectionBounds new_bounds =
74 parent.getProjectionBounds();
75 max_east_west =
76 new_bounds.max.east() - new_bounds.min.east();
77 }
78
79 Point vmin = getPoint(bounds.min);
80 Point vmax = getPoint(bounds.max);
81 int w = vmax.x + 1;
82 int h = vmin.y + 1;
83
84 if (w <= ground_width && h <= ground_height) {
85 graphics.setClip(0, 0, w, h);
86 return;
87 }
88
89 if (w > ground_width)
90 ground_width = w;
91 if (h > ground_height)
92 ground_height = h;
93
94 ground_image = new BufferedImage(ground_width,
95 ground_height,
96 BufferedImage.TYPE_INT_RGB);
97 graphics = ground_image.createGraphics();
98 graphics.setClip(0, 0, w, h);
99 }
100
101 public ProjectionBounds getProjectionBounds() {
102 return view_bounds;
103 }
104
105 public Point getPoint(EastNorth p) {
106 double x = p.east() - view_bounds.min.east();
107 double y = view_bounds.max.north() - p.north();
108 x /= this.scale;
109 y /= this.scale;
110
111 return new Point((int) x, (int) y);
112 }
113
114 public EastNorth getEastNorth(int x, int y) {
115 return new EastNorth(
116 view_bounds.min.east() + x * this.scale,
117 view_bounds.min.north() - y * this.scale);
118 }
119
120 public boolean isVisible(int x, int y) {
121 return true;
122 }
123
124 public Graphics getGraphics() {
125 return graphics;
126 }
127
128 public void repaint() {
129 }
130}
Note: See TracBrowser for help on using the repository browser.