Revision 46313

View differences:

tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/WLDFile.java
1

  
2
package org.gvsig.basicformats;
3

  
4
import org.gvsig.fmap.geom.primitive.Envelope;
5

  
6

  
7
public interface WLDFile extends FormatFile {
8

  
9
    String FILE_EXTENSION = "wld";
10

  
11
    Envelope getEnvelope(int rows, int columns);
12

  
13
    void setValue(Envelope envelope, int rows, int columns);
14

  
15
    /**
16
     * @return the pixelSizeX
17
     */
18
    double getPixelSizeX();
19

  
20
    /**
21
     * @return the pixelSizeY
22
     */
23
    double getPixelSizeY();
24

  
25
    /**
26
     * @return the rotationAxisX
27
     */
28
    double getRotationAxisX();
29

  
30
    /**
31
     * @return the rotationAxisY
32
     */
33
    double getRotationAxisY();
34

  
35
    /**
36
     * @return the upperLeftPixelCenterCoordX
37
     */
38
    double getUpperLeftPixelCenterCoordX();
39

  
40
    /**
41
     * @return the upperLeftPixelCenterCoordY
42
     */
43
    double getUpperLeftPixelCenterCoordY();
44

  
45
}
tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/FormatFile.java
1

  
2
package org.gvsig.basicformats;
3

  
4
import java.io.File;
5
import java.io.IOException;
6

  
7

  
8
public interface FormatFile {
9
 
10
    public File getFile(File file);
11

  
12
    public File getFile();
13

  
14
    public void read(File file) throws IOException ;
15

  
16
    public void write(File file) throws IOException ;       
17

  
18
    public void writeQuietly(File file) throws IOException ;       
19
}
tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/CPGFile.java
1

  
2
package org.gvsig.basicformats;
3

  
4
public interface CPGFile extends FormatFile {
5

  
6
    public String FILE_EXTENSION = "cpg";
7

  
8
    public String getCharsetName();
9

  
10
    public void setCharsetName(String charsetName);
11

  
12
    /**
13
     * Returns the code page corresponding to the
14
     * provided charset name
15
     *
16
     * @param charsetName
17
     * @return The code page, or 0x00 if no equivalent code page was found for
18
     * the provided charsetName
19
     */
20
    public int toCPG(String charsetName);
21

  
22
    public String toCPGName(String charsetName);
23

  
24
    public String toCharsetName(String codePageName);
25

  
26
    /**
27
     * Gets the Java NIO charset name equivalent to the provided code page.
28
     * Gets null if the provided code page is not recognised
29
     * as a valid code
30
     *
31
     * @param codePage
32
     * @return
33
     */
34
    public String toCharsetName(int codePage);
35
    
36
}
tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/FormatsFile.java
1

  
2
package org.gvsig.basicformats;
3

  
4
import java.io.File;
5
import java.io.IOException;
6
import org.gvsig.basicformats.impl.DefaultCLRFile;
7
import org.gvsig.basicformats.impl.DefaultCPGFile;
8
import org.gvsig.basicformats.impl.DefaultHDRFile;
9
import org.gvsig.basicformats.impl.DefaultPRJFile;
10
import org.gvsig.basicformats.impl.DefaultSTXFile;
11
import org.gvsig.basicformats.impl.DefaultWLDFile;
12

  
13
public class FormatsFile {
14
    
15
    public static CPGFile createCPGFile() {
16
        return new DefaultCPGFile();
17
    }
18

  
19
    public static CPGFile getCPGFile(File file)  {
20
        try {
21
            DefaultCPGFile format = new DefaultCPGFile();
22
            format.read(file);
23
            return format;
24
        } catch (IOException ex) {
25
            return null;
26
        }
27
    }
28

  
29
    public static HDRFile createHDRFile() {
30
        return new DefaultHDRFile();
31
    }
32

  
33
    public static HDRFile getHDRFile(File file)  {
34
        try {
35
            DefaultHDRFile format = new DefaultHDRFile();
36
            format.read(file);
37
            return format;
38
        } catch (IOException ex) {
39
            return null;
40
        }
41
    }
42

  
43
    public static PRJFile createPRJFile() {
44
        return new DefaultPRJFile();
45
    }
46

  
47
    public static PRJFile getPRJFile(File file)  {
48
        try {
49
            DefaultPRJFile format = new DefaultPRJFile();
50
            format.read(file);
51
            return format;
52
        } catch (IOException ex) {
53
            return null;
54
        }
55
    }
56

  
57
    public static STXFile createSTXFile() {
58
        return new DefaultSTXFile();
59
    }
60

  
61
    public static STXFile getSTXFile(File file)  {
62
        try {
63
            DefaultSTXFile format = new DefaultSTXFile();
64
            format.read(file);
65
            return format;
66
        } catch (IOException ex) {
67
            return null;
68
        }
69
    }
70

  
71
    public static WLDFile createWLDFile() {
72
        return new DefaultWLDFile();
73
    }
74

  
75
    public static WLDFile getWLDFile(File file) {
76
        try {
77
            DefaultWLDFile format = new DefaultWLDFile();
78
            format.read(file);
79
            return format;
80
        } catch (IOException ex) {
81
            return null;
82
        }
83
    }
84

  
85
    public static CLRFile createCLRFile() {
86
        return new DefaultCLRFile();
87
    }
88

  
89
    public static CLRFile getCLRFile(File file) {
90
        try {
91
            DefaultCLRFile format = new DefaultCLRFile();
92
            format.read(file);
93
            return format;
94
        } catch (IOException ex) {
95
            return null;
96
        }
97
    }
98

  
99
}
tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/PRJFile.java
1

  
2
package org.gvsig.basicformats;
3

  
4
import org.cresques.cts.IProjection;
5

  
6

  
7
public interface PRJFile extends FormatFile {
8

  
9
    public String FILE_EXTENSION = "prj";
10
    
11
    public IProjection getCRS();
12

  
13
    public void setCRS(IProjection crs);
14

  
15
}
tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/HDRFile.java
1

  
2
package org.gvsig.basicformats;
3

  
4
import java.nio.ByteOrder;
5
import org.gvsig.fmap.geom.primitive.Envelope;
6

  
7

  
8
public interface HDRFile extends FormatFile {
9

  
10
    public String FILE_EXTENSION = "hdr";
11
    public String LAYOUT_BIL = "bil";
12
    public String LAYOUT_BIP = "bip";
13
    public String LAYOUT_BSQ = "bsq";
14
    
15
    public int NONE = -1;
16

  
17
    public Envelope getEnvelope();
18
    
19
    public int getDataType();
20
    
21
    /**
22
     * @return the bandgapbytes
23
     */
24
    public int getBandgapbytes();
25

  
26
    /**
27
     * @return the bandrowbytes
28
     */
29
    public int getBandrowbytes();
30

  
31
    /**
32
     * @return the byteorder
33
     */
34
    public ByteOrder getByteorder();
35

  
36
    /**
37
     * @return the comments
38
     */
39
    public String getComments();
40

  
41
    /**
42
     * @return the layout
43
     */
44
    public String getLayout();
45

  
46
    /**
47
     * @return the nbands
48
     */
49
    public int getNbands();
50

  
51
    /**
52
     * @return the nbits
53
     */
54
    public int getNbits();
55

  
56
    /**
57
     * @return the ncols
58
     */
59
    public int getNcols();
60

  
61
    /**
62
     * @return the nrows
63
     */
64
    public int getNrows();
65

  
66
    /**
67
     * @return the skipbytes
68
     */
69
    public int getSkipbytes();
70

  
71
    /**
72
     * @return the totalrowbytes
73
     */
74
    public int getTotalrowbytes();
75

  
76
    /**
77
     * @return the ulxmax
78
     */
79
    public double getUlxmax();
80

  
81
    /**
82
     * @return the ulymax
83
     */
84
    public double getUlymax();
85

  
86
    /**
87
     * @return the xdim
88
     */
89
    public double getXdim();
90

  
91
    /**
92
     * @return the ydim
93
     */
94
    public double getYdim();
95

  
96
    /**
97
     * @return the isValid
98
     */
99
    public boolean isValid();
100

  
101
    /**
102
     * @param comments the comments to set
103
     */
104
    public void setComments(String comments);
105

  
106
    public int getBandSize();
107

  
108
    /**
109
     * @return the cellsize
110
     */
111
    public int getCellsize();
112

  
113
    /**
114
     * @return the nodata_value
115
     */
116
    public double getNodata_value();
117

  
118
    /**
119
     * @return the xllcorner
120
     */
121
    public double getXllcorner();
122

  
123
    /**
124
     * @return the yllcorner
125
     */
126
    public double getYllcorner();
127

  
128
    public void setDataType(int dataType);
129

  
130
    public void setDimensions(int nrows, int ncols, Envelope envelope);
131

  
132
    public void setNBands(int nbands);
133
    
134
    public void setByteOrder(ByteOrder order);
135

  
136
}
tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/STXFile.java
1
package org.gvsig.basicformats;
2

  
3
import java.util.List;
4

  
5
public interface STXFile extends FormatFile {
6

  
7
    public interface STXBand {
8
        
9
        public void parse(String line);
10

  
11
        @Override
12
        public String toString();
13
        
14
        public int getBand();
15
        
16
        public double getMinimum();
17
        
18
        public double getMaximum();
19
        
20
        public double getMean();
21
        
22
        public double getStdDeviation();
23
        
24
        public double getLinearStretchMin();
25
        
26
        public double getLinearStretchMax();
27

  
28
        public boolean isValid();
29
    }
30
    
31
    public String FILE_EXTENSION = "stx";
32

  
33
    public void addBand(int band, double minimum, double maximum, double mean, double std_deviation, double linear_stretch_min, double linear_stretch_max);
34

  
35
    public void clear();
36

  
37
    public List<STXBand> getBands();
38
   
39
}
tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/CLRFile.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.basicformats;
7

  
8
import org.gvsig.raster.lib.legend.api.colortable.ColorTable;
9

  
10

  
11
public interface CLRFile extends FormatFile {
12

  
13
    public String FILE_EXTENSION = "clr";
14
    
15
    ColorTable getColorTable();
16

  
17
    void setColorTable(ColorTable colorTable);
18
    
19
}
tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/impl/DefaultWLDFile.java
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 extends AbstractFormatFile 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
}
tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/impl/AbstractFormatFile.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.basicformats.impl;
7

  
8
import java.io.File;
9
import java.io.IOException;
10
import org.gvsig.basicformats.FormatFile;
11

  
12
/**
13
 *
14
 * @author fdiaz
15
 */
