1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
|
---|
2 | package cadastre_fr;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Graphics;
|
---|
8 | import java.awt.Graphics2D;
|
---|
9 | import java.awt.Image;
|
---|
10 | import java.awt.Point;
|
---|
11 | import java.awt.RenderingHints;
|
---|
12 | import java.awt.Toolkit;
|
---|
13 | import java.awt.image.BufferedImage;
|
---|
14 | import java.awt.image.ImageObserver;
|
---|
15 | import java.io.EOFException;
|
---|
16 | import java.io.IOException;
|
---|
17 | import java.io.ObjectInputStream;
|
---|
18 | import java.io.ObjectOutputStream;
|
---|
19 | import java.util.ArrayList;
|
---|
20 | import java.util.HashSet;
|
---|
21 | import java.util.Vector;
|
---|
22 | import java.util.concurrent.locks.Lock;
|
---|
23 | import java.util.concurrent.locks.ReentrantLock;
|
---|
24 |
|
---|
25 | import javax.swing.Action;
|
---|
26 | import javax.swing.Icon;
|
---|
27 | import javax.swing.ImageIcon;
|
---|
28 | import javax.swing.JOptionPane;
|
---|
29 |
|
---|
30 | import org.openstreetmap.josm.Main;
|
---|
31 | import org.openstreetmap.josm.data.Bounds;
|
---|
32 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
33 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
34 | import org.openstreetmap.josm.gui.MapView;
|
---|
35 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
36 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
37 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * This is a layer that grabs the current screen from the French cadastre WMS
|
---|
41 | * server. The data fetched this way is tiled and managed to the disc to reduce
|
---|
42 | * server load.
|
---|
43 | */
|
---|
44 | public class WMSLayer extends Layer implements ImageObserver {
|
---|
45 |
|
---|
46 | private int lambertZone = -1;
|
---|
47 |
|
---|
48 | public CadastreGrabber grabber = new CadastreGrabber();
|
---|
49 |
|
---|
50 | protected static final Icon icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(
|
---|
51 | CadastrePlugin.class.getResource("/images/cadastre_small.png")));
|
---|
52 |
|
---|
53 | private Vector<GeorefImage> images = new Vector<GeorefImage>();
|
---|
54 |
|
---|
55 | public Lock imagesLock = new ReentrantLock();
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * v1 to v2 = not supported
|
---|
59 | * v2 to v3 = add 4 more EastNorth coordinates in GeorefImages
|
---|
60 | * v3 to v4 = add original raster image width and height
|
---|
61 | */
|
---|
62 | protected final int serializeFormatVersion = 4;
|
---|
63 |
|
---|
64 | public static int currentFormat;
|
---|
65 |
|
---|
66 | private ArrayList<EastNorthBound> dividedBbox = new ArrayList<EastNorthBound>();
|
---|
67 |
|
---|
68 | private String location = "";
|
---|
69 |
|
---|
70 | private String departement = "";
|
---|
71 |
|
---|
72 | private String codeCommune = "";
|
---|
73 |
|
---|
74 | public EastNorthBound communeBBox = new EastNorthBound(new EastNorth(0,0), new EastNorth(0,0));
|
---|
75 |
|
---|
76 | private boolean isRaster = false;
|
---|
77 | private boolean isAlreadyGeoreferenced = false;
|
---|
78 | public double X0, Y0, angle, fX, fY;
|
---|
79 |
|
---|
80 | // bbox of the georeferenced raster image (the nice horizontal and vertical box)
|
---|
81 | private EastNorth rasterMin;
|
---|
82 | private EastNorth rasterMax;
|
---|
83 | private double rasterRatio;
|
---|
84 |
|
---|
85 | private Action saveAsPng;
|
---|
86 |
|
---|
87 | private Action cancelGrab;
|
---|
88 |
|
---|
89 | public boolean adjustModeEnabled;
|
---|
90 |
|
---|
91 | public GrabThread grabThread;
|
---|
92 |
|
---|
93 | public WMSLayer() {
|
---|
94 | this(tr("Blank Layer"), "", -1);
|
---|
95 | }
|
---|
96 |
|
---|
97 | public WMSLayer(String location, String codeCommune, int lambertZone) {
|
---|
98 | super(buildName(location, codeCommune));
|
---|
99 | this.location = location;
|
---|
100 | this.codeCommune = codeCommune;
|
---|
101 | this.lambertZone = lambertZone;
|
---|
102 | grabThread = new GrabThread(this);
|
---|
103 | grabThread.start();
|
---|
104 | // enable auto-sourcing option
|
---|
105 | CadastrePlugin.pluginUsed = true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public void destroy() {
|
---|
110 | // if the layer is currently saving the images in the cache, wait until it's finished
|
---|
111 | grabThread.cancel();
|
---|
112 | grabThread = null;
|
---|
113 | super.destroy();
|
---|
114 | images = null;
|
---|
115 | dividedBbox = null;
|
---|
116 | System.out.println("Layer "+location+" destroyed");
|
---|
117 | }
|
---|
118 |
|
---|
119 | private static String buildName(String location, String codeCommune) {
|
---|
120 | String ret = location.toUpperCase();
|
---|
121 | if (codeCommune != null && !codeCommune.equals(""))
|
---|
122 | ret += "(" + codeCommune + ")";
|
---|
123 | return ret;
|
---|
124 | }
|
---|
125 |
|
---|
126 | private String rebuildName() {
|
---|
127 | return buildName(this.location.toUpperCase(), this.codeCommune);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void grab(Bounds b) throws IOException {
|
---|
131 | grabThread.setCancelled(false);
|
---|
132 | grabThread.setGrabber(grabber);
|
---|
133 | // if it is the first layer, use the communeBBox as grab bbox (and not divided)
|
---|
134 | if (Main.map.mapView.getAllLayers().size() == 1 ) {
|
---|
135 | b = this.getCommuneBBox().toBounds();
|
---|
136 | Main.map.mapView.zoomTo(b);
|
---|
137 | divideBbox(b, 1);
|
---|
138 | } else {
|
---|
139 | if (isRaster) {
|
---|
140 | b = new Bounds(Main.proj.eastNorth2latlon(rasterMin), Main.proj.eastNorth2latlon(rasterMax));
|
---|
141 | divideBbox(b, Integer.parseInt(Main.pref.get("cadastrewms.rasterDivider",
|
---|
142 | CadastrePreferenceSetting.DEFAULT_RASTER_DIVIDER)));
|
---|
143 | } else
|
---|
144 | divideBbox(b, Integer.parseInt(Main.pref.get("cadastrewms.scale", Scale.X1.toString())));
|
---|
145 | }
|
---|
146 | grabThread.addImages(dividedBbox);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Divides the bounding box in smaller squares. Their size (and quantity) is configurable in Preferences.
|
---|
151 | *
|
---|
152 | * @param b the original bbox, usually the current bbox on screen
|
---|
153 | * @param factor 1 = source bbox 1:1
|
---|
154 | * 2 = source bbox divided by 2x2 smaller boxes
|
---|
155 | * 3 = source bbox divided by 3x3 smaller boxes
|
---|
156 | * 4 = configurable size from preferences (100 meters per default) rounded
|
---|
157 | * allowing grabbing of next contiguous zone
|
---|
158 | */
|
---|
159 | private void divideBbox(Bounds b, int factor) {
|
---|
160 | EastNorth lambertMin = Main.proj.latlon2eastNorth(b.getMin());
|
---|
161 | EastNorth lambertMax = Main.proj.latlon2eastNorth(b.getMax());
|
---|
162 | double minEast = lambertMin.east();
|
---|
163 | double minNorth = lambertMin.north();
|
---|
164 | double dEast = (lambertMax.east() - minEast) / factor;
|
---|
165 | double dNorth = (lambertMax.north() - minNorth) / factor;
|
---|
166 | dividedBbox.clear();
|
---|
167 | if (factor < 4 || isRaster) {
|
---|
168 | for (int xEast = 0; xEast < factor; xEast++)
|
---|
169 | for (int xNorth = 0; xNorth < factor; xNorth++) {
|
---|
170 | dividedBbox.add(new EastNorthBound(new EastNorth(minEast + xEast * dEast, minNorth + xNorth * dNorth),
|
---|
171 | new EastNorth(minEast + (xEast + 1) * dEast, minNorth + (xNorth + 1) * dNorth)));
|
---|
172 | }
|
---|
173 | } else {
|
---|
174 | // divide to fixed size squares
|
---|
175 | // grab all square in a spiral starting from the center (usually the most interesting place)
|
---|
176 | int c = Integer.parseInt(Main.pref.get("cadastrewms.squareSize", "100"));
|
---|
177 | lambertMin = lambertMin.add(- minEast%c, - minNorth%c);
|
---|
178 | lambertMax = lambertMax.add(c - lambertMax.east()%c, c - lambertMax.north()%c);
|
---|
179 | EastNorth mid = lambertMax.getCenter(lambertMin);
|
---|
180 | mid = mid.add(-1, 1); // in case the boxes side is a pair, select the one one top,left to follow the rotation
|
---|
181 | mid = mid.add(- mid.east()%c, - mid.north()%c);
|
---|
182 | int x = (int)(lambertMax.east() - lambertMin.east())/c;
|
---|
183 | int y = (int)(lambertMax.north() - lambertMin.north())/c;
|
---|
184 | int dx[] = {+1, 0,-1, 0};
|
---|
185 | int dy[] = {0,-1, 0,+1};
|
---|
186 | int currDir = -1, lDir = 1, i = 1, j = 0, k = -1;
|
---|
187 | if (x == 1)
|
---|
188 | currDir = 0;
|
---|
189 | dividedBbox.add(new EastNorthBound(mid, new EastNorth(mid.east()+c, mid.north()+c)));
|
---|
190 | while (i < (x*y)) {
|
---|
191 | i++;
|
---|
192 | j++;
|
---|
193 | if (j >= lDir) {
|
---|
194 | k++;
|
---|
195 | if (k > 1) {
|
---|
196 | lDir++;
|
---|
197 | k = 0;
|
---|
198 | }
|
---|
199 | j = 0;
|
---|
200 | currDir = (currDir+1)%4;
|
---|
201 | } else if (currDir >= 0 && j >= (currDir == 0 || currDir == 2 ? x-1 : y-1)) {
|
---|
202 | // the overall is a rectangle, not a square. Jump to the other side to grab next square.
|
---|
203 | k++;
|
---|
204 | if (k > 1) {
|
---|
205 | lDir++;
|
---|
206 | k = 0;
|
---|
207 | }
|
---|
208 | j = lDir-1;
|
---|
209 | currDir = (currDir+1)%4;
|
---|
210 | mid = new EastNorth(mid.east() + dx[currDir]*c*(lDir-1), mid.north() + dy[currDir]*c*(lDir-1));
|
---|
211 | }
|
---|
212 | mid = new EastNorth(mid.east() + dx[currDir]*c, mid.north() + dy[currDir]*c);
|
---|
213 | dividedBbox.add(new EastNorthBound(mid, new EastNorth(mid.east()+c, mid.north()+c)));
|
---|
214 | }
|
---|
215 | // // simple algorithm to grab all squares
|
---|
216 | // minEast = minEast - minEast % cSquare;
|
---|
217 | // minNorth = minNorth - minNorth % cSquare;
|
---|
218 | // for (int xEast = (int)minEast; xEast < lambertMax.east(); xEast+=cSquare)
|
---|
219 | // for (int xNorth = (int)minNorth; xNorth < lambertMax.north(); xNorth+=cSquare) {
|
---|
220 | // dividedBbox.add(new EastNorthBound(new EastNorth(xEast, xNorth),
|
---|
221 | // new EastNorth(xEast + cSquare, xNorth + cSquare)));
|
---|
222 | // }
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | @Override
|
---|
227 | public Icon getIcon() {
|
---|
228 | return icon;
|
---|
229 | }
|
---|
230 |
|
---|
231 | @Override
|
---|
232 | public String getToolTipText() {
|
---|
233 | String str = tr("WMS layer ({0}), {1} tile(s) loaded", getName(), images.size());
|
---|
234 | if (isRaster) {
|
---|
235 | str += "\n"+tr("Is not vectorized.");
|
---|
236 | str += "\n"+tr("Raster size: {0}", communeBBox);
|
---|
237 | } else
|
---|
238 | str += "\n"+tr("Is vectorized.");
|
---|
239 | str += "\n"+tr("Commune bbox: {0}", communeBBox);
|
---|
240 | return str;
|
---|
241 | }
|
---|
242 |
|
---|
243 | @Override
|
---|
244 | public boolean isMergable(Layer other) {
|
---|
245 | return false;
|
---|
246 | }
|
---|
247 |
|
---|
248 | @Override
|
---|
249 | public void mergeFrom(Layer from) {
|
---|
250 | }
|
---|
251 |
|
---|
252 | @Override
|
---|
253 | public void paint(Graphics2D g, final MapView mv, Bounds bounds) {
|
---|
254 | synchronized(this){
|
---|
255 | Object savedInterpolation = g.getRenderingHint(RenderingHints.KEY_INTERPOLATION);
|
---|
256 | if (savedInterpolation == null) savedInterpolation = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
|
---|
257 | String interpolation = Main.pref.get("cadastrewms.imageInterpolation", "standard");
|
---|
258 | if (interpolation.equals("bilinear"))
|
---|
259 | g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
---|
260 | else if (interpolation.equals("bicubic"))
|
---|
261 | g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
---|
262 | else
|
---|
263 | g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
|
---|
264 | imagesLock.lock();
|
---|
265 | for (GeorefImage img : images)
|
---|
266 | img.paint(g, mv, CadastrePlugin.backgroundTransparent,
|
---|
267 | CadastrePlugin.transparency, CadastrePlugin.drawBoundaries);
|
---|
268 | imagesLock.unlock();
|
---|
269 | g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, savedInterpolation);
|
---|
270 | }
|
---|
271 | if (this.isRaster) {
|
---|
272 | paintCrosspieces(g, mv);
|
---|
273 | }
|
---|
274 | grabThread.paintBoxesToGrab(g, mv);
|
---|
275 | if (this.adjustModeEnabled) {
|
---|
276 | WMSAdjustAction.paintAdjustFrames(g, mv);
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | @Override
|
---|
281 | public void visitBoundingBox(BoundingXYVisitor v) {
|
---|
282 | for (GeorefImage img : images) {
|
---|
283 | v.visit(img.min);
|
---|
284 | v.visit(img.max);
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | @Override
|
---|
289 | public Object getInfoComponent() {
|
---|
290 | return getToolTipText();
|
---|
291 | }
|
---|
292 |
|
---|
293 | @Override
|
---|
294 | public Action[] getMenuEntries() {
|
---|
295 | saveAsPng = new MenuActionSaveRasterAs(this);
|
---|
296 | saveAsPng.setEnabled(isRaster);
|
---|
297 | cancelGrab = new MenuActionCancelGrab(this);
|
---|
298 | cancelGrab.setEnabled(!isRaster && grabThread.getImagesToGrabSize() > 0);
|
---|
299 | return new Action[] {
|
---|
300 | LayerListDialog.getInstance().createShowHideLayerAction(),
|
---|
301 | LayerListDialog.getInstance().createDeleteLayerAction(),
|
---|
302 | new MenuActionLoadFromCache(),
|
---|
303 | saveAsPng,
|
---|
304 | cancelGrab,
|
---|
305 | new LayerListPopup.InfoAction(this),
|
---|
306 |
|
---|
307 | };
|
---|
308 | }
|
---|
309 |
|
---|
310 | public GeorefImage findImage(EastNorth eastNorth) {
|
---|
311 | // Iterate in reverse, so we return the image which is painted last.
|
---|
312 | // (i.e. the topmost one)
|
---|
313 | for (int i = images.size() - 1; i >= 0; i--) {
|
---|
314 | if (images.get(i).contains(eastNorth)) {
|
---|
315 | return images.get(i);
|
---|
316 | }
|
---|
317 | }
|
---|
318 | return null;
|
---|
319 | }
|
---|
320 |
|
---|
321 | public boolean isOverlapping(Bounds bounds) {
|
---|
322 | GeorefImage georefImage =
|
---|
323 | new GeorefImage(null,
|
---|
324 | Main.proj.latlon2eastNorth(bounds.getMin()),
|
---|
325 | Main.proj.latlon2eastNorth(bounds.getMax()));
|
---|
326 | for (GeorefImage img : images) {
|
---|
327 | if (img.overlap(georefImage))
|
---|
328 | return true;
|
---|
329 | }
|
---|
330 | return false;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Convert the eastNorth input coordinates to raster coordinates.
|
---|
335 | * The original raster size is [0,0,12286,8730] where 0,0 is the upper left corner and
|
---|
336 | * 12286,8730 is the approx. raster max size.
|
---|
337 | * @return the raster coordinates for the wms server request URL (minX,minY,maxX,maxY)
|
---|
338 | */
|
---|
339 | public String eastNorth2raster(EastNorth min, EastNorth max) {
|
---|
340 | double minX = (min.east() - rasterMin.east()) / rasterRatio;
|
---|
341 | double minY = (min.north() - rasterMin.north()) / rasterRatio;
|
---|
342 | double maxX = (max.east() - rasterMin.east()) / rasterRatio;
|
---|
343 | double maxY = (max.north() - rasterMin.north()) / rasterRatio;
|
---|
344 | return minX+","+minY+","+maxX+","+maxY;
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | public String getLocation() {
|
---|
349 | return location;
|
---|
350 | }
|
---|
351 |
|
---|
352 | public void setLocation(String location) {
|
---|
353 | this.location = location;
|
---|
354 | setName(rebuildName());
|
---|
355 | }
|
---|
356 |
|
---|
357 | public String getDepartement() {
|
---|
358 | return departement;
|
---|
359 | }
|
---|
360 |
|
---|
361 | public void setDepartement(String departement) {
|
---|
362 | this.departement = departement;
|
---|
363 | }
|
---|
364 |
|
---|
365 | public String getCodeCommune() {
|
---|
366 | return codeCommune;
|
---|
367 | }
|
---|
368 |
|
---|
369 | public void setCodeCommune(String codeCommune) {
|
---|
370 | this.codeCommune = codeCommune;
|
---|
371 | setName(rebuildName());
|
---|
372 | }
|
---|
373 |
|
---|
374 | public boolean isRaster() {
|
---|
375 | return isRaster;
|
---|
376 | }
|
---|
377 |
|
---|
378 | public void setRaster(boolean isRaster) {
|
---|
379 | this.isRaster = isRaster;
|
---|
380 | if (saveAsPng != null)
|
---|
381 | saveAsPng.setEnabled(isRaster);
|
---|
382 | }
|
---|
383 |
|
---|
384 | public boolean isAlreadyGeoreferenced() {
|
---|
385 | return isAlreadyGeoreferenced;
|
---|
386 | }
|
---|
387 |
|
---|
388 | public void setAlreadyGeoreferenced(boolean isAlreadyGeoreferenced) {
|
---|
389 | this.isAlreadyGeoreferenced = isAlreadyGeoreferenced;
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Set raster positions used for grabbing and georeferencing.
|
---|
394 | * rasterMin is the Eaast North of bottom left corner raster image on the screen when image is grabbed.
|
---|
395 | * The bounds width and height are the raster width and height. The image width matches the current view
|
---|
396 | * and the image height is adapted.
|
---|
397 | * Required: the communeBBox must be set (normally it is catched by CadastreInterface and saved by DownloadWMSPlanImage)
|
---|
398 | * @param bounds the current main map view boundaries
|
---|
399 | */
|
---|
400 | public void setRasterBounds(Bounds bounds) {
|
---|
401 | EastNorth rasterCenter = Main.proj.latlon2eastNorth(bounds.getCenter());
|
---|
402 | EastNorth eaMin = Main.proj.latlon2eastNorth(bounds.getMin());
|
---|
403 | EastNorth eaMax = Main.proj.latlon2eastNorth(bounds.getMax());
|
---|
404 | double rasterSizeX = communeBBox.max.getX() - communeBBox.min.getX();
|
---|
405 | double rasterSizeY = communeBBox.max.getY() - communeBBox.min.getY();
|
---|
406 | double ratio = rasterSizeY/rasterSizeX;
|
---|
407 | // keep same ratio on screen as WMS bbox (stored in communeBBox)
|
---|
408 | rasterMin = new EastNorth(eaMin.getX(), rasterCenter.getY()-(eaMax.getX()-eaMin.getX())*ratio/2);
|
---|
409 | rasterMax = new EastNorth(eaMax.getX(), rasterCenter.getY()+(eaMax.getX()-eaMin.getX())*ratio/2);
|
---|
410 | rasterRatio = (rasterMax.getX()-rasterMin.getX())/rasterSizeX;
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * Called by CacheControl when a new cache file is created on disk.
|
---|
415 | * Save only primitives to keep cache independent of software changes.
|
---|
416 | * @param oos
|
---|
417 | * @throws IOException
|
---|
418 | */
|
---|
419 | public void write(ObjectOutputStream oos) throws IOException {
|
---|
420 | currentFormat = this.serializeFormatVersion;
|
---|
421 | oos.writeInt(this.serializeFormatVersion);
|
---|
422 | oos.writeObject(this.location); // String
|
---|
423 | oos.writeObject(this.codeCommune); // String
|
---|
424 | oos.writeInt(this.lambertZone);
|
---|
425 | oos.writeBoolean(this.isRaster);
|
---|
426 | oos.writeBoolean(false); // previously buildingsOnly
|
---|
427 | if (this.isRaster) {
|
---|
428 | oos.writeDouble(this.rasterMin.getX());
|
---|
429 | oos.writeDouble(this.rasterMin.getY());
|
---|
430 | oos.writeDouble(this.rasterMax.getX());
|
---|
431 | oos.writeDouble(this.rasterMax.getY());
|
---|
432 | oos.writeDouble(this.rasterRatio);
|
---|
433 | }
|
---|
434 | oos.writeDouble(this.communeBBox.min.getX());
|
---|
435 | oos.writeDouble(this.communeBBox.min.getY());
|
---|
436 | oos.writeDouble(this.communeBBox.max.getX());
|
---|
437 | oos.writeDouble(this.communeBBox.max.getY());
|
---|
438 | }
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * Called by CacheControl when a cache file is read from disk.
|
---|
442 | * Cache uses only primitives to stay independent of software changes.
|
---|
443 | * @param ois
|
---|
444 | * @throws IOException
|
---|
445 | * @throws ClassNotFoundException
|
---|
446 | */
|
---|
447 | public boolean read(ObjectInputStream ois, int currentLambertZone) throws IOException, ClassNotFoundException {
|
---|
448 | currentFormat = ois.readInt();;
|
---|
449 | if (currentFormat < 2) {
|
---|
450 | JOptionPane.showMessageDialog(Main.parent, tr("Unsupported cache file version; found {0}, expected {1}\nCreate a new one.",
|
---|
451 | currentFormat, this.serializeFormatVersion), tr("Cache Format Error"), JOptionPane.ERROR_MESSAGE);
|
---|
452 | return false;
|
---|
453 | }
|
---|
454 | this.setLocation((String) ois.readObject());
|
---|
455 | this.setCodeCommune((String) ois.readObject());
|
---|
456 | this.lambertZone = ois.readInt();
|
---|
457 | this.setRaster(ois.readBoolean());
|
---|
458 | if (currentFormat >= 4)
|
---|
459 | ois.readBoolean();
|
---|
460 | if (this.isRaster) {
|
---|
461 | double X = ois.readDouble();
|
---|
462 | double Y = ois.readDouble();
|
---|
463 | this.rasterMin = new EastNorth(X, Y);
|
---|
464 | X = ois.readDouble();
|
---|
465 | Y = ois.readDouble();
|
---|
466 | this.rasterMax = new EastNorth(X, Y);
|
---|
467 | this.rasterRatio = ois.readDouble();
|
---|
468 | }
|
---|
469 | double minX = ois.readDouble();
|
---|
470 | double minY = ois.readDouble();
|
---|
471 | double maxX = ois.readDouble();
|
---|
472 | double maxY = ois.readDouble();
|
---|
473 | this.communeBBox = new EastNorthBound(new EastNorth(minX, minY), new EastNorth(maxX, maxY));
|
---|
474 | if (this.lambertZone != currentLambertZone && currentLambertZone != -1) {
|
---|
475 | JOptionPane.showMessageDialog(Main.parent, tr("Lambert zone {0} in cache "+
|
---|
476 | "incompatible with current Lambert zone {1}",
|
---|
477 | this.lambertZone+1, currentLambertZone), tr("Cache Lambert Zone Error"), JOptionPane.ERROR_MESSAGE);
|
---|
478 | return false;
|
---|
479 | }
|
---|
480 | synchronized(this){
|
---|
481 | boolean EOF = false;
|
---|
482 | try {
|
---|
483 | while (!EOF) {
|
---|
484 | GeorefImage newImage = (GeorefImage) ois.readObject();
|
---|
485 | for (GeorefImage img : this.images) {
|
---|
486 | if (CadastrePlugin.backgroundTransparent) {
|
---|
487 | if (img.overlap(newImage))
|
---|
488 | // mask overlapping zone in already grabbed image
|
---|
489 | img.withdraw(newImage);
|
---|
490 | else
|
---|
491 | // mask overlapping zone in new image only when
|
---|
492 | // new image covers completely the existing image
|
---|
493 | newImage.withdraw(img);
|
---|
494 | }
|
---|
495 | }
|
---|
496 | this.images.add(newImage);
|
---|
497 | }
|
---|
498 | } catch (EOFException ex) {
|
---|
499 | // expected exception when all images are read
|
---|
500 | }
|
---|
501 | }
|
---|
502 | System.out.println("Cache loaded for location "+location+" with "+images.size()+" images");
|
---|
503 | return true;
|
---|
504 | }
|
---|
505 |
|
---|
506 | /**
|
---|
507 | * Join the grabbed images into one single.
|
---|
508 | */
|
---|
509 | public void joinBufferedImages() {
|
---|
510 | if (images.size() > 1) {
|
---|
511 | EastNorth min = images.get(0).min;
|
---|
512 | EastNorth max = images.get(images.size()-1).max;
|
---|
513 | int oldImgWidth = images.get(0).image.getWidth();
|
---|
514 | int oldImgHeight = images.get(0).image.getHeight();
|
---|
515 | HashSet<Double> lx = new HashSet<Double>();
|
---|
516 | HashSet<Double> ly = new HashSet<Double>();
|
---|
517 | for (GeorefImage img : images) {
|
---|
518 | lx.add(img.min.east());
|
---|
519 | ly.add(img.min.north());
|
---|
520 | }
|
---|
521 | int newWidth = oldImgWidth*lx.size();
|
---|
522 | int newHeight = oldImgHeight*ly.size();
|
---|
523 | BufferedImage new_img = new BufferedImage(newWidth, newHeight, images.get(0).image.getType()/*BufferedImage.TYPE_INT_ARGB*/);
|
---|
524 | Graphics g = new_img.getGraphics();
|
---|
525 | // Coordinate (0,0) is on top,left corner where images are grabbed from bottom left
|
---|
526 | int rasterDivider = (int)Math.sqrt(images.size());
|
---|
527 | for (int h = 0; h < lx.size(); h++) {
|
---|
528 | for (int v = 0; v < ly.size(); v++) {
|
---|
529 | int newx = h*oldImgWidth;
|
---|
530 | int newy = newHeight - oldImgHeight - (v*oldImgHeight);
|
---|
531 | int j = h*rasterDivider + v;
|
---|
532 | g.drawImage(images.get(j).image, newx, newy, this);
|
---|
533 | }
|
---|
534 | }
|
---|
535 | synchronized(this) {
|
---|
536 | images.clear();
|
---|
537 | images.add(new GeorefImage(new_img, min, max));
|
---|
538 | }
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * Image cropping based on two EN coordinates pointing to two corners in diagonal
|
---|
544 | * Because it's coming from user mouse clics, we have to sort de positions first.
|
---|
545 | * Works only for raster image layer (only one image in collection).
|
---|
546 | * Updates layer georeferences.
|
---|
547 | * @param en1
|
---|
548 | * @param en2
|
---|
549 | */
|
---|
550 | public void cropImage(EastNorth en1, EastNorth en2){
|
---|
551 | // adj1 is corner bottom, left
|
---|
552 | EastNorth adj1 = new EastNorth(en1.east() <= en2.east() ? en1.east() : en2.east(),
|
---|
553 | en1.north() <= en2.north() ? en1.north() : en2.north());
|
---|
554 | // adj2 is corner top, right
|
---|
555 | EastNorth adj2 = new EastNorth(en1.east() > en2.east() ? en1.east() : en2.east(),
|
---|
556 | en1.north() > en2.north() ? en1.north() : en2.north());
|
---|
557 | images.get(0).crop(adj1, adj2);
|
---|
558 | // update the layer georefs
|
---|
559 | rasterMin = adj1;
|
---|
560 | rasterMax = adj2;
|
---|
561 | setCommuneBBox(new EastNorthBound(new EastNorth(0,0), new EastNorth(images.get(0).image.getWidth()-1,images.get(0).image.getHeight()-1)));
|
---|
562 | rasterRatio = (rasterMax.getX()-rasterMin.getX())/(communeBBox.max.getX() - communeBBox.min.getX());
|
---|
563 | }
|
---|
564 |
|
---|
565 | public EastNorthBound getCommuneBBox() {
|
---|
566 | return communeBBox;
|
---|
567 | }
|
---|
568 |
|
---|
569 | public void setCommuneBBox(EastNorthBound entireCommune) {
|
---|
570 | this.communeBBox = entireCommune;
|
---|
571 | }
|
---|
572 |
|
---|
573 | /**
|
---|
574 | * Method required by ImageObserver when drawing an image
|
---|
575 | */
|
---|
576 | public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
|
---|
577 | return false;
|
---|
578 | }
|
---|
579 |
|
---|
580 | public int getLambertZone() {
|
---|
581 | return lambertZone;
|
---|
582 | }
|
---|
583 |
|
---|
584 | public EastNorth getRasterCenter() {
|
---|
585 | return new EastNorth((images.get(0).max.east()+images.get(0).min.east())/2,
|
---|
586 | (images.get(0).max.north()+images.get(0).min.north())/2);
|
---|
587 | }
|
---|
588 |
|
---|
589 | public void displace(double dx, double dy) {
|
---|
590 | this.rasterMin = new EastNorth(rasterMin.east() + dx, rasterMin.north() + dy);
|
---|
591 | this.rasterMax = new EastNorth(rasterMax.east() + dx, rasterMax.north() + dy);
|
---|
592 | images.get(0).shear(dx, dy);
|
---|
593 | }
|
---|
594 |
|
---|
595 | public void resize(EastNorth rasterCenter, double proportion) {
|
---|
596 | this.rasterMin = rasterMin.interpolate(rasterCenter, proportion);
|
---|
597 | this.rasterMax = rasterMax.interpolate(rasterCenter, proportion);
|
---|
598 | images.get(0).scale(rasterCenter, proportion);
|
---|
599 | }
|
---|
600 |
|
---|
601 | public void rotate(EastNorth rasterCenter, double angle) {
|
---|
602 | this.rasterMin = rasterMin.rotate(rasterCenter, angle);
|
---|
603 | this.rasterMax = rasterMax.rotate(rasterCenter, angle);
|
---|
604 | // double proportion = dst1.distance(dst2)/org1.distance(org2);
|
---|
605 | images.get(0).rotate(rasterCenter, angle);
|
---|
606 | this.angle += angle;
|
---|
607 | }
|
---|
608 |
|
---|
609 | private void paintCrosspieces(Graphics g, MapView mv) {
|
---|
610 | String crosspieces = Main.pref.get("cadastrewms.crosspieces", "0");
|
---|
611 | if (!crosspieces.equals("0")) {
|
---|
612 | int modulo = 25;
|
---|
613 | if (crosspieces.equals("2")) modulo = 50;
|
---|
614 | if (crosspieces.equals("3")) modulo = 100;
|
---|
615 | EastNorthBound currentView = new EastNorthBound(mv.getEastNorth(0, mv.getHeight()),
|
---|
616 | mv.getEastNorth(mv.getWidth(), 0));
|
---|
617 | int minX = ((int)currentView.min.east()/modulo+1)*modulo;
|
---|
618 | int minY = ((int)currentView.min.north()/modulo+1)*modulo;
|
---|
619 | int maxX = ((int)currentView.max.east()/modulo)*modulo;
|
---|
620 | int maxY = ((int)currentView.max.north()/modulo)*modulo;
|
---|
621 | int size=(maxX-minX)/modulo;
|
---|
622 | if (size<20) {
|
---|
623 | int px= size > 10 ? 2 : Math.abs(12-size);
|
---|
624 | g.setColor(Color.green);
|
---|
625 | for (int x=minX; x<=maxX; x+=modulo) {
|
---|
626 | for (int y=minY; y<=maxY; y+=modulo) {
|
---|
627 | Point p = mv.getPoint(new EastNorth(x,y));
|
---|
628 | g.drawLine(p.x-px, p.y, p.x+px, p.y);
|
---|
629 | g.drawLine(p.x, p.y-px, p.x, p.y+px);
|
---|
630 | }
|
---|
631 | }
|
---|
632 | }
|
---|
633 | }
|
---|
634 | }
|
---|
635 |
|
---|
636 | public GeorefImage getImage(int index) {
|
---|
637 | imagesLock.lock();
|
---|
638 | GeorefImage img = null;
|
---|
639 | try {
|
---|
640 | img = this.images.get(index);
|
---|
641 | } catch (ArrayIndexOutOfBoundsException e) {
|
---|
642 | e.printStackTrace(System.out);
|
---|
643 | }
|
---|
644 | imagesLock.unlock();
|
---|
645 | return img;
|
---|
646 | }
|
---|
647 |
|
---|
648 | public Vector<GeorefImage> getImages() {
|
---|
649 | return this.images;
|
---|
650 | }
|
---|
651 |
|
---|
652 | public void addImage(GeorefImage img) {
|
---|
653 | imagesLock.lock();
|
---|
654 | this.images.add(img);
|
---|
655 | imagesLock.unlock();
|
---|
656 | }
|
---|
657 |
|
---|
658 | public void setImages(Vector<GeorefImage> images) {
|
---|
659 | imagesLock.lock();
|
---|
660 | this.images = images;
|
---|
661 | imagesLock.unlock();
|
---|
662 | }
|
---|
663 |
|
---|
664 | public void clearImages() {
|
---|
665 | imagesLock.lock();
|
---|
666 | this.images.clear();
|
---|
667 | imagesLock.unlock();
|
---|
668 | }
|
---|
669 |
|
---|
670 | }
|
---|