/*****************************************************************
 * PointExample.java
 * Just an example of how to write a Point Shapefile with the 
 * NVS Shapefile library, v2.0
 *
 ****************************************************************/

import java.io.*;
import com.nvs.shapefile.*;
import java.util.*;

public class PointExample 
{	
	public static void main(String args[])
	{		
		new PointExample();
	}
	
	public PointExample()
	{
		// Create a new Shapefile of type POINT
		Shapefile shp = new Shapefile(Shapefile.SHAPETYPE_POINT);
		// Add a table description for the name "soil_type". You have to do this
		//  before you add a ShapeObject with a record, else you'll get an exception
		//  because the Shapefile's TableDescription won't match the ShapeObject's
		//  Record
		shp.getTableDescription().addTableDescriptor(new TableDescriptor("soil_type"));
		
		// Create a ShapeObject with an UNDEFINED type. This is handy when you don't
		//  know what type you'll need the ShapeObject to be.
		ShapeObject shpObj = new ShapeObject();
		
		// Create some points and add them to the Shapefile
		Point p = new Point(1, 2);
		shpObj.addPoint(p);
		shp.addShapeObject(shpObj);
		
		shpObj = new ShapeObject();
		p = new Point(2, 3);
		shpObj.addPoint(p);
		shp.addShapeObject(shpObj);
		
		shpObj = new ShapeObject();
		p = new Point(4, 3);
		shpObj.addPoint(p);
		
		// Create a new record and add a "soil_type" field to it
		Record rec = new Record();
		rec.addField(new RecordField("soil_type", "sandy"));
		
		// Set the ShapeObject's Record 
		shpObj.setRecord(rec);
		
		// Add the ShapeObject to the Shapefile
		shp.addShapeObject(shpObj);
		
		// Write what we've got so far
		try
		{
			shp.write("PointExample_1");
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		
		shp = new Shapefile(Shapefile.SHAPETYPE_POINT);
		shp.getTableDescription().addTableDescriptor(new TableDescriptor("soilmoisture"));
		
		// Instead of adding ShapeObjects one at a time, we'll add a collection this time
		//  loop through ten times, adding a point to the ArrayList each time
		ArrayList arlShapes = new ArrayList();
		for(int i=0;i<10;i++)
		{
			p = new Point(i, i*2);
			shpObj = new ShapeObject(ShapeObject.POINT);
			rec = new Record();
			rec.addField(new RecordField("soilmoisture", "moisture" + i));
			shpObj.setRecord(rec);
			shpObj.addPoint(p);
			arlShapes.add(shpObj);
		}
		
		// Set the Shapefile's ShapeObjects to the ArrayList.
		shp.setShapeObjects(arlShapes);
		
		// Write the shapefile. Handle the IOException that may occur. 
		try
		{
			shp.write("PointExample_2");
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
}