⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createshapefile.java

📁 使用Acrgis Engine 的java api 开发的创建一个shape文件
💻 JAVA
字号:
/*
 Copyright 1995-2004 ESRI

 All rights reserved under the copyright laws of the United States.

 You may freely redistribute and use this sample code, with or without modification.

 Disclaimer: THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 
 WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESRI OR 
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED AND ON ANY 
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING IN ANY 
 WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 SUCH DAMAGE.

 For additional information contact: Environmental Systems Research Institute, Inc.

 Attn: Contracts Dept.

 380 New York Street

 Redlands, California, U.S.A. 92373 

 Email: contracts@esri.com
*/
/*
 * Application Name: CreateShapefile.java
 * Creation Modification History
 * Aug 07,2003         created
 * Aug 18, 2003        refactored
 * Sep 22, 2003        added AoInitialize support for ArcGIS Engine/Desktop
 */


import java.io.File;

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.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IFeatureWorkspaceProxy;
import com.esri.arcgis.geodatabase.IField;
import com.esri.arcgis.geodatabase.IFieldEdit;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.geodatabase.IFieldsEdit;
import com.esri.arcgis.geodatabase.IGeometryDef;
import com.esri.arcgis.geodatabase.IGeometryDefEdit;
import com.esri.arcgis.geodatabase.IWorkspaceFactory;
import com.esri.arcgis.geodatabase.esriFeatureType;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geometry.UnknownCoordinateSystem;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

/**
 *
 *Description:    This sample will demonstrate the steps needed to create a new shapefile.
 *
 * @param       folderName - a directory path on disk where the source shapefile has to be craeted
 * @param       shapeName - the name of the source shapefile
 */
public class CreateShapefile {
    public static void create(String folderName,String shapeName){
        try{
            String strFolder = folderName;
            String strName = shapeName;
            String strShapeFieldName= "Shape";

            //Open the folder to contain the shapefile as a workspace
            System.out.println("Open " + strFolder +" to contain the shapefile as a workspace");
            IFeatureWorkspace pFWS = null;
            IWorkspaceFactory pWorkspaceFactory = null;
            pWorkspaceFactory = new ShapefileWorkspaceFactory();
            pFWS = new IFeatureWorkspaceProxy(pWorkspaceFactory.openFromFile(strFolder, 0));

            //Set up a simple fields collection
            IFields pFields = null;
            IFieldsEdit pFieldsEdit = null;
            pFields = new Fields();
            pFieldsEdit = (IFieldsEdit) pFields;

            IField pField = null;
            IFieldEdit pFieldEdit = null;

            //Make the shape field it will need a geometry definition, with a spatial reference
            pField = new Field();
            pFieldEdit = (IFieldEdit) pField;
            pFieldEdit.setName(strShapeFieldName);
            pFieldEdit.setType(esriFieldType.esriFieldTypeGeometry);

            IGeometryDef pGeomDef = null;
            IGeometryDefEdit pGeomDefEdit = null;
            pGeomDef = new GeometryDef();
            pGeomDefEdit = (IGeometryDefEdit) pGeomDef;
            pGeomDefEdit.setGeometryType(esriGeometryType.esriGeometryPolygon);
            pGeomDefEdit.setSpatialReferenceByRef(new UnknownCoordinateSystem());

            pFieldEdit.setGeometryDefByRef(pGeomDef);
            pFieldsEdit.addField(pField);


            //Add another miscellaneous text field
            pField = new Field();
            pFieldEdit = (IFieldEdit) pField;
            pFieldEdit.setLength(30);
            pFieldEdit.setName("TextField");
            pFieldEdit.setType(esriFieldType.esriFieldTypeString);
            pFieldsEdit.addField(pField);

            //Create the shapefile (some parameters apply to geodatabase options and can be defaulted as Nothing)
            System.out.println("Create " + strName);
            IFeatureClass pFeatClass = null;
            pFeatClass = pFWS.createFeatureClass(strName,  pFields, null, null,esriFeatureType.esriFTSimple, strShapeFieldName, "");
        }catch (java.lang.Exception e){
            System.out.println("Exception :"+ e.getMessage());
        }
    }

    public static void main(String[] args){
        if(args.length != 1) {
            System.out.println("CreateShapefile - ArcGIS Engine Developer Sample");
            System.out.println("Usage: CreateShapefile [Shapefile Path]");
            System.exit(0);
        }
        System.out.println("CreateShapefile - ArcGIS Engine Developer Sample");
        EngineInitializer.initializeEngine();
        initializeArcObjects();

        String inShape = args[0];

        String shpPath = inShape.substring(0, inShape.lastIndexOf(File.separator));
        String shpName = inShape.substring(inShape.lastIndexOf(File.separator) + 1);

        create(shpPath,shpName);

        System.out.println("Done!");
    }

    /**
     * @return
     */
    private static void initializeArcObjects(){
        try {
            aoInit = new AoInitialize();

            if( aoInit.isProductCodeAvailable( esriLicenseProductCode.esriLicenseProductCodeEngine ) == esriLicenseStatus.esriLicenseAvailable ){
                aoInit.initialize( esriLicenseProductCode.esriLicenseProductCodeEngine );
            }else if( aoInit.isProductCodeAvailable( esriLicenseProductCode.esriLicenseProductCodeArcView ) == esriLicenseStatus.esriLicenseAvailable ){
                aoInit.initialize( esriLicenseProductCode.esriLicenseProductCodeArcView );
            }else if( aoInit.isProductCodeAvailable( esriLicenseProductCode.esriLicenseProductCodeArcEditor ) == esriLicenseStatus.esriLicenseAvailable ){
                aoInit.initialize( esriLicenseProductCode.esriLicenseProductCodeArcEditor );
            }else if( aoInit.isProductCodeAvailable( esriLicenseProductCode.esriLicenseProductCodeArcInfo ) == esriLicenseStatus.esriLicenseAvailable ){
                aoInit.initialize( esriLicenseProductCode.esriLicenseProductCodeArcInfo );
            }else{
                System.out.println(" Program Exit: Unable initialize ArcObjects ");
                System.exit(0);
            }
        } catch ( Exception e ) {
            System.out.println(" Program Exit: Unable initialize ArcObjects ");
            System.exit(0);
        } // end try - catch
    } // end initializeArcObjects

    private static AoInitialize aoInit;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -