📄 tintopoint.java
字号:
/* * Copyright (c) 2006 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 use restrictions at /arcgis/java/samples/userestrictions. *//** * ArcGIS Engine Developer Sample * Application Name: TintoPoint */package arcgissamples.analyst3d.dataconversion;import java.io.File;import java.io.IOException;import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;import com.esri.arcgis.geodatabase.Field;import com.esri.arcgis.geodatabase.Fields;import com.esri.arcgis.geodatabase.GeometryDef;import com.esri.arcgis.geodatabase.IFeatureBuffer;import com.esri.arcgis.geodatabase.IFeatureClass;import com.esri.arcgis.geodatabase.IFeatureCursor;import com.esri.arcgis.geodatabase.ITinNode;import com.esri.arcgis.geodatabase.Tin;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.EngineInitializer;import com.esri.arcgis.system.esriLicenseProductCode;/** * This developer sample takes a base tin with terrain elevation * data and converts the interpolated elevation nodes into a point * featureclass. The input is a base tin with terrain elevation * data, and the output is a 3D point shapefile containing the * interpolated Z points along the tin nodes. */public class TintoPoint { /** * @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 static 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); ISpatialReference tinSpatialRef = tin.getSpatialReference(); Fields fields = createBasicFields(esriGeometryType.esriGeometryPoint, false, true, tinSpatialRef); //Create output shape file ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory(); Workspace workspace = new Workspace(shapefileWorkspaceFactory.openFromFile(shapePath, 0)); IFeatureClass outFeatureClass = workspace.createFeatureClass(shapeFile, fields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); //Get tin node Enum TinNodeEnumerator tinNodeEnumerator = (TinNodeEnumerator) tin.makeNodeEnumerator(tin.getExtent(), esriTinQualification.esriTinInsideDataArea, null); //Store node to shape file IFeatureCursor outCursor = outFeatureClass.IFeatureClass_insert(true); IFeatureBuffer outBuffer = outFeatureClass.createFeatureBuffer(); Point point = new Point(); ITinNode node = tinNodeEnumerator.IEnumTinNode_next(); while (node != null) { node.queryAsPoint(point); outBuffer.setShapeByRef(point); outCursor.insertFeature(outBuffer); node = tinNodeEnumerator.IEnumTinNode_next(); } outCursor.flush(); tin.setEmpty(); }catch (Exception ex) { ex.printStackTrace(); } } /** * This method creates the field collection with OBJECTID and SHAPE fileds. * @param shapeType - a geometry object type * @param hasM - m-value precision defined * @param hasZ - z-value precision defined * @param spatialRef - Spatial Reference * * @return Fields - a collection of columns in a table */ private static Fields createBasicFields(int shapeType, boolean hasM, boolean hasZ, ISpatialReference spatialRef) { try { GeometryDef geometryDef = new GeometryDef(); double gridSize; if (spatialRef.hasXYPrecision()) { double[] xmin = {0}; double[] ymin = {0}; double[] xmax = {0}; double[] ymax = {0}; spatialRef.getDomain(xmin, xmax, ymin, ymax); double dArea = (xmax[0] - xmin[0]) * (ymax[0] - ymin[0]); gridSize = Math.sqrt(dArea / 100); }else { gridSize = 1000; } geometryDef.setGeometryType(shapeType); geometryDef.setHasM(hasM); geometryDef.setHasZ(hasZ); geometryDef.setSpatialReferenceByRef(spatialRef); geometryDef.setGridCount(1); geometryDef.setGridSize(0, gridSize); Fields fields = new Fields(); // add oid field - must come before geometry field Field f1 = new Field(); f1.setName("OBJECTID"); f1.setAliasName("OBJECTID"); f1.setType(esriFieldType.esriFieldTypeOID); fields.addField(f1); //add Geometry field Field f2 = new Field(); f2.setName("SHAPE"); f2.setIsNullable(true); f2.setType(esriFieldType.esriFieldTypeGeometry); f2.setGeometryDefByRef(geometryDef); f2.setRequired(true); fields.addField(f2); return fields; }catch (IOException ex) { ex.printStackTrace(); return null; } } public static void main(String[] args) { System.out.println("Starting Tin To Point - An ArcObjects Java SDK Developer Sample"); String arcGISHome = System.getenv("ARCGISHOME"); if (!(new File(arcGISHome).exists())) { System.out.println(arcGISHome + " does not exist.\nExiting..."); System.exit(-1); } // // Change the following lines if you want to use different data // String inDataPath = arcGISHome + "/java/samples/data/site1"; String inDataName = "dtm_tin"; String outDataPath = arcGISHome + "/java/samples/output_data/tintopoint"; String outDataName = "point.shp"; File outDataDir = new File(outDataPath); outDataDir.mkdir(); File outDatafile = new File(outDataPath, outDataName); 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); } EngineInitializer.initializeEngine(); try { AoInitialize aoInit = new AoInitialize(); aoInit.initialize( esriLicenseProductCode.esriLicenseProductCodeEngine ); tinToPoint( inDataPath, inDataName, outDataPath, outDataName ); System.out.println("Done. Generated Shapefile " + outDatafile.getAbsolutePath()); aoInit.shutdown(); } catch ( Exception e ) { System.out.println("Error: " + e.getMessage()); System.exit(-1); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -