source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/WMSLayer.java@ 29714

Last change on this file since 29714 was 29714, checked in by pieren, 11 years ago

fix raster image georeferencing issues. Add new MenuActionRefineGeoRef for a new georeferencing already referenced plan image.

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