Revision 2669 branches/CqCMSDvp/libraries/libCq CMS for java.old/src/org/cresques/io/TrkFile.java
TrkFile.java | ||
---|---|---|
1 | 1 |
/* |
2 | 2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications. |
3 |
* |
|
4 |
* Copyright (C) 2004-5. |
|
5 | 3 |
* |
4 |
* Copyright (C) 2004-5. |
|
5 |
* |
|
6 | 6 |
* This program is free software; you can redistribute it and/or |
7 | 7 |
* modify it under the terms of the GNU General Public License |
8 | 8 |
* as published by the Free Software Foundation; either version 2 |
... | ... | |
18 | 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
19 | 19 |
* |
20 | 20 |
* For more information, contact: |
21 |
*
|
|
21 |
* |
|
22 | 22 |
* cresques@gmail.com |
23 | 23 |
*/ |
24 | 24 |
package org.cresques.io; |
25 | 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 |
|
|
26 | 33 |
import java.io.DataInputStream; |
27 | 34 |
import java.io.EOFException; |
28 | 35 |
import java.io.FileInputStream; |
... | ... | |
30 | 37 |
import java.io.IOException; |
31 | 38 |
import java.io.InputStream; |
32 | 39 |
|
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 | 40 |
|
39 | 41 |
/** |
40 | 42 |
* @author luisw |
41 | 43 |
*/ |
42 |
public class TrkFile extends GeoFile { |
|
44 |
public class TrkFile extends GeoFile { |
|
45 |
private DataInputStream is; |
|
46 |
long l = 0; |
|
47 |
LineString line; |
|
43 | 48 |
|
44 |
private DataInputStream is;
|
|
45 |
long l = 0;
|
|
46 |
LineString line;
|
|
49 |
public TrkFile(IProjection proj, String name) {
|
|
50 |
super(proj, name);
|
|
51 |
}
|
|
47 | 52 |
|
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 |
} |
|
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 |
} |
|
139 | 69 |
|
70 |
return this; |
|
71 |
} |
|
140 | 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 |
} |
|
141 | 165 |
} |
Also available in: Unified diff