GeoTessJavaExamples  2.0
GeoTessModelExtended.java
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
4 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
5 //- retains certain rights in this software.
6 //-
7 //- BSD Open Source License.
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- * Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //- * Redistributions in binary form must reproduce the above copyright
16 //- notice, this list of conditions and the following disclaimer in the
17 //- documentation and/or other materials provided with the distribution.
18 //- * Neither the name of Sandia National Laboratories nor the names of its
19 //- contributors may be used to endorse or promote products derived from
20 //- this software without specific prior written permission.
21 //-
22 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //- POSSIBILITY OF SUCH DAMAGE.
33 //-
34 //- ****************************************************************************
35 
36 package gov.sandia.geotess.examples;
37 
38 import java.io.DataInputStream;
39 import java.io.DataOutputStream;
40 import java.io.File;
41 import java.io.IOException;
42 import java.io.Writer;
43 import java.util.Scanner;
44 
45 import gov.sandia.geotess.GeoTessException;
46 import gov.sandia.geotess.GeoTessGrid;
47 import gov.sandia.geotess.GeoTessMetaData;
48 import gov.sandia.geotess.GeoTessModel;
49 import gov.sandia.geotess.GeoTessUtils;
50 
51 /**
52  * This class is an example of a class that extends GeoTessModel.
53  * It inherits all the functionality of GeoTessModel but adds an extra
54  * data item to the model. In this example, the extra data is
55  * just a simple String, but in real models that extend
56  * GeoTessModel, it could be anything.
57  *
58  * <p>Classes that extend GeoTessModel should provide
59  * implementations of all the GeoTessModel constructors and
60  * perform the following initialization functions (order is important!):
61  * <ul>
62  * <li>perform any initialization functions required by the
63  * derived class.
64  * <li>call one of the super class loadModel() methods,
65  * if appropriate.
66  * </ul>
67  * <p>In addition, classes that extend GeoTessModel should
68  * override 4 IO functions: loadModelBinary(), writeModelBinary(),
69  * loadModelAscii() and writeModelAscii().
70  * See examples below.
71  * <p>The first thing that these methods do is call the super
72  * class implementations to read/write the standard
73  * GeoTessModel information. After that, the methods
74  * may read/write the application specific data from/to
75  * the end of the standard GeoTessModel file.
76  * @author sballar
77  *
78  */
79 public class GeoTessModelExtended extends GeoTessModel
80 {
81  /**
82  * This string is just an example that represents whatever
83  * extra data users application may require.
84  * <br>Do not initialize extraData!
85  */
86  protected String extraData = "default value";
87 
89  {
90  super();
92  }
93 
94  /**
95  * Construct a new GeoTessModel object and populate it with information from
96  * the specified file.
97  *
98  * @param modelInputFile
99  * name of file containing the model.
100  * @param relativeGridPath
101  * the relative path from the directory where the model is stored
102  * to the directory where the grid is stored. Often, the model
103  * and grid are stored together in the same file in which case
104  * this parameter is ignored. Sometimes, however, the grid is
105  * stored in a separate file and only the name of the grid file
106  * (without path information) is stored in the model file. In
107  * this case, the code needs to know which directory to search
108  * for the grid file. The default is "" (empty string), which
109  * will cause the code to search for the grid file in the same
110  * directory in which the model file resides. Bottom line is that
111  * the default value is appropriate when the grid is stored in
112  * the same file as the model, or the model file is in the same
113  * directory as the model file.
114  * @throws IOException
115  */
116  public GeoTessModelExtended(File modelInputFile, String relativeGridPath) throws IOException
117  {
118  this();
119  loadModel(modelInputFile, relativeGridPath);
120  }
121 
122  /**
123  * Construct a new GeoTessModel object and populate it with information from
124  * the specified file.
125  *
126  * <p>relativeGridPath is assumed to be "" (empty string), which is appropriate
127  * when the grid information is stored in the same file as the model or when
128  * the grid is stored in a separate file located in the same directory as the
129  * model file.
130  *
131  * @param modelInputFile
132  * name of file containing the model.
133  * @throws IOException
134  */
135  public GeoTessModelExtended(File modelInputFile) throws IOException
136  {
137  this();
138  loadModel(modelInputFile, "");
139  }
140 
141  /**
142  * Construct a new GeoTessModel object and populate it with information from
143  * the specified file.
144  *
145  * @param modelInputFile
146  * name of file containing the model.
147  * @param relativeGridPath
148  * the relative path from the directory where the model is stored
149  * to the directory where the grid is stored. Often, the model
150  * and grid are stored together in the same file in which case
151  * this parameter is ignored. Sometimes, however, the grid is
152  * stored in a separate file and only the name of the grid file
153  * (without path information) is stored in the model file. In
154  * this case, the code needs to know which directory to search
155  * for the grid file. The default is "" (empty string), which
156  * will cause the code to search for the grid file in the same
157  * directory in which the model file resides. Bottom line is that
158  * the default value is appropriate when the grid is stored in
159  * the same file as the model, or the model file is in the same
160  * directory as the model file.
161  * @throws IOException
162  */
163  public GeoTessModelExtended(String modelInputFile, String relativeGridPath) throws IOException
164  {
165  this();
166  loadModel(modelInputFile, relativeGridPath);
167  }
168 
169  /**
170  * Construct a new GeoTessModel object and populate it with information from
171  * the specified file.
172  *
173  * <p>relativeGridPath is assumed to be "" (empty string), which is appropriate
174  * when the grid information is stored in the same file as the model or when
175  * the grid is stored in a separate file located in the same directory as the
176  * model file.
177  *
178  * @param modelInputFile
179  * name of file containing the model.
180  * @throws IOException
181  */
182  public GeoTessModelExtended(String modelInputFile) throws IOException
183  {
184  this();
185  loadModel(modelInputFile, "");
186  }
187 
188  /**
189  * Construct a new GeoTessModelExtended object and populate it with information from
190  * the specified DataInputStream. The GeoTessGrid will be read directly from
191  * the inputStream as well.
192  * @param inputStream
193  * @throws GeoTessException
194  * @throws IOException
195  */
196  public GeoTessModelExtended(DataInputStream inputStream) throws GeoTessException, IOException
197  {
198  this();
199  loadModelBinary(inputStream, null, "*");
200  }
201 
202  /**
203  * Construct a new GeoTessModelExtended object and populate it with information from
204  * the specified Scanner. The GeoTessGrid will be read directly from
205  * the inputScanner as well.
206  * @param inputScanner
207  * @throws GeoTessException
208  * @throws IOException
209  */
210  public GeoTessModelExtended(Scanner inputScanner) throws GeoTessException, IOException
211  {
212  this();
213  loadModelAscii(inputScanner, null, "*");
214  }
215 
216  /**
217  * Parameterized constructor, specifying the grid and metadata for the
218  * model. The grid is constructed and the data structures are initialized
219  * based on information supplied in metadata. The data structures are not
220  * populated with any information however (all Profiles are null). The
221  * application should populate the new model's Profiles after this
222  * constructor completes.
223  *
224  * <p>
225  * Before calling this constructor, the supplied MetaData object must be
226  * populated with required information by calling the following MetaData
227  * methods:
228  * <ul>
229  * <li>setDescription()
230  * <li>setLayerNames()
231  * <li>setAttributes()
232  * <li>setDataType()
233  * <li>setLayerTessIds() (only required if grid has more than one
234  * multi-level tessellation)
235  * </ul>
236  *
237  * @param gridFileName
238  * name of file from which to load the grid.
239  * @param metaData
240  * MetaData the new GeoTessModel instantiates a reference to the
241  * supplied metaData. No copy is made.
242  * @throws IOException
243  */
244  public GeoTessModelExtended(String gridFileName, GeoTessMetaData metaData) throws IOException
245  {
246  super(gridFileName, metaData);
247  initializeData();
248  }
249 
250  /**
251  * Parameterized constructor, specifying the grid and metadata for the
252  * model. The grid is constructed and the data structures are initialized
253  * based on information supplied in metadata. The data structures are not
254  * populated with any information however (all Profiles are null). The
255  * application should populate the new model's Profiles after this
256  * constructor completes.
257  *
258  * <p>
259  * Before calling this constructor, the supplied MetaData object must be
260  * populated with required information by calling the following MetaData
261  * methods:
262  * <ul>
263  * <li>setDescription()
264  * <li>setLayerNames()
265  * <li>setAttributes()
266  * <li>setDataType()
267  * <li>setLayerTessIds() (only required if grid has more than one
268  * multi-level tessellation)
269  * <li>setSoftwareVersion()
270  * <li>setGenerationDate()
271  * </ul>
272  *
273  * @param grid
274  * a reference to the GeoTessGrid that will support this
275  * GeoTessModel.
276  * @param metaData
277  * MetaData the new GeoTessModel instantiates a reference to the
278  * supplied metaData. No copy is made.
279  * @throws GeoTessException
280  * if metadata is incomplete.
281  */
282  public GeoTessModelExtended(GeoTessGrid grid, GeoTessMetaData metaData) throws GeoTessException, IOException
283  {
284  super(grid, metaData);
285  initializeData();
286  }
287 
288  /**
289  * Construct a new GeoTessModelExtended with all the structures from the supplied
290  * baseModel. The new GeoTessModelExtended will be built with references to the
291  * GeoTessMetaData, GeoTessGrid and all the Profiles in the baseModel.
292  * No copies are made. Changes to one will be reflected in the other.
293  * All of the extraData will be set to default values.
294  * @param baseModel
295  * @throws GeoTessException
296  */
297  public GeoTessModelExtended(GeoTessModel baseModel) throws GeoTessException
298  {
299  super(baseModel.getGrid(), baseModel.getMetaData());
300  for (int i = 0; i < baseModel.getNVertices(); ++i)
301  for (int j=0; j<baseModel.getNLayers(); ++j)
302  setProfile(i,j,baseModel.getProfile(i, j));
303 
304  initializeData();
305  }
306 
307  /**
308  * Protected method to initialize extraData.
309  */
310  protected void initializeData()
311  {
312  extraData = "extraData initialized in GeoTessModelExtended.initializeData()";
313  }
314 
315  /**
316  * Getter.
317  * @return extraData
318  */
319  public String getExtraData()
320  {
321  return extraData;
322  }
323 
324  /**
325  * Setter
326  * @param extraData
327  */
328  public void setExtraData(String extraData)
329  {
330  this.extraData = extraData;
331  }
332 
333  @Override
334  public boolean equals(Object other)
335  {
336  if (!(other instanceof GeoTessModelExtended)) return false;
337  GeoTessModelExtended otherModel = (GeoTessModelExtended) other;
338  return super.equals(otherModel) && this.extraData.equals(otherModel.extraData);
339  }
340 
341  /**
342  * Overridden IO method.
343  */
344  @Override
345  protected void loadModelBinary(DataInputStream input,
346  String inputDirectory, String relGridFilePath)
347  throws GeoTessException, IOException
348  {
349  // call super class to load model data from binary file.
350  super.loadModelBinary(input, inputDirectory, relGridFilePath);
351 
352  // it is good practice, but not required, to store the class
353  // name as the first thing added by the extending class.
354  String className = GeoTessUtils.readString(input);
355  if (!className.equals(this.getClass().getSimpleName()))
356  throw new IOException("Found class name "+className
357  +" but expecting "
358  +this.getClass().getSimpleName());
359 
360  // it is good practice, but not required, to store a format
361  // version number as the second thing added by the extending class.
362  // With this information, if the format changes in a future release
363  // it may be possible to make the class backward compatible.
364  int formatVersion = input.readInt();
365 
366  if (formatVersion == 1)
367  initializeData();
368  else
369  throw new IOException("Format version "+formatVersion+" is not supported.");
370 
371  // load application specific data in binary format.
372  extraData = GeoTessUtils.readString(input);
373 
374  }
375 
376  /**
377  * Overridden IO method.
378  */
379  @Override
380  protected void writeModelBinary(DataOutputStream output, String gridFileName)
381  throws IOException
382  {
383  // call super class to write standard model information to binary file.
384  super.writeModelBinary(output, gridFileName);
385 
386  // it is good practice, but not required, to store the class
387  // name as the first thing added by the extending class.
388  GeoTessUtils.writeString(output, this.getClass().getSimpleName());
389 
390  // it is good practice, but not required, to store a format
391  // version number as the second thing added by the extending class.
392  // With this information, if the format changes in a future release
393  // it may be possible to make the class backward compatible.
394  output.writeInt(1);
395 
396  // now output the extraData
397  GeoTessUtils.writeString(output, extraData);
398  }
399 
400  /**
401  * Overridden IO method.
402  */
403  @Override
404  protected void loadModelAscii(Scanner input, String inputDirectory,
405  String relGridFilePath) throws GeoTessException, IOException
406  {
407  super.loadModelAscii(input, inputDirectory, relGridFilePath);
408 
409  // it is good practice, but not required, to store the class
410  // name as the first thing added by the extending class.
411  String className = input.nextLine();
412  if (!className.equals(this.getClass().getSimpleName()))
413  throw new IOException("Found class name "+className
414  +" but expecting "
415  +this.getClass().getSimpleName());
416 
417  // it is good practice, but not required, to store a format
418  // version number as the second thing added by the extending class.
419  // With this information, if the format changes in a future release
420  // it may be possible to make the class backward compatible.
421  int formatVersion = input.nextInt();
422  input.nextLine();
423 
424  if (formatVersion == 1)
425  initializeData();
426  else
427  throw new IOException("Format version "+formatVersion+" is not supported.");
428 
429  // load application specific data in binary format.
430  extraData = input.nextLine();
431 
432  }
433 
434  /**
435  * Overridden IO method.
436  */
437  @Override
438  protected void writeModelAscii(Writer output, String gridFileName)
439  throws IOException
440  {
441  // call super class to write standard model information to ascii file.
442  super.writeModelAscii(output, gridFileName);
443 
444  // it is good practice, but not required, to store the class
445  // name and a format version number as the first things added
446  // by the extending class.
447  // With this information, if the format changes in a future release
448  // it may be possible to make the class backward compatible.
449  output.write(String.format("%s%n%d%n", this.getClass().getSimpleName(), 1));
450 
451  // write application specific data in ascii format.
452  output.write(extraData);
453  output.write("\n");
454  output.flush();
455  }
456 
457 }
gov.sandia.geotess.examples.GeoTessModelExtended.initializeData
void initializeData()
Protected method to initialize extraData.
Definition: GeoTessModelExtended.java:310
gov.sandia.geotess.examples.GeoTessModelExtended.getExtraData
String getExtraData()
Getter.
Definition: GeoTessModelExtended.java:319
gov.sandia.geotess.examples.GeoTessModelExtended.extraData
String extraData
This string is just an example that represents whatever extra data users application may require.
Definition: GeoTessModelExtended.java:86
gov.sandia.geotess.examples.GeoTessModelExtended.GeoTessModelExtended
GeoTessModelExtended(String gridFileName, GeoTessMetaData metaData)
Parameterized constructor, specifying the grid and metadata for the model.
Definition: GeoTessModelExtended.java:244
gov.sandia.geotess.examples.GeoTessModelExtended.GeoTessModelExtended
GeoTessModelExtended(String modelInputFile, String relativeGridPath)
Construct a new GeoTessModel object and populate it with information from the specified file.
Definition: GeoTessModelExtended.java:163
gov.sandia.geotess.examples.GeoTessModelExtended.GeoTessModelExtended
GeoTessModelExtended(GeoTessModel baseModel)
Construct a new GeoTessModelExtended with all the structures from the supplied baseModel.
Definition: GeoTessModelExtended.java:297
gov
gov.sandia.geotess.examples.GeoTessModelExtended.writeModelAscii
void writeModelAscii(Writer output, String gridFileName)
Overridden IO method.
Definition: GeoTessModelExtended.java:438
gov.sandia.geotess.examples.GeoTessModelExtended.GeoTessModelExtended
GeoTessModelExtended(GeoTessGrid grid, GeoTessMetaData metaData)
Parameterized constructor, specifying the grid and metadata for the model.
Definition: GeoTessModelExtended.java:282
gov.sandia.geotess.examples.GeoTessModelExtended.equals
boolean equals(Object other)
Definition: GeoTessModelExtended.java:334
gov.sandia.geotess.examples.GeoTessModelExtended.GeoTessModelExtended
GeoTessModelExtended(Scanner inputScanner)
Construct a new GeoTessModelExtended object and populate it with information from the specified Scann...
Definition: GeoTessModelExtended.java:210
gov.sandia.geotess.examples.GeoTessModelExtended.loadModelAscii
void loadModelAscii(Scanner input, String inputDirectory, String relGridFilePath)
Overridden IO method.
Definition: GeoTessModelExtended.java:404
gov.sandia.geotess
gov.sandia.geotess.examples.GeoTessModelExtended.GeoTessModelExtended
GeoTessModelExtended(DataInputStream inputStream)
Construct a new GeoTessModelExtended object and populate it with information from the specified DataI...
Definition: GeoTessModelExtended.java:196
gov.sandia.geotess.examples.GeoTessModelExtended.GeoTessModelExtended
GeoTessModelExtended(File modelInputFile, String relativeGridPath)
Construct a new GeoTessModel object and populate it with information from the specified file.
Definition: GeoTessModelExtended.java:116
gov.sandia.geotess.examples.GeoTessModelExtended.GeoTessModelExtended
GeoTessModelExtended()
Definition: GeoTessModelExtended.java:88
gov.sandia.geotess.examples.GeoTessModelExtended.setExtraData
void setExtraData(String extraData)
Setter.
Definition: GeoTessModelExtended.java:328
gov.sandia.geotess.examples.GeoTessModelExtended.writeModelBinary
void writeModelBinary(DataOutputStream output, String gridFileName)
Overridden IO method.
Definition: GeoTessModelExtended.java:380
gov.sandia.geotess.examples.GeoTessModelExtended.loadModelBinary
void loadModelBinary(DataInputStream input, String inputDirectory, String relGridFilePath)
Overridden IO method.
Definition: GeoTessModelExtended.java:345
gov.sandia.geotess.examples.GeoTessModelExtended.GeoTessModelExtended
GeoTessModelExtended(File modelInputFile)
Construct a new GeoTessModel object and populate it with information from the specified file.
Definition: GeoTessModelExtended.java:135
gov.sandia.geotess.examples.GeoTessModelExtended
This class is an example of a class that extends GeoTessModel.
Definition: GeoTessModelExtended.java:80
gov.sandia.geotess.examples.GeoTessModelExtended.GeoTessModelExtended
GeoTessModelExtended(String modelInputFile)
Construct a new GeoTessModel object and populate it with information from the specified file.
Definition: GeoTessModelExtended.java:182
gov.sandia