Search in sources :

Example 6 with TrpPage

use of eu.transkribus.core.model.beans.TrpPage in project TranskribusCore by Transkribus.

the class DocExporter method exportDoc.

/**
 * Export current document with the provided parameters.
 * @param doc current document
 * @param pars export settings
 * @return directory to which the export files were written
 * @throws IOException
 * @throws IllegalArgumentException
 * @throws URISyntaxException
 * @throws JAXBException
 * @throws TransformerException
 */
public File exportDoc(TrpDoc doc, CommonExportPars pars) throws IOException, IllegalArgumentException, URISyntaxException, JAXBException, TransformerException {
    FimgStoreGetClient getter = null;
    FimgStoreUriBuilder uriBuilder = null;
    ImgType imgType = pars.getRemoteImgQuality() == null ? ImgType.orig : pars.getRemoteImgQuality();
    if (doc.isRemoteDoc()) {
        // FIXME fimagestore path should be read from docMd!
        getter = new FimgStoreGetClient("dbis-thure.uibk.ac.at", "f");
        final String scheme = pars.isUseHttps() ? "https" : "http";
        final int port = pars.isUseHttps() ? 443 : 80;
        uriBuilder = new FimgStoreUriBuilder(scheme, getter.getHost(), port, getter.getServerContext());
    }
    // create copy of object, as we alter it here while exporting
    TrpDoc doc2;
    doc2 = new TrpDoc(doc);
    // check and create output directory
    File outputDir = new File(pars.getDir());
    if (!pars.isDoOverwrite() && outputDir.exists()) {
        throw new IOException("File path already exists.");
    }
    outputDir.mkdir();
    // decide where to put the images
    final File imgOutputDir;
    if (pars.isUseOcrMasterDir()) {
        imgOutputDir = new File(outputDir.getAbsolutePath() + File.separatorChar + LocalDocConst.OCR_MASTER_DIR);
        imgOutputDir.mkdir();
    } else {
        imgOutputDir = outputDir;
    }
    File pageOutputDir = null, altoOutputDir = null;
    // check PAGE export settings and create output directory
    String pageDirName = pars.getPageDirName();
    if (pars.isDoExportPageXml() && !StringUtils.isEmpty(pageDirName)) {
        pageOutputDir = new File(outputDir.getAbsolutePath() + File.separatorChar + pageDirName);
        if (pageOutputDir.mkdir()) {
            logger.debug("pageOutputDir created successfully ");
        } else {
            logger.debug("pageOutputDir could not be created!");
        }
    } else {
        // if pageDirName is not set, export the PAGE XMLs to imgOutputDir
        pageOutputDir = imgOutputDir;
    }
    // check Alto export settings and create output directory
    AltoExporter altoEx = new AltoExporter();
    if (pars.isDoExportAltoXml()) {
        altoOutputDir = altoEx.createAltoOuputDir(doc2, outputDir.getAbsolutePath());
    }
    // check and write metadata
    if (doc2.getMd() != null) {
        File fileOut = new File(outputDir.getAbsolutePath() + File.separatorChar + LocalDocConst.METADATA_FILENAME);
        try {
            JaxbUtils.marshalToFile(doc2.getMd(), fileOut);
        } catch (JAXBException e) {
            throw new IOException("Could not marshal metadata to file.", e);
        }
    }
    List<TrpPage> pages = doc2.getPages();
    Set<Integer> pageIndices = pars.getPageIndices(doc.getNPages());
    // do export for all defined pages
    for (int i = 0; i < pages.size(); ++i) {
        if (pageIndices != null && !pageIndices.contains(i)) {
            continue;
        }
        TrpPage p = pages.get(i);
        File imgFile = null, xmlFile = null, altoFile = null;
        URL imgUrl = p.getUrl();
        final String baseFileName = ExportFilePatternUtils.buildBaseFileName(pars.getFileNamePattern(), p);
        final String imgExt = "." + FilenameUtils.getExtension(p.getImgFileName());
        final String xmlExt = ".xml";
        // gather remote files and export document
        if (doc2.isRemoteDoc()) {
            if (pars.isDoWriteImages()) {
                final String msg = "Downloading " + imgType.toString() + " image for page nr. " + p.getPageNr();
                logger.debug(msg);
                updateStatus(msg);
                final URI imgUri = uriBuilder.getImgUri(p.getKey(), imgType);
                imgFile = getter.saveFile(imgUri, imgOutputDir.getAbsolutePath(), baseFileName + imgExt);
                p.setUrl(imgFile.toURI().toURL());
                p.setKey(null);
            }
            if (pars.isDoExportPageXml()) {
                // old
                // TrpTranscriptMetadata t = p.getCurrentTranscript();
                /*
					 * new: to get the previously stored chosen version
					 */
                TrpTranscriptMetadata transcriptMd;
                JAXBPageTranscript transcript = cache.getPageTranscriptAtIndex(i);
                // set up transcript metadata
                if (transcript == null) {
                    transcriptMd = p.getCurrentTranscript();
                    logger.warn("Have to unmarshall transcript in DocExporter for transcript " + transcriptMd + " - should have been built before using ExportUtils::storePageTranscripts4Export!");
                    transcript = new JAXBPageTranscript(transcriptMd);
                    transcript.build();
                } else {
                    transcriptMd = transcript.getMd();
                }
                URL xmlUrl = transcriptMd.getUrl();
                if (pars.isExportTranscriptMetadata()) {
                    MetadataType md = transcript.getPage().getPcGtsType().getMetadata();
                    if (md == null) {
                        throw new JAXBException("Transcript does not contain a metadata element: " + transcriptMd);
                    }
                    String imgUrlStr = CoreUtils.urlToString(imgUrl);
                    String xmlUrlStr = CoreUtils.urlToString(xmlUrl);
                    String status = transcriptMd.getStatus() == null ? null : transcriptMd.getStatus().toString();
                    TranskribusMetadataType tmd = new TranskribusMetadataType();
                    tmd.setDocId(doc.getId());
                    tmd.setPageId(p.getPageId());
                    tmd.setPageNr(p.getPageNr());
                    tmd.setTsid(transcriptMd.getTsId());
                    tmd.setStatus(status);
                    tmd.setUserId(transcriptMd.getUserId());
                    tmd.setImgUrl(imgUrlStr);
                    tmd.setXmlUrl(xmlUrlStr);
                    tmd.setImageId(p.getImageId());
                    md.setTranskribusMetadata(tmd);
                }
                // write transcript to file
                xmlFile = new File(FilenameUtils.normalizeNoEndSeparator(pageOutputDir.getAbsolutePath()) + File.separator + baseFileName + xmlExt);
                logger.debug("PAGE XMl output file: " + xmlFile.getAbsolutePath());
                transcript.write(xmlFile);
                // old code: save file by just downloading to disk
                // xmlFile = getter.saveFile(transcriptMd.getUrl().toURI(), pageOutputDir.getAbsolutePath(), baseFileName + xmlExt);
                // make sure (for other exports) that the transcript that is exported is the only one set in the transcripts list of TrpPage
                p.getTranscripts().clear();
                TrpTranscriptMetadata tCopy = new TrpTranscriptMetadata(transcriptMd, p);
                tCopy.setUrl(xmlFile.toURI().toURL());
                p.getTranscripts().add(tCopy);
            }
        } else {
            updateStatus("Copying local files for page nr. " + p.getPageNr());
            // copy local files during export
            if (pars.isDoWriteImages()) {
                imgFile = LocalDocWriter.copyImgFile(p, p.getUrl(), imgOutputDir.getAbsolutePath(), baseFileName + imgExt);
            }
            if (pars.isDoExportPageXml()) {
                xmlFile = LocalDocWriter.copyTranscriptFile(p, pageOutputDir.getAbsolutePath(), baseFileName + xmlExt, cache);
            }
        }
        // export alto:
        if (pars.isDoExportAltoXml()) {
            altoFile = altoEx.exportAltoFile(p, baseFileName + xmlExt, altoOutputDir, pars.isSplitIntoWordsInAltoXml());
        }
        if (imgFile != null)
            logger.debug("Written image file " + imgFile.getAbsolutePath());
        if (xmlFile != null) {
            logger.debug("Written transcript xml file " + xmlFile.getAbsolutePath());
        } else {
            logger.warn("No transcript was exported for page ");
        }
        if (altoFile != null) {
            logger.debug("Written ALTO xml file " + altoFile.getAbsolutePath());
        } else {
            logger.warn("No alto was exported for page ");
        }
        setChanged();
        notifyObservers(Integer.valueOf(p.getPageNr()));
    }
    if (pars.isDoWriteMets()) {
        // load the exported doc from its new location
        // FIXME this does not work for export of PAGE XMLs only!
        // final TrpDoc localDoc = LocalDocReader.load(outputDir.getAbsolutePath(), false);
        // set local folder or else TrpMetsBuilder will treat this as remote doc!
        doc2.getMd().setLocalFolder(outputDir);
        // write mets with file pointers to local files
        Mets mets = TrpMetsBuilder.buildMets(doc2, pars.isDoExportPageXml(), pars.isDoExportAltoXml(), pars.isDoWriteImages(), pageIndices);
        File metsFile = new File(outputDir.getAbsolutePath() + File.separator + TrpMetsBuilder.METS_FILE_NAME);
        try {
            JaxbUtils.marshalToFile(mets, metsFile, TrpDocMetadata.class);
        } catch (JAXBException e) {
            throw new IOException("Could not marshal METS to file!", e);
        }
    }
    return outputDir;
}
Also used : JAXBPageTranscript(eu.transkribus.core.model.beans.JAXBPageTranscript) TrpPage(eu.transkribus.core.model.beans.TrpPage) ImgType(org.dea.fimgstoreclient.beans.ImgType) JAXBException(javax.xml.bind.JAXBException) TranskribusMetadataType(eu.transkribus.core.model.beans.pagecontent.TranskribusMetadataType) TranskribusMetadataType(eu.transkribus.core.model.beans.pagecontent.TranskribusMetadataType) MetadataType(eu.transkribus.core.model.beans.pagecontent.MetadataType) TrpTranscriptMetadata(eu.transkribus.core.model.beans.TrpTranscriptMetadata) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) AltoExporter(eu.transkribus.core.model.builder.alto.AltoExporter) FimgStoreGetClient(org.dea.fimgstoreclient.FimgStoreGetClient) Mets(eu.transkribus.core.model.beans.mets.Mets) TrpDoc(eu.transkribus.core.model.beans.TrpDoc) File(java.io.File) FimgStoreUriBuilder(org.dea.fimgstoreclient.utils.FimgStoreUriBuilder)

