Statistics
| Revision:

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

History | View | Annotate | Download (8.68 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.util.Hashtable;
27

    
28
import java.io.FileReader;
29
import java.io.BufferedReader;
30
import java.io.FileNotFoundException;
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.io.InputStreamReader;
34
import java.io.Reader;
35

    
36
import org.cresques.cts.ICoordTrans;
37
import org.cresques.cts.IProjection;
38

    
39
import org.cresques.px.IObjList;
40
import org.cresques.px.gml.Feature;
41
import org.cresques.px.gml.FeatureCollection;
42
import org.cresques.px.gml.Geometry;
43
import org.cresques.px.gml.LineString;
44
import org.cresques.px.gml.MultiGeometry;
45
import org.cresques.px.gml.MultiPolygon;
46
import org.cresques.px.gml.Polygon;
47
import org.cresques.px.gml.Point;
48

    
49
/**
50
 * Cargador de ficheros .gml de JUMP de vivid solutions.
51
 * Actualmente lee un subconjunto de datos. Datos de prueba generados por JUMP.
52
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
53
 */
54

    
55
public class GmlFile extends GeoFile {
56
        final static int NOWHERE                = -1;
57
        final static int INCOORDINATES        = 0;
58
        final static int INPROPERTY                = 1;
59

    
60
        int where = NOWHERE;
61
        Hashtable tipos = new Hashtable();
62
        
63
        final static String LT="<", GT=">";
64

    
65
        FeatureCollection collection = null;
66
        Feature activeFeature = null;
67
        Geometry activeGeometry = null;
68
        Geometry activeMember = null;
69

    
70
        String buf = null;
71
        String propName = null;
72
        long lineNr = 0;
73
        
74
        BufferedReader fi;
75
        long l = 0;
76
        int count = 0;
77
        
78
        /**
79
         * Constructor de la clase Gml
80
         *
81
         */
82
        
83
        public GmlFile(IProjection proj, String name) {
84
                super(proj, name);
85
        }
86

    
87
        /**
88
         * Carga un .gml
89
         * @param name nombre del fichero
90
         */
91
        
92
        public GeoFile load() {
93
                System.out.println("Cargando "+name+" ...");
94
                try {
95
                        if (FileFolder.isUrl(name)) {
96
                                ZipFileFolder zFolder = new ZipFileFolder(name);
97
                                InputStream is = zFolder.getInputStream(name);
98
                                return load(new InputStreamReader(is));
99
                        } else 
100
                                return load(new FileReader(name));
101
                } catch (FileNotFoundException e) {
102
                        e.printStackTrace();
103
                } catch (IOException ie) {
104
                        System.err.println("ERROR."+l+"lineas leidas");
105
                        ie.printStackTrace();
106
                }
107
                return this;
108
        }
109
        
110
        public GeoFile load(Reader fr) {
111
                try {
112
                        fi = new BufferedReader(fr);
113
                        while ((buf = fi.readLine()) != null) {
114
                                //System.out.println(buf);
115
                                l++;
116
                                preParseLine(buf.trim());
117
                        }
118
                        fi.close();
119
                        System.out.println("'"+name+"' cargado. ("+l+" l?neas).");
120
                } catch (FileNotFoundException e) {
121
                        e.printStackTrace();
122
                } catch (IOException ie) {
123
                        System.err.println("ERROR."+l+"lineas leidas");
124
                        ie.printStackTrace();
125
                }
126
                this.lineNr = l;
127
                extent.add(collection.getExtent());
128
                return this;
129
        }
130
        
131
        /**
132
         * Analiza una l?nea del fichero .gml
133
         * @param l linea que analiza
134
         */
135
        
136
        void preParseLine(String l) {
137
                String line1;
138
                //System.err.println("pre0: '"+l+"'");
139
                //if (count++>500) System.exit(1);
140

    
141
                if (l.indexOf(LT) > 0) {
142
                        line1 = l.substring(0, l.indexOf(LT)).trim();
143
                        l = l.substring(l.indexOf(LT)).trim();
144
                        //System.err.println("pre1: '"+line1+"', '"+l+"'");
145
                        preParseLine(line1);
146
                        preParseLine(l);
147
                        return;
148
                }
149
                if (l.indexOf(GT) >= 0 && l.indexOf(GT) != l.length()-1) {
150
                        line1 = l.substring(0, l.indexOf(GT)+1).trim();
151
                        l = l.substring(l.indexOf(GT)+1).trim();
152
                        //System.err.println("pre2: '"+line1+"', '"+l+"'");
153
                        preParseLine(line1);
154
                        preParseLine(l);
155
                        return;
156
                }
157
                parseToken(l);
158
        }
159
        
160
        /**
161
         * Analiza un token
162
         * @param l 
163
         */
164
        
165
        void parseToken(String l) {
166
                String token = null;
167

    
168
                //System.err.println(l);
169
                if (l.length()>0 && l.substring(0,1).compareTo(LT) == 0) {
170
                        token = l.substring(1,l.indexOf(GT));
171
                        if (token.compareTo("gml:coordinates") == 0) {
172
                                where = INCOORDINATES;
173
                        } else if (token.compareTo("/gml:coordinates") == 0) {
174
                                where = NOWHERE;
175
                        } else if (token.compareTo("gml:LinearRing") == 0) {
176
                        } else if (token.compareTo("/gml:LinearRing") == 0) {
177
                        } else if (token.compareTo("gml:Polygon") == 0) {
178
                                activeMember = new Polygon();
179
                                if (activeGeometry == null)
180
                                        activeGeometry = activeMember;
181
                        } else if (token.compareTo("/gml:Polygon") == 0) {
182
                                if (activeGeometry.getClass() == MultiPolygon.class)
183
                                        ((MultiPolygon) activeGeometry).add(activeMember);
184
                                activeMember = null;
185
                        } else if (token.compareTo("gml:LineString") == 0) {
186
                                activeMember = new LineString();
187
                                if (activeGeometry == null)
188
                                        activeGeometry = activeMember;
189
                        } else if (token.compareTo("/gml:LineString") == 0) {
190
                                if (activeGeometry.getClass() == MultiGeometry.class)
191
                                        ((MultiGeometry) activeGeometry).add(activeMember);
192
                                activeMember = null;
193
                        } else if (token.compareTo("gml:outerBoundaryIs") == 0) {
194
                        } else if (token.compareTo("/gml:outerBoundaryIs") == 0) {
195
                        } else if (token.compareTo("featureCollection") == 0) {
196
                                collection = new FeatureCollection(proj);
197
                        } else if (token.compareTo("/featureCollection") == 0) {
198
                        } else if (token.compareTo("feature") == 0) {
199
                                System.err.println("feature");
200
                                activeFeature = new Feature();
201
                        } else if (token.compareTo("/feature") == 0) {
202
                                collection.add(activeFeature);
203
                                activeFeature = null;
204
                        } else if (token.compareTo("geometry") == 0) {
205
                        } else if (token.compareTo("/geometry") == 0) {
206
                                activeFeature.setGeometry(activeGeometry);
207
                                activeGeometry = null;
208
                                activeMember = null;
209
                        } else if (token.compareTo("gml:Point") == 0) {
210
                                activeMember = new Point();
211
                                if (activeGeometry == null)
212
                                        activeGeometry = activeMember;
213
                        } else if (token.compareTo("/gml:Point") == 0) {
214
                                activeMember = null;
215
                        } else if (token.compareTo("gml:MultiPolygon") == 0) {
216
                                activeGeometry = new MultiPolygon();
217
                        } else if (token.compareTo("/gml:MultiPolygon") == 0) {
218
                        } else if (token.compareTo("gml:MultiPoint") == 0) {
219
                                //activeGeometry = new MultiPoint();
220
                        } else if (token.compareTo("/gml:MultiPoint") == 0) {
221
                        } else if (token.compareTo("gml:polygonMember") == 0) {
222
                        } else if (token.compareTo("/gml:polygonMember") == 0) {
223
                        } else if (token.compareTo("gml:innerBoundaryIs") == 0) {
224
                                ((Polygon) activeMember).setInnerBoundary();
225
                        } else if (token.compareTo("/gml:innerBoundaryIs") == 0) {
226
                        } else if (token.startsWith("property")) {
227
                                where = INPROPERTY;
228
                                propName = l.substring(l.indexOf("\"")+1);
229
                                propName = propName.substring(0,propName.indexOf("\""));
230
                        } else if (token.startsWith("/property")) {
231
                                where = NOWHERE;
232

    
233
                                Integer tipo = new Integer(0);
234
                                if (tipos.containsKey(token)) tipo = (Integer) tipos.get(token);
235
                                tipos.put(token, new Integer(tipo.intValue() + 1));
236
                        }
237
                } else {
238
                        switch (where) {
239
                        case INPROPERTY:
240
                                activeFeature.setProp(propName, l);
241
                                break;
242
                        case INCOORDINATES:
243
                                String [] txt = l.split(",");
244
                                double [] pt = new double[3];
245
                                for (int i=0; i<txt.length; i++)
246
                                        pt[i] = Double.parseDouble(txt[i]);
247
                                activeMember.add(proj.createPoint(pt[0], pt[1]));
248
                                break;
249
                        }
250
                }
251
        }
252

    
253
        /**
254
         * Obtiene la lista de features.
255
         */
256
        
257
        public IObjList getObjects() {
258
                return collection;
259
        }
260
        
261
        /**
262
         * Saca las estadisticas del fichero por stdout.
263
         */
264
        
265
        public void pintaStats() {
266
/*                print "GML file '%s': %d lineas" % (self.name, self.lineNr)
267
                print "GML Tokens:"
268
                kk = self.tipos.keys()
269
                kk.sort()
270
                for k in kk:
271
                        print k, self.tipos[k]
272
                print "\nTotal Coordenadas %d\n" % geo.Polygon.numCoords
273
                #print "\nExtent: min=(%.2f,%df), max=(%.2f,%.2f)" % (self.minX, self.minY, self.maxX, self.maxY)
274
                print "\nExtent: ", (self.minX, self.minY, self.maxX, self.maxY)*/
275
                String txt = "GML file: '"+name+"': "+lineNr+" l?neas\n";
276
                txt += "\nGML file: Total Coordenadas: "+Polygon.pointNr+"\n";
277
                txt += "GML file: Total features: "+collection.size()+"\n";
278
                txt += "GML file: "+collection.getExtent()+"\n";
279
                txt += "GML file: "+((Feature) collection.get(0)).getExtent();
280
                System.out.println(txt);
281
        }
282

    
283
        public void reProject(ICoordTrans rp) {
284
                collection.reProject(rp);
285
                setProjection(rp.getPDest());
286
        }
287

    
288
        /* (non-Javadoc)
289
         * @see org.cresques.io.GeoFile#close()
290
         */
291
        public void close() {
292
                // TODO Auto-generated method stub
293
                
294
        }
295
        
296
        
297
}