Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.basicformats / src / main / java / org / gvsig / basicformats / impl / DefaultPRJFile.java @ 47121

History | View | Annotate | Download (2.74 KB)

1

    
2
package org.gvsig.basicformats.impl;
3

    
4

    
5
import java.io.BufferedOutputStream;
6
import java.io.File;
7
import java.io.IOException;
8
import java.io.OutputStream;
9
import java.nio.charset.StandardCharsets;
10
import org.apache.commons.io.FileUtils;
11
import org.apache.commons.io.FilenameUtils;
12
import org.apache.commons.io.IOUtils;
13
import org.apache.commons.lang3.StringUtils;
14
import org.cresques.cts.ICRSFactory;
15
import org.cresques.cts.IProjection;
16
import org.gvsig.basicformats.PRJFile;
17
import org.gvsig.fmap.crs.CRSFactory;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20

    
21

    
22
public class DefaultPRJFile extends AbstractFormatFile implements PRJFile {
23
            
24
    private static final Logger logger = LoggerFactory.getLogger(DefaultPRJFile.class);
25
    
26
        
27
    private File source;
28
    private IProjection crs;
29
    
30
    public DefaultPRJFile() {
31
        this.source = null;
32
        this.crs = null;        
33
    }
34

    
35
    @Override
36
    public File getFile(File file) {
37
        File f = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+"."+FILE_EXTENSION);
38
        return f;
39
    }
40
    
41
    @Override
42
    public File getFile() {
43
        return source;
44
    }
45
    
46
    @Override
47
    public IProjection getCRS() {
48
        return this.crs;
49
    }
50
    
51
    @Override
52
    public void setCRS(IProjection crs) {
53
        this.crs = crs;
54
    }
55
    
56
    @Override
57
    public void read(File file) throws IOException {
58
        File f = this.getFile(file);
59
        if (f.exists()) {
60
            try {
61
                String theContents = FileUtils.readFileToString(f);
62
                if (StringUtils.isNotEmpty(theContents)){
63
                    IProjection theCrs=CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, theContents);
64
                    this.crs = theCrs;
65
                    this.source = f.getAbsoluteFile();
66
                }
67
            } catch (IOException e) {
68
                logger.warn("Couldn't read "+FILE_EXTENSION+" file ("+f.getAbsolutePath()+").",e);
69
                throw e;
70
            }
71
        }
72
            
73
    }
74
    
75
    @Override
76
    public void write(File file) throws IOException {
77
        File f = this.getFile(file);
78
        try {
79
            String contents = crs.export(ICRSFactory.FORMAT_WKT_ESRI);
80
            if(contents!=null){
81
                OutputStream os = null;
82
                try {
83
                    os = new BufferedOutputStream(FileUtils.openOutputStream(f));
84
                    IOUtils.write(contents, os, StandardCharsets.UTF_8);
85
                } finally {
86
                    IOUtils.closeQuietly(os);
87
                }
88
                this.source = f;
89
            }
90
        } catch (Exception e) {
91
            logger.warn("Couldn't write "+FILE_EXTENSION+" file ("+f.getAbsolutePath()+").",e);
92
            throw e;
93
        }
94
    }
95
}