Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1008 / libraries / libCq CMS for java.old / src / org / cresques / io / GmlFile.java @ 12520

History | View | Annotate | Download (10.6 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.gml.Feature;
31
import org.cresques.px.gml.FeatureCollection;
32
import org.cresques.px.gml.Geometry;
33
import org.cresques.px.gml.LineString;
34
import org.cresques.px.gml.MultiGeometry;
35
import org.cresques.px.gml.MultiPolygon;
36
import org.cresques.px.gml.Point;
37
import org.cresques.px.gml.Polygon;
38

    
39
import java.io.BufferedReader;
40
import java.io.FileNotFoundException;
41
import java.io.FileReader;
42
import java.io.IOException;
43
import java.io.InputStream;
44
import java.io.InputStreamReader;
45
import java.io.Reader;
46

    
47
import java.util.Hashtable;
48

    
49

    
50
/**
51
 * Cargador de ficheros .gml de JUMP de vivid solutions.
52
 * Actualmente lee un subconjunto de datos. Datos de prueba generados por JUMP.
53
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
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
    final static String LT = "<";
60
    final static String GT = ">";
61
    int where = NOWHERE;
62
    Hashtable tipos = new Hashtable();
63
    FeatureCollection collection = null;
64
    Feature activeFeature = null;
65
    Geometry activeGeometry = null;
66
    Geometry activeMember = null;
67
    String buf = null;
68
    String propName = null;
69
    long lineNr = 0;
70
    BufferedReader fi;
71
    long l = 0;
72
    int count = 0;
73

    
74
    /**
75
     * Constructor de la clase Gml
76
     *
77
     */
78
    public GmlFile(IProjection proj, String name) {
79
        super(proj, name);
80
    }
81

    
82
    /**
83
     * Carga un .gml
84
     * @param name nombre del fichero
85
     */
86
    public GeoFile load() {
87
        System.out.println("Cargando " + name + " ...");
88

    
89
        try {
90
            if (FileFolder.isUrl(name)) {
91
                ZipFileFolder zFolder = new ZipFileFolder(name);
92
                InputStream is = zFolder.getInputStream(name);
93

    
94
                return load(new InputStreamReader(is));
95
            } else {
96
                return load(new FileReader(name));
97
            }
98
        } catch (FileNotFoundException e) {
99
            e.printStackTrace();
100
        } catch (IOException ie) {
101
            System.err.println("ERROR." + l + "lineas leidas");
102
            ie.printStackTrace();
103
        }
104

    
105
        return this;
106
    }
107

    
108
    public GeoFile load(Reader fr) {
109
        try {
110
            fi = new BufferedReader(fr);
111

    
112
            while ((buf = fi.readLine()) != null) {
113
                //System.out.println(buf);
114
                l++;
115
                preParseLine(buf.trim());
116
            }
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

    
127
        this.lineNr = l;
128
        extent.add(collection.getExtent());
129

    
130
        return this;
131
    }
132

    
133
    /**
134
     * Analiza una l?nea del fichero .gml
135
     * @param l linea que analiza
136
     */
137
    void preParseLine(String l) {
138
        String line1;
139

    
140
        //System.err.println("pre0: '"+l+"'");
141
        //if (count++>500) System.exit(1);
142
        if (l.indexOf(LT) > 0) {
143
            line1 = l.substring(0, l.indexOf(LT)).trim();
144
            l = l.substring(l.indexOf(LT)).trim();
145

    
146
            //System.err.println("pre1: '"+line1+"', '"+l+"'");
147
            preParseLine(line1);
148
            preParseLine(l);
149

    
150
            return;
151
        }
152

    
153
        if ((l.indexOf(GT) >= 0) && (l.indexOf(GT) != (l.length() - 1))) {
154
            line1 = l.substring(0, l.indexOf(GT) + 1).trim();
155
            l = l.substring(l.indexOf(GT) + 1).trim();
156

    
157
            //System.err.println("pre2: '"+line1+"', '"+l+"'");
158
            preParseLine(line1);
159
            preParseLine(l);
160

    
161
            return;
162
        }
163

    
164
        parseToken(l);
165
    }
166

    
167
    /**
168
     * Analiza un token
169
     * @param l
170
     */
171
    void parseToken(String l) {
172
        String token = null;
173

    
174
        //System.err.println(l);
175
        if ((l.length() > 0) && (l.substring(0, 1).compareTo(LT) == 0)) {
176
            token = l.substring(1, l.indexOf(GT));
177

    
178
            if (token.compareTo("gml:coordinates") == 0) {
179
                where = INCOORDINATES;
180
            } else if (token.compareTo("/gml:coordinates") == 0) {
181
                where = NOWHERE;
182
            } else if (token.compareTo("gml:LinearRing") == 0) {
183
            } else if (token.compareTo("/gml:LinearRing") == 0) {
184
            } else if (token.compareTo("gml:Polygon") == 0) {
185
                activeMember = new Polygon();
186

    
187
                if (activeGeometry == null) {
188
                    activeGeometry = activeMember;
189
                }
190
            } else if (token.compareTo("/gml:Polygon") == 0) {
191
                if (activeGeometry.getClass() == MultiPolygon.class) {
192
                    ((MultiPolygon) activeGeometry).add(activeMember);
193
                }
194

    
195
                activeMember = null;
196
            } else if (token.compareTo("gml:LineString") == 0) {
197
                activeMember = new LineString();
198

    
199
                if (activeGeometry == null) {
200
                    activeGeometry = activeMember;
201
                }
202
            } else if (token.compareTo("/gml:LineString") == 0) {
203
                if (activeGeometry.getClass() == MultiGeometry.class) {
204
                    ((MultiGeometry) activeGeometry).add(activeMember);
205
                }
206

    
207
                activeMember = null;
208
            } else if (token.compareTo("gml:outerBoundaryIs") == 0) {
209
            } else if (token.compareTo("/gml:outerBoundaryIs") == 0) {
210
            } else if (token.compareTo("featureCollection") == 0) {
211
                collection = new FeatureCollection(proj);
212
            } else if (token.compareTo("/featureCollection") == 0) {
213
            } else if (token.compareTo("feature") == 0) {
214
                System.err.println("feature");
215
                activeFeature = new Feature();
216
            } else if (token.compareTo("/feature") == 0) {
217
                collection.add(activeFeature);
218
                activeFeature = null;
219
            } else if (token.compareTo("geometry") == 0) {
220
            } else if (token.compareTo("/geometry") == 0) {
221
                activeFeature.setGeometry(activeGeometry);
222
                activeGeometry = null;
223
                activeMember = null;
224
            } else if (token.compareTo("gml:Point") == 0) {
225
                activeMember = new Point();
226

    
227
                if (activeGeometry == null) {
228
                    activeGeometry = activeMember;
229
                }
230
            } else if (token.compareTo("/gml:Point") == 0) {
231
                activeMember = null;
232
            } else if (token.compareTo("gml:MultiPolygon") == 0) {
233
                activeGeometry = new MultiPolygon();
234
            } else if (token.compareTo("/gml:MultiPolygon") == 0) {
235
            } else if (token.compareTo("gml:MultiPoint") == 0) {
236
                //activeGeometry = new MultiPoint();
237
            } else if (token.compareTo("/gml:MultiPoint") == 0) {
238
            } else if (token.compareTo("gml:polygonMember") == 0) {
239
            } else if (token.compareTo("/gml:polygonMember") == 0) {
240
            } else if (token.compareTo("gml:innerBoundaryIs") == 0) {
241
                ((Polygon) activeMember).setInnerBoundary();
242
            } else if (token.compareTo("/gml:innerBoundaryIs") == 0) {
243
            } else if (token.startsWith("property")) {
244
                where = INPROPERTY;
245
                propName = l.substring(l.indexOf("\"") + 1);
246
                propName = propName.substring(0, propName.indexOf("\""));
247
            } else if (token.startsWith("/property")) {
248
                where = NOWHERE;
249

    
250
                Integer tipo = new Integer(0);
251

    
252
                if (tipos.containsKey(token)) {
253
                    tipo = (Integer) tipos.get(token);
254
                }
255

    
256
                tipos.put(token, new Integer(tipo.intValue() + 1));
257
            }
258
        } else {
259
            switch (where) {
260
            case INPROPERTY:
261
                activeFeature.setProp(propName, l);
262

    
263
                break;
264

    
265
            case INCOORDINATES:
266

    
267
                String[] txt = l.split(",");
268
                double[] pt = new double[3];
269

    
270
                for (int i = 0; i < txt.length; i++)
271
                    pt[i] = Double.parseDouble(txt[i]);
272

    
273
                activeMember.add(proj.createPoint(pt[0], pt[1]));
274

    
275
                break;
276
            }
277
        }
278
    }
279

    
280
    /**
281
     * Obtiene la lista de features.
282
     */
283
    public IObjList getObjects() {
284
        return collection;
285
    }
286

    
287
    /**
288
     * Saca las estadisticas del fichero por stdout.
289
     */
290
    public void pintaStats() {
291
        /*                print "GML file '%s': %d lineas" % (self.name, self.lineNr)
292
                        print "GML Tokens:"
293
                        kk = self.tipos.keys()
294
                        kk.sort()
295
                        for k in kk:
296
                                print k, self.tipos[k]
297
                        print "\nTotal Coordenadas %d\n" % geo.Polygon.numCoords
298
                        #print "\nExtent: min=(%.2f,%df), max=(%.2f,%.2f)" % (self.minX, self.minY, self.maxX, self.maxY)
299
                        print "\nExtent: ", (self.minX, self.minY, self.maxX, self.maxY)*/
300
        String txt = "GML file: '" + name + "': " + lineNr + " l?neas\n";
301
        txt += ("\nGML file: Total Coordenadas: " + Polygon.pointNr + "\n");
302
        txt += ("GML file: Total features: " + collection.size() + "\n");
303
        txt += ("GML file: " + collection.getExtent() + "\n");
304
        txt += ("GML file: " + ((Feature) collection.get(0)).getExtent());
305
        System.out.println(txt);
306
    }
307

    
308
    public void reProject(ICoordTrans rp) {
309
        collection.reProject(rp);
310
        setProjection(rp.getPDest());
311
    }
312

    
313
    /* (non-Javadoc)
314
     * @see org.cresques.io.GeoFile#close()
315
     */
316
    public void close() {
317
        // TODO Auto-generated method stub
318
    }
319
}