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.event.ActionEvent;
|
---|
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.HashSet;
|
---|
22 | import java.util.Vector;
|
---|
23 | import java.util.concurrent.locks.Lock;
|
---|
24 | import java.util.concurrent.locks.ReentrantLock;
|
---|
25 |
|
---|
26 | import javax.swing.Action;
|
---|
27 | import javax.swing.Icon;
|
---|
28 | import javax.swing.ImageIcon;
|
---|
29 | import javax.swing.JOptionPane;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
33 | import org.openstreetmap.josm.data.Bounds;
|
---|
34 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
35 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
36 | import org.openstreetmap.josm.gui.MapView;
|
---|
37 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
38 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
39 | import 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 | */
|
---|
46 | public 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("Bounding box: {0}", communeBBox);
|
---|
251 | if(images.size()>0)
|
---|
252 | str += "\n"+tr("Image size (px): {0}/{1}", images.get(0).image.getWidth(), images.get(0).image.getHeight());
|
---|
253 | } else
|
---|
254 | str += "\n"+tr("Is vectorized.");
|
---|
255 | str += "\n"+tr("Commune bbox: {0}", communeBBox);
|
---|
256 | return str;
|
---|
257 | }
|
---|
258 |
|
---|
259 | @Override
|
---|
260 | public boolean isMergable(Layer other) {
|
---|
261 | return false;
|
---|
262 | }
|
---|
263 |
|
---|
264 | @Override
|
---|
265 | public void mergeFrom(Layer from) {
|
---|
266 | }
|
---|
267 |
|
---|
268 | @Override
|
---|
269 | public void paint(Graphics2D g, final MapView mv, Bounds bounds) {
|
---|
270 | synchronized(this){
|
---|
271 | Object savedInterpolation = g.getRenderingHint(RenderingHints.KEY_INTERPOLATION);
|
---|
272 | if (savedInterpolation == null) savedInterpolation = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
|
---|
273 | String interpolation = Main.pref.get("cadastrewms.imageInterpolation", "standard");
|
---|
274 | if (interpolation.equals("bilinear"))
|
---|
275 | g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
---|
276 | else if (interpolation.equals("bicubic"))
|
---|
277 | g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
---|
278 | else
|
---|
279 | g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
|
---|
280 | imagesLock.lock();
|
---|
281 | for (GeorefImage img : images)
|
---|
282 | img.paint(g, mv, CadastrePlugin.backgroundTransparent,
|
---|
283 | CadastrePlugin.transparency, CadastrePlugin.drawBoundaries);
|
---|
284 | imagesLock.unlock();
|
---|
285 | g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, savedInterpolation);
|
---|
286 | }
|
---|
287 | if (this.isRaster) {
|
---|
288 | paintCrosspieces(g, mv);
|
---|
289 | }
|
---|
290 | grabThread.paintBoxesToGrab(g, mv);
|
---|
291 | if (this.adjustModeEnabled) {
|
---|
292 | WMSAdjustAction.paintAdjustFrames(g, mv);
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | @Override
|
---|
297 | public void visitBoundingBox(BoundingXYVisitor v) {
|
---|
298 | for (GeorefImage img : images) {
|
---|
299 | v.visit(img.min);
|
---|
300 | v.visit(img.max);
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | @Override
|
---|
305 | public Object getInfoComponent() {
|
---|
306 | return getToolTipText();
|
---|
307 | }
|
---|
308 |
|
---|
309 | @Override
|
---|
310 | public Action[] getMenuEntries() {
|
---|
311 | saveAsPng = new MenuActionSaveRasterAs(this);
|
---|
312 | saveAsPng.setEnabled(isRaster);
|
---|
313 | cancelGrab = new MenuActionCancelGrab(this);
|
---|
314 | cancelGrab.setEnabled(!isRaster && grabThread.getImagesToGrabSize() > 0);
|
---|
315 | refineGeoRef = new MenuActionRefineGeoRef(this);
|
---|
316 | refineGeoRef.setEnabled(isRaster && grabThread.getImagesToGrabSize() == 0);
|
---|
317 | Action resetOffset = new ResetOffsetActionMenu();
|
---|
318 | resetOffset.setEnabled(!isRaster && images.size() > 0 && (deltaEast!=0.0 || deltaNorth!=0.0));
|
---|
319 | return new Action[] {
|
---|
320 | LayerListDialog.getInstance().createShowHideLayerAction(),
|
---|
321 | LayerListDialog.getInstance().createDeleteLayerAction(),
|
---|
322 | new MenuActionLoadFromCache(),
|
---|
323 | saveAsPng,
|
---|
324 | cancelGrab,
|
---|
325 | refineGeoRef,
|
---|
326 | resetOffset,
|
---|
327 | new LayerListPopup.InfoAction(this),
|
---|
328 |
|
---|
329 | };
|
---|
330 | }
|
---|
331 |
|
---|
332 | public GeorefImage findImage(EastNorth eastNorth) {
|
---|
333 | // Iterate in reverse, so we return the image which is painted last.
|
---|
334 | // (i.e. the topmost one)
|
---|
335 | for (int i = images.size() - 1; i >= 0; i--) {
|
---|
336 | if (images.get(i).contains(eastNorth)) {
|
---|
337 | return images.get(i);
|
---|
338 | }
|
---|
339 | }
|
---|
340 | return null;
|
---|
341 | }
|
---|
342 |
|
---|
343 | public boolean isOverlapping(Bounds bounds) {
|
---|
344 | GeorefImage georefImage =
|
---|
345 | new GeorefImage(null,
|
---|
346 | Main.getProjection().latlon2eastNorth(bounds.getMin()),
|
---|
347 | Main.getProjection().latlon2eastNorth(bounds.getMax()), this);
|
---|
348 | for (GeorefImage img : images) {
|
---|
349 | if (img.overlap(georefImage))
|
---|
350 | return true;
|
---|
351 | }
|
---|
352 | return false;
|
---|
353 | }
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Convert the eastNorth input coordinates to raster coordinates.
|
---|
357 | * The original raster size is [0,0,12286,8730] where 0,0 is the upper left corner and
|
---|
358 | * 12286,8730 is the approx. raster max size.
|
---|
359 | * @return the raster coordinates for the wms server request URL (minX,minY,maxX,maxY)
|
---|
360 | */
|
---|
361 | public String eastNorth2raster(EastNorth min, EastNorth max) {
|
---|
362 | double minX = (min.east() - rasterMin.east()) / rasterRatio;
|
---|
363 | double minY = (min.north() - rasterMin.north()) / rasterRatio;
|
---|
364 | double maxX = (max.east() - rasterMin.east()) / rasterRatio;
|
---|
365 | double maxY = (max.north() - rasterMin.north()) / rasterRatio;
|
---|
366 | return minX+","+minY+","+maxX+","+maxY;
|
---|
367 | }
|
---|
368 |
|
---|
369 |
|
---|
370 | public String getLocation() {
|
---|
371 | return location;
|
---|
372 | }
|
---|
373 |
|
---|
374 | public void setLocation(String location) {
|
---|
375 | this.location = location;
|
---|
376 | setName(rebuildName());
|
---|
377 | }
|
---|
378 |
|
---|
379 | public String getDepartement() {
|
---|
380 | return departement;
|
---|
381 | }
|
---|
382 |
|
---|
383 | public void setDepartement(String departement) {
|
---|
384 | this.departement = departement;
|
---|
385 | }
|
---|
386 |
|
---|
387 | public String getCodeCommune() {
|
---|
388 | return codeCommune;
|
---|
389 | }
|
---|
390 |
|
---|
391 | public void setCodeCommune(String codeCommune) {
|
---|
392 | this.codeCommune = codeCommune;
|
---|
393 | setName(rebuildName());
|
---|
394 | }
|
---|
395 |
|
---|
396 | public boolean isRaster() {
|
---|
397 | return isRaster;
|
---|
398 | }
|
---|
399 |
|
---|
400 | public void setRaster(boolean isRaster) {
|
---|
401 | this.isRaster = isRaster;
|
---|
402 | if (saveAsPng != null)
|
---|
403 | saveAsPng.setEnabled(isRaster);
|
---|
404 | }
|
---|
405 |
|
---|
406 | public boolean isAlreadyGeoreferenced() {
|
---|
407 | return isAlreadyGeoreferenced;
|
---|
408 | }
|
---|
409 |
|
---|
410 | public void setAlreadyGeoreferenced(boolean isAlreadyGeoreferenced) {
|
---|
411 | this.isAlreadyGeoreferenced = isAlreadyGeoreferenced;
|
---|
412 | }
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Set raster positions used for grabbing and georeferencing.
|
---|
416 | * rasterMin is the Eaast North of bottom left corner raster image on the screen when image is grabbed.
|
---|
417 | * The bounds width and height are the raster width and height. The image width matches the current view
|
---|
418 | * and the image height is adapted.
|
---|
419 | * Required: the communeBBox must be set (normally it is catched by CadastreInterface and saved by DownloadWMSPlanImage)
|
---|
420 | * @param bounds the current main map view boundaries
|
---|
421 | */
|
---|
422 | public void setRasterBounds(Bounds bounds) {
|
---|
423 | EastNorth rasterCenter = Main.getProjection().latlon2eastNorth(bounds.getCenter());
|
---|
424 | EastNorth eaMin = Main.getProjection().latlon2eastNorth(bounds.getMin());
|
---|
425 | EastNorth eaMax = Main.getProjection().latlon2eastNorth(bounds.getMax());
|
---|
426 | double rasterSizeX = communeBBox.max.getX() - communeBBox.min.getX();
|
---|
427 | double rasterSizeY = communeBBox.max.getY() - communeBBox.min.getY();
|
---|
428 | double ratio = rasterSizeY/rasterSizeX;
|
---|
429 | // keep same ratio on screen as WMS bbox (stored in communeBBox)
|
---|
430 | rasterMin = new EastNorth(eaMin.getX(), rasterCenter.getY()-(eaMax.getX()-eaMin.getX())*ratio/2);
|
---|
431 | rasterMax = new EastNorth(eaMax.getX(), rasterCenter.getY()+(eaMax.getX()-eaMin.getX())*ratio/2);
|
---|
432 | rasterRatio = (rasterMax.getX()-rasterMin.getX())/rasterSizeX;
|
---|
433 | }
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * Called by CacheControl when a new cache file is created on disk.
|
---|
437 | * Save only primitives to keep cache independent of software changes.
|
---|
438 | * @param oos
|
---|
439 | * @throws IOException
|
---|
440 | */
|
---|
441 | public void write(ObjectOutputStream oos) throws IOException {
|
---|
442 | currentFormat = this.serializeFormatVersion;
|
---|
443 | oos.writeInt(this.serializeFormatVersion);
|
---|
444 | oos.writeObject(this.location); // String
|
---|
445 | oos.writeObject(this.codeCommune); // String
|
---|
446 | oos.writeInt(this.lambertZone);
|
---|
447 | oos.writeBoolean(this.isRaster);
|
---|
448 | oos.writeBoolean(false); // previously buildingsOnly
|
---|
449 | if (this.isRaster) {
|
---|
450 | oos.writeDouble(this.rasterMin.getX());
|
---|
451 | oos.writeDouble(this.rasterMin.getY());
|
---|
452 | oos.writeDouble(this.rasterMax.getX());
|
---|
453 | oos.writeDouble(this.rasterMax.getY());
|
---|
454 | oos.writeDouble(this.rasterRatio);
|
---|
455 | }
|
---|
456 | oos.writeDouble(this.communeBBox.min.getX());
|
---|
457 | oos.writeDouble(this.communeBBox.min.getY());
|
---|
458 | oos.writeDouble(this.communeBBox.max.getX());
|
---|
459 | oos.writeDouble(this.communeBBox.max.getY());
|
---|
460 | }
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * Called by CacheControl when a cache file is read from disk.
|
---|
464 | * Cache uses only primitives to stay independent of software changes.
|
---|
465 | * @param ois
|
---|
466 | * @throws IOException
|
---|
467 | * @throws ClassNotFoundException
|
---|
468 | */
|
---|
469 | public boolean read(ObjectInputStream ois, int currentLambertZone) throws IOException, ClassNotFoundException {
|
---|
470 | currentFormat = ois.readInt();;
|
---|
471 | if (currentFormat < 2) {
|
---|
472 | JOptionPane.showMessageDialog(Main.parent, tr("Unsupported cache file version; found {0}, expected {1}\nCreate a new one.",
|
---|
473 | currentFormat, this.serializeFormatVersion), tr("Cache Format Error"), JOptionPane.ERROR_MESSAGE);
|
---|
474 | return false;
|
---|
475 | }
|
---|
476 | this.setLocation((String) ois.readObject());
|
---|
477 | this.setCodeCommune((String) ois.readObject());
|
---|
478 | this.lambertZone = ois.readInt();
|
---|
479 | this.setRaster(ois.readBoolean());
|
---|
480 | if (currentFormat >= 4)
|
---|
481 | ois.readBoolean();
|
---|
482 | if (this.isRaster) {
|
---|
483 | double X = ois.readDouble();
|
---|
484 | double Y = ois.readDouble();
|
---|
485 | this.rasterMin = new EastNorth(X, Y);
|
---|
486 | X = ois.readDouble();
|
---|
487 | Y = ois.readDouble();
|
---|
488 | this.rasterMax = new EastNorth(X, Y);
|
---|
489 | this.rasterRatio = ois.readDouble();
|
---|
490 | }
|
---|
491 | double minX = ois.readDouble();
|
---|
492 | double minY = ois.readDouble();
|
---|
493 | double maxX = ois.readDouble();
|
---|
494 | double maxY = ois.readDouble();
|
---|
495 | this.communeBBox = new EastNorthBound(new EastNorth(minX, minY), new EastNorth(maxX, maxY));
|
---|
496 | if (this.lambertZone != currentLambertZone && currentLambertZone != -1) {
|
---|
497 | JOptionPane.showMessageDialog(Main.parent, tr("Lambert zone {0} in cache "+
|
---|
498 | "incompatible with current Lambert zone {1}",
|
---|
499 | this.lambertZone+1, currentLambertZone), tr("Cache Lambert Zone Error"), JOptionPane.ERROR_MESSAGE);
|
---|
500 | return false;
|
---|
501 | }
|
---|
502 | synchronized(this){
|
---|
503 | boolean EOF = false;
|
---|
504 | try {
|
---|
505 | while (!EOF) {
|
---|
506 | GeorefImage newImage = (GeorefImage) ois.readObject();
|
---|
507 | for (GeorefImage img : this.images) {
|
---|
508 | if (CadastrePlugin.backgroundTransparent) {
|
---|
509 | if (img.overlap(newImage))
|
---|
510 | // mask overlapping zone in already grabbed image
|
---|
511 | img.withdraw(newImage);
|
---|
512 | else
|
---|
513 | // mask overlapping zone in new image only when
|
---|
514 | // new image covers completely the existing image
|
---|
515 | newImage.withdraw(img);
|
---|
516 | }
|
---|
517 | }
|
---|
518 | newImage.wmsLayer = this;
|
---|
519 | this.images.add(newImage);
|
---|
520 | }
|
---|
521 | } catch (EOFException ex) {
|
---|
522 | // expected exception when all images are read
|
---|
523 | }
|
---|
524 | }
|
---|
525 | System.out.println("Cache loaded for location "+location+" with "+images.size()+" images");
|
---|
526 | return true;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /**
|
---|
530 | * Join the grabbed images into one single.
|
---|
531 | */
|
---|
532 | public void joinBufferedImages() {
|
---|
533 | if (images.size() > 1) {
|
---|
534 | EastNorth min = images.get(0).min;
|
---|
535 | EastNorth max = images.get(images.size()-1).max;
|
---|
536 | int oldImgWidth = images.get(0).image.getWidth();
|
---|
537 | int oldImgHeight = images.get(0).image.getHeight();
|
---|
538 | HashSet<Double> lx = new HashSet<Double>();
|
---|
539 | HashSet<Double> ly = new HashSet<Double>();
|
---|
540 | for (GeorefImage img : images) {
|
---|
541 | lx.add(img.min.east());
|
---|
542 | ly.add(img.min.north());
|
---|
543 | }
|
---|
544 | int newWidth = oldImgWidth*lx.size();
|
---|
545 | int newHeight = oldImgHeight*ly.size();
|
---|
546 | BufferedImage new_img = new BufferedImage(newWidth, newHeight, images.get(0).image.getType()/*BufferedImage.TYPE_INT_ARGB*/);
|
---|
547 | Graphics g = new_img.getGraphics();
|
---|
548 | // Coordinate (0,0) is on top,left corner where images are grabbed from bottom left
|
---|
549 | int rasterDivider = (int)Math.sqrt(images.size());
|
---|
550 | for (int h = 0; h < lx.size(); h++) {
|
---|
551 | for (int v = 0; v < ly.size(); v++) {
|
---|
552 | int newx = h*oldImgWidth;
|
---|
553 | int newy = newHeight - oldImgHeight - (v*oldImgHeight);
|
---|
554 | int j = h*rasterDivider + v;
|
---|
555 | g.drawImage(images.get(j).image, newx, newy, this);
|
---|
556 | }
|
---|
557 | }
|
---|
558 | synchronized(this) {
|
---|
559 | images.clear();
|
---|
560 | images.add(new GeorefImage(new_img, min, max, this));
|
---|
561 | }
|
---|
562 | }
|
---|
563 | }
|
---|
564 |
|
---|
565 | /**
|
---|
566 | * Image cropping based on two EN coordinates pointing to two corners in diagonal
|
---|
567 | * Because it's coming from user mouse clics, we have to sort de positions first.
|
---|
568 | * Works only for raster image layer (only one image in collection).
|
---|
569 | * Updates layer georeferences.
|
---|
570 | * @param en1
|
---|
571 | * @param en2
|
---|
572 | */
|
---|
573 | public void cropImage(EastNorth en1, EastNorth en2){
|
---|
574 | // adj1 is corner bottom, left
|
---|
575 | EastNorth adj1 = new EastNorth(en1.east() <= en2.east() ? en1.east() : en2.east(),
|
---|
576 | en1.north() <= en2.north() ? en1.north() : en2.north());
|
---|
577 | // adj2 is corner top, right
|
---|
578 | EastNorth adj2 = new EastNorth(en1.east() > en2.east() ? en1.east() : en2.east(),
|
---|
579 | en1.north() > en2.north() ? en1.north() : en2.north());
|
---|
580 | images.get(0).crop(adj1, adj2);
|
---|
581 | // update the layer georefs
|
---|
582 | rasterMin = adj1;
|
---|
583 | rasterMax = adj2;
|
---|
584 | setCommuneBBox(new EastNorthBound(new EastNorth(0,0), new EastNorth(images.get(0).image.getWidth()-1,images.get(0).image.getHeight()-1)));
|
---|
585 | rasterRatio = (rasterMax.getX()-rasterMin.getX())/(communeBBox.max.getX() - communeBBox.min.getX());
|
---|
586 | }
|
---|
587 |
|
---|
588 | public EastNorthBound getCommuneBBox() {
|
---|
589 | return communeBBox;
|
---|
590 | }
|
---|
591 |
|
---|
592 | public EastNorthBound getFirstViewFromCacheBBox() {
|
---|
593 | if (isRaster) {
|
---|
594 | return communeBBox;
|
---|
595 | }
|
---|
596 | double min_x = Double.MAX_VALUE;
|
---|
597 | double max_x = Double.MIN_VALUE;
|
---|
598 | double min_y = Double.MAX_VALUE;
|
---|
599 | double max_y = Double.MIN_VALUE;
|
---|
600 | for (GeorefImage image:images){
|
---|
601 | min_x = image.min.east() < min_x ? image.min.east() : min_x;
|
---|
602 | max_x = image.max.east() > max_x ? image.max.east() : max_x;
|
---|
603 | min_y = image.min.north() < min_y ? image.min.north() : min_y;
|
---|
604 | max_y = image.max.north() > max_y ? image.max.north() : max_y;
|
---|
605 | }
|
---|
606 | EastNorthBound maxGrabbedBBox = new EastNorthBound(new EastNorth(min_x, min_y), new EastNorth(max_x, max_y));
|
---|
607 | return maxGrabbedBBox;
|
---|
608 | }
|
---|
609 |
|
---|
610 | public void setCommuneBBox(EastNorthBound entireCommune) {
|
---|
611 | this.communeBBox = entireCommune;
|
---|
612 | }
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * Method required by ImageObserver when drawing an image
|
---|
616 | */
|
---|
617 | public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
|
---|
618 | return false;
|
---|
619 | }
|
---|
620 |
|
---|
621 | public int getLambertZone() {
|
---|
622 | return lambertZone;
|
---|
623 | }
|
---|
624 |
|
---|
625 | public EastNorth getRasterCenter() {
|
---|
626 | return new EastNorth((images.get(0).max.east()+images.get(0).min.east())/2,
|
---|
627 | (images.get(0).max.north()+images.get(0).min.north())/2);
|
---|
628 | }
|
---|
629 |
|
---|
630 | public void displace(double dx, double dy) {
|
---|
631 | if (isRaster) {
|
---|
632 | this.rasterMin = new EastNorth(rasterMin.east() + dx, rasterMin.north() + dy);
|
---|
633 | this.rasterMax = new EastNorth(rasterMax.east() + dx, rasterMax.north() + dy);
|
---|
634 | images.get(0).shear(dx, dy);
|
---|
635 | } else {
|
---|
636 | deltaEast+=dx;
|
---|
637 | deltaNorth+=dy;
|
---|
638 | }
|
---|
639 | }
|
---|
640 |
|
---|
641 | public void resize(EastNorth rasterCenter, double proportion) {
|
---|
642 | this.rasterMin = rasterMin.interpolate(rasterCenter, proportion);
|
---|
643 | this.rasterMax = rasterMax.interpolate(rasterCenter, proportion);
|
---|
644 | images.get(0).scale(rasterCenter, proportion);
|
---|
645 | }
|
---|
646 |
|
---|
647 | public void rotate(EastNorth rasterCenter, double angle) {
|
---|
648 | this.rasterMin = rasterMin.rotate(rasterCenter, angle);
|
---|
649 | this.rasterMax = rasterMax.rotate(rasterCenter, angle);
|
---|
650 | // double proportion = dst1.distance(dst2)/org1.distance(org2);
|
---|
651 | images.get(0).rotate(rasterCenter, angle);
|
---|
652 | this.angle += angle;
|
---|
653 | }
|
---|
654 |
|
---|
655 | private void paintCrosspieces(Graphics g, MapView mv) {
|
---|
656 | String crosspieces = Main.pref.get("cadastrewms.crosspieces", "0");
|
---|
657 | if (!crosspieces.equals("0")) {
|
---|
658 | int modulo = 25;
|
---|
659 | if (crosspieces.equals("2")) modulo = 50;
|
---|
660 | if (crosspieces.equals("3")) modulo = 100;
|
---|
661 | EastNorthBound currentView = new EastNorthBound(mv.getEastNorth(0, mv.getHeight()),
|
---|
662 | mv.getEastNorth(mv.getWidth(), 0));
|
---|
663 | int minX = ((int)currentView.min.east()/modulo+1)*modulo;
|
---|
664 | int minY = ((int)currentView.min.north()/modulo+1)*modulo;
|
---|
665 | int maxX = ((int)currentView.max.east()/modulo)*modulo;
|
---|
666 | int maxY = ((int)currentView.max.north()/modulo)*modulo;
|
---|
667 | int size=(maxX-minX)/modulo;
|
---|
668 | if (size<20) {
|
---|
669 | int px= size > 10 ? 2 : Math.abs(12-size);
|
---|
670 | g.setColor(Color.green);
|
---|
671 | for (int x=minX; x<=maxX; x+=modulo) {
|
---|
672 | for (int y=minY; y<=maxY; y+=modulo) {
|
---|
673 | Point p = mv.getPoint(new EastNorth(x,y));
|
---|
674 | g.drawLine(p.x-px, p.y, p.x+px, p.y);
|
---|
675 | g.drawLine(p.x, p.y-px, p.x, p.y+px);
|
---|
676 | }
|
---|
677 | }
|
---|
678 | }
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | public GeorefImage getImage(int index) {
|
---|
683 | imagesLock.lock();
|
---|
684 | GeorefImage img = null;
|
---|
685 | try {
|
---|
686 | img = this.images.get(index);
|
---|
687 | } catch (ArrayIndexOutOfBoundsException e) {
|
---|
688 | e.printStackTrace(System.out);
|
---|
689 | }
|
---|
690 | imagesLock.unlock();
|
---|
691 | return img;
|
---|
692 | }
|
---|
693 |
|
---|
694 | public Vector<GeorefImage> getImages() {
|
---|
695 | return this.images;
|
---|
696 | }
|
---|
697 |
|
---|
698 | public void addImage(GeorefImage img) {
|
---|
699 | imagesLock.lock();
|
---|
700 | this.images.add(img);
|
---|
701 | imagesLock.unlock();
|
---|
702 | }
|
---|
703 |
|
---|
704 | public void setImages(Vector<GeorefImage> images) {
|
---|
705 | imagesLock.lock();
|
---|
706 | this.images = images;
|
---|
707 | imagesLock.unlock();
|
---|
708 | }
|
---|
709 |
|
---|
710 | public void clearImages() {
|
---|
711 | imagesLock.lock();
|
---|
712 | this.images.clear();
|
---|
713 | imagesLock.unlock();
|
---|
714 | }
|
---|
715 |
|
---|
716 | }
|
---|