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