Search in sources :

Example 1 with ShapeFileWrapper

use of it.geosolutions.imageio.plugins.nitronitf.wrapper.ShapeFileWrapper in project imageio-ext by geosolutions-it.

the class NITFImageWriter method write.

@Override
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
    // Headers and segments initialization
    NITFImageWriteParam nitfParam = null;
    HeaderWrapper header = null;
    Map<String, Map<String, String>> extensionsMap = null;
    ShapeFileWrapper shape = null;
    List<TextWrapper> texts = null;
    WriteCompression compression = null;
    List<ImageWrapper> inputImages = null;
    if (param != null && param instanceof NITFImageWriteParam) {
        nitfParam = (NITFImageWriteParam) param;
        NITFProperties nitfMetadata = nitfParam.getNitfProperties();
        if (nitfMetadata != null) {
            header = nitfMetadata.getHeader();
            shape = nitfMetadata.getShape();
            texts = nitfMetadata.getTextsWrapper();
            inputImages = nitfMetadata.getImagesWrapper();
        }
        compression = nitfParam.getWriteCompression();
    }
    ImageWrapper imageW = inputImages.get(0);
    RenderedImage ri = imageW.getImage();
    final boolean isJP2 = (compression != null && compression != WriteCompression.UNCOMPRESSED);
    FileImageInputStreamExt jp2Stream = null;
    File tempFile = null;
    try {
        Record record = new Record(Version.NITF_21);
        if (isJP2) {
            // Proceeding with jp2 compression
            if (JP2_TEMP_FOLDER != null) {
                tempFile = File.createTempFile("jp2compressed", ".jpc", new File(JP2_TEMP_FOLDER));
            }
            String parentPath = outputFile.getParent();
            String name = FilenameUtils.getBaseName(outputFile.getCanonicalPath());
            tempFile = new File(parentPath + File.separatorChar + name + ".j2c");
            prepareJP2Image(ri, tempFile, compression);
            jp2Stream = new FileImageInputStreamExtImpl(tempFile);
        }
        // populating the file header
        initFileHeader(record, header);
        // adding an image segment to the record
        addImageSegment(record, inputImages, jp2Stream, compression);
        if (texts != null && !texts.isEmpty()) {
            // adding a text segment if present
            addTextSegment(record, texts);
        }
        if (!writeNITF(record, inputImages, shape, jp2Stream, texts)) {
            throw new IOException("Unable to successfully write");
        }
    } catch (Throwable t) {
        IOException ioe = new IOException();
        ioe.initCause(t);
        throw ioe;
    } finally {
        // Releasing resources
        if (jp2Stream != null) {
            try {
                jp2Stream.close();
            } catch (Throwable thr) {
            // Eat exception
            }
        }
        if (tempFile != null) {
            try {
                tempFile.delete();
            } catch (Throwable thr) {
            }
        }
    }
    // record.destruct();
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(Level.FINE, "Successfully wrote NITF: " + outputFile);
    }
}
Also used : NITFProperties(it.geosolutions.imageio.plugins.nitronitf.wrapper.NITFProperties) ShapeFileWrapper(it.geosolutions.imageio.plugins.nitronitf.wrapper.ShapeFileWrapper) IOException(java.io.IOException) TextWrapper(it.geosolutions.imageio.plugins.nitronitf.wrapper.TextWrapper) ImageWrapper(it.geosolutions.imageio.plugins.nitronitf.wrapper.ImageWrapper) FileImageInputStreamExt(it.geosolutions.imageio.stream.input.FileImageInputStreamExt) HeaderWrapper(it.geosolutions.imageio.plugins.nitronitf.wrapper.HeaderWrapper) Record(nitf.Record) FileImageInputStreamExtImpl(it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl) RenderedImage(java.awt.image.RenderedImage) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) File(java.io.File) WriteCompression(it.geosolutions.imageio.plugins.nitronitf.NITFUtilities.WriteCompression)

Aggregations

WriteCompression (it.geosolutions.imageio.plugins.nitronitf.NITFUtilities.WriteCompression)1 HeaderWrapper (it.geosolutions.imageio.plugins.nitronitf.wrapper.HeaderWrapper)1 ImageWrapper (it.geosolutions.imageio.plugins.nitronitf.wrapper.ImageWrapper)1 NITFProperties (it.geosolutions.imageio.plugins.nitronitf.wrapper.NITFProperties)1 ShapeFileWrapper (it.geosolutions.imageio.plugins.nitronitf.wrapper.ShapeFileWrapper)1 TextWrapper (it.geosolutions.imageio.plugins.nitronitf.wrapper.TextWrapper)1 FileImageInputStreamExt (it.geosolutions.imageio.stream.input.FileImageInputStreamExt)1 FileImageInputStreamExtImpl (it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl)1 RenderedImage (java.awt.image.RenderedImage)1 File (java.io.File)1 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Record (nitf.Record)1