Changeset 26452 in osm for applications/editors/josm/plugins/ImportImagePlugin
- Timestamp:
- 2011-08-05T09:22:17+02:00 (13 years ago)
- Location:
- applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin/ImageLayer.java
r26413 r26452 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java.awt.Component;6 import java.awt.Color;7 5 import java.awt.Graphics2D; 8 6 import java.awt.Image; … … 17 15 import javax.swing.Icon; 18 16 import javax.swing.ImageIcon; 19 import javax.swing.JMenuItem;20 import javax.swing.JOptionPane;21 import javax.swing.JSeparator;22 17 23 18 import org.apache.log4j.Logger; … … 36 31 import org.openstreetmap.josm.data.coor.LatLon; 37 32 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 33 import org.openstreetmap.josm.gui.ExtendedDialog; 38 34 import org.openstreetmap.josm.gui.MapView; 39 35 import org.openstreetmap.josm.gui.dialogs.LayerListDialog; … … 43 39 /** 44 40 * Layer which contains spatial referenced image data. 45 * 41 * 46 42 * @author Christoph Beekmans, Fabian Kowitz, Anna Robaszkiewicz, Oliver Kuhn, Martin Ulitzny 47 43 * … … 52 48 53 49 private File imageFile; 54 50 55 51 private BufferedImage image = null; 56 52 … … 62 58 // current bbox 63 59 private Envelope2D bbox; 64 60 65 61 // Layer icon 66 62 private Icon layericon = null; 67 63 68 64 // reference system of the oringinal image 69 65 private CoordinateReferenceSystem sourceRefSys; … … 71 67 /** 72 68 * Constructor 73 * 69 * 74 70 * @param file 75 * @throws IOException 71 * @throws IOException 76 72 */ 77 73 public ImageLayer(File file) throws IOException { 78 74 super(file.getName()); 79 75 80 76 this.imageFile = file; 81 77 this.image = (BufferedImage) createImage(); … … 85 81 /** 86 82 * create spatial referenced image. 87 * 83 * 88 84 * @return 89 85 * @throws IOException … … 97 93 coverage = PluginOperations.createGridFromFile(imageFile, null, true); 98 94 this.sourceRefSys = coverage.getCoordinateReferenceSystem(); 99 95 100 96 // now reproject grid coverage 101 coverage = PluginOperations.reprojectCoverage(coverage, CRS.decode(Main. proj.toCode()));102 97 coverage = PluginOperations.reprojectCoverage(coverage, CRS.decode(Main.getProjection().toCode())); 98 103 99 } catch (FactoryException e) { 104 100 logger.error("Error while creating GridCoverage:",e); … … 107 103 if(e.getMessage().contains("No projection file found")) 108 104 { 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."); 112 try { 113 // create a grid coverage from the image 114 coverage = PluginOperations.createGridFromFile(imageFile, null, false); 115 this.sourceRefSys = coverage.getCoordinateReferenceSystem(); 116 } catch (Exception e1) { 117 logger.error("Error while creating GridCoverage:",e1); 118 throw new IOException(e1); 119 } 120 } else { // No 105 ExtendedDialog ex = new ExtendedDialog(Main.parent, tr("Warning"), new String[] {tr("Default image projection"), tr("JOSM's current projection"), tr("Cancel")}); 106 ex.setContent(tr("No projection file (.prj) found.<br>" 107 + "You can choose the default image projection ({0}) or JOSM''s current editor projection ({1}) as original image projection.<br>" 108 + "(It can be changed later from the right click menu of the image layer.)", 109 ImportImagePlugin.pluginProps.getProperty("default_crs_srid"), Main.getProjection().toCode())); 110 ex.showDialog(); 111 int val = ex.getValue(); 112 if (val == 3) { 121 113 logger.debug("No projection and user declined un-projected use"); 122 114 throw new LayerCreationCancledException(); 115 } 116 CoordinateReferenceSystem src = null; 117 try { 118 if (val == 1) { 119 src = PluginOperations.defaultSourceCRS; 120 } else { 121 logger.debug("Passing through image un-projected."); 122 src = CRS.decode(Main.getProjection().toCode()); 123 } 124 // create a grid coverage from the image 125 coverage = PluginOperations.createGridFromFile(imageFile, src, false); 126 this.sourceRefSys = coverage.getCoordinateReferenceSystem(); 127 if (val == 1) { 128 coverage = PluginOperations.reprojectCoverage(coverage, CRS.decode(Main.getProjection().toCode())); 129 } 130 } catch (Exception e1) { 131 logger.error("Error while creating GridCoverage:",e1); 132 throw new IOException(e1); 123 133 } 124 134 } … … 171 181 // Rotate image by angle 172 182 g.rotate(angle * Math.PI / 180.0); 173 183 174 184 // Determine scale to fit JOSM extents 175 185 ProjectionBounds projbounds = Main.map.mapView … … 199 209 // Draw picture 200 210 g.drawImage(image, 0, 0, null); 201 211 202 212 } else { 203 213 logger.error("Error while dawing image: image == null or Graphics == null"); 204 214 } 205 215 } 206 216 207 217 public Envelope2D getBbox() { 208 218 return bbox; … … 236 246 @Override 237 247 public boolean isMergable(Layer arg0) { 238 // TODO Auto-generated method stub239 248 return false; 240 249 } … … 242 251 @Override 243 252 public void mergeFrom(Layer arg0) { 244 // TODO Auto-generated method stub 245 246 } 247 248 @Override 249 public void visitBoundingBox(BoundingXYVisitor arg0) { 250 // TODO Auto-generated method stub 251 } 252 253 throw new UnsupportedOperationException(); 254 } 255 256 @Override 257 public void visitBoundingBox(BoundingXYVisitor visitor) { 258 EastNorth min = new EastNorth(getBbox().getMinX(), getBbox().getMinY()); 259 EastNorth max = new EastNorth(getBbox().getMaxX(), getBbox().getMaxY()); 260 visitor.visit(min); 261 visitor.visit(max); 262 } 253 263 254 264 @Override … … 257 267 return this.getName(); 258 268 } 259 269 260 270 261 271 public File getImageFile() { … … 266 276 return image; 267 277 } 268 278 269 279 /** 270 280 * loads the image and reprojects it using a transformation 271 281 * calculated by the new reference system. 272 * 282 * 273 283 * @param newRefSys 274 * @throws IOException 275 * @throws FactoryException 276 * @throws NoSuchAuthorityCodeException 284 * @throws IOException 285 * @throws FactoryException 286 * @throws NoSuchAuthorityCodeException 277 287 */ 278 288 void resample(CoordinateReferenceSystem refSys) throws IOException, NoSuchAuthorityCodeException, FactoryException … … 280 290 logger.debug("resample"); 281 291 GridCoverage2D coverage = PluginOperations.createGridFromFile(this.imageFile, refSys, true); 282 coverage = PluginOperations.reprojectCoverage(coverage, CRS.decode(Main. proj.toCode()));292 coverage = PluginOperations.reprojectCoverage(coverage, CRS.decode(Main.getProjection().toCode())); 283 293 this.bbox = coverage.getEnvelope2D(); 284 294 this.image = ((PlanarImage)coverage.getRenderedImage()).getAsBufferedImage(); 285 286 // TODO 287 upperLeft = new EastNorth(coverage.getEnvelope2D().y, coverage 288 .getEnvelope2D().x 289 + coverage.getEnvelope2D().width); 295 296 upperLeft = new EastNorth(coverage.getEnvelope2D().x, coverage 297 .getEnvelope2D().y 298 + coverage.getEnvelope2D().height); 290 299 angle = 0; 291 300 292 301 // repaint and zoom to new bbox 293 Main.map.mapView.repaint(); 294 LatLon min = new LatLon(bbox.getMinX(), bbox.getMinY()); 295 LatLon max = new LatLon(bbox.getMaxX(), bbox.getMaxY()); 296 Main.map.mapView.zoomTo(new Bounds(min, max)); 297 298 299 } 300 302 BoundingXYVisitor boundingXYVisitor = new BoundingXYVisitor(); 303 visitBoundingBox(boundingXYVisitor); 304 Main.map.mapView.recalculateCenterScale(boundingXYVisitor); 305 } 306 301 307 /** 302 308 * Action that creates a dialog GUI element with properties of a layer. 303 * 309 * 304 310 */ 305 311 public class LayerPropertiesAction extends AbstractAction 306 312 { 307 313 public ImageLayer imageLayer; 308 314 309 315 public LayerPropertiesAction(ImageLayer imageLayer){ 310 316 super(tr("Layer Properties")); … … 313 319 314 320 public void actionPerformed(ActionEvent arg0) { 315 321 316 322 LayerPropertiesDialog layerProps = new LayerPropertiesDialog(imageLayer, PluginOperations.crsDescriptions); 317 323 layerProps.setLocation(Main.parent.getWidth() / 4 , Main.parent.getHeight() / 4); 318 324 layerProps.setVisible(true); 319 325 } 320 321 } 322 326 327 } 328 323 329 /** 324 330 * Exception which represents that the layer creation has been cancled by the … … 328 334 class LayerCreationCancledException extends IOException{ 329 335 } 330 336 331 337 public CoordinateReferenceSystem getSourceRefSys() { 332 338 return sourceRefSys; -
applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin/ImportImagePlugin.java
r23305 r26452 25 25 * Plugin class. 26 26 * Provides basic routines for plugin installation and provides the plugin properties. 27 * 28 * 27 * 28 * 29 29 * @author Christoph Beekmans, Fabian Kowitz, Anna Robaszkiewicz, Oliver Kuhn, Martin Ulitzny 30 30 * 31 31 */ 32 32 public class ImportImagePlugin extends Plugin{ 33 33 34 34 private static Logger logger; 35 35 36 36 JMenu mainmenu = null; 37 37 JosmAction loadFileAction = null; 38 38 39 39 // custom Classloader 40 40 static ClassLoader pluginClassLoader; 41 41 42 42 // plugin proerties 43 43 static Properties pluginProps; 44 44 45 45 // path constants 46 46 static final String PLUGIN_DIR = Main.pref.getPluginsDirectory().getAbsolutePath() + "/ImportImagePlugin/"; 47 static final String PLUGINPROPERTIES_PATH = Main.pref.getPluginsDirectory().getAbsolutePath() + "/ImportImagePlugin/pluginProperties.properties"; 48 static final String PLUGINLIBRARIES_DIR = Main.pref.getPluginsDirectory().getAbsolutePath() + "/ImportImagePlugin/lib/"; 47 static final String PLUGINPROPERTIES_PATH = Main.pref.getPluginsDirectory().getAbsolutePath() + "/ImportImagePlugin/pluginProperties.properties"; 48 static final String PLUGINLIBRARIES_DIR = Main.pref.getPluginsDirectory().getAbsolutePath() + "/ImportImagePlugin/lib/"; 49 49 static final String PLUGINPROPERTIES_FILENAME = "pluginProperties.properties"; 50 50 static final String LOGGING_PROPERTIES_FILEPATH = Main.pref.getPluginsDirectory().getAbsolutePath() + "/ImportImagePlugin/log4j.properties/"; 51 52 51 52 53 53 public Properties getPluginProps() { 54 54 return pluginProps; … … 58 58 /** 59 59 * constructor 60 * 60 * 61 61 * @param info 62 62 */ 63 63 public ImportImagePlugin(PluginInformation info){ 64 64 super(info); 65 65 66 // switch to x=lon and y=lat for EPSG:4326 as JOSM does 67 // (formally incorrect, but reasonable) 68 System.setProperty("org.geotools.referencing.forceXY", "true"); 69 66 70 try { 67 71 68 72 // First create custom ClassLoader to load resources from the main JAR 69 73 pluginClassLoader = createPluginClassLoader(); 70 74 71 75 // Initialize logger 72 76 initializeLogger(pluginClassLoader); 73 77 74 78 // Check whether plugin has already been installed. Otherwise install 75 79 checkInstallation(); 76 80 77 81 // If resources are available load properties from plugin directory 78 82 if(pluginProps == null || pluginProps.isEmpty()) … … 97 101 method.invoke(sysLoader, new Object[]{library.toURI().toURL()}); 98 102 } 99 100 103 104 101 105 // load information about supported reference systems 102 106 PluginOperations.loadCRSData(pluginProps); 103 107 104 108 // create new Action for menu entry 105 109 LoadImageAction loadFileAction = new LoadImageAction(); 106 110 loadFileAction.setEnabled(true); 107 111 108 112 // add menu entries 109 113 Main.main.menu.fileMenu.insert(loadFileAction, 8); 110 114 Main.main.menu.fileMenu.insertSeparator(9); 111 115 112 116 113 117 } catch (Exception e) { … … 121 125 122 126 logger.info("Plugin successfully loaded."); 123 124 } 125 126 127 127 128 } 129 128 130 /** 129 131 * Checks whether plugin resources are available. 130 132 * If not, start install procedure. 131 * 133 * 132 134 * @throws IOException 133 135 */ 134 136 private void checkInstallation() throws IOException 135 137 { 136 138 137 139 // check plugin resource state 138 140 boolean isInstalled = true; … … 141 143 || !new File(PLUGINLIBRARIES_DIR).exists()) 142 144 isInstalled = false; 143 144 145 146 145 147 // if properties file doesn't exist, install plugin 146 148 if(!isInstalled) 147 149 { 148 150 149 151 /*----------- Begin installation ---------------*/ 150 152 151 153 // check if plugin directory exist 152 154 File pluginDir = new File(PLUGIN_DIR); … … 154 156 pluginDir.mkdir(); 155 157 } 156 158 157 159 // check if "lib" directory exist 158 160 File libDir = new File(PLUGINLIBRARIES_DIR); … … 160 162 libDir.mkdir(); 161 163 } 162 164 163 165 // create local properties file 164 166 if(pluginProps == null || pluginProps.isEmpty()){ 165 167 166 168 FileWriter fw = new FileWriter(new File(PLUGINPROPERTIES_PATH)); 167 169 URL propertiesURL = pluginClassLoader.getResource("resources/" + PLUGINPROPERTIES_FILENAME); … … 172 174 logger.debug("Plugin properties loaded"); 173 175 } 174 176 175 177 if(!new File(LOGGING_PROPERTIES_FILEPATH).exists()) 176 178 { … … 184 186 } 185 187 186 187 // Copy all needed JAR files to $PLUGIN_DIR$/lib/ 188 189 // Copy all needed JAR files to $PLUGIN_DIR$/lib/ 188 190 String[] libStrings = pluginProps.getProperty("libraries").split(","); 189 191 190 192 for (int i = 0; i < libStrings.length; i++) { 191 193 192 194 URL url = pluginClassLoader.getResource("lib/" + libStrings[i]); 193 195 194 196 FileOutputStream out = null; 195 197 196 198 try{ 197 199 out = new FileOutputStream(new File(libDir, libStrings[i])); … … 199 201 break; 200 202 } 201 203 202 204 BufferedInputStream in = null; 203 205 try … … 227 229 /** 228 230 * Initialize logger using plugin classloader. 229 * 231 * 230 232 * @param cl 231 233 */ … … 235 237 try { 236 238 props.load(new File(LOGGING_PROPERTIES_FILEPATH).toURI().toURL().openStream()); 237 239 238 240 // Set file for logging here: 239 241 props.setProperty("log4j.appender.MyRoFiAppender.file", 240 242 (Main.pref.getPluginsDirectory().getAbsolutePath() + "/ImportImagePlugin/" + "log.log")); 241 243 242 244 PropertyConfigurator.configure(props); 243 245 244 246 logger = Logger.getLogger(ImportImagePlugin.class); 245 247 246 248 logger.info("Logger successfully initialized."); 247 249 248 250 return; 249 251 250 252 } catch (IOException e) { 251 253 System.out.println("Logging properties file not found. Using standard settings."); 252 254 } 253 255 254 256 // if no log4j.properties file can be found, initialize manually: 255 257 256 258 props.setProperty("log4j.rootLogger", "INFO, A"); 257 259 props.setProperty("log4j.appender.A", "org.apache.log4j.FileAppender"); … … 265 267 props.setProperty("log4j.appender.A.file", 266 268 (Main.pref.getPluginsDirectory().getAbsolutePath() + "/ImportImagePlugin/" + "log.log")); 267 269 268 270 PropertyConfigurator.configure(props); 269 271 logger = Logger.getLogger(ImportImagePlugin.class); 270 272 logger.info("Logger successfully initialized with standard settings."); 271 272 } 273 273 274 } 275 274 276 /** 275 277 * get a plugin-specific classloader. 276 * 278 * 277 279 * @return 278 * @throws MalformedURLException 279 */ 280 private ClassLoader createPluginClassLoader() throws MalformedURLException 280 * @throws MalformedURLException 281 */ 282 private ClassLoader createPluginClassLoader() throws MalformedURLException 281 283 { 282 284 ClassLoader loader = null; … … 285 287 ImportImagePlugin.class.getClassLoader() 286 288 ); 287 289 288 290 return loader; 289 291 } 290 292 291 293 } -
applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin/LayerPropertiesDialog.java
r23305 r26452 565 565 eastingFirstCheckBox = new JCheckBox(); 566 566 eastingFirstCheckBox.setBounds(new Rectangle(345, 255, 21, 21)); 567 eastingFirstCheckBox.setSelected(true); 567 568 } 568 569 return eastingFirstCheckBox; -
applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin/LoadImageAction.java
r26413 r26452 1 1 package org.openstreetmap.josm.plugins.ImportImagePlugin; 2 2 import java.awt.event.ActionEvent; 3 import java.io.IOException;4 3 5 4 import javax.swing.JFileChooser; 6 import javax.swing.JFrame;7 5 import javax.swing.JOptionPane; 8 6 … … 13 11 import org.openstreetmap.josm.Main; 14 12 import org.openstreetmap.josm.actions.JosmAction; 15 import org.openstreetmap.josm.data.Bounds;16 import org.openstreetmap.josm.data.coor.EastNorth;17 13 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 18 14 import org.openstreetmap.josm.plugins.ImportImagePlugin.ImageLayer.LayerCreationCancledException; … … 21 17 /** 22 18 * Class extends JosmAction and creates a new image layer. 23 * 19 * 24 20 * @author Christoph Beekmans, Fabian Kowitz, Anna Robaszkiewicz, Oliver Kuhn, Martin Ulitzny 25 21 * 26 22 */ 27 23 public class LoadImageAction extends JosmAction { 28 24 29 25 private Logger logger = Logger.getLogger(LoadImageAction.class); 30 26 … … 39 35 40 36 // Choose a file 41 JFileChooser fc = new JFileChooser( );37 JFileChooser fc = new JFileChooser(Main.pref.get("plugins.importimage.importpath", null)); 42 38 fc.setAcceptAllFileFilterUsed(false); 43 39 int result = fc.showOpenDialog(Main.parent); 44 40 45 41 ImageLayer layer = null; 46 42 if (result == JFileChooser.APPROVE_OPTION) { 43 Main.pref.put("plugins.importimage.importpath", fc.getCurrentDirectory().getAbsolutePath()); 47 44 logger.info("File choosen:" + fc.getSelectedFile()); 48 45 try { … … 55 52 JOptionPane.showMessageDialog(null, marktr("Error while creating image layer: " + e.getCause())); 56 53 return; 57 54 58 55 } 59 56 60 57 // Add layer: 61 58 Main.main.addLayer(layer); 62 EastNorth min = new EastNorth(layer.getBbox().getMinX(), layer.getBbox().getMinY());63 EastNorth max = new EastNorth(layer.getBbox().getMaxX(), layer.getBbox().getMaxY());64 59 BoundingXYVisitor boundingXYVisitor = new BoundingXYVisitor(); 65 boundingXYVisitor.visit(min); 66 boundingXYVisitor.visit(max); 60 layer.visitBoundingBox(boundingXYVisitor); 67 61 Main.map.mapView.recalculateCenterScale(boundingXYVisitor); 68 62 } -
applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin/PluginOperations.java
r26413 r26452 41 41 /** 42 42 * Class provides methods for resampling operations, IO and stores important data. 43 * 43 * 44 44 * @author Christoph Beekmans, Fabian Kowitz, Anna Robaszkiewicz, Oliver Kuhn, Martin Ulitzny 45 45 * … … 48 48 49 49 private static final Logger logger = Logger.getLogger(PluginOperations.class); 50 50 51 51 // contains descriptions of all available CRS 52 52 static Vector<String> crsDescriptions; 53 53 54 // the standard native CRS of user images 54 // the standard native CRS of user images 55 55 static CoordinateReferenceSystem defaultSourceCRS; 56 56 // description of 'defaultSourceCRS' 57 57 static String defaultSourceCRSDescription; 58 59 60 58 59 60 61 61 public static enum SUPPORTEDIMAGETYPES { 62 tiff, tif, jpg, jpeg, bmp, png 62 tiff, tif, jpg, jpeg, bmp, png 63 63 } 64 64 65 65 public static enum POSTFIXES_WORLDFILE { 66 wld, jgw, jpgw, pgw, pngw, tfw, tifw, bpw, bmpw, 66 wld, jgw, jpgw, pgw, pngw, tfw, tifw, bpw, bmpw, 67 67 }; 68 68 69 69 /** 70 70 * Reprojects a GridCoverage to a given CRS. 71 * 71 * 72 72 * @param coverage 73 73 * @param targetCrs 74 74 * @return destination 75 * @throws FactoryException 76 * @throws NoSuchAuthorityCodeException 75 * @throws FactoryException 76 * @throws NoSuchAuthorityCodeException 77 77 */ 78 78 public static GridCoverage2D reprojectCoverage(GridCoverage2D coverage, … … 81 81 // TODO: add category for NO_DATA values in coverage (transparency in 82 82 // image) 83 83 84 84 GridCoverage2D destination = null; 85 85 … … 100 100 101 101 /** 102 * Creates a org.geotools.coverage.grid.GridCoverage2D from a given file. 103 * 102 * Creates a org.geotools.coverage.grid.GridCoverage2D from a given file. 103 * 104 104 * @param file 105 105 * @return 106 * @throws IOException 106 * @throws IOException 107 107 * @throws Exception 108 108 */ … … 110 110 111 111 GridCoverage2D coverage = null; 112 112 113 113 if (!file.exists()) throw new FileNotFoundException("File not found."); 114 114 … … 123 123 || extension.equalsIgnoreCase(".tiff")) 124 124 { 125 125 126 126 // try to read GeoTIFF: 127 127 try { … … 136 136 throw new IOException(facte); 137 137 } 138 139 // file is no GeoTiff, searching for Worldfile and projection file: 138 139 // file is no GeoTiff, searching for Worldfile and projection file: 140 140 String[] postfixes = {"wld", "tfw", "tifw"}; 141 141 // try to read Worldfile: … … 151 151 throw new IOException("No Worldfile found."); 152 152 } 153 153 154 154 if (refSys == null) { 155 155 // if no crs is delivered try to read projection file: … … 160 160 } 161 161 } 162 162 163 163 BufferedImage img = ImageIO.read(file); 164 164 165 165 // create Envelope 166 166 double width = (double) (img.getWidth() * tfwReader.getXPixelSize()); … … 169 169 double lowerLeft_y = (double) tfwReader.getYULC() - height; 170 170 Envelope2D bbox = new Envelope2D(null, new Rectangle2D.Double(lowerLeft_x, lowerLeft_y, width, height)); 171 171 172 172 coverage = createGridCoverage(img, bbox, refSys); 173 174 } 175 // 173 } 174 // 176 175 else if (extension.equalsIgnoreCase(".jpg") 177 176 || extension.equalsIgnoreCase(".jpeg")) … … 187 186 } 188 187 if (tfwReader == null) throw new IOException("No Worldfile found."); 189 188 190 189 if (refSys == null) { 191 190 // if no crs is delivered try to read projection file: … … 196 195 } 197 196 } 198 197 199 198 BufferedImage img = ImageIO.read(file); 200 199 201 200 // create Envelope 202 201 double width = (double) (img.getWidth() * tfwReader.getXPixelSize()); … … 205 204 double lowerLeft_y = (double) tfwReader.getYULC() - height; 206 205 Envelope2D bbox = new Envelope2D(null, new Rectangle2D.Double(lowerLeft_x, lowerLeft_y, width, height)); 207 206 208 207 coverage = createGridCoverage(img, bbox, refSys); 209 210 208 } 211 209 else if(extension.equalsIgnoreCase(".bmp")) … … 230 228 } 231 229 } 232 230 233 231 BufferedImage img = ImageIO.read(file); 234 232 235 233 // create Envelope 236 234 double width = (double) (img.getWidth() * tfwReader.getXPixelSize()); … … 239 237 double lowerLeft_y = (double) tfwReader.getYULC() - height; 240 238 Envelope2D bbox = new Envelope2D(null, new Rectangle2D.Double(lowerLeft_x, lowerLeft_y, width, height)); 241 239 242 240 coverage = createGridCoverage(img, bbox, refSys); 243 241 } 244 242 else if(extension.equalsIgnoreCase(".png")) 245 243 { 246 244 247 245 String[] postfixes = {"wld", "pgw", "pngw"}; 248 246 // try to read Worldfile: … … 255 253 } 256 254 if(tfwReader == null) throw new IOException("No Worldfile found."); 257 255 258 256 if (refSys == null) { 259 257 // if no crs is delivered try to read projection file: … … 264 262 } 265 263 } 266 264 267 265 BufferedImage img = ImageIO.read(file); 268 266 269 267 // create Envelope 270 268 double width = (double) (img.getWidth() * tfwReader.getXPixelSize()); … … 273 271 double lowerLeft_y = (double) tfwReader.getYULC() - height; 274 272 Envelope2D bbox = new Envelope2D(null, new Rectangle2D.Double(lowerLeft_x, lowerLeft_y, width, height)); 275 273 276 274 coverage = createGridCoverage(img, bbox, refSys); 277 275 } … … 283 281 return coverage; 284 282 } 285 286 /** 287 * Searches for a projection file (.prj) with the same name of 'file' 283 284 /** 285 * Searches for a projection file (.prj) with the same name of 'file' 288 286 * tries to parse it. 289 * 290 * 287 * 288 * 291 289 * @param file image file, not the real world file (will be searched) 292 * @return 293 * @throws IOException 290 * @return 291 * @throws IOException 294 292 */ 295 293 public static CoordinateReferenceSystem readPrjFile(File file) throws IOException 296 294 { 297 295 298 296 CoordinateReferenceSystem refSys = null; 299 297 300 298 String prjFilename = null; 301 299 int dotPos = file.getAbsolutePath().lastIndexOf("."); 302 300 prjFilename = file.getAbsolutePath().substring(0, dotPos) + ".prj"; 303 301 304 302 File prjFile = new File(prjFilename); 305 303 if (!prjFile.exists()) return null; 306 304 logger.debug("Loading .prj file: " + prjFile.getAbsolutePath()); 307 305 308 306 StringBuilder sb = new StringBuilder(); 309 307 String content = null; … … 313 311 sb.append(content); 314 312 } 315 313 316 314 try { 317 315 refSys = CRS.parseWKT(sb.toString().trim()); … … 319 317 throw new IOException("Unable to parse prj-file: '" + prjFile.getName() + "'"); 320 318 } 321 319 322 320 return refSys; 323 324 } 325 326 321 322 } 323 324 327 325 /** 328 326 * Method for external use. 329 * 327 * 330 328 * @param img 331 329 * @param bbox … … 338 336 return new GridCoverageFactory().create("", img, bbox); 339 337 } 340 338 341 339 /** 342 340 * Method for reading a GeoTIFF file. 343 * 344 * @param file 341 * 342 * @param file 345 343 * @param refSys if delivered, the coverage will be forced to use this crs 346 344 * @return … … 359 357 // dont't use the EPSG-Factory because of wrong behaviour 360 358 hints.put(Hints.CRS_AUTHORITY_FACTORY, CRS.getAuthorityFactory(true)); 361 359 362 360 GeoTiffReader reader = new GeoTiffReader(file, hints); 363 361 364 362 coverage = (GridCoverage2D) reader.read(null); 365 363 366 364 return coverage; 367 365 } 368 369 370 /** 371 * Loads CRS data from an EPSG database and creates descrptions for each one. 372 * 366 367 368 /** 369 * Loads CRS data from an EPSG database and creates descrptions for each one. 370 * 373 371 * @param pluginProps 374 372 * @throws Exception … … 377 375 { 378 376 String defaultcrsString = pluginProps.getProperty("default_crs_srid"); 379 377 380 378 crsDescriptions = new Vector<String>(); 381 379 Set<String> supportedCodes = CRS.getSupportedCodes("EPSG"); 382 380 CRSAuthorityFactory fac = CRS.getAuthorityFactory(false); 383 381 384 382 for (Iterator iterator = supportedCodes.iterator(); iterator.hasNext();) { 385 383 String string = (String) iterator.next(); 386 384 try { 385 if ("WGS84(DD)".equals(string)) { 386 continue; 387 } 387 388 InternationalString desc = fac.getDescriptionText("EPSG:" + string); 388 389 389 390 String description = desc.toString() + " [-EPSG:" + string + "-]"; 390 391 391 392 crsDescriptions.add(description); 392 393 393 394 if(defaultcrsString != null && defaultcrsString.equalsIgnoreCase("EPSG:" + string)){ 394 395 boolean isEastingFirst = Boolean.valueOf(pluginProps.getProperty("default_crs_eastingfirst")); … … 397 398 } 398 399 } catch (NoSuchAuthorityCodeException e) { 399 if(!string.equalsIgnoreCase("WGS84(DD)")){ 400 logger.error("Error while loading EPSG data: " + e.getMessage()); 401 } 400 logger.error("Error while loading EPSG data: " + e.getMessage()); 402 401 } catch (FactoryException e) { 403 402 logger.error("Error while loading EPSG data: " + e.getMessage()); … … 405 404 } 406 405 } 407 406 408 407 }
Note:
See TracChangeset
for help on using the changeset viewer.