Example 7 with TrpPage

use of eu.transkribus.core.model.beans.TrpPage in project TranskribusCore by Transkribus.

the class LocalDocWriter method updateImageDimension.

public static void updateImageDimension(JAXBPageTranscript transcript) throws FileNotFoundException, IOException {
    TrpPage page = transcript.getMd().getPagePageReferenceForLocalDocs();
    if (page == null) {
        logger.warn("Could not update transcript image dimensions - page object is null - should not happen for local docs: " + transcript.getMd().getUrl());
        return;
    }
    // update img file name and dimensions in page XML
    Dimension dim = ImgUtils.readImageDimensions(FileUtils.toFile(page.getUrl()));
    final String imgFileName = page.getImgFileName();
    transcript.getPage().setImageFilename(imgFileName);
    transcript.getPage().setImageHeight(dim.height);
    transcript.getPage().setImageWidth(dim.width);
    logger.debug("Updated PAGE XML: ImgFileName = " + imgFileName + ", dim = " + dim.width + "x" + dim.height);
}
Also used : TrpPage(eu.transkribus.core.model.beans.TrpPage) Dimension(java.awt.Dimension)

Example 8 with TrpPage

use of eu.transkribus.core.model.beans.TrpPage in project TranskribusCore by Transkribus.

the class LocalDocWriter method createThumbsForDoc.

