svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.compat.cdc / org.gvsig.basicformats / src / main / java / org / gvsig / basicformats / impl / DefaultPRJFile.java @ 43876
History | View | Annotate | Download (2.32 KB)
1 |
|
---|---|
2 |
package org.gvsig.basicformats.impl; |
3 |
|
4 |
|
5 |
import java.io.File; |
6 |
import java.io.IOException; |
7 |
import org.apache.commons.io.FileUtils; |
8 |
import org.apache.commons.io.FilenameUtils; |
9 |
import org.apache.commons.lang3.StringUtils; |
10 |
import org.cresques.cts.ICRSFactory; |
11 |
import org.cresques.cts.IProjection; |
12 |
import org.gvsig.basicformats.PRJFile; |
13 |
import org.gvsig.fmap.crs.CRSFactory; |
14 |
import org.slf4j.Logger; |
15 |
import org.slf4j.LoggerFactory; |
16 |
|
17 |
|
18 |
public class DefaultPRJFile implements PRJFile { |
19 |
|
20 |
private static final Logger logger = LoggerFactory.getLogger(DefaultPRJFile.class); |
21 |
|
22 |
|
23 |
private File source; |
24 |
private IProjection crs;
|
25 |
|
26 |
public DefaultPRJFile() {
|
27 |
this.source = null; |
28 |
this.crs = null; |
29 |
} |
30 |
|
31 |
@Override
|
32 |
public File getFile(File file) { |
33 |
File f = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+"."+FILE_EXTENSION); |
34 |
return f;
|
35 |
} |
36 |
|
37 |
@Override
|
38 |
public File getFile() { |
39 |
return source;
|
40 |
} |
41 |
|
42 |
@Override
|
43 |
public IProjection getCRS() {
|
44 |
return this.crs; |
45 |
} |
46 |
|
47 |
@Override
|
48 |
public void setCRS(IProjection crs) { |
49 |
this.crs = crs;
|
50 |
} |
51 |
|
52 |
@Override
|
53 |
public void read(File file) throws IOException { |
54 |
File f = this.getFile(file); |
55 |
if (f.exists()) {
|
56 |
try {
|
57 |
String theContents = FileUtils.readFileToString(f);
|
58 |
if (StringUtils.isNotEmpty(theContents)){
|
59 |
IProjection theCrs=CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, theContents); |
60 |
this.crs = theCrs;
|
61 |
this.source = f.getAbsoluteFile();
|
62 |
} |
63 |
} catch (IOException e) { |
64 |
logger.warn("Couldn't read "+FILE_EXTENSION+" file ("+f.getAbsolutePath()+").",e); |
65 |
throw e;
|
66 |
} |
67 |
} |
68 |
|
69 |
} |
70 |
|
71 |
@Override
|
72 |
public void write(File file) throws IOException { |
73 |
File f = this.getFile(file); |
74 |
try {
|
75 |
String export = crs.export(ICRSFactory.FORMAT_WKT_ESRI); |
76 |
if(export!=null){ |
77 |
FileUtils.writeStringToFile(f, export);
|
78 |
this.source = f;
|
79 |
} |
80 |
} catch (Exception e) { |
81 |
logger.warn("Couldn't write "+FILE_EXTENSION+" file ("+f.getAbsolutePath()+").",e); |
82 |
throw e;
|
83 |
} |
84 |
} |
85 |
} |