/***************************************************************** * PolygonExample.java * Just an example of how to write a Polygon Shapefile with the * NVS Shapefile library, v2.0 * ****************************************************************/ import java.io.*; import com.nvs.shapefile.*; import java.util.*; import java.math.*; public class PolygonExample { public static void main(String args[]) { new PolygonExample(); } public PolygonExample() { // Create a new Shapefile of type POLYGON Shapefile shp = new Shapefile(Shapefile.SHAPETYPE_POLYGON); // 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(); // Instead of adding points one at a time, we'll add a collection this time // loop through 360 times, adding a point to the ArrayList each time to // make a circle ArrayList arlPoints = new ArrayList(); for(int i=0;i<360;i++) { double radians = Math.toRadians((double)i); arlPoints.add(new Point(Math.cos(radians), Math.sin(radians))); } // Set the new ShapeObject's points to the ArrayList shpObj.setPoints(arlPoints); shp.addShapeObject(shpObj); // Write the shapefile. Handle the IOException that may occur. Note that // the BoundingBox's for this shapefile and for each shapeobject have been // calculated as they were added/writen. try { shp.write("PolygonExample"); } catch(IOException e) { e.printStackTrace(); } } }