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