Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / io / TrkFile.java @ 2312

History | View | Annotate | Download (3.53 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 * 
4
 * Copyright (C) 2004-5. 
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 * 
22
 * cresques@gmail.com
23
 */
24
package org.cresques.io;
25

    
26
import java.io.DataInputStream;
27
import java.io.EOFException;
28
import java.io.FileInputStream;
29
import java.io.FileNotFoundException;
30
import java.io.IOException;
31
import java.io.InputStream;
32

    
33
import org.cresques.cts.ICoordTrans;
34
import org.cresques.cts.IProjection;
35
import org.cresques.px.IObjList;
36
import org.cresques.px.PxObjList;
37
import org.cresques.px.gml.LineString;
38

    
39
/**
40
 * @author luisw
41
 */
42
public class TrkFile  extends GeoFile {
43

    
44
        private DataInputStream is;
45
        long l = 0;
46
        LineString line;
47

    
48
        public TrkFile(IProjection proj, String name) {
49
                super(proj, name);
50
        }
51
        /* (no Javadoc)
52
         * @see org.cresques.io.GeoFile#load()
53
         */
54
        public GeoFile load() {
55
                try {
56
                        return load(new FileInputStream(name));
57
                } catch (NumberFormatException e) {
58
                        // TODO Bloque catch generado autom?ticamente
59
                        e.printStackTrace();
60
                } catch (FileNotFoundException e) {
61
                        // TODO Bloque catch generado autom?ticamente
62
                        e.printStackTrace();
63
                } catch (Exception e) {
64
                        // TODO Bloque catch generado autom?ticamente
65
                        e.printStackTrace();
66
                }
67
                return this;
68
        }
69
        
70
        public GeoFile load(InputStream is) throws NumberFormatException, Exception {
71
                System.out.println("Trk: Cargando '"+name+"' ...");
72
                this.is = new DataInputStream(is);
73
                line = new LineString();
74
                while (readPoint()) {
75
                        l++;
76
                }
77
                extent.add(line.getExtent());
78
                is.close();
79
                System.out.println("Trk: '"+name+"' cargado. ("+l+" puntos).");
80
                return this;
81
        }
82
        
83
        public boolean readPoint() {
84
                int        h, m, s, sgn = 1;
85
                double        D, M, S;
86
                double lat, lng;
87
                try {
88
                        h = is.readUnsignedByte();
89
                        m = is.readUnsignedByte();
90
                        s = is.readUnsignedByte();
91
                        is.skipBytes(1);
92
                        D = is.readUnsignedByte();
93
                        M = is.readByte();
94
                        S = is.readUnsignedShort();
95
                        S /= 1000.0;
96
                        if (M<0) {
97
                                sgn = -1;
98
                                M *= -1;
99
                        } else sgn = 1;
100
                        lat = sgn * (D + (M/60D+S/3600D));
101
                        D = is.readUnsignedByte();
102
                        M = is.readByte();
103
                        S = is.readUnsignedShort();
104
                        S /= 1000.0;
105
                        if (M<0) {
106
                                sgn = -1;
107
                                M *= -1;
108
                        } else sgn = 1;
109
                        lng = sgn * (D + (M/60D+S/3600D));
110
                        line.add(proj.createPoint(lng, lat));
111
                } catch (EOFException eof) {
112
                        return false;
113
                } catch (IOException e) {
114
                        // TODO Bloque catch generado autom?ticamente
115
                        e.printStackTrace();
116
                        return false;
117
                }
118
                return true;
119
        }
120
        /* (no Javadoc)
121
         * @see org.cresques.io.GeoFile#reProject(org.cresques.geo.ReProjection)
122
         */
123
        public IObjList getObjects() {
124
                IObjList oList = new PxObjList(proj);
125
                oList.add(line);
126
                return oList;
127
        }
128
        public void reProject(ICoordTrans rp) {
129
                line.reProject(rp);
130
                setProjection(rp.getPDest());
131
        }
132
        /* (non-Javadoc)
133
         * @see org.cresques.io.GeoFile#close()
134
         */
135
        public void close() {
136
                // TODO Auto-generated method stub
137
                
138
        }
139

    
140

    
141
}