svn-gvsig-desktop / tags / v1_1_Build_1010 / libraries / libCq CMS for java.old / src / org / cresques / io / TrkFile.java @ 12804
History | View | Annotate | Download (4.28 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 org.cresques.cts.ICoordTrans; |
27 |
import org.cresques.cts.IProjection; |
28 |
|
29 |
import org.cresques.px.IObjList; |
30 |
import org.cresques.px.PxObjList; |
31 |
import org.cresques.px.gml.LineString; |
32 |
|
33 |
import java.io.DataInputStream; |
34 |
import java.io.EOFException; |
35 |
import java.io.FileInputStream; |
36 |
import java.io.FileNotFoundException; |
37 |
import java.io.IOException; |
38 |
import java.io.InputStream; |
39 |
|
40 |
|
41 |
/**
|
42 |
* @author luisw
|
43 |
*/
|
44 |
public class TrkFile extends GeoFile { |
45 |
private DataInputStream is; |
46 |
long l = 0; |
47 |
LineString line; |
48 |
|
49 |
public TrkFile(IProjection proj, String name) { |
50 |
super(proj, name);
|
51 |
} |
52 |
|
53 |
/* (no Javadoc)
|
54 |
* @see org.cresques.io.GeoFile#load()
|
55 |
*/
|
56 |
public GeoFile load() {
|
57 |
try {
|
58 |
return load(new FileInputStream(name)); |
59 |
} catch (NumberFormatException e) { |
60 |
// TODO Bloque catch generado autom?ticamente
|
61 |
e.printStackTrace(); |
62 |
} catch (FileNotFoundException e) { |
63 |
// TODO Bloque catch generado autom?ticamente
|
64 |
e.printStackTrace(); |
65 |
} catch (Exception e) { |
66 |
// TODO Bloque catch generado autom?ticamente
|
67 |
e.printStackTrace(); |
68 |
} |
69 |
|
70 |
return this; |
71 |
} |
72 |
|
73 |
public GeoFile load(InputStream is) throws NumberFormatException, Exception { |
74 |
System.out.println("Trk: Cargando '" + name + "' ..."); |
75 |
this.is = new DataInputStream(is); |
76 |
line = new LineString();
|
77 |
|
78 |
while (readPoint()) {
|
79 |
l++; |
80 |
} |
81 |
|
82 |
extent.add(line.getExtent()); |
83 |
is.close(); |
84 |
System.out.println("Trk: '" + name + "' cargado. (" + l + " puntos)."); |
85 |
|
86 |
return this; |
87 |
} |
88 |
|
89 |
public boolean readPoint() { |
90 |
int h;
|
91 |
int m;
|
92 |
int s;
|
93 |
int sgn = 1; |
94 |
double D;
|
95 |
double M;
|
96 |
double S;
|
97 |
double lat;
|
98 |
double lng;
|
99 |
|
100 |
try {
|
101 |
h = is.readUnsignedByte(); |
102 |
m = is.readUnsignedByte(); |
103 |
s = is.readUnsignedByte(); |
104 |
is.skipBytes(1);
|
105 |
D = is.readUnsignedByte(); |
106 |
M = is.readByte(); |
107 |
S = is.readUnsignedShort(); |
108 |
S /= 1000.0;
|
109 |
|
110 |
if (M < 0) { |
111 |
sgn = -1;
|
112 |
M *= -1;
|
113 |
} else {
|
114 |
sgn = 1;
|
115 |
} |
116 |
|
117 |
lat = sgn * (D + ((M / 60D) + (S / 3600D))); |
118 |
D = is.readUnsignedByte(); |
119 |
M = is.readByte(); |
120 |
S = is.readUnsignedShort(); |
121 |
S /= 1000.0;
|
122 |
|
123 |
if (M < 0) { |
124 |
sgn = -1;
|
125 |
M *= -1;
|
126 |
} else {
|
127 |
sgn = 1;
|
128 |
} |
129 |
|
130 |
lng = sgn * (D + ((M / 60D) + (S / 3600D))); |
131 |
line.add(proj.createPoint(lng, lat)); |
132 |
} catch (EOFException eof) { |
133 |
return false; |
134 |
} catch (IOException e) { |
135 |
// TODO Bloque catch generado autom?ticamente
|
136 |
e.printStackTrace(); |
137 |
|
138 |
return false; |
139 |
} |
140 |
|
141 |
return true; |
142 |
} |
143 |
|
144 |
/* (no Javadoc)
|
145 |
* @see org.cresques.io.GeoFile#reProject(org.cresques.geo.ReProjection)
|
146 |
*/
|
147 |
public IObjList getObjects() {
|
148 |
IObjList oList = new PxObjList(proj);
|
149 |
oList.add(line); |
150 |
|
151 |
return oList;
|
152 |
} |
153 |
|
154 |
public void reProject(ICoordTrans rp) { |
155 |
line.reProject(rp); |
156 |
setProjection(rp.getPDest()); |
157 |
} |
158 |
|
159 |
/* (non-Javadoc)
|
160 |
* @see org.cresques.io.GeoFile#close()
|
161 |
*/
|
162 |
public void close() { |
163 |
// TODO Auto-generated method stub
|
164 |
} |
165 |
} |