svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.basicformats / src / main / java / org / gvsig / basicformats / impl / DefaultWLDFile.java @ 44831
History | View | Annotate | Download (5.61 KB)
1 |
package org.gvsig.basicformats.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.util.List; |
6 |
import org.apache.commons.io.FileUtils; |
7 |
import org.apache.commons.io.FilenameUtils; |
8 |
import org.gvsig.basicformats.WLDFile; |
9 |
import org.gvsig.fmap.geom.Geometry; |
10 |
import org.gvsig.fmap.geom.GeometryLocator; |
11 |
import org.gvsig.fmap.geom.primitive.Envelope; |
12 |
import org.slf4j.Logger; |
13 |
import org.slf4j.LoggerFactory; |
14 |
|
15 |
|
16 |
public class DefaultWLDFile implements WLDFile { |
17 |
|
18 |
private static final Logger logger = LoggerFactory.getLogger(DefaultWLDFile.class); |
19 |
|
20 |
|
21 |
private File source; |
22 |
|
23 |
private double pixelSizeX; |
24 |
private double rotationAxisY; |
25 |
private double rotationAxisX; |
26 |
private double pixelSizeY; |
27 |
private double upperLeftPixelCenterCoordX; |
28 |
private double upperLeftPixelCenterCoordY; |
29 |
|
30 |
public DefaultWLDFile() {
|
31 |
this.source = null; |
32 |
} |
33 |
|
34 |
@Override
|
35 |
public File getFile(File file) { |
36 |
File f = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+"."+FILE_EXTENSION); |
37 |
return f;
|
38 |
} |
39 |
|
40 |
@Override
|
41 |
public File getFile() { |
42 |
return source;
|
43 |
} |
44 |
|
45 |
@Override
|
46 |
public void read(File file) throws IOException { |
47 |
File f = this.getFile(file); |
48 |
if (f.exists()) {
|
49 |
try {
|
50 |
List<String> lines = FileUtils.readLines(f); |
51 |
if (lines!=null ) { |
52 |
this.source = f.getAbsoluteFile();
|
53 |
int lineno = 0; |
54 |
for (String line : lines) { |
55 |
line = line.trim(); |
56 |
char ch = line.charAt(0); |
57 |
if( lineno>5 || Character.isLetter(ch) || ch==';' || ch=='#' ) { |
58 |
continue;
|
59 |
} |
60 |
try {
|
61 |
double value = Double.parseDouble(line); |
62 |
switch(lineno) {
|
63 |
case 0: |
64 |
this.pixelSizeX=value;
|
65 |
break;
|
66 |
case 1: |
67 |
this.rotationAxisY = value;
|
68 |
break;
|
69 |
case 2: |
70 |
this.rotationAxisX = value;
|
71 |
break;
|
72 |
case 3: |
73 |
this.pixelSizeY = value;
|
74 |
break;
|
75 |
case 4: |
76 |
this.upperLeftPixelCenterCoordX = value;
|
77 |
break;
|
78 |
case 5: |
79 |
this.upperLeftPixelCenterCoordY = value;
|
80 |
break;
|
81 |
} |
82 |
lineno++; |
83 |
} catch(NumberFormatException e) { |
84 |
|
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
} catch (IOException e) { |
90 |
logger.warn("Couldn't read "+FILE_EXTENSION+" file ("+f.getAbsolutePath()+").",e); |
91 |
throw e;
|
92 |
} |
93 |
} |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public Envelope getEnvelope(int rows, int columns) { |
98 |
Envelope envelope = null;
|
99 |
if (0.0 != getRotationAxisX() || 0.0 != getRotationAxisY()) { |
100 |
logger.warn("Rotation in wld file not implemented yet. It will be ignored");
|
101 |
} |
102 |
|
103 |
double leftMostX = 0; |
104 |
double upperMostY = 0; |
105 |
double height = 0; |
106 |
double width = 0; |
107 |
try {
|
108 |
leftMostX = getUpperLeftPixelCenterCoordX() - (getPixelSizeX() * 0.5);
|
109 |
upperMostY = getUpperLeftPixelCenterCoordY() - (getPixelSizeY() * 0.5);
|
110 |
height=rows*getPixelSizeY(); |
111 |
width=columns*getPixelSizeX(); |
112 |
|
113 |
envelope = GeometryLocator.getGeometryManager().createEnvelope( |
114 |
Math.min(leftMostX,leftMostX + width),
|
115 |
Math.min(upperMostY,upperMostY + height),
|
116 |
Math.max(leftMostX,leftMostX + width),
|
117 |
Math.max(upperMostY,upperMostY + height),
|
118 |
Geometry.SUBTYPES.GEOM2D); |
119 |
} catch (Exception e) { |
120 |
logger.warn( |
121 |
"Failed to create envelope from wld file with coords: minx:"+leftMostX+
|
122 |
", miny:"+upperMostY+", maxX: "+leftMostX + width+", maxY: "+upperMostY + height, e); |
123 |
} |
124 |
return envelope;
|
125 |
} |
126 |
|
127 |
@Override
|
128 |
public void write(File file) { |
129 |
// TODO: Falta implementar el write del WLDFile
|
130 |
File f = this.getFile(file); |
131 |
|
132 |
} |
133 |
|
134 |
@Override
|
135 |
public void setValue(Envelope envelope, int rows, int columns) { |
136 |
// TODO: Falta implementar el setValue del WLDFile
|
137 |
} |
138 |
|
139 |
/**
|
140 |
* @return the pixelSizeX
|
141 |
*/
|
142 |
@Override
|
143 |
public double getPixelSizeX() { |
144 |
return pixelSizeX;
|
145 |
} |
146 |
|
147 |
/**
|
148 |
* @return the rotationAxisY
|
149 |
*/
|
150 |
@Override
|
151 |
public double getRotationAxisY() { |
152 |
return rotationAxisY;
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* @return the rotationAxisX
|
157 |
*/
|
158 |
@Override
|
159 |
public double getRotationAxisX() { |
160 |
return rotationAxisX;
|
161 |
} |
162 |
|
163 |
/**
|
164 |
* @return the pixelSizeY
|
165 |
*/
|
166 |
@Override
|
167 |
public double getPixelSizeY() { |
168 |
return pixelSizeY;
|
169 |
} |
170 |
|
171 |
/**
|
172 |
* @return the upperLeftPixelCenterCoordX
|
173 |
*/
|
174 |
@Override
|
175 |
public double getUpperLeftPixelCenterCoordX() { |
176 |
return upperLeftPixelCenterCoordX;
|
177 |
} |
178 |
|
179 |
/**
|
180 |
* @return the upperLeftPixelCenterCoordY
|
181 |
*/
|
182 |
@Override
|
183 |
public double getUpperLeftPixelCenterCoordY() { |
184 |
return upperLeftPixelCenterCoordY;
|
185 |
} |
186 |
} |