16
public abstract class AbstractFormatFile implements FormatFile {
17

  
18
    @Override
19
    public void writeQuietly(File file) throws IOException {
20
        try {
21
            write(file);
22
        } catch (Exception e) {
23
        }
24
    }
25

  
26
}
tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/impl/DefaultCPGFile.java
1
package org.gvsig.basicformats.impl;
2

  
3
import java.io.File;
4
import java.io.IOException;
5
import org.apache.commons.io.FileUtils;
6
import org.apache.commons.io.FilenameUtils;
7
import org.apache.commons.lang3.StringUtils;
8
import org.gvsig.basicformats.CPGFile;
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11

  
12

  
13
public class DefaultCPGFile extends AbstractFormatFile implements CPGFile {
14

  
15
    private static final Logger logger = LoggerFactory.getLogger(DefaultPRJFile.class);
16

  
17

  
18
    private File source;
19
    private String charsetName = null;
20

  
21
    /**
22
     * Define the valid code pages (equivalent to MSDOS code pages). 
23
     * This codes are used on the byte 29 of the DBF header to define the DBF
24
     * codepage.
25
     *
26
     * The equivalences of these charsets using Java NIO charset names are
27
     * defined on the {@link #charsetNames} array (so 0x01 is
28
     * equivalent to IBM437, 0x02 to IBM850, etc)
29
     *
30
     * See some other equivalences in:
31
     * https://github.com/infused/dbf/blob/master/docs/supported_encodings.csv
32
     * https://github.com/olemb/dbfread/blob/master/dbfread/codepages.py
33
     * https://joinup.ec.europa.eu/svn/gvsig-desktop/trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/drivers/dbf/DbfEncodings.java
34
     */
35
    private static final short[] codePages = {
36
        0x01, 0x02, 0x03, 0x04,
37
        0x08, 0x09, 0x0a, 0x0b,
38
        0x0d, 0x0e, 0x0f, 0x10,
39
        0x11, 0x12, 0x13, 0x14,
40
        0x15, 0x16, 0x17, 0x18,
41
        0x19, 0x1a, 0x1b, 0x1c,
42
        0x1d, 0x1f, 0x22, 0x23,
43
        0x24, 0x25, 0x26, 0x37,
44
        0x40, 0x4d, 0x4e, 0x4f,
45
        0x50, 0x57, 0x58, 0x59,
46
        0x64, 0x65, 0x66, 0x67,
47
        0x68, 0x69, 0x6a, 0x6b,
48
        0x6c, 0x78, 0x79, 0x7a,
49
        0x7b, 0x7c, 0x7d, 0x7d,
50
        0x86, 0x87, 0x88, 0xc8,
51
        0xc9, 0xca, 0xcb, 0xcc};
52

  
53
    /**
54
     * Equivalent Java charset names to the code pages defined in
55
     * {@link #codePages}, using Java NIO Charset names (which differ
56
     * from JAVA IO names, see
57
     * https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html)
58
     */
59
    private static final String[] charsetNames = new String[]{
60
        "IBM437", "IBM850", "windows-1252", "x-MacRoman",
61
        "IBM865", "IBM437", "IBM850", "IBM437",
62
        "IBM437", "IBM850", "IBM437", "IBM850",
63
        "IBM437", "IBM850", "x-IBM943", "IBM850",
64
        "IBM437", "IBM850", "IBM865", "IBM437",
65
        "IBM437", "IBM850", "IBM437", "IBM863",
66
        "IBM850", "IBM852", "IBM852", "IBM852",
67
        "IBM860", "IBM850", "IBM866", "IBM850",
68
        "IBM852", "x-mswin-936", "x-IBM949", "IBM950",
69
        "x-IBM874", "windows-1252", "windows-1252", "windows-1252",
70
        "IBM852", "IBM866", "IBM865", "IBM861",
71
        // 0x68 and 0x69 are unofficial "Codepage 895 Kamenicky (Czech) MS-DOS" and "Codepage 620  Mazovia (Polish) MS-DOS",
72
        // but there is no Java equivalent
73
        // so we use CP437 which is the closest charset for the latin characters part
74
        "IBM437", "IBM437", "x-IBM737", "IBM857",
75
        "IBM863", "x-IBM950", "x-IBM949", "x-mswin-936",
76
        "x-IBM942", "x-IBM874", "windows-1255", "windows-1256",
77
        "x-IBM737", "IBM852", "IBM857", "windows-1250",
78
        "windows-1251", "windows-1254", "windows-1253", "windows-1257"};
79

  
80
    public DefaultCPGFile() {
81
        this.charsetName = null;
82
        this.source = null;
83
    }
84

  
85
    @Override
86
    public File getFile(File file) {
87
        File f = new File(FilenameUtils.removeExtension(file.getAbsolutePath()) + "." + FILE_EXTENSION);
88
        return f;
89
    }
90

  
91
    @Override
92
    public File getFile() {
93
        return source;
94
    }
95

  
96
    @Override
97
    public String getCharsetName() {
98
        return this.charsetName;
99
    }
100

  
101
    @Override
102
    public void setCharsetName(String charsetName) {
103
        this.charsetName = charsetName;
104
    }
105

  
106
    @Override
107
    public String toCharsetName(String codePageName) {
108
        if (codePageName.equals("UTF8")) {
109
            return "UTF-8";
110
        }
111
        if (codePageName.equals("SJIS")) {
112
            return "Shift_JIS";
113
        }
114

  
115
        if (StringUtils.isNumeric(codePageName)) {
116
            if (codePageName.startsWith("8859") && codePageName.length() > 4) {
117
                return "ISO-8859-" + codePageName.substring(4);
118
            }
119
            if (codePageName.startsWith("125") && codePageName.length() == 4) {
120
                return "windows-" + codePageName;
121
            }
122
            if (codePageName.length() == 3) {
123
                return "IBM-" + codePageName;
124
            }
125
            for (int i = 0; i < charsetNames.length; i++) {
126
                if (charsetNames[i].contains(codePageName)) {
127
                    return codePageName;
128
                }
129
            }
130
        }
131
        if (codePageName.equals("65001")) {
132
            return "UTF-8";
133
        }
134
        return codePageName;
135
    }
136

  
137
    /**
138
     * Gets the Java NIO charset name equivalent to the provided code page.
139
     * Gets null if the provided code page is not recognised
140
     * as a valid code
141
     *
142
     * @param codePage
143
     * @return
144
     */
145
    @Override
146
    public String toCharsetName(int codePage) {
147
        if (codePage != 0) {
148
            for (int i = 0; i < codePages.length; i++) {
149
                if (codePages[i] == codePage) {
150
                    return charsetNames[i];
151
                }
152
            }
153
        }
154
        return null;
155
    }
156

  
157
    @Override
158
    public String toCPGName(String charsetName) {
159
        if (charsetName.startsWith("windows-")
160
                || charsetName.startsWith("ISO-8859")
161
                || charsetName.startsWith("IBM-")
162
                || charsetName.startsWith("x-IBM")
163
                || charsetName.startsWith("x-mswin-")) {
164
            return charsetName.replaceAll("[^\\d]", "");
165
        }
166
        if (charsetName.equals("Shift_JIS")) {
167
            return "SJIS";
168
        }
169
        // For the rest of the charsets, we'll directly write the Java NIO Charset
170
        // Probably they will only be recognized by gvSIG, but it's better than nothing
171
        return charsetName;
172
    }
173

  
174
    /**
175
     * Returns the code page corresponding to the
176
     * provided charset name
177
     *
178
     * @param charsetName
179
     * @return The code page, or 0x00 if no equivalent code page was found for
180
     * the provided charsetName
181
     */
182
    @Override
183
    public int toCPG(String charsetName) {
184
        for (int i = 0; i < charsetNames.length; i++) {
185
            if (charsetNames[i].equals(charsetName)) {
186
                return codePages[i];
187
            }
188
        }
189
        // default
190
        return 0x00;
191
    }
192

  
193
    @Override
194
    public void read(File file) throws IOException {
195
        File f = this.getFile(file);
196
        if (f.exists()) {
197
            try {
198
                String theContents = FileUtils.readFileToString(f);
199
                theContents = StringUtils.trim(theContents);
200
                if (StringUtils.isNotEmpty(theContents)) {
201
                    String theCharset = toCharsetName(theContents);
202
                    this.charsetName = theCharset;
203
                    this.source = f.getAbsoluteFile();
204
                }
205
            } catch (IOException e) {
206
                logger.warn("Couldn't read " + FILE_EXTENSION + " file (" + f.getAbsolutePath() + ").", e);
207
                throw e;
208
            }
209
        }
210
    }
211

  
212
    @Override
213
    public void write(File file) throws IOException {
214
        File f = this.getFile(file);
215
        try {
216
            String export = toCPGName(this.charsetName) + "\n";
217
            FileUtils.writeStringToFile(f, export, "ISO-8859-1");
218
            this.source = f;
219
        } catch (Exception e) {
220
            logger.warn("Couldn't write " + FILE_EXTENSION + " file (" + f.getAbsolutePath() + ").", e);
221
            throw e;
222
        }
223
    }
224

  
225
}
tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/impl/DefaultPRJFile.java
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 extends AbstractFormatFile 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
}
tags/org.gvsig.desktop-2.0.365/org.gvsig.desktop.compat.cdc/org.gvsig.basicformats/src/main/java/org/gvsig/basicformats/impl/DefaultHDRFile.java
1
package org.gvsig.basicformats.impl;
2

  
3

  
4
import java.io.File;
5
import java.io.IOException;
6
import java.nio.ByteOrder;
7
import java.util.List;
8
import org.apache.commons.io.FileUtils;
9
import org.apache.commons.io.FilenameUtils;
10
import org.apache.commons.lang3.StringUtils;
11
import org.gvsig.basicformats.HDRFile;
12
import org.gvsig.fmap.geom.Geometry;
13
import org.gvsig.fmap.geom.GeometryLocator;
14
import org.gvsig.fmap.geom.primitive.Envelope;
15
import org.gvsig.fmap.geom.primitive.Point;
16
import org.gvsig.raster.lib.buffer.api.BufferManager;
17
import org.slf4j.Logger;
18
import org.slf4j.LoggerFactory;
19

  
20
public class DefaultHDRFile extends AbstractFormatFile implements HDRFile {
21

  
22
    // http://resources.esri.com/help/9.3/arcgisdesktop/com/gp_toolref/spatial_analyst_tools/esri_ascii_raster_format.htm
23
    private static final Logger logger = LoggerFactory.getLogger(DefaultHDRFile.class);
24

  
25
    private File source;
26
    private String comments;
27

  
28
    private int nrows = NONE;
29
    private int ncols = NONE;
30
    private int nbands = 1;
31
    private int nbits = 8;
32
    private String pixeltype;
33
    private String byteorder_word;
34
    private ByteOrder byteorder = ByteOrder.nativeOrder();
35
    private String layout = LAYOUT_BIL;
36
    private int skipbytes = 0;
37
    private double ulxmax = Double.NaN;
38
    private double ulymax = Double.NaN;
39
    private double xdim = Double.NaN;
40
    private double ydim = Double.NaN;
41
    private int bandrowbytes = NONE;
42
    private int totalrowbytes = NONE;
43
    private int bandgapbytes = 0;
44
    private boolean valid = false;
45
    private double nodata_value = Double.NaN;
46

  
47
    // X coordinate of the origin (by center or lower left corner of the cell).
48
    private double xllcorner = Double.NaN;
49

  
50
    // Y coordinate of the origin (by center or lower left corner of the cell).
51
    private double yllcorner = Double.NaN;
52

  
53
    private int cellsize = NONE;
54

  
55
    public DefaultHDRFile() {
56
        this.source = null;
57
    }
58

  
59
    @Override
60
    public File getFile(File file) {
61
        File f = new File(FilenameUtils.removeExtension(file.getAbsolutePath()) + "." + FILE_EXTENSION);
62
        return f;
63
    }
64

  
65
    @Override
66
    public File getFile() {
67
        return source;
68
    }
69

  
70
    @Override
71
    public void read(File file) throws IOException {
72
        File f = this.getFile(file);
73
        if (f.exists()) {
74
            try {
75
                List<String> lines = FileUtils.readLines(f);
76
                if (lines != null) {
77
                    this.source = f.getAbsoluteFile();
78
                    int lineno = 1;
79
                    for (String line : lines) {
80
                        String[] words = StringUtils.split(line.trim().toLowerCase());
81
                        try {
82
                            switch (words[0]) {
83
                                case "nrows":
84
                                    this.nrows = Integer.parseInt(words[1]);
85
                                    break;
86
                                case "ncols":
87
                                    this.ncols = Integer.parseInt(words[1]);
88
                                    break;
89
                                case "nbands":
90
                                    this.nbands = Integer.parseInt(words[1]);
91
                                    break;
92
                                case "nbits":
93
                                    this.nbits = Integer.parseInt(words[1]);
94
                                    break;
95
                                case "pixeltype":
96
                                    this.pixeltype = words[1];
97
                                    break;
98
                                case "byteorder":
99
                                    this.byteorder_word = words[1];
100
                                    break;
101
                                case "layout":
102
                                    this.layout = words[1];
103
                                    break;
104
                                case "skipbytes":
105
                                    this.skipbytes = Integer.parseInt(words[1]);
106
                                    break;
107
                                case "nodata_value":
108
                                    this.nodata_value = Double.parseDouble(words[1]);
109
                                    break;
110
                                case "cellsize":
111
                                    this.cellsize = Integer.parseInt(words[1]);
112
                                    break;
113
                                case "xllcorner":
114
                                case "xllcenter":
115
                                    this.xllcorner = Double.parseDouble(words[1]);
116
                                    break;
117
                                case "yllcorner":
118
                                case "yllcenter":
119
                                    this.yllcorner = Double.parseDouble(words[1]);
120
                                    break;
121
                                case "ulxmax":
122
                                    this.ulxmax = Double.parseDouble(words[1]);
123
                                    break;
124
                                case "ulymax":
125
                                    this.ulymax = Double.parseDouble(words[1]);
126
                                    break;
127
                                case "xdim":
128
                                    this.xdim = Double.parseDouble(words[1]);
129
                                    break;
130
                                case "ydim":
131
                                    this.ydim = Double.parseDouble(words[1]);
132
                                    break;
133
                                case "bandrowbytes":
134
                                    this.bandrowbytes = Integer.parseInt(words[1]);
135
                                    break;
136
                                case "totalrowbytes":
137
                                    this.totalrowbytes = Integer.parseInt(words[1]);
138
                                    break;
139
                                case "bandgapbytes":
140
                                    this.bandgapbytes = Integer.parseInt(words[1]);
141
                                    break;
142
                            }
143
                        } catch (NumberFormatException e) {
144
                            logger.warn("Can't parse keyword '" + words[0]
145
                                    + "' (value='" + words[1]
146
                                    + "', lineno=+" + lineno
147
                                    + ", file=" + f.getAbsoluteFile()
148
                                    + ").", e);
149
                        }
150
                        lineno++;
151
                    }
152
                    this.fixValues();
153
                }
154

  
155
            } catch (IOException e) {
156
                logger.warn("Couldn't read " + FILE_EXTENSION + " file (" + f.getAbsoluteFile() + ")", e);
157
                throw e;
158
            } catch (Exception e) {
159
                String msg = "Couldn't read " + FILE_EXTENSION + " file (" + f.getAbsoluteFile() + ")";
160
                logger.warn(msg, e);
161
                throw new IOException(msg, e);
162
            }
163
        }
164
    }
165

  
166
    private void fixValues() {
167
        @SuppressWarnings("LocalVariableHidesMemberVariable")
168
        boolean valid = true;
169

  
170
        if (this.pixeltype != null) {
171
            switch (this.pixeltype) {
172
                case "signedint":
173
                    this.nbits = 32;
174
                    break;
175
                case "byte_unsigned":
176
                    this.nbits = 8;
177
                    break;
178
            }
179
        }
180

  
181
        switch (this.getNbits()) {
182
            case 1:
183
            case 4:
184
                logger.warn("nbits keyword has a non supported value of " + this.getNbits() + " (" + this.source.getAbsolutePath() + ").");
185
                valid = false;
186
                break;
187
            case 8:
188
            case 16:
189
            case 32:
190
            case 64:
191
                break;
192
            default:
193
                logger.warn("nbits keyword has an invalid value of " + this.getNbits() + " (" + this.source.getAbsolutePath() + ").");
194
                this.nbits = 1;
195
                valid = false;
196
                break;
197
        }
198

  
199
        switch (this.getLayout()) {
200
            case LAYOUT_BIL:
201
            case LAYOUT_BIP:
202
            case LAYOUT_BSQ:
203
                break;
204
            default:
205
                logger.warn("layout keyword has an invalid value of '" + this.getLayout() + "' (" + this.source.getAbsolutePath() + ").");
206
                this.layout = LAYOUT_BIL;
207
                valid = false;
208
                break;
209
        }
210

  
211
        if( StringUtils.isEmpty(this.byteorder_word) ) {
212
            this.byteorder = ByteOrder.nativeOrder();
213
            if( this.byteorder == ByteOrder.BIG_ENDIAN ) {
214
                this.byteorder_word = "msbfirst";
215
            } else {
216
                this.byteorder_word = "lsbfirst";
217
            }
218
        } else {
219
            switch(this.byteorder_word) {
220
                case "lsbfirst":
221
                case "little_endian":
222
                case "littleendian":
223
                case "intel":
224
                case "i":
225
                    this.byteorder = ByteOrder.LITTLE_ENDIAN;
226
                    break;
227
                case "motorola":
228
                case "m":
229
                case "msbfirst":
230
                case "big_endian":
231
                case "bigendian":
232
                    this.byteorder = ByteOrder.BIG_ENDIAN;
233
                    break;
234
                default:
235
                    logger.warn("byteorder keyword has an invalid value of '" + this.byteorder_word + "' (" + this.source.getAbsolutePath() + ").");
236
                    this.byteorder = ByteOrder.nativeOrder();
237
            }
238
        }
239
        if (this.getNrows() == NONE || this.getNcols() == NONE) {
240
            logger.warn("nrows/ncols keywords has an invalid value of '" + this.getNrows() + "/" + this.getNcols() + "' (" + this.source.getAbsolutePath() + ").");
241
            valid = false;
242
        }
243
        if (this.getUlymax() == NONE) {
244
            this.ulymax = this.getNrows() - 1;
245
        }
246
        if (this.getBandrowbytes() == NONE) {
247
            this.bandrowbytes = (this.getNcols() * this.getNbits()) / 8;
248
        }
249
        switch (this.getLayout()) {
250
            case LAYOUT_BIL:
251
                if (this.getTotalrowbytes() == NONE) {
252
                    this.totalrowbytes = this.getNbands() * this.getBandrowbytes();
253
                }
254
                this.bandgapbytes = 0;
255
                break;
256
            case LAYOUT_BIP:
257
                if (this.getTotalrowbytes() == NONE) {
258
                    this.totalrowbytes = (this.getNcols() * this.getNbands() * this.getNbits()) / 8;
259
                }
260
                this.bandgapbytes = 0;
261
                break;
262
            case LAYOUT_BSQ:
263
                if (this.getTotalrowbytes() == NONE) {
264
                    this.totalrowbytes = 0;
265
                }
266
                if (this.getBandgapbytes() == NONE) {
267
                    this.bandgapbytes = 0;
268
                }
269
                break;
270
        }
271
        this.valid = valid;
272
    }
273

  
274
    @Override
275
    public int getDataType() {
276
        if (this.pixeltype != null) {
277
            switch (this.pixeltype) {
278
                case "signedint":
279
                    return BufferManager.TYPE_INT;
280
                case "byte_unsigned":
281
                    return BufferManager.TYPE_BYTE;
282
            }
283
        }
284
        switch (this.getNbits()) {
285
            case 1:
286
            case 4:
287
            default:
288
                return BufferManager.TYPE_UNDEFINED;
289
            case 8:
290
                return BufferManager.TYPE_BYTE;
291
            case 16:
292
                return BufferManager.TYPE_USHORT;
293
            case 32:
294
                return BufferManager.TYPE_INT;
295
            case 64:
296
                return BufferManager.TYPE_DOUBLE;
297
        }
298
    }
299

  
300
    @Override
301
    public void write(File file) throws IOException {
302
        File f = this.getFile(file);
303
        StringBuilder builder = new StringBuilder();
304
        builder.append("; ").append(this.comments).append("\n");
305
        builder.append("nrows ").append(this.nrows).append("\n");
306
        builder.append("ncols ").append(this.ncols).append("\n");
307
        builder.append("nbands ").append(this.nbands).append("\n");
308
        builder.append("nbits ").append(this.nbits).append("\n");
309
        builder.append("layout ").append(LAYOUT_BSQ).append("\n");
310
        builder.append("byteorder ").append(this.byteorder_word).append("\n");
311
        builder.append("ulxmax ").append(this.ulxmax).append("\n");
312
        builder.append("ulymax ").append(this.ulymax).append("\n");
313
        builder.append("ydim ").append(this.ydim).append("\n");
314
        builder.append("xdim ").append(this.xdim).append("\n");
315
        
316
        FileUtils.write(file, builder.toString());
317
    }
318
    
319
    @Override
320
    public void setByteOrder(ByteOrder order) {
321
        this.byteorder = order;
322
        if( order == ByteOrder.BIG_ENDIAN ) {
323
            this.byteorder_word = "msbfirst";
324
        } else {
325
            this.byteorder_word = "lsbfirst";
326
        }
327
    }
328
    
329
    @Override
330
    public void setNBands(int nbands) {
331
        this.nbands = nbands;
332
    }
333
    
334
    @Override
335
    public void setDataType(int dataType) {
336
        switch(dataType) {
337
            case BufferManager.TYPE_BYTE:
338
                this.nbits = 8;
339
                break;
340
            case BufferManager.TYPE_USHORT:
341
                this.nbits = 16;
342
                break;
343
            case BufferManager.TYPE_INT:
344
                this.nbits = 32;
345
                break;
346
            case BufferManager.TYPE_DOUBLE:
347
                this.nbits = 64;
348
                break;
349
            default:
350
                throw new IllegalArgumentException("The data type "+dataType +" is not supported");
351
        }
352
        
353
    }
354
    
355
    @Override
356
    public void setDimensions(int nrows, int ncols, Envelope envelope) {
357
        this.ncols = ncols;
358
        this.nrows = nrows;
359

  
360
        Point min = envelope.getLowerCorner();
361
        Point max = envelope.getUpperCorner();
362
        
363
        this.ulxmax = min.getX();
364
        this.ulymax = max.getY();
365
        this.ydim = (min.getY() - max.getY()) / this.nrows;
366
        this.xdim = (max.getX() - min.getX()) / this.ncols;
367
    }
368
    
369
    @Override
370
    public Envelope getEnvelope() {
371
        Envelope envelope = null;
372
        try {
373
            if (!Double.isNaN(this.xllcorner)
374
                    && !Double.isNaN(this.yllcorner)
375
                    && this.cellsize > 0) {
376
                envelope = GeometryLocator.getGeometryManager().createEnvelope(
377
                        this.xllcorner,
378
                        this.yllcorner,
379
                        this.xllcorner + (this.ncols * this.cellsize),
380
                        this.yllcorner + (this.nrows * this.cellsize),
381
                        Geometry.SUBTYPES.GEOM2D);
382
            }
383
            if (envelope == null && !Double.isNaN(this.ulxmax)
384
                    && !Double.isNaN(this.ulymax)
385
                    && !Double.isNaN(this.xdim)
386
                    && !Double.isNaN(this.ydim)) {
387
                envelope = GeometryLocator.getGeometryManager().createEnvelope(
388
                        this.ulxmax,
389
                        this.ulymax - (this.nrows * this.ydim),
390
                        this.ulxmax + (this.ncols * this.xdim),
391
                        this.ulymax,
392
                        Geometry.SUBTYPES.GEOM2D);
393
            }
394
            if (envelope == null && !Double.isNaN(this.xdim)
395
                    && !Double.isNaN(this.ydim)) {
396
                envelope = GeometryLocator.getGeometryManager().createEnvelope(
397
                        0,
398
                        0,
399
                        this.ncols * this.xdim,
400
                        this.nrows * this.ydim,
401
                        Geometry.SUBTYPES.GEOM2D);
402
            }
403
            if (envelope == null && this.cellsize > 0) {
404
                envelope = GeometryLocator.getGeometryManager().createEnvelope(
405
                        0,
406
                        0,
407
                        this.ncols * this.cellsize,
408
                        this.nrows * this.cellsize,
409
                        Geometry.SUBTYPES.GEOM2D);
410
            }
411
            if (envelope == null) {
412
                envelope = GeometryLocator.getGeometryManager().createEnvelope(
413
                        0,
414
                        0,
415
                        this.ncols,
416
                        this.nrows,
417
                        Geometry.SUBTYPES.GEOM2D);
418
            }
419
        } catch (Exception ex) {
420
            logger.debug("Can't calculate envelope.", ex);
421
        }
422
        return envelope;
423
    }
424

  
425
    /**
426
     * @return the nrows
427
     */
428
    @Override
429
    public int getNrows() {
430
        return nrows;
431
    }
432

  
433
    /**
434
     * @return the ncols
435
     */
436
    @Override
437
    public int getNcols() {
438
        return ncols;
439
    }
440

  
441
    /**
442
     * @return the nbands
443
     */
444
    @Override
445
    public int getNbands() {
446
        return nbands;
447
    }
448

  
449
    /**
450
     * @return the nbits
451
     */
452
    @Override
453
    public int getNbits() {
454
        return nbits;
455
    }
456

  
457
    /**
458
     * @return the byteorder
459
     */
460
    @Override
461
    public ByteOrder getByteorder() {
462
        return byteorder;
463
    }
464

  
465
    /**
466
     * @return the layout
467
     */
468
    @Override
469
    public String getLayout() {
470
        return layout;
471
    }
472

  
473
    /**
474
     * @return the skipbytes
475
     */
476
    @Override
477
    public int getSkipbytes() {
478
        return skipbytes;
479
    }
480

  
481
    /**
482
     * @return the ulxmax
483
     */
484
    @Override
485
    public double getUlxmax() {
486
        return ulxmax;
487
    }
488

  
489
    /**
490
     * @return the ulymax
491
     */
492
    @Override
493
    public double getUlymax() {
494
        return ulymax;
495
    }
496

  
497
    /**
498
     * @return the xdim
499
     */
500
    @Override
501
    public double getXdim() {
502
        return xdim;
503
    }
504

  
505
    /**
506
     * @return the ydim
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff