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