Statistics
| Revision:

root / trunk / libraries / libCq CMS for java.old / src / org / cresques / io / MdtFile.java @ 9

History | View | Annotate | Download (11.4 KB)

1
/*
2
 * Created on 04-may-2004
3
 */
4
package org.cresques.io;
5

    
6
import java.io.BufferedReader;
7
import java.io.File;
8
import java.io.FileReader;
9
import java.io.IOException;
10
import java.io.InputStreamReader;
11
import java.io.Reader;
12
import java.util.Date;
13
import java.util.Vector;
14
import java.util.zip.ZipEntry;
15

    
16
import java.awt.Color;
17
import java.awt.Image;
18
import java.awt.Point;
19
import java.awt.geom.Point2D;
20
import java.awt.image.BufferedImage;
21

    
22
import org.cresques.geo.GeoPoint;
23
import org.cresques.geo.Geodetic;
24
import org.cresques.geo.ProjPoint;
25
import org.cresques.geo.Projection;
26
import org.cresques.geo.ReProjection;
27
import org.cresques.px.Extent;
28

    
29
class CoordList extends Vector {
30
        
31
}
32

    
33
/**
34
 * Soporte para modelos digitales de terreno.
35
 * 
36
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
37
 */
38

    
39
public class MdtFile extends GeoRasterFile {
40
        private String name;
41
        BufferedReader fi;
42
        String buf = null, token = null;
43
        
44
        double lastLat, lastLong;
45
        double distX, distY;
46
//        double minZ = -3500.0, maxZ = 3500.0;
47
        double minZ = 100000.0, maxZ = -100000.0;
48
        int width = 0, height = 0;
49
        long numPt = 0;
50

    
51
        MdtPalette paleta = null;
52
        BufferedImage im = null;
53
        Extent v = null;
54
        
55
        Projection projGeo = null;
56
        private Date reloj = new Date();
57
        
58
        /**
59
         * Constructor de la clase MdtFile
60
         *
61
         */
62
        
63
        public MdtFile(Projection proj, String name) {
64
                super(proj);
65
                this.name = name;
66
                if (!(proj instanceof Geodetic))
67
                        projGeo = Geodetic.getProjection(proj.getEllipsoid());
68
        }
69

    
70
        /**
71
         * Carga un .gml
72
         * @param name nombre del fichero
73
         */
74
        
75
        public GeoFile load() {
76
                System.out.println("MDT: Intento de carga de '" + name + "'.");
77
                long t1, t2;
78
                try {
79
                        if (FileFolder.isUrl(name)) {
80
                                ZipFileFolder zFolder = new ZipFileFolder(name);
81
                                ZipEntry ze = zFolder.getZipEntry(name);
82
                                fileSize = ze.getSize();
83
                                
84
                                // El primero calcula el tama?o, el segundo la imagen
85
                                t1 = getTime();
86
                                load2(new InputStreamReader(zFolder.getInputStream(ze)));
87
                                t2 = getTime();
88
                                System.out.println("MDT: primer load: " + (t2-t1)/1000 + " seg.");
89
                                t1 = getTime();
90
                                load2(new InputStreamReader(zFolder.getInputStream(ze)));
91
                                t2 = getTime();
92
                                System.out.println("MDT: segundo load: " + (t2-t1)/1000 + " seg.");
93
                        } else {
94
                                File f = new File(name);
95
                                fileSize = f.length();
96
                                
97
                                // El primero calcula el tama?o, el segundo la imagen
98
                                t1 = getTime();
99
                                load2(new FileReader(name));
100
                                t2 = getTime();
101
                                System.out.println("MDT: primer load: " + (t2-t1)/1000 + " seg.");
102
                                t1 = getTime();
103
                                load2(new FileReader(name));
104
                                t2 = getTime();
105
                                System.out.println("MDT: segundo load: " + (t2-t1)/1000 + " seg.");
106
                        }
107
                        v = new Extent(extent);
108
                        return this;
109
                } catch (Exception e) {
110
                        System.err.println("MDT: ERROR: "+lineCnt+" lineas leidas ("+bytesReaded+"/"+fileSize+") bytes.");
111
                        if (e instanceof java.lang.ArrayIndexOutOfBoundsException)
112
                                System.err.println("     row = "+rowAct+", col ="+colAct); 
113
                        e.printStackTrace();
114
                }
115
                return this;
116
        }
117

    
118
//
119
///
120
//// Carga antigua. TODO Mejorarla
121
///
122
//
123
        
124
        public GeoFile load(Reader fr) throws IOException {
125
                lineCnt = 0;
126
                if (im == null){
127
                        System.out.println("MDT: Cargando "+name+" ...");
128
                } else
129
                        System.out.println("MDT: generando imagen ...");
130
                fi = new BufferedReader(fr);
131
                while ((buf = fi.readLine()) != null) {
132
                          bytesReaded += buf.length()+1;
133
                          parseLine(buf);
134
                          lineCnt++;
135
                }
136
                fi.close();
137
                if (im == null) {
138
                          width = (int) numPt/height;
139
                          System.out.println("MDT: '"+name+"' cargado. ("+lineCnt+" l?neas).");
140
                          System.out.println("MDT: Puntos="+numPt+
141
                                  ",  size="+width+","+height+").");
142
                          System.out.println("MDT: extent="+extent);
143
                          createImage();
144
                          System.out.println("MDT: dist = ("+distX+
145
                                  ","+distY+").");
146
                          System.out.println("MDT: elevaciones : "+minZ+
147
                                  " < z < "+maxZ+".");
148
                          String pName = "E:/data/GLOBE_DEM/data/elev/esri/clr/"+"c10g.clr";//+"lw.clr";
149
                          //paleta = (new MdtPalette()).loadClr(pName, true);
150
                          //paleta = MdtPalette.atlas;
151
                          if (paleta == null)
152
                                  paleta = (new MdtPalette()).createGrayScale((int)minZ, (int) maxZ);
153
                          }
154
                return this;
155
        }
156
        
157
        public void parseLine(String buf) {
158
                  //  359.5166  40.0039    787.00
159
                  Point2D pt = null;
160
                  GeoPoint gPt = null;
161
                  buf = filterWS(buf);
162
                  if (buf.length() > 4) {
163
                          String [] datos = buf.split(" ");
164
                          if (datos.length<3) return;
165
                          double lng = Double.parseDouble(datos[0]);
166
                          if (lng > 180.0 && lng < 360.0) lng = -(360.0-lng);
167
                          double lat = Double.parseDouble(datos[1]);
168
                          double z = Double.parseDouble(datos[2])+.99;
169
                          if (im == null) {
170
                                  if (token == null) token = datos[1];
171
                                  if (token.compareTo(datos[1]) == 0) height++;
172
                                  lastLat = lat; lastLong = lng;
173
                                  numPt ++;
174
                                  if (projGeo == null) {
175
                                          pt = proj.createPoint(lng, lat);
176
                                  } else {
177
                                          gPt = (GeoPoint) projGeo.createPoint(lng, lat);
178
                                          pt = (ProjPoint) proj.createPoint(0.0, 0.0);
179
                                          pt = proj.fromGeo(gPt, (ProjPoint) proj.createPoint(0.0, 0.0));
180
                                  }
181
                                  extent.add(pt);
182
                                  minZ = Math.min(minZ, z);
183
                                  maxZ = Math.max(maxZ, z);
184
                                  //System.out.println(buf+" "+pt);
185
                          } else {
186
                                  if (projGeo != null) {
187
                                          gPt = (GeoPoint) projGeo.createPoint(lng, lat);
188
                                          pt = (ProjPoint) proj.createPoint(0.0, 0.0);
189
                                          pt = proj.fromGeo(gPt, (ProjPoint) proj.createPoint(0.0, 0.0));
190
                                          lng = pt.getX(); lat = pt.getY();
191
                                  }
192
                                  set(lng, lat, z);
193
                          }
194
                  }
195
        }
196
        
197
        public void createImage() {
198
                  distX = (extent.width()/(height-1)); distY = (extent.height()/(width-1));
199
                  System.out.println("MDT: extent.size=("+extent.width()+","+extent.height()+")");
200
                  im = new BufferedImage(height, width, BufferedImage.TYPE_3BYTE_BGR);
201
        }
202
          
203
//
204
///
205
////  Carga Nueva TODO Acabar de dejar fina
206
///
207
//
208
        
209
        public GeoFile load2(Reader fr) throws IOException {
210
                lineCnt = 0;
211
                if (im == null){
212
                        System.out.println("MDT: Cargando "+name+" ...");
213
                        xCoord = new Vector();
214
                        yCoord = new Vector();
215
                        rows = new Vector();
216
                        firstRow = new Vector();
217
                        row = null;
218
                        numCols = 0;
219
                        colAct = 0;
220
                } else
221
                        return this;
222
                        //System.out.println("MDT: generando imagen ...");
223
                fi = new BufferedReader(fr);
224
                while ((buf = fi.readLine()) != null) {
225
                        bytesReaded += buf.length()+1;
226
                        parseLine2(buf);
227
                        lineCnt++;
228
                }
229
                fi.close();
230
                if (im == null) {
231
                        width = (int) numPt/height;
232
                        System.out.println("MDT: '"+name+"' cargado. ("+lineCnt+" l?neas).");
233
                        System.out.println("MDT: Puntos="+numPt+
234
                                ",  size="+width+","+height+").");
235
                        System.out.println("MDT: extent="+extent);
236
                        createImage2();
237
                        System.out.println("MDT: dist = ("+distX+
238
                                ","+distY+").");
239
                        System.out.println("MDT: elevaciones : "+minZ+
240
                                " < z < "+maxZ+".");
241
                        String pName = "E:/data/GLOBE_DEM/data/elev/esri/clr/"+"c10g.clr";//+"lw.clr";
242
                        //paleta = (new MdtPalette()).loadClr(pName, true);
243
                        //paleta = MdtPalette.atlas;
244
                        if (paleta == null)
245
                                paleta = (new MdtPalette()).createGrayScale((int)minZ, (int) maxZ);
246
                        }
247
                return this;
248
        }
249
        
250
        Vector xCoord = new Vector();
251
        Vector yCoord = new Vector();
252
        Vector rows = new Vector();
253
        Vector firstRow = new Vector();
254
        float [] row = null;
255
        int numCols = 0;
256
        int rowAct = 0, colAct = 0;
257
        
258
        public void parseLine2(String buf) {
259
                //  359.5166  40.0039    787.00
260
                Point2D pt = null;
261
                GeoPoint gPt = null;
262
                buf = filterWS(buf);
263
                if (buf.length() > 4) {
264
                        String [] datos = buf.split(" ");
265
                        if (datos.length<3) return;
266
                        //System.out.println("1: "+lineCnt+","+numCols);
267
                        double lng = Double.parseDouble(datos[0]);
268
                        if (lng > 180.0 && lng < 360.0) lng = -(360.0-lng);
269
                        double lat = Double.parseDouble(datos[1]);
270
                        float z = Float.parseFloat(datos[2]);
271
                        if (im == null) {
272
                                if (token == null) {
273
                                        token = datos[1];
274
                                        yCoord.add(new Double(lat));
275
                                } else if (token.compareTo(datos[1]) != 0) {
276
                                        token = datos[1];
277
                                        if (height == 0) {
278
                                                System.out.println("MDT: Width: "+numCols);
279
                                                row = new float[numCols];
280
                                                for (int i=0; i<numCols; i++)
281
                                                        row[i] = ((Float) firstRow.get(i)).floatValue();
282
                                                firstRow = null;
283
                                        }
284
                                        height++; rowAct++;
285
                                        yCoord.add(new Double(lat));
286
                                        rows.add(row);
287
                                        row = new float[numCols];
288
                                        colAct = 0;
289
                                } 
290
                                if (firstRow != null) {
291
                                        //System.out.println("2: "+lineCnt+","+numCols);
292
                                        xCoord.add(new Double(lng));
293
                                        firstRow.add(new Float(z));
294
                                        numCols++;
295
                                } else {
296
                                        row[colAct] = z;
297
                                        colAct++;
298
                                }
299
                                lastLat = lat; lastLong = lng;
300
                                numPt ++;
301
                                //Point2D pt = new Point2D.Double(lng, lat);
302
                                if (projGeo == null) {
303
                                        pt = proj.createPoint(lng, lat);
304
                                } else {
305
                                        gPt = (GeoPoint) projGeo.createPoint(lng, lat);
306
                                        pt = (ProjPoint) proj.createPoint(0.0, 0.0);
307
                                        pt = proj.fromGeo(gPt, (ProjPoint) proj.createPoint(0.0, 0.0));
308
                                }
309
                                extent.add(pt);
310
                                minZ = Math.min(minZ, z);
311
                                maxZ = Math.max(maxZ, z);
312
                                //System.out.println(buf+" "+pt);
313
                        } else {
314
                                if (projGeo != null) {
315
                                        gPt = (GeoPoint) projGeo.createPoint(lng, lat);
316
                                        pt = (ProjPoint) proj.createPoint(0.0, 0.0);
317
                                        pt = proj.fromGeo(gPt, (ProjPoint) proj.createPoint(0.0, 0.0));
318
                                        lng = pt.getX(); lat = pt.getY();
319
                                }
320
                                set(lng, lat, z);
321
                        }
322
                }
323
        }
324
        public void createImage2() {
325
                distX = (extent.width()/(width-1)); distY = (extent.height()/(height-1));
326
                System.out.println("MDT: extent.size=("+extent.width()+","+extent.height()+")");
327
                im = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
328
                for (int y=0; y<height; y++) {
329
                        float [] rowAct = (float []) rows.get(y);
330
                        for (int x=0; x<numCols; x++) {
331
                                im.setRGB(x, y,  paleta.getRGB(rowAct[x]));
332
                        }
333
                }        
334
        }
335

    
336
        private double getZ(double lng, double lat) {
337
                double z = 0.0;
338
                return z;
339
        }
340

    
341
//
342
///
343
//// Continua ...
344
///
345
//
346
        
347
        private void set(double lng, double lat, double z) {
348
                new Color(0,0,0);
349
                Point pt = toImageCoord(lng, lat);
350
                //int x = (int) ((lng-extent.minX()+(distX/2))/distX);
351
                //int y = (int) ((extent.maxY()-lat+(distY/2))/distY);
352
                //int lum = (int)(256.0*(1.0-(maxZ-z)/(maxZ-minZ)));
353
                int color = paleta.getRGB(z); //lum*256*256 + lum*256 + lum;
354
                //System.out.println("("+lng+","+lat+","+z+": "+x+","+y+","+color % 256+")");
355
                //im.setRGB(x, y, color);
356
                colAct = (int) pt.getX();
357
                rowAct = (int) pt.getY();
358
                im.setRGB( colAct, rowAct, color);
359
        }
360
        
361
        private Point toImageCoord(double lng, double lat) {
362
                Point pt = new Point();
363
                pt.setLocation( (int) ((lng-extent.minX()+(distX/2.0))/distX),
364
                        (int) ((extent.maxY()-lat+(distY/2.0))/distY));
365
                return pt;
366
        }
367
        
368
        public void setPalette(MdtPalette pal) {
369
                paleta = pal;
370
        }
371

    
372
        public void reProject(ReProjection rp) {
373
                // TODO Auto-generated method stub
374
        }
375

    
376
        public void setView(Extent e) { v = new Extent(e); }
377
        public Extent getView() { return v; }
378

    
379
        public void setTransparency(boolean t) {
380
                // TODO Auto-generated method stub
381
        }
382

    
383
        public Image updateImage(int w, int h) {
384
                Image mdtImage = null;
385
                double dFileAspect, dWindowAspect;
386

    
387
                // Work out the correct aspect for the setView call.
388
                dFileAspect = (double)v.width()/(double)v.height();
389
                dWindowAspect = (double)w /(double)h;
390

    
391
                /*if (dFileAspect > dWindowAspect) {
392
                  h =(int)((double)w/dFileAspect);
393
                } else {
394
                  w = (int)((double)h*dFileAspect);
395
                }*/
396
                mdtImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
397
                double dX = v.width()/(double) w;
398
                double dY = v.height()/(double) h;
399
                int color = 0;
400
                Point pt;
401
                for (int y=0; y<h; y++) {
402
                        for (int x=0; x<w; x++) {
403
                                pt = toImageCoord(v.minX()+(x*dX), v.maxY()-(y*dY));
404
                                color = im.getRGB((int) pt.getX(), (int)pt.getY());
405
                                ((BufferedImage) mdtImage).setRGB(x, y, color);
406
                        }
407
                }
408

    
409
                //mdtImage = im.getScaledInstance(w, h, 0);
410

    
411
                return mdtImage;
412
        }
413
}