arcgissamples\analyst3d\ConvertTinToPoint.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ package arcgissamples.analyst3d; import java.io.File; import java.io.IOException; import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory; import com.esri.arcgis.geodatabase.Feature; import com.esri.arcgis.geodatabase.FeatureClass; import com.esri.arcgis.geodatabase.FeatureCursor; import com.esri.arcgis.geodatabase.Field; import com.esri.arcgis.geodatabase.Fields; import com.esri.arcgis.geodatabase.GeometryDef; import com.esri.arcgis.geodatabase.Tin; import com.esri.arcgis.geodatabase.TinNode; import com.esri.arcgis.geodatabase.TinNodeEnumerator; import com.esri.arcgis.geodatabase.Workspace; import com.esri.arcgis.geodatabase.esriFeatureType; import com.esri.arcgis.geodatabase.esriFieldType; import com.esri.arcgis.geodatabase.esriTinQualification; import com.esri.arcgis.geometry.ISpatialReference; import com.esri.arcgis.geometry.Point; import com.esri.arcgis.geometry.esriGeometryType; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.Cleaner; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseExtensionCode; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; public class ConvertTinToPoint { public ConvertTinToPoint(){ } /** * Main Method - The console application entry point. * * @param args String[] Command line argument */ public static void main(String[] args) { System.out.println("Starting ConvertTinToPoint - An ArcObjects SDK Developer Sample"); try{ //Initialize engine console application EngineInitializer.initializeEngine(); //Initialize ArcGIS license AoInitialize aoInit = new AoInitialize(); initializeArcGISLicenses(aoInit); //Get DEVKITHOME Home String devKitHome = System.getenv("AGSDEVKITJAVA"); //Data access setup String inputPath = devKitHome + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "site1"; String inputTin = "dtm_tin"; //Data output setup String outputPath = getOutputDir() + File.separator + "tintopoint"; String outputShapefile = "point.shp"; File outDataDir = new File(outputPath); outDataDir.mkdir(); File outDatafile = new File(outputPath, outputShapefile); if (outDatafile.exists()) { System.out.println("Output datafile already exists: " + outDatafile.getAbsolutePath()); System.out.println("Delete it (plus .shx and .dbf files) and rerun"); System.exit(-1); } ConvertTinToPoint convertTinToPoint = new ConvertTinToPoint(); convertTinToPoint.tinToPoint(inputPath, inputTin, outputPath, outputShapefile); System.out.println("Done. Output shapefile created in " + outDatafile.getAbsolutePath()); //Ensure any ESRI libraries are unloaded in the correct order aoInit.checkInExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst); aoInit.shutdown(); }catch(Exception e){ System.out.println("Error: " + e.getMessage()); e.printStackTrace(); System.exit(-1); } } /** * Checks to see if an ArcGIS Engine Runtime license or an ArcView License * is available. If so, then the appropriate ArcGIS License is initialized. * * @param aoInit The AoInitialize object instantiated in the main method. */ private static void initializeArcGISLicenses(AoInitialize aoInit) { try { if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView); else{ System.err.println("Could not initialize an Engine or ArcView license. Exiting application."); System.exit(-1); } aoInit.checkOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst); } catch (Exception e) {e.printStackTrace();} } /** * Converts an existing Tin file to a new point shapefile * * @param tinPath - path to input tin data source * @param tinName - name of input tin data source * @param shapePath - path to output shapefile data source * @param shapeFile - name of output shapefile data source */ private void tinToPoint(String tinPath, String tinName, String shapePath, String shapeFile){ try{ //Get tin from tin file Tin tin = new Tin(); tin.init(tinPath + File.separator + tinName); //Get existing Tin's spatial reference and use when defining the new //shapefile's spatial reference. ISpatialReference tinSpatialRef = tin.getSpatialReference(); Fields fields = createBasicFields(esriGeometryType.esriGeometryPoint, false, true, tinSpatialRef); //Create the output shapefile ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory(); Workspace workspace = new Workspace(shapefileWorkspaceFactory.openFromFile(shapePath, 0)); FeatureClass outFeatureClass = new FeatureClass( workspace.createFeatureClass(shapeFile, fields, null, null, esriFeatureType.esriFTSimple, "Shape", "")); //Get tin node Enumeration based on the extent of the Tin's envelope TinNodeEnumerator tinNodeEnumerator = (TinNodeEnumerator) tin.makeNodeEnumerator(tin.getExtent(), esriTinQualification.esriTinInsideDataArea, null); //Create an insert feature cursor for the new shapefile FeatureCursor outCursor = new FeatureCursor(outFeatureClass.IFeatureClass_insert(true)); //Create a feature buffer when creating new features with an insert cursor Feature outputBuffer = (Feature) outFeatureClass.createFeatureBuffer(); //Get the first Tin node TinNode node = (TinNode) tinNodeEnumerator.IEnumTinNode_next(); //While the TinNode is not empty continue processing each node while (node != null) { //Instantiate point object for a place holder Point point = new Point(); //Writes the X, Y and Z values of the node to an existing, //pre-instantiated, Point object. node.queryAsPoint(point); //Set the geometry for the feature buffer outputBuffer.setShapeByRef(point); //Insert the point's geometry into the new shapefile outCursor.insertFeature(outputBuffer); //Get the next node node = (TinNode) tinNodeEnumerator.IEnumTinNode_next(); Cleaner.release(point); } //Flush the insert cursor outCursor.flush(); //Release the cursor after all inserts are made Cleaner.release(outCursor); //Uninitialize the Tin tin.setEmpty(); }catch (Exception ex) { System.out.println("Unable to convert Tin to Point.\n Exiting...."); ex.printStackTrace(); System.exit(-1); } } /** * This method creates the Fields collection with OID and Shape fields. * * @param shapeType A geometry object type (e.g. Point, Polyline, etc.) * @param hasM m-value precision defined * @param hasZ z-value precision defined * @param spatialRef Spatial Reference to be used when defining the Shape field * * @return Fields A collection of columns in a table. In this case the sample will * will return two fields: OID and Shape fields. */ private static Fields createBasicFields(int shapeType, boolean hasM, boolean hasZ, ISpatialReference spatialRef) { Fields fields = null; try { //Get the grid size if the spatial reference has XY precision; //otherwise, set the grid size to 1000. double gridSize; if (spatialRef.hasXYPrecision()) { double[] xmin = {0}; double[] ymin = {0}; double[] xmax = {0}; double[] ymax = {0}; //Get the spatial reference's square domain spatialRef.getDomain(xmin, xmax, ymin, ymax); //Calculate the area based on the domain and set the grid size double dArea = (xmax[0] - xmin[0]) * (ymax[0] - ymin[0]); gridSize = Math.sqrt(dArea / 100); }else { gridSize = 1000; } //Define the geometry for the shape field GeometryDef geometryDef = new GeometryDef(); geometryDef.setGeometryType(shapeType); geometryDef.setHasM(hasM); geometryDef.setHasZ(hasZ); geometryDef.setSpatialReferenceByRef(spatialRef); geometryDef.setGridCount(1); geometryDef.setGridSize(0, gridSize); //Define the OID field - must come before geometry field Field oidField = new Field(); oidField.setName("OBJECTID"); oidField.setAliasName("OBJECTID"); oidField.setType(esriFieldType.esriFieldTypeOID); //Define the Geometry (i.e. Shape) field Field shapeField = new Field(); shapeField.setName("Shape"); shapeField.setIsNullable(true); shapeField.setType(esriFieldType.esriFieldTypeGeometry); shapeField.setGeometryDefByRef(geometryDef); shapeField.setRequired(true); //Create fields colleciton object and add fields fields = new Fields(); fields.addField(oidField); fields.addField(shapeField); }catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); System.exit(-1); } //Return the new fields collection return fields; } /** * Convenience method to generate an output directory based on the operating * system that the sample is being executed on. * * @return A path to the new directory is return */ private static String getOutputDir() { String userDir; //Get the operating systems user profile or home location depending //on which operating system this sample is executed on. if(System.getProperty("os.name").toLowerCase().indexOf("win") > -1){ userDir = System.getenv("UserProfile"); }else{ userDir = System.getenv("HOME"); } String outputDir = userDir + File.separator + "arcgis_sample_output"; System.out.println("Creating output directory - " + outputDir); new File(outputDir).mkdir(); return outputDir; } }