| | 1 | package org.openstreetmap.josm.gui.layer; |
| | 2 | |
| | 3 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 4 | |
| | 5 | import geotiffplugin.GeoTiffPlugin; |
| | 6 | |
| | 7 | import java.awt.Component; |
| | 8 | import java.awt.Graphics; |
| | 9 | import java.awt.Graphics2D; |
| | 10 | import java.awt.Toolkit; |
| | 11 | import java.awt.geom.AffineTransform; |
| | 12 | import java.awt.image.RenderedImage; |
| | 13 | import java.io.File; |
| | 14 | import java.io.IOException; |
| | 15 | import java.util.concurrent.ExecutorService; |
| | 16 | import java.util.concurrent.Executors; |
| | 17 | |
| | 18 | import javax.swing.Icon; |
| | 19 | import javax.swing.ImageIcon; |
| | 20 | import javax.swing.JFileChooser; |
| | 21 | import javax.swing.JOptionPane; |
| | 22 | import javax.swing.filechooser.FileFilter; |
| | 23 | |
| | 24 | import javax.media.jai.*; |
| | 25 | |
| | 26 | import org.geotools.coverage.grid.GridCoverage2D; |
| | 27 | import org.geotools.data.DataSourceException; |
| | 28 | import org.geotools.gce.geotiff.GeoTiffReader; |
| | 29 | import org.opengis.geometry.DirectPosition; |
| | 30 | import org.opengis.geometry.Envelope; |
| | 31 | import org.opengis.referencing.crs.CoordinateReferenceSystem; |
| | 32 | import org.openstreetmap.josm.Main; |
| | 33 | import org.openstreetmap.josm.actions.ExtensionFileFilter; |
| | 34 | import org.openstreetmap.josm.data.Bounds; |
| | 35 | import org.openstreetmap.josm.data.coor.EastNorth; |
| | 36 | import org.openstreetmap.josm.data.coor.LatLon; |
| | 37 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; |
| | 38 | import org.openstreetmap.josm.gui.MapView; |
| | 39 | import org.openstreetmap.josm.tools.ImageProvider; |
| | 40 | |
| | 41 | |
| | 42 | /** |
| | 43 | * This is a layer that displays a geotiff image |
| | 44 | */ |
| | 45 | public class geoTiffLayer extends Layer { |
| | 46 | |
| | 47 | protected static final Icon icon = |
| | 48 | new ImageIcon(Toolkit.getDefaultToolkit().createImage(GeoTiffPlugin.class.getResource("/images/geotiff_small.png"))); |
| | 49 | |
| | 50 | protected ExecutorService executor = null; |
| | 51 | protected String baseURL; |
| | 52 | protected GeoTiffReader gtRreader = null; |
| | 53 | protected GridCoverage2D coverage = null; |
| | 54 | protected File file; |
| | 55 | protected RenderedImage image; |
| | 56 | protected CoordinateReferenceSystem crs; |
| | 57 | protected Envelope env; |
| | 58 | |
| | 59 | protected DirectPosition lc; |
| | 60 | protected EastNorth lcEN; |
| | 61 | protected DirectPosition uc; |
| | 62 | protected EastNorth ucEN; |
| | 63 | |
| | 64 | public geoTiffLayer() { |
| | 65 | this(tr("Blank Layer"), null); |
| | 66 | } |
| | 67 | |
| | 68 | |
| | 69 | public geoTiffLayer(String name, String baseURL) { |
| | 70 | super(name); |
| | 71 | this.baseURL = baseURL; |
| | 72 | try { |
| | 73 | file = new File(baseURL); |
| | 74 | } catch (Exception e) { |
| | 75 | JFileChooser fc = createAndOpenFileChooser(true, false); |
| | 76 | if (fc == null){ |
| | 77 | System.out.println("no file"); |
| | 78 | return; |
| | 79 | }else{ |
| | 80 | file = fc.getSelectedFile(); |
| | 81 | } |
| | 82 | } |
| | 83 | file = new File(file.getAbsolutePath()); |
| | 84 | try { |
| | 85 | gtRreader = new GeoTiffReader(file); |
| | 86 | } catch (DataSourceException e) { |
| | 87 | e.printStackTrace(); |
| | 88 | return; |
| | 89 | } |
| | 90 | try { |
| | 91 | coverage = (GridCoverage2D) gtRreader.read(null); |
| | 92 | } catch (IOException ex) { |
| | 93 | ex.printStackTrace(); |
| | 94 | return; |
| | 95 | } |
| | 96 | this.name = file.getName(); |
| | 97 | crs = coverage.getCoordinateReferenceSystem2D(); |
| | 98 | env = coverage.getEnvelope(); |
| | 99 | image = coverage.getRenderedImage(); |
| | 100 | |
| | 101 | lc = env.getLowerCorner(); |
| | 102 | lcEN = Main.proj.latlon2eastNorth(new LatLon(lc.getCoordinate()[1],lc.getCoordinate()[0])); |
| | 103 | uc = env.getUpperCorner(); |
| | 104 | ucEN = Main.proj.latlon2eastNorth(new LatLon(uc.getCoordinate()[1],uc.getCoordinate()[0])); |
| | 105 | |
| | 106 | executor = Executors.newFixedThreadPool(3); |
| | 107 | } |
| | 108 | |
| | 109 | public void destroy() { |
| | 110 | try { |
| | 111 | executor.shutdown(); |
| | 112 | // Might not be initalized, so catch NullPointer as well |
| | 113 | } catch(Exception x) {} |
| | 114 | } |
| | 115 | |
| | 116 | @Override public Icon getIcon() { |
| | 117 | return ImageProvider.get("layer", "geotiff_small"); |
| | 118 | } |
| | 119 | |
| | 120 | @Override public String getToolTipText() { |
| | 121 | |
| | 122 | return tr("GeoTifflayer ({0}), file: {1}", name, baseURL); |
| | 123 | |
| | 124 | } |
| | 125 | |
| | 126 | @Override public boolean isMergable(Layer other) { |
| | 127 | return false; |
| | 128 | } |
| | 129 | |
| | 130 | @Override public void mergeFrom(Layer from) { |
| | 131 | } |
| | 132 | |
| | 133 | @Override public void paint(Graphics g, final MapView mv) { |
| | 134 | if (env == null){ |
| | 135 | System.out.println("file error"); |
| | 136 | return; |
| | 137 | } |
| | 138 | |
| | 139 | java.awt.Point min = mv.getPoint(lcEN); |
| | 140 | java.awt.Point max = mv.getPoint(ucEN); |
| | 141 | |
| | 142 | double width = Math.abs(max.getX()-min.getX()); |
| | 143 | double height = Math.abs(max.getY()-min.getY()); |
| | 144 | |
| | 145 | Graphics2D g2d = (Graphics2D)g; |
| | 146 | |
| | 147 | AffineTransform trans = new AffineTransform(); |
| | 148 | trans.translate(min.getX(), max.getY()); |
| | 149 | trans.scale(width/image.getWidth(),height/image.getHeight()); |
| | 150 | |
| | 151 | g2d.drawRenderedImage(image, trans); |
| | 152 | |
| | 153 | } |
| | 154 | |
| | 155 | @Override public void visitBoundingBox(BoundingXYVisitor v) { |
| | 156 | v.visit(lcEN); |
| | 157 | v.visit(ucEN); |
| | 158 | } |
| | 159 | |
| | 160 | @Override public Object getInfoComponent() { |
| | 161 | return getToolTipText(); |
| | 162 | } |
| | 163 | |
| | 164 | @Override public Component[] getMenuEntries() { |
| | 165 | return new Component[]{ |
| | 166 | }; |
| | 167 | } |
| | 168 | |
| | 169 | |
| | 170 | protected static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple) { |
| | 171 | String curDir = Main.pref.get("lastDirectory"); |
| | 172 | if (curDir.equals("")) |
| | 173 | curDir = "."; |
| | 174 | JFileChooser fc = new JFileChooser(new File(curDir)); |
| | 175 | fc.setMultiSelectionEnabled(multiple); |
| | 176 | for (int i = 0; i < ExtensionFileFilter.filters.length; ++i) |
| | 177 | fc.addChoosableFileFilter(ExtensionFileFilter.filters[i]); |
| | 178 | fc.setAcceptAllFileFilterUsed(true); |
| | 179 | |
| | 180 | int answer = open ? fc.showOpenDialog(Main.parent) : fc.showSaveDialog(Main.parent); |
| | 181 | if (answer != JFileChooser.APPROVE_OPTION) |
| | 182 | return null; |
| | 183 | |
| | 184 | if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) |
| | 185 | Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath()); |
| | 186 | |
| | 187 | if (!open) { |
| | 188 | File file = fc.getSelectedFile(); |
| | 189 | if (file == null || (file.exists() && JOptionPane.YES_OPTION != |
| | 190 | JOptionPane.showConfirmDialog(Main.parent, tr("File exists. Overwrite?"), tr("Overwrite"), JOptionPane.YES_NO_OPTION))) |
| | 191 | return null; |
| | 192 | } |
| | 193 | |
| | 194 | return fc; |
| | 195 | } |
| | 196 | |
| | 197 | public static File openFileDialog(boolean open) { |
| | 198 | JFileChooser fc = createAndOpenFileChooser(open, false); |
| | 199 | if (fc == null) |
| | 200 | return null; |
| | 201 | |
| | 202 | File file = fc.getSelectedFile(); |
| | 203 | |
| | 204 | |
| | 205 | String fn = file.getPath(); |
| | 206 | if (fn.indexOf('.') == -1) { |
| | 207 | FileFilter ff = fc.getFileFilter(); |
| | 208 | if (ff instanceof ExtensionFileFilter) |
| | 209 | fn = "." + ((ExtensionFileFilter)ff).defaultExtension; |
| | 210 | else |
| | 211 | fn += ".tif"; |
| | 212 | file = new File(fn); |
| | 213 | } |
| | 214 | return file; |
| | 215 | } |
| | 216 | } |