1 /* -------------------------------------------------------------------
2 GeoVISTA Center (Penn State, Dept. of Geography)
3 Java source file for the class ShapeFileDataReader
4 Copyright (c), 2002, GeoVISTA Center
5 All Rights Reserved.
6 Original Author: Frank Hardisty
7 $Author: xpdai $
8 $Id: ShapeFileDataReader.java,v 1.5 2003/07/09 19:38:48 xpdai Exp $
9 $Date: 2003/07/09 19:38:48 $
10 Reference: Document no:
11 ___ ___
12 ------------------------------------------------------------------- *
13 */
14
15
16 package edu.psu.geovista.data.geog;
17 import java.awt.*;
18 import java.awt.event.*;
19 import java.awt.geom.*;
20 import java.io.*;
21 import java.util.*;
22
23 import javax.swing.event.*;
24
25 import edu.psu.geovista.db.dbase.*;
26 import edu.psu.geovista.io.csv.*;
27 import edu.psu.geovista.io.geog.*;
28
29 /***
30 * Takes a file name and returns an Object[] with:
31 * Object[0] = names of variables
32 * 0bject[1] = data (double[], int[], or String[])
33 * 0bject[1] = data (double[], int[], or String[])
34 * ...
35 * Object[n-1] = the shapefile data
36 *
37 * also see DBaseFile, ShapeFile
38 *
39 */
40 public class ShapeFileDataReader implements Serializable {
41
42 public static final String COMMAND_DATA_SET_MADE = "dataMade";
43 public static final int FILE_TYPE_DBF = 0;
44 public static final int FILE_TYPE_CSV = 1;
45
46 private transient DataSetForApps dataForApps;
47 private transient String fileName;
48 private transient EventListenerList listenerList;
49
50 private transient long pointCount = 0;
51
52 public ShapeFileDataReader() {
53 super();
54 listenerList = new EventListenerList();
55
56 }
57 private Shape[] makeShapes(ShapeFile shp){
58 int numShapes = shp.getFileHeader().getFileLength();
59 Shape[] shapes = (Shape[])this.transform(shp);
60
61
62 return shapes;
63
64 }
65 private DataSetForApps makeDataSetForApps(String fileName){
66 Object[] shpData = null;
67 try {
68 String dbFileName = fileName + ".dbf";
69 DBaseFile db = new DBaseFile(dbFileName);
70 Object[] dbData = db.getDataSet();
71 shpData = new Object[dbData.length + 2];
72 for (int i = 0; i < dbData.length; i++) {
73 shpData[i] = dbData[i];
74 }
75
76 ShapeFile shp = new ShapeFile(fileName + ".shp");
77 shpData[dbData.length] = this.transform(shp);
78
79 AttributeDescriptionFile desc = null;
80
81 desc = new AttributeDescriptionFile(fileName + ".desc");
82
83 shpData[dbData.length + 1] = desc.getAttributeDescriptions();
84 } catch (Exception ex) {
85 ex.printStackTrace();
86 }
87 //this.fireActionPerformed(COMMAND_DATA_SET_MADE);
88 this.dataForApps = new DataSetForApps();
89 dataForApps.setDataObject(shpData);
90 return dataForApps;
91 }
92
93 private DataSetForApps makeDataSetForAppsCSV(String fileName){
94 Object[] shpData = null;
95 try {
96 String dbFileName = fileName + ".csv";
97
98 GeogCSVReader csv = new GeogCSVReader();
99 FileInputStream inStream = new FileInputStream(dbFileName);
100 Object[] dbData = csv.readFile(inStream);
101 shpData = new Object[dbData.length + 2];
102 for (int i = 0; i < dbData.length; i++) {
103 shpData[i] = dbData[i];
104 }
105 ShapeFile shp = new ShapeFile(fileName + ".shp");
106 shpData[dbData.length] = this.transform(shp);
107
108 AttributeDescriptionFile desc = new AttributeDescriptionFile(fileName + ".desc");
109 shpData[dbData.length + 1] = desc.getAttributeDescriptions();
110 } catch (Exception ex) {
111 ex.printStackTrace();
112 }
113 //this.fireActionPerformed(COMMAND_DATA_SET_MADE);
114 this.dataForApps = new DataSetForApps();
115 dataForApps.setDataObject(shpData);
116 return dataForApps;
117
118 }
119 private Object[] makeDataSet(String fileName){
120 this.makeDataSetForApps(fileName);
121 return dataForApps.getDataObjectOriginal();
122 }
123
124
125 private String removeExtension(String fileName){
126 String removed = fileName;
127 int index = fileName.lastIndexOf(".");
128 if (index > -1) { //if it was found
129 int len = fileName.length();
130 removed = fileName.substring(0,index);
131 }
132 return removed;
133 }
134
135 public void setDataForApps (DataSetForApps dataForApps) {
136 this.dataForApps = dataForApps;
137 }
138 public DataSetForApps getDataForApps() {
139 return this.dataForApps;
140 }
141
142 public Object[] getDataSet() {
143 return this.dataForApps.getDataObjectOriginal();
144 }
145
146 public void setFileName (String fileName) {
147 this.fileName = this.removeExtension(fileName);
148 this.dataForApps = this.makeDataSetForApps(this.fileName);
149 this.fireActionPerformed(COMMAND_DATA_SET_MADE);
150 //for (int i = 0; i < dataSet.length; i++) {
151 // System.out.println(dataSet[i]);
152 //}
153 }
154 public void setFileNameCSV(String fileName){
155 this.fileName = this.removeExtension(fileName);
156 this.dataForApps = this.makeDataSetForAppsCSV(this.fileName);
157 this.fireActionPerformed(COMMAND_DATA_SET_MADE);
158 }
159 public void setFileName (String fileName, int fileType){
160 if (fileType == this.FILE_TYPE_DBF){
161 this.setFileName(fileName);
162 } else if (fileType == this.FILE_TYPE_CSV){
163 this.setFileNameCSV(fileName);
164 } else{
165 throw new IllegalArgumentException("ShapeFileDataReader, unexpected file type");
166 }
167 }
168
169 public String getFileName () {
170 return this.fileName;
171 }
172
173 private void writeObject(ObjectOutputStream oos) throws IOException {
174 oos.defaultWriteObject();
175 }
176 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
177 ois.defaultReadObject();
178 }
179 public Object[] convertShpToShape(Object[] dataIn){
180 if (dataIn[dataIn.length-1] instanceof ShapeFile){
181 ShapeFile shp = (ShapeFile)dataIn[dataIn.length-1];
182 dataIn[dataIn.length-1] = this.makeShapes(shp);
183
184 }
185 return dataIn;
186 }
187 private Object[] transform(ShapeFile shpFile){
188 if (shpFile == null) {
189 throw new IllegalArgumentException("null arg passed in to ShapeFileToShape.transform");
190 }
191 //Shape[] shpNew = new Shape[shpFile.getData().size()];
192 Vector oldData = shpFile.getData();
193 Object[] newData = null;
194
195 switch(shpFile.getFileHeader().getShapeType())
196 {
197 case ShapeFile.SHAPE_TYPE_POINT:
198 newData = transformPoints(oldData);
199 //outData.setSpatialType(outData.SPATIAL_TYPE_POINT);
200 break;
201 case ShapeFile.SHAPE_TYPE_POLYLINE: newData = transformPolylines(oldData); break;
202 case ShapeFile.SHAPE_TYPE_POLYGON:
203 newData = transformPolygons(oldData);
204 //outData.setSpatialType(outData.SPATIAL_TYPE_POLYGON);
205 break;
206 default: throw new IllegalArgumentException("ShapeFileToShape.transform,"
207 + " unknown file type");
208 }
209 return newData;
210 }
211
212 private Point2D[] transformPoints(Vector shapeFileData) {
213 Point2D[] newShapes = new Point2D[shapeFileData.size()];
214 int currShape = 0;
215 double x = 0;
216 double y = 0;
217 for (Enumeration e = shapeFileData.elements() ; e.hasMoreElements() ;) {
218 ShapeFileRecordPoint pointOld = (ShapeFileRecordPoint)e.nextElement();
219 x = pointOld.getX();
220 y = pointOld.getY();
221
222 Point2D newShape = new Point2D.Double(x,y);
223
224
225
226 newShapes[currShape] = newShape;
227 currShape++;
228 }
229
230 return newShapes;
231 }
232
233 private Shape[] transformPolylines(Vector shapeFileData) {
234 Shape[] newShapes = new Shape[shapeFileData.size()];
235
236
237 return newShapes;
238 }
239
240 private Shape[] transformPolygons(Vector shapeFileData) {
241 GeneralPath[] newShapes = new GeneralPath[shapeFileData.size()];
242 int currShape = 0;
243 for (Enumeration e = shapeFileData.elements() ; e.hasMoreElements() ;) {
244 ShapeFileRecordPolygon polyOld = (ShapeFileRecordPolygon)e.nextElement();
245
246 double[][] dataOld = polyOld.getPoints();
247 //XXX next line
248 pointCount = pointCount + dataOld.length;
249 int[] parts = polyOld.getParts();
250 int part = 0;
251 GeneralPath newShape = new GeneralPath();
252 for (int counter = 0; counter < dataOld.length; counter++){
253 if (part < parts.length && counter == parts[part]) {
254 newShape.moveTo((float)dataOld[counter][0],(float)dataOld[counter][1]);
255 part++;
256 } else {
257 newShape.lineTo((float)dataOld[counter][0],(float)dataOld[counter][1]);
258 }
259 }
260 newShapes[currShape] = newShape;
261 currShape++;
262 }
263
264 return newShapes;
265 }
266 /***
267 * implements ActionListener
268 */
269 public void addActionListener(ActionListener l) {
270 listenerList.add(ActionListener.class, l);
271 }
272
273 /***
274 * removes an ActionListener from the button
275 */
276 public void removeActionListener(ActionListener l) {
277 listenerList.remove(ActionListener.class, l);
278 }
279
280 /***
281 * Notify all listeners that have registered interest for
282 * notification on this event type. The event instance
283 * is lazily created using the parameters passed into
284 * the fire method.
285 * @see EventListenerList
286 */
287 protected void fireActionPerformed(String command) {
288 // Guaranteed to return a non-null array
289 Object[] listeners = listenerList.getListenerList();
290 ActionEvent e = null;
291 // Process the listeners last to first, notifying
292 // those that are interested in this event
293 for (int i = listeners.length - 2; i >= 0; i -= 2) {
294 if (listeners[i] == ActionListener.class) {
295 // Lazily create the event:
296 if (e == null) {
297 e = new ActionEvent(this,
298 ActionEvent.ACTION_PERFORMED,
299 command);
300 }
301 ((ActionListener)listeners[i + 1]).actionPerformed(e);
302 }
303 }
304 }
305
306 }
This page was automatically generated by Maven