Ignore:
Timestamp:
2017-09-25T21:47:56+02:00 (7 years ago)
Author:
donvip
Message:

Edigeo: support reading tar.bz2 files directly

Location:
applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/edigeo/pci
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/edigeo/pci/EdigeoPciImporter.java

    r33642 r33671  
    1313import org.openstreetmap.josm.data.osm.DataSet;
    1414import org.openstreetmap.josm.gui.io.importexport.OsmImporter;
    15 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1615import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1716import org.openstreetmap.josm.io.IllegalDataException;
     
    2423
    2524    static final ExtensionFileFilter EDIGEO_FILE_FILTER = new ExtensionFileFilter(
    26             "thf", "thf", tr("Cadastre Edigeo files") + " (*.thf)");
     25            "thf,tar.bz2", "thf", tr("Cadastre Edigeo files") + " (*.thf, *.tar.bz2)");
    2726
    2827    protected File file;
     
    3837    public void importData(File file, ProgressMonitor progressMonitor)
    3938            throws IOException, IllegalDataException {
    40         if (file != null) {
    41             this.file = file;
    42         }
     39        this.file = file;
    4340        // Do not call super.importData because Compression.getUncompressedFileInputStream skips the first entry
    4441        try (InputStream in = new FileInputStream(file)) {
     
    5855        }
    5956    }
    60 
    61     @Override
    62     protected OsmDataLayer createLayer(DataSet dataSet, File associatedFile, String layerName) {
    63         // FIXME: mapping pci => osm
    64         //DataSetUpdater.updateDataSet(dataSet, handler, associatedFile);
    65         return new OsmDataLayer(dataSet, layerName, associatedFile);
    66     }
    6757}
  • applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/edigeo/pci/EdigeoPciReader.java

    r33665 r33671  
    22package org.openstreetmap.josm.plugins.fr.cadastre.edigeo.pci;
    33
     4import java.io.BufferedInputStream;
    45import java.io.File;
     6import java.io.FileOutputStream;
    57import java.io.IOException;
    68import java.io.InputStream;
     9import java.nio.file.Files;
    710import java.nio.file.Path;
    811import java.util.Arrays;
     
    1215import java.util.Map.Entry;
    1316
     17import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
     18import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
     19import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
     20import org.apache.commons.compress.utils.IOUtils;
    1421import org.openstreetmap.josm.data.osm.DataSet;
    1522import org.openstreetmap.josm.data.osm.DataSet.UploadPolicy;
     
    2027import org.openstreetmap.josm.plugins.fr.cadastre.edigeo.EdigeoFileTHF;
    2128import org.openstreetmap.josm.plugins.fr.cadastre.edigeo.EdigeoFileVEC;
     29import org.openstreetmap.josm.tools.Utils;
    2230
    2331/**
     
    180188
    181189    DataSet parse(Path path, ProgressMonitor instance) throws IOException, ReflectiveOperationException {
    182         DataSet data = new DataSet();
    183         data.setUploadPolicy(UploadPolicy.DISCOURAGED);
    184         EdigeoFileTHF thf = new EdigeoFileTHF(path).read().fill(data);
    185         data.setName(thf.getSupport().getBlockIdentifier());
    186         return data;
     190        Path tmpDir = null;
     191        Path thfPath = path;
     192        try {
     193            if (thfPath.toString().endsWith(".tar.bz2")) {
     194                try (InputStream fin = Files.newInputStream(path);
     195                     BufferedInputStream in = new BufferedInputStream(fin);
     196                     BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
     197                     TarArchiveInputStream tar = new TarArchiveInputStream(bzIn)
     198                ) {
     199                    TarArchiveEntry entry;
     200                    tmpDir = Files.createTempDirectory(Utils.getJosmTempDir().toPath(), "cadastre");
     201                    while ((entry = tar.getNextTarEntry()) != null) {
     202                        File file = tmpDir.resolve(entry.getName()).toFile();
     203                        try (FileOutputStream out = new FileOutputStream(file)) {
     204                            if (IOUtils.copy(tar, out) < entry.getSize()) {
     205                                throw new IOException(String.format("Unable to write ''{0}'' entirely", file));
     206                            } else if (file.toString().endsWith(".THF")) {
     207                                thfPath = file.toPath();
     208                            }
     209                        }
     210                    }
     211                }
     212            }
     213            DataSet data = new DataSet();
     214            data.setUploadPolicy(UploadPolicy.DISCOURAGED);
     215            EdigeoFileTHF thf = new EdigeoFileTHF(thfPath).read().fill(data);
     216            data.setName(thf.getSupport().getBlockIdentifier());
     217            return data;
     218        } finally {
     219            if (tmpDir != null) {
     220                Utils.deleteDirectory(tmpDir.toFile());
     221            }
     222        }
    187223    }
    188224
Note: See TracChangeset for help on using the changeset viewer.