// public static String getThumbFileName(TrpPage page) throws IOException {
// File imgFile = FileUtils.toFile(page.getUrl());
// if (imgFile == null)
// throw new IOException("Cannot retrieve image url from: "+page.getUrl());
// 
// File thmbsDir = new File(FilenameUtils.getFullPath(imgFile.getAbsolutePath())+"/"+LocalDocConst.THUMBS_FILE_SUB_FOLDER);
// File outFile = new File(thmbsDir.getAbsolutePath()+"/"+FilenameUtils.getBaseName(imgFile.getName())+".jpg");
// 
// return outFile.getAbsolutePath();
// }
/* @deprecated Does not work on mac */
@Deprecated
public static void createThumbsForDoc(TrpDoc doc, boolean overwrite) throws Exception {
    checkIfLocalDoc(doc);
    File thmbsDir = new File(doc.getMd().getLocalFolder().getAbsolutePath() + File.separator + LocalDocConst.THUMBS_FILE_SUB_FOLDER);
    FileUtils.forceMkdir(thmbsDir);
    int newHeight = LocalDocConst.THUMB_SIZE_HEIGHT;
    for (TrpPage p : doc.getPages()) {
        File imgFile = FileUtils.toFile(p.getUrl());
        if (imgFile == null)
            throw new IOException("Cannot retrieve image url from: " + p.getUrl());
        File thumbsFile = FileUtils.toFile(p.getThumbUrl());
        if (thumbsFile == null)
            throw new IOException("Cannot retrieve thumbs url from: " + p.getThumbUrl());
        if (// skip if already there and overwrite not specified
        thumbsFile.exists() && !overwrite)
            continue;
        logger.debug("creating thumb file: " + thumbsFile);
        long st = System.currentTimeMillis();
        if (true) {
        }
        if (false) {
            BufferedImage originalImage = ImgUtils.readImage(imgFile);
            if (originalImage == null)
                throw new IOException("Cannot load image " + imgFile.getAbsolutePath());
            double sf = (double) newHeight / (double) originalImage.getHeight();
            int newWidth = (int) (sf * originalImage.getWidth());
            BufferedImage thmbImg = new BufferedImage(newWidth, newHeight, originalImage.getType());
            Graphics2D g = thmbImg.createGraphics();
            RenderingHints rh = new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g.setRenderingHints(rh);
            g.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
            g.dispose();
            if (!ImageIO.write(thmbImg, FilenameUtils.getExtension(thumbsFile.getName()), thumbsFile))
                throw new Exception("Could not write thumb file - no appropriate writer found!");
        }
        logger.debug("created thumb file: " + thumbsFile.getAbsolutePath() + " time = " + (System.currentTimeMillis() - st));
    }
}
Also used : TrpPage(eu.transkribus.core.model.beans.TrpPage) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) RenderingHints(java.awt.RenderingHints) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) Graphics2D(java.awt.Graphics2D)

