Changeset 26413 in osm for applications/editors/josm/plugins/ImportImagePlugin/src
- Timestamp:
- 2011-07-28T23:31:27+02:00 (13 years ago)
- Location:
- applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin/ImageLayer.java
r25934 r26413 4 4 5 5 import java.awt.Component; 6 import java.awt.Color; 6 7 import java.awt.Graphics2D; 7 8 import java.awt.Image; … … 68 69 private CoordinateReferenceSystem sourceRefSys; 69 70 70 71 72 71 /** 73 72 * Constructor … … 82 81 this.image = (BufferedImage) createImage(); 83 82 layericon = new ImageIcon(ImportImagePlugin.pluginClassLoader.getResource("images/layericon.png")); 84 85 83 } 86 84 … … 97 95 try { 98 96 // create a grid coverage from the image 99 coverage = PluginOperations.createGridFromFile(imageFile, null); 97 coverage = PluginOperations.createGridFromFile(imageFile, null, true); 100 98 this.sourceRefSys = coverage.getCoordinateReferenceSystem(); 101 99 … … 109 107 if(e.getMessage().contains("No projection file found")) 110 108 { 111 int use DefaultCRS= JOptionPane.showConfirmDialog(Main.parent, "<html>No projection file (.prj) found.<br>Use thedefault Coordinate Reference System instead?</html>", "Missing projection", JOptionPane.YES_NO_OPTION);112 if (use DefaultCRS == 0)113 {109 int useUnprojected = JOptionPane.showConfirmDialog(Main.parent, "<html>No projection file (.prj) found.<br />Use the image unprojected?</html>", "Missing projection", JOptionPane.YES_NO_OPTION); 110 if (useUnprojected == 0) { // Yes 111 logger.debug("Passing through image un-projected."); 114 112 try { 115 113 // create a grid coverage from the image 116 coverage = PluginOperations.createGridFromFile(imageFile, PluginOperations.defaultSourceCRS);114 coverage = PluginOperations.createGridFromFile(imageFile, null, false); 117 115 this.sourceRefSys = coverage.getCoordinateReferenceSystem(); 118 119 // now reproject grid coverage120 coverage = PluginOperations.reprojectCoverage(coverage, CRS.decode(Main.proj.toCode()));121 116 } catch (Exception e1) { 122 117 logger.error("Error while creating GridCoverage:",e1); 123 118 throw new IOException(e1); 124 119 } 125 } 126 else{ 127 logger.debug("Layer creation cancled by user due to missing projection information."); 120 } else { // No 121 logger.debug("No projection and user declined un-projected use"); 128 122 throw new LayerCreationCancledException(); 129 123 } 130 131 124 } 132 125 else … … 140 133 141 134 // TODO 142 upperLeft = new EastNorth(coverage.getEnvelope2D().y, coverage 143 .getEnvelope2D().x 144 + coverage.getEnvelope2D().width); 135 upperLeft = new EastNorth(coverage.getEnvelope2D().x, 136 coverage.getEnvelope2D().y + coverage.getEnvelope2D().height); 145 137 angle = 0; 146 138 bbox = coverage.getEnvelope2D(); … … 163 155 EastNorth center = Main.map.mapView.getCenter(); 164 156 EastNorth leftop = Main.map.mapView.getEastNorth(0, 0); 165 double pixel_per_ lon_degree= (Main.map.mapView.getWidth() / 2.0)157 double pixel_per_east_unit = (Main.map.mapView.getWidth() / 2.0) 166 158 / (center.east() - leftop.east()); 167 double pixel_per_ lat_degree= (Main.map.mapView.getHeight() / 2.0)159 double pixel_per_north_unit = (Main.map.mapView.getHeight() / 2.0) 168 160 / (leftop.north() - center.north()); 169 161 170 162 // This is now the offset in screen pixels 171 double pic_offset_x = ((upperLeft.east() - leftop.east()) * pixel_per_ lon_degree);172 double pic_offset_y = ((leftop.north() - upperLeft.north()) * pixel_per_ lat_degree);163 double pic_offset_x = ((upperLeft.east() - leftop.east()) * pixel_per_east_unit); 164 double pic_offset_y = ((leftop.north() - upperLeft.north()) * pixel_per_north_unit); 173 165 174 166 Graphics2D g = (Graphics2D) g2.create(); … … 187 179 double height = projbounds.maxNorth - projbounds.minNorth; 188 180 189 double ratio_x = (this.bbox.getMax Y() - this.bbox.getMinY())181 double ratio_x = (this.bbox.getMaxX() - this.bbox.getMinX()) 190 182 / width; 191 double ratio_y = (this.bbox.getMax X() - this.bbox.getMinX())183 double ratio_y = (this.bbox.getMaxY() - this.bbox.getMinY()) 192 184 / height; 193 185 … … 199 191 double scaley = pixels4bbox_height / image.getHeight(); 200 192 193 if ((scalex > 10) || (scaley > 10)) { 194 logger.warn("Not drawing image - scale too big"); 195 return; 196 } 201 197 g.scale(scalex, scaley); 202 198 … … 282 278 void resample(CoordinateReferenceSystem refSys) throws IOException, NoSuchAuthorityCodeException, FactoryException 283 279 { 284 285 GridCoverage2D coverage = PluginOperations.createGridFromFile(this.imageFile, refSys); 280 logger.debug("resample"); 281 GridCoverage2D coverage = PluginOperations.createGridFromFile(this.imageFile, refSys, true); 286 282 coverage = PluginOperations.reprojectCoverage(coverage, CRS.decode(Main.proj.toCode())); 287 283 this.bbox = coverage.getEnvelope2D(); … … 333 329 } 334 330 335 336 337 331 public CoordinateReferenceSystem getSourceRefSys() { 338 332 return sourceRefSys; -
applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin/LoadImageAction.java
r23305 r26413 14 14 import org.openstreetmap.josm.actions.JosmAction; 15 15 import org.openstreetmap.josm.data.Bounds; 16 import org.openstreetmap.josm.data.coor. LatLon;16 import org.openstreetmap.josm.data.coor.EastNorth; 17 17 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 18 18 import org.openstreetmap.josm.plugins.ImportImagePlugin.ImageLayer.LayerCreationCancledException; … … 45 45 ImageLayer layer = null; 46 46 if (result == JFileChooser.APPROVE_OPTION) { 47 logger.info("File choose d:" + fc.getSelectedFile());47 logger.info("File choosen:" + fc.getSelectedFile()); 48 48 try { 49 49 layer = new ImageLayer(fc.getSelectedFile()); … … 51 51 // if user decides that layer should not be created just return. 52 52 return; 53 }catch (Exception e) { 53 } catch (Exception e) { 54 54 logger.error("Error while creating image layer: \n" + e.getMessage()); 55 55 JOptionPane.showMessageDialog(null, marktr("Error while creating image layer: " + e.getCause())); … … 60 60 // Add layer: 61 61 Main.main.addLayer(layer); 62 LatLonmin = newLatLon(layer.getBbox().getMinX(), layer.getBbox().getMinY());63 LatLonmax = newLatLon(layer.getBbox().getMaxX(), layer.getBbox().getMaxY());62 EastNorth min = new EastNorth(layer.getBbox().getMinX(), layer.getBbox().getMinY()); 63 EastNorth max = new EastNorth(layer.getBbox().getMaxX(), layer.getBbox().getMaxY()); 64 64 BoundingXYVisitor boundingXYVisitor = new BoundingXYVisitor(); 65 boundingXYVisitor.visit(new Bounds(min, max)); 65 boundingXYVisitor.visit(min); 66 boundingXYVisitor.visit(max); 66 67 Main.map.mapView.recalculateCenterScale(boundingXYVisitor); 67 Main.map.mapView.zoomTo(new Bounds(min, max));68 68 } 69 69 } -
applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin/PluginOperations.java
r23305 r26413 107 107 * @throws Exception 108 108 */ 109 public static GridCoverage2D createGridFromFile(File file, CoordinateReferenceSystem refSys) throws IOException{ 109 public static GridCoverage2D createGridFromFile(File file, CoordinateReferenceSystem refSys, boolean failIfNoPrjFile) throws IOException { 110 110 111 111 GridCoverage2D coverage = null; … … 125 125 126 126 // try to read GeoTIFF: 127 try{ 127 try { 128 128 coverage = readGeoTiff(file, refSys); 129 129 return coverage; 130 }catch (DataSourceException dse) { 131 if(!dse.getMessage().contains("Coordinate Reference System is not available")){ 130 } catch (DataSourceException dse) { 131 if (!dse.getMessage().contains("Coordinate Reference System is not available")){ 132 132 dse.printStackTrace(); 133 133 } … … 143 143 for (int i = 0; i < postfixes.length; i++) { 144 144 File prjFile = new File(fileNameWithoutExt + "." + postfixes[i]); 145 if(prjFile.exists()){ 145 if (prjFile.exists()){ 146 146 tfwReader = new WorldFileReader(prjFile); 147 147 } 148 148 } 149 if(tfwReader == null){ 149 150 if (tfwReader == null) { 150 151 throw new IOException("No Worldfile found."); 151 152 } 152 153 153 if(refSys == null){ 154 if (refSys == null) { 154 155 // if no crs is delivered try to read projection file: 155 156 refSys = readPrjFile(file); 156 if(refSys == null) throw new IOException("No projection file found."); 157 if (refSys == null) { 158 if (failIfNoPrjFile) throw new IOException("No projection file found."); 159 logger.debug("no projection given, no projection file found; using unprojected file."); 160 } 157 161 } 158 162 … … 178 182 for (int i = 0; i < postfixes.length; i++) { 179 183 File prjFile = new File(fileNameWithoutExt + "." + postfixes[i]); 180 if(prjFile.exists()){ 184 if (prjFile.exists()){ 181 185 tfwReader = new WorldFileReader(prjFile); 182 186 } 183 187 } 184 if(tfwReader == null) throw new IOException("No Worldfile found."); 185 186 if(refSys == null){ 188 if (tfwReader == null) throw new IOException("No Worldfile found."); 189 190 if (refSys == null) { 187 191 // if no crs is delivered try to read projection file: 188 192 refSys = readPrjFile(file); 189 if(refSys == null) throw new IOException("No projection file found."); 193 if (refSys == null) { 194 if (failIfNoPrjFile) throw new IOException("No projection file found."); 195 logger.debug("no projection given, no projection file found; using unprojected file."); 196 } 190 197 } 191 198 … … 209 216 for (int i = 0; i < postfixes.length; i++) { 210 217 File prjFile = new File(fileNameWithoutExt + "." + postfixes[i]); 211 if(prjFile.exists()){ 218 if (prjFile.exists()){ 212 219 tfwReader = new WorldFileReader(prjFile); 213 220 } 214 221 } 215 if(tfwReader == null) throw new IOException("No Worldfile found."); 216 217 if(refSys == null){ 222 if (tfwReader == null) throw new IOException("No Worldfile found."); 223 224 if (refSys == null) { 218 225 // if no crs is delivered try to read projection file: 219 226 refSys = readPrjFile(file); 220 if(refSys == null) throw new IOException("No projection file found."); 227 if (refSys == null) { 228 if (failIfNoPrjFile) throw new IOException("No projection file found."); 229 logger.debug("no projection given, no projection file found; using unprojected file."); 230 } 221 231 } 222 232 … … 240 250 for (int i = 0; i < postfixes.length; i++) { 241 251 File prjFile = new File(fileNameWithoutExt + "." + postfixes[i]); 242 if(prjFile.exists()){ 252 if (prjFile.exists()){ 243 253 tfwReader = new WorldFileReader(prjFile); 244 254 } … … 246 256 if(tfwReader == null) throw new IOException("No Worldfile found."); 247 257 248 if(refSys == null){ 258 if (refSys == null) { 249 259 // if no crs is delivered try to read projection file: 250 260 refSys = readPrjFile(file); 251 if(refSys == null) throw new IOException("No projection file found."); 261 if (refSys == null) { 262 if (failIfNoPrjFile) throw new IOException("No projection file found."); 263 logger.debug("no projection given, no projection file found; using unprojected file."); 264 } 252 265 } 253 266 … … 290 303 291 304 File prjFile = new File(prjFilename); 292 if(!prjFile.exists()) throw new IOException("No projection file found (.prj) for image '" + file.getName() + "'");305 if (!prjFile.exists()) return null; 293 306 logger.debug("Loading .prj file: " + prjFile.getAbsolutePath()); 294 307
Note:
See TracChangeset
for help on using the changeset viewer.