Revision 2669 branches/CqCMSDvp/libraries/libCq CMS for java.old/src/org/cresques/io/GmlFile.java

View differences:

GmlFile.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 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 26
import org.cresques.cts.ICoordTrans;
37 27
import org.cresques.cts.IProjection;
38 28

  
......
43 33
import org.cresques.px.gml.LineString;
44 34
import org.cresques.px.gml.MultiGeometry;
45 35
import org.cresques.px.gml.MultiPolygon;
36
import org.cresques.px.gml.Point;
46 37
import org.cresques.px.gml.Polygon;
47
import org.cresques.px.gml.Point;
48 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

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

  
55 55
public class GmlFile extends GeoFile {
56
	final static int NOWHERE		= -1;
57
	final static int INCOORDINATES	= 0;
58
	final static int INPROPERTY		= 1;
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;
59 73

  
60
	int where = NOWHERE;
61
	Hashtable tipos = new Hashtable();
62
	
63
	final static String LT="<", GT=">";
74
    /**
75
     * Constructor de la clase Gml
76
     *
77
     */
78
    public GmlFile(IProjection proj, String name) {
79
        super(proj, name);
80
    }
64 81

  
65
	FeatureCollection collection = null;
66
	Feature activeFeature = null;
67
	Geometry activeGeometry = null;
68
	Geometry activeMember = null;
82
    /**
83
     * Carga un .gml
84
     * @param name nombre del fichero
85
     */
86
    public GeoFile load() {
87
        System.out.println("Cargando " + name + " ...");
69 88

  
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
	}
89
        try {
90
            if (FileFolder.isUrl(name)) {
91
                ZipFileFolder zFolder = new ZipFileFolder(name);
92
                InputStream is = zFolder.getInputStream(name);
86 93

  
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);
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
        }
140 104

  
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;
105
        return this;
106
    }
167 107

  
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;
108
    public GeoFile load(Reader fr) {
109
        try {
110
            fi = new BufferedReader(fr);
232 111

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

  
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
	}
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
        }
282 126

  
283
	public void reProject(ICoordTrans rp) {
284
		collection.reProject(rp);
285
		setProjection(rp.getPDest());
286
	}
127
        this.lineNr = l;
128
        extent.add(collection.getExtent());
287 129

  
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
}
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
}

Also available in: Unified diff