Example 9 with TrpPage

use of eu.transkribus.core.model.beans.TrpPage in project TranskribusCore by Transkribus.

the class LocalDocWriter method writeTrpDoc.

@Deprecated
public static /*
	 * Use {@link DocExporter#writeRawDoc}
	 * @deprecated
	 */
void writeTrpDoc(TrpDoc doc, String path, ExportCache cache) throws Exception {
    if (doc == null) {
        throw new Exception("Null document given!");
    }
    // if (doc.getMd().getLocalFolder()==null)
    // throw new Exception("This is not a local document!");
    File dir = new File(path);
    FileUtils.forceMkdir(dir);
    String pathN = FilenameUtils.normalize(path);
    // write metadata:
    if (doc.getMd() != null) {
        writeTrpDocMetadata(doc, path);
    }
    // write all images and page xml:
    for (TrpPage p : doc.getPages()) {
        File imgFile = copyImgFile(p, p.getUrl(), pathN);
        logger.debug("Written image file " + imgFile.getAbsolutePath());
        File xmlFile = copyTranscriptFile(p, pathN + "/" + LocalDocConst.PAGE_FILE_SUB_FOLDER, cache);
        if (xmlFile != null)
            logger.debug("Written transcript xml file " + xmlFile.getAbsolutePath());
        else
            logger.debug("No transcript found for page " + p.getPageNr());
    }
}
Also used : TrpPage(eu.transkribus.core.model.beans.TrpPage) File(java.io.File) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException)

Example 10 with TrpPage

use of eu.transkribus.core.model.beans.TrpPage in project TranskribusCore by Transkribus.

the class TrpDocUploadBuilder method build.

public static DocumentUploadDescriptor build(TrpDoc doc) {
    if (doc == null) {
        throw new IllegalArgumentException("doc is null.");
    }
    if (doc.getMd().getLocalFolder() == null) {
        throw new IllegalArgumentException("doc is no localdocument.");
    }
    if (doc.getNPages() < 1) {
        throw new IllegalArgumentException("doc has no pages.");
    }
    DocumentUploadDescriptor s = new DocumentUploadDescriptor();
    s.setMd(doc.getMd());
    for (TrpPage p : doc.getPages()) {
        s.getPages().add(buildPageUploadDescriptor(p));
    }
    return s;
}
Also used : TrpPage(eu.transkribus.core.model.beans.TrpPage) DocumentUploadDescriptor(eu.transkribus.core.model.beans.DocumentUploadDescriptor)

Aggregations

TrpPage (eu.transkribus.core.model.beans.TrpPage)32 TrpTranscriptMetadata (eu.transkribus.core.model.beans.TrpTranscriptMetadata)14 File (java.io.File)14 IOException (java.io.IOException)14 JAXBPageTranscript (eu.transkribus.core.model.beans.JAXBPageTranscript)10 PcGtsType (eu.transkribus.core.model.beans.pagecontent.PcGtsType)7 TrpPageType (eu.transkribus.core.model.beans.pagecontent_trp.TrpPageType)7 URL (java.net.URL)7 ArrayList (java.util.ArrayList)7 TrpDoc (eu.transkribus.core.model.beans.TrpDoc)6 TrpDocMetadata (eu.transkribus.core.model.beans.TrpDocMetadata)5 FileType (eu.transkribus.core.model.beans.mets.FileType)5 JAXBException (javax.xml.bind.JAXBException)5 TrpTextRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType)4 Dimension (java.awt.Dimension)4 FileNotFoundException (java.io.FileNotFoundException)4 CorruptImageException (eu.transkribus.core.exceptions.CorruptImageException)3 DivType (eu.transkribus.core.model.beans.mets.DivType)3 Fptr (eu.transkribus.core.model.beans.mets.DivType.Fptr)3 FileGrpType (eu.transkribus.core.model.beans.mets.FileGrpType)3