Changeset 34219 in osm


Ignore:
Timestamp:
2018-05-27T02:55:44+02:00 (7 years ago)
Author:
donvip
Message:

fix #josm12108 - Cannot display GeoTiff image with elevation from ASTER

Location:
applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin/ImageLayer.java

    r33562 r34219  
    66import java.awt.Graphics2D;
    77import java.awt.GraphicsEnvironment;
    8 import java.awt.Image;
    98import java.awt.event.ActionEvent;
    109import java.awt.image.BufferedImage;
     10import java.awt.image.RenderedImage;
    1111import java.io.File;
    1212import java.io.IOException;
     
    3939import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
    4040import org.openstreetmap.josm.gui.layer.Layer;
     41import org.openstreetmap.josm.tools.Logging;
    4142
    4243/**
     
    7576
    7677        this.imageFile = file;
    77         this.image = (BufferedImage) createImage();
    78         URL iconURL = ImportImagePlugin.pluginClassLoader.getResource("images/layericon.png");
     78        this.image = createImage();
     79        URL iconURL = getClass().getResource("images/layericon.png");
    7980        if (iconURL != null) {
    8081            layericon = new ImageIcon(iconURL);
     
    8586     * create spatial referenced image.
    8687     */
    87     private Image createImage() throws IOException {
     88    private BufferedImage createImage() throws IOException {
    8889
    8990        // geotools type for images and value coverages
     
    144145        logger.debug("Coverage created: " + coverage);
    145146
    146         // TODO
    147147        upperLeft = new EastNorth(coverage.getEnvelope2D().x,
    148148                coverage.getEnvelope2D().y + coverage.getEnvelope2D().height);
     
    150150        bbox = coverage.getEnvelope2D();
    151151
    152         // Refresh
    153         // Main.map.mapView.repaint();
    154 //      PlanarImage image = (PlanarImage) coverage.getRenderedImage();
    155 //      logger.info("Color Model: " + coverage.getRenderedImage().getColorModel());
    156         ImageWorker worker = new ImageWorker(coverage.getRenderedImage());
    157 
    158         return worker.getBufferedImage();
     152        RenderedImage img = coverage.getRenderedImage();
     153        try {
     154            BufferedImage bi = new ImageWorker(img).getBufferedImage();
     155            BufferedImage dst = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_ARGB);
     156            Graphics2D g2d = dst.createGraphics();
     157            try {
     158                // Check image can be drawn correctly
     159                g2d.drawImage(bi, 0, 0, null);
     160            } finally {
     161                g2d.dispose();
     162            }
     163            return bi;
     164        } catch (ArrayIndexOutOfBoundsException e) {
     165            Logging.debug(e);
     166            // See #12108 - rescale to bytes in case of ComponentColorModel index error
     167            return new ImageWorker(img).rescaleToBytes().getBufferedImage();
     168        }
    159169    }
    160170
  • applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin/ImportImageFileImporter.java

    r33562 r34219  
    77import java.io.IOException;
    88import java.util.List;
     9
    910import javax.swing.JOptionPane;
     11
    1012import org.apache.log4j.Logger;
    1113import org.openstreetmap.josm.Main;
    1214import org.openstreetmap.josm.actions.ExtensionFileFilter;
    13 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    1415import org.openstreetmap.josm.gui.MainApplication;
    1516import org.openstreetmap.josm.gui.io.importexport.FileImporter;
    1617import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     18import org.openstreetmap.josm.gui.util.GuiHelper;
    1719import org.openstreetmap.josm.io.IllegalDataException;
    1820import org.openstreetmap.josm.plugins.ImportImagePlugin.ImageLayer.LayerCreationCanceledException;
     21import org.openstreetmap.josm.tools.Logging;
    1922
    2023/**
     
    4245    @Override
    4346    public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    44         if (null == files || files.isEmpty()) return;
     47        if (null == files) return;
    4548
    4649        for (File file: files) {
     
    5457                continue;
    5558            } catch (Exception e) {
    56                logger.error("Error while creating image layer: \n" + e.getMessage());
    57                 JOptionPane.showMessageDialog(null, tr("Error while creating image layer: {0}", e.getCause()));
     59                Logging.error(e);
     60                logger.error("Error while creating image layer: \n" + e.getMessage());
     61                GuiHelper.runInEDT(() ->
     62                    JOptionPane.showMessageDialog(Main.parent, tr("Error while creating image layer: {0}", e.getCause())));
    5863                continue;
    5964            }
    6065
    61             // Add layer:
    6266            MainApplication.getLayerManager().addLayer(layer);
    63             BoundingXYVisitor boundingXYVisitor = new BoundingXYVisitor();
    64             layer.visitBoundingBox(boundingXYVisitor);
    65             MainApplication.getMap().mapView.zoomTo(boundingXYVisitor);
    6667        }
    6768    }
  • applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin/ImportImagePlugin.java

    r33563 r34219  
    55import java.io.FileWriter;
    66import java.io.IOException;
    7 import java.net.MalformedURLException;
    87import java.net.URL;
    9 import java.net.URLClassLoader;
    108import java.util.Properties;
    119
     
    2119import org.openstreetmap.josm.plugins.Plugin;
    2220import org.openstreetmap.josm.plugins.PluginInformation;
    23 import org.openstreetmap.josm.tools.JosmRuntimeException;
    2421import org.openstreetmap.josm.tools.Utils;
    2522
     
    3936    JosmAction loadFileAction = null;
    4037
    41     // custom Classloader to load resources from the main JAR
    42     static ClassLoader pluginClassLoader = createPluginClassLoader();
    43 
    44     // plugin proerties
     38    // plugin properties
    4539    static Properties pluginProps;
    4640
     
    6660        try {
    6761            // Initialize logger
    68             initializeLogger(pluginClassLoader);
     62            initializeLogger();
    6963
    7064            // Check whether plugin has already been installed. Otherwise install
     
    9488            throw e;
    9589        }
    96 
    97         logger.info("Plugin successfully loaded.");
    9890    }
    9991
     
    131123            if (pluginProps == null || pluginProps.isEmpty()) {
    132124                try (FileWriter fw = new FileWriter(new File(PLUGINPROPERTIES_PATH))) {
    133                     URL propertiesURL = pluginClassLoader.getResource("resources/" + PLUGINPROPERTIES_FILENAME);
     125                    URL propertiesURL = getClass().getResource("resources/" + PLUGINPROPERTIES_FILENAME);
    134126                    pluginProps = new Properties();
    135127                    pluginProps.load(propertiesURL.openStream());
     
    141133            if (!new File(LOGGING_PROPERTIES_FILEPATH).exists()) {
    142134                try (FileWriter fw = new FileWriter(new File(LOGGING_PROPERTIES_FILEPATH))) {
    143                     URL propertiesURL = pluginClassLoader.getResource("resources/log4j.properties");
     135                    URL propertiesURL = getClass().getResource("resources/log4j.properties");
    144136                    Properties loggingProps = new Properties();
    145137                    loggingProps.load(propertiesURL.openStream());
     
    154146
    155147    /**
    156      * Initialize logger using plugin classloader.
     148     * Initialize logger.
    157149     */
    158     private void initializeLogger(ClassLoader cl) {
     150    private void initializeLogger() {
    159151
    160152        Properties props = new Properties();
     
    194186        PropertyConfigurator.configure(props);
    195187        logger = Logger.getLogger(ImportImagePlugin.class);
    196         logger.info("Logger successfully initialized with standard settings.");
    197 
    198     }
    199 
    200     /**
    201      * get a plugin-specific classloader.
    202      */
    203     private static ClassLoader createPluginClassLoader() {
    204         try {
    205             return URLClassLoader.newInstance(
    206                     new URL[] {new File(Main.pref.getPluginsDirectory().getAbsolutePath() + "/ImportImagePlugin.jar").toURI().toURL()},
    207                     ImportImagePlugin.class.getClassLoader()
    208                     );
    209         } catch (MalformedURLException e) {
    210             throw new JosmRuntimeException(e);
    211         }
    212188    }
    213189}
  • applications/editors/josm/plugins/ImportImagePlugin/src/org/openstreetmap/josm/plugins/ImportImagePlugin/LayerPropertiesDialog.java

    r33563 r34219  
    3333import org.opengis.referencing.NoSuchAuthorityCodeException;
    3434import org.opengis.referencing.crs.CoordinateReferenceSystem;
    35 import org.openstreetmap.josm.Main;
    3635import org.openstreetmap.josm.tools.Logging;
    3736
Note: See TracChangeset for help on using the changeset viewer.