Search in sources :

Example 1 with ShpFileType

use of org.geotools.data.shapefile.files.ShpFileType in project hale by halestudio.

the class ShapefileAdvisor method copyResource.

@Override
public void copyResource(LocatableInputSupplier<? extends InputStream> resource, final Path target, IContentType resourceType, boolean includeRemote, IOReporter reporter) throws IOException {
    URI orgUri = resource.getLocation();
    if (orgUri == null) {
        throw new IOException("URI for original resource must be known");
    }
    // copy if files can be resolved as a Path
    Path orgPath = null;
    try {
        orgPath = Paths.get(orgUri);
    } catch (Exception e) {
    // ignore
    }
    if (orgPath != null) {
        // determine the filename w/o extension
        String filemain = orgPath.getFileName().toString();
        int extPos = filemain.lastIndexOf('.');
        if (extPos > 0) {
            filemain = filemain.substring(0, extPos);
        }
        // matcher for associated files
        final PathMatcher auxfiles = orgPath.getFileSystem().getPathMatcher("glob:" + filemain + ".???");
        // find all associated files
        Path orgDir = orgPath.getParent();
        try (DirectoryStream<Path> files = Files.newDirectoryStream(orgDir, new DirectoryStream.Filter<Path>() {

            @Override
            public boolean accept(Path entry) throws IOException {
                return auxfiles.matches(entry.getFileName());
            }
        })) {
            // copy the files
            for (Path orgFile : files) {
                Path targetFile = target.resolveSibling(orgFile.getFileName());
                Files.copy(orgFile, targetFile);
            }
        }
    } else {
        // copy the main file
        try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(target));
            InputStream in = new DefaultInputSupplier(orgUri).getInput()) {
            ByteStreams.copy(in, out);
        }
        // determine base URI w/o dot and extension
        String base = orgUri.toASCIIString();
        int extPos = base.lastIndexOf('.');
        if (extPos > 0) {
            base = base.substring(0, extPos);
        }
        // determine file base name w/o dot and extension
        String filemain = target.getFileName().toString();
        extPos = filemain.lastIndexOf('.');
        if (extPos > 0) {
            filemain = filemain.substring(0, extPos);
        }
        for (ShpFileType type : ShpFileType.values()) {
            if (!type.equals(ShpFileType.SHP)) {
                try {
                    URI source = URI.create(base + type.extensionWithPeriod);
                    if (HaleIO.testStream(source, true)) {
                        Path targetFile = target.resolveSibling(filemain + type.extensionWithPeriod);
                        // copy the auxiliary file
                        try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(targetFile));
                            InputStream in = new DefaultInputSupplier(source).getInput()) {
                            ByteStreams.copy(in, out);
                        }
                    }
                } catch (Exception e) {
                    log.debug("Failed to copy auxiliary file for Shapefile", e);
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DirectoryStream(java.nio.file.DirectoryStream) IOException(java.io.IOException) URI(java.net.URI) IOException(java.io.IOException) PathMatcher(java.nio.file.PathMatcher) BufferedOutputStream(java.io.BufferedOutputStream) ShpFileType(org.geotools.data.shapefile.files.ShpFileType)

Aggregations

DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)1 BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 DirectoryStream (java.nio.file.DirectoryStream)1 Path (java.nio.file.Path)1 PathMatcher (java.nio.file.PathMatcher)1 ShpFileType (org.geotools.data.shapefile.files.ShpFileType)1