001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.io;
034:
035: import java.io.*;
036:
037: import com.vividsolutions.jts.geom.GeometryFactory;
038: import com.vividsolutions.jts.io.ParseException;
039: import com.vividsolutions.jump.feature.*;
040:
041: /**
042: * WKTReader is a {@link JUMPReader} specialized to read WTK (Well Known Text) files.
043: *
044: * <p>
045: * DataProperties for the JUMPReader load(DataProperties)
046: * interface:<br>
047: * </p>
048: *
049: * <p>
050: * <table border='1' cellspacing='0' cellpadding='4'>
051: * <tr>
052: * <th>Parameter</th>
053: * <th>Meaning</th>
054: * </tr>
055: * <tr>
056: * <td>File or DefaultValue</td>
057: * <td>File name for the input WKT file</td>
058: * </tr>
059: * <tr>
060: * <td>CompressedFile</td>
061: * <td>File name (a .zip or .gz) with a .jml/.xml/.gml inside
062: * (specified by File)</td>
063: * </tr>
064: * </table> <br>
065: *</p>
066: *
067: */
068: public class WKTReader implements JUMPReader {
069: private GeometryFactory geometryFactory = new GeometryFactory();
070: private com.vividsolutions.jts.io.WKTReader wktReader = new com.vividsolutions.jts.io.WKTReader(
071: geometryFactory);
072:
073: /**constructor**/
074: public WKTReader() {
075: }
076:
077: /**
078: * Main function -read in a file containing a list of WKT geometries
079: * @param dp 'InputFile' or 'DefaultValue' to specify where the WKT file is.
080: */
081: public FeatureCollection read(DriverProperties dp)
082: throws IllegalParametersException, Exception {
083: FeatureCollection fc;
084:
085: String inputFname;
086: boolean isCompressed;
087: Reader fileReader;
088:
089: isCompressed = (dp.getProperty("CompressedFile") != null);
090:
091: inputFname = dp.getProperty("File");
092:
093: if (inputFname == null) {
094: inputFname = dp.getProperty("DefaultValue");
095: }
096:
097: if (inputFname == null) {
098: throw new IllegalParametersException(
099: "call to WKTReader.read() has DataProperties w/o a InputFile specified");
100: }
101:
102: if (isCompressed) {
103: fileReader = new InputStreamReader(CompressedFile.openFile(
104: inputFname, dp.getProperty("CompressedFile")));
105: } else {
106: fileReader = new FileReader(inputFname);
107: }
108:
109: try {
110: BufferedReader bufferedReader = new BufferedReader(
111: fileReader);
112:
113: try {
114: fc = read(bufferedReader);
115: } finally {
116: bufferedReader.close();
117: }
118: } finally {
119: fileReader.close();
120: }
121:
122: return fc;
123: }
124:
125: /**
126: * Reads in the actual WKT geometries
127: *@param reader where to read the geometries from
128: */
129: public FeatureCollection read(Reader reader) throws Exception {
130: FeatureSchema featureSchema = new FeatureSchema();
131: featureSchema.addAttribute("Geometry", AttributeType.GEOMETRY);
132:
133: FeatureCollection featureCollection = new FeatureDataset(
134: featureSchema);
135: BufferedReader bufferedReader = new BufferedReader(reader);
136:
137: try {
138: while (!isAtEndOfFile(bufferedReader)) {
139: featureCollection.add(nextFeature(bufferedReader,
140: featureSchema));
141: }
142: } finally {
143: bufferedReader.close();
144: }
145:
146: return featureCollection;
147: }
148:
149: /**
150: * returns true if at the end of the file.
151: */
152: private boolean isAtEndOfFile(BufferedReader bufferedReader)
153: throws IOException, ParseException {
154: bufferedReader.mark(1000);
155:
156: try {
157: StreamTokenizer tokenizer = new StreamTokenizer(
158: bufferedReader);
159: int type = tokenizer.nextToken();
160:
161: if (type == StreamTokenizer.TT_EOF) {
162: return true;
163: }
164:
165: if (type == StreamTokenizer.TT_WORD) {
166: return false;
167: }
168:
169: throw new ParseException(
170: "Expected word or end-of-file but encountered StreamTokenizer type "
171: + type);
172: } finally {
173: bufferedReader.reset();
174: }
175: }
176:
177: /**
178: * Reads 1 feature
179: */
180: private Feature nextFeature(Reader reader,
181: FeatureSchema featureSchema) throws ParseException {
182: Feature feature = new BasicFeature(featureSchema);
183: feature.setGeometry(wktReader.read(reader));
184:
185: return feature;
186: }
187: }
|