JAVA 创建Shape文件。并在文件中添加一条折线

import java.io.File;
import java.io.IOException;

import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.FeatureClass;
import com.esri.arcgis.geodatabase.Field;
import com.esri.arcgis.geodatabase.Fields;
import com.esri.arcgis.geodatabase.GeometryDef;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureBuffer;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
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.IWorkspaceFactory;
import com.esri.arcgis.geodatabase.Workspace;
import com.esri.arcgis.geodatabase.esriFeatureType;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geometry.IGeographicCoordinateSystem;
import com.esri.arcgis.geometry.ILine;
import com.esri.arcgis.geometry.Line;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.geometry.SpatialReferenceEnvironment;
import com.esri.arcgis.geometry.UnknownCoordinateSystem;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.geometry.esriSRGeoCSType;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

public class run2 {

	/**
	 * Initialize ArcObjects for Engine, call the method to create a shapefile,
	 * and shutdown.
	 * 
	 * @param args
	 *            shapefile name and path
	 */
	public static void main(String[] args) {
		System.out
				.println("Starting CreateShapefile - An ArcObjects Java SDK Developer Sample");
		String arcGISHome = null;
		try {
			arcGISHome = System.getenv("ARCGISHOME");
			System.out.println("arcGISHome: " + arcGISHome);
		} catch (Error error) { // for Java 1.4
		// BufferedReader console = new BufferedReader(new
		// InputStreamReader(System.in));
		// System.out.print("Please enter the ArcGIS installation directory: ");
		// try {
		// arcGISHome = console.readLine();
		// }
		// catch (IOException exception) {
		// exception.printStackTrace();
		// }
			System.err.println("获取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 outPath = getOutputDir() + "/createshapefile";
		String outName = "newshape.shp";

		File outShapefileDir = new File(outPath);
		outShapefileDir.mkdir();

		File outShapefileFile = new File(outShapefileDir, outName);
		if (outShapefileFile.exists()) {
			System.out.println("Output shapefile already exists: "
					+ outShapefileFile.getAbsolutePath());
			System.out.println("Delete it (plus related files) and rerun");
			System.exit(0);
		}

		try {
			EngineInitializer.initializeEngine();
			AoInitialize aoInitializer = new AoInitialize();
			aoInitializer
					.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
			run2 thisSampleApp = new run2();
			System.out.println("Creating new shapefile "
					+ outShapefileFile.getAbsolutePath());

			// 创建shape文件
			 FeatureClass feature=
			thisSampleApp.createAShapefile(outPath, outName);
			// 添加对应的台风信息
			AddData.addInfo(feature);

			aoInitializer.shutdown();
		} catch (IOException e) {
			System.out.println(e.getMessage());
			System.out.println("Sample failed.  Exiting...");
			System.exit(-1);
		}
		System.out.println("Sample finished.");
	}

	private static String getOutputDir() {
		String userDir;
		if (System.getProperty("os.name").toLowerCase().indexOf("win") > -1)
			userDir = System.getenv("UserProfile");
		else
			userDir = System.getenv("HOME");
		String outputDir = userDir + "/arcgis_sample_output";
		System.out.println("Creating output directory - " + outputDir);
		new File(outputDir).mkdir();
		return outputDir;

	}

	// 创建Shape文件
	private FeatureClass createAShapefile(String shapefilePath,
			String shapefileName) throws IOException {
		final String geometryShapeFieldName = "Shape";
		Workspace workspace = null;
		try {
			//
			// Get a feature workspace from the specified shapefile location
			//
			ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory();
			workspace = new Workspace(shapefileWorkspaceFactory.openFromFile(
					shapefilePath, 0));
		} catch (IOException e) {
			throw e;
		}
		try {

			IFeatureClass iFeatureClass = workspace.createFeatureClass(
					shapefileName, ShapeHeaderName.createHeader(), null, null,
					esriFeatureType.esriFTSimple, geometryShapeFieldName, "");

			return new FeatureClass(iFeatureClass);
		} catch (IOException e) {
			System.out
					.println("Could not create feature class for shapefile named: "
							+ shapefileName);
			throw e;
		}

		
	}
	
	//添加创建Shape对应的信息
	public void addInfo(FeatureClass featureClass) throws IOException {
		IFields fields = featureClass.getFields();
        //int lngIndex = fields.findField("TextField");
        int latIndex = fields.findField("eeee");
        
        Point point = new Point();
        point.setX(100);
        point.setY(100);
        //
        // Create a new feature and add the point's geometry to it
        //
        IFeature feature = featureClass.createFeature();
        //feature.setShapeByRef(point);
        //
        // Add longitude, latitude, and value values to their individual fields
        //
        // feature.setValue(lngIndex, "sdfsdsf");
        feature.setValue(latIndex,"safassdfsf");
        
       
		
        feature.store();
        
	}
	
	public void finalize(){
		System.out.println("此方法被调用");
	}
	
}

 

 

import java.io.IOException;

import com.esri.arcgis.geodatabase.Feature;
import com.esri.arcgis.geodatabase.FeatureClass;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureBuffer;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.geometry.ILine;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.Line;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.geometry.Polyline;
import com.esri.arcgis.interop.AutomationException;


public class AddData {
	
	/**
	 * 功能说明:往表中添加数据
	 */
	public static void addInfo(FeatureClass featureClass){
		
		try {
			IFields fields = featureClass.getFields();
			int ShapeIndex = fields.findField("Shape");
	        int F_YEARIndex = fields.findField("F_YEAR");
	        int F_NATIONCODEIndex = fields.findField("F_ZYCODE");
	        int F_INTERNATIONCODEIndex = fields.findField("F_GJCODE");
	        int F_CHINESENAMEIndex = fields.findField("F_ZWNAME");
	        int F_ENGNAMEIndex = fields.findField("F_YWNAME");
	        int F_CHINEIDIndex = fields.findField("F_LJID");
	        int F_GENERATIONTIMEIndex = fields.findField("F_LJNYR");
	        int F_LANDINGPLACEIndex = fields.findField("F_DLDD");
	        int F_LANDINGTIMEIndex = fields.findField("F_DLSJ");
	        int F_ROUTELONGIndex = fields.findField("F_LJLONG");
	        int F_ROUTELATIIndex = fields.findField("F_LJLATI");
	        int F_TYPHOONCLASSIndex = fields.findField("F_TFJB");
	        int F_CENTERATMOSPRESS = fields.findField("F_ZXQY");
	        int F_BIGGESTWEEDSPEEDIndex = fields.findField("F_ZDFS");
	        int F_MOVEDIREIndex = fields.findField("F_YDFX");
	        int F_MOVESPEEDIndex = fields.findField("F_YDSD");
	        int F_STYLEIndex = fields.findField("F_TFTYPE");
	        int F_TYPHOONRADIUSIndex = fields.findField("F_TFBJ");
	        int F_NUMCHINEIDIndex = fields.findField("F_SJID");
	        int F_DATEIndex = fields.findField("F_SCTIME");
	        
//	        Point[] pointes=new Point[2];
//	        for(int i=0;i<2;i++){
//	        	pointes[i]=new Point();
//	        }
//	        ILine pLine= new Line(); 
//	        pointes[0].putCoords(100,100);
//	        pointes[1].putCoords(100,99);
//	        pLine.putCoords(pointes[0], pointes[1]);
//	        IFeatureBuffer pFeatureBuffer = featureClass.createFeatureBuffer();
//	        pFeatureBuffer.setShapeByRef(pLine);
			
	        //Feature featurew = (Feature) featureClass.createFeature();
			
			Polyline polyline  = new Polyline();
			
			IPoint point = new Point();
			point.putCoords(496508.604 , 3417364.905);
			
			Point point2 = new Point();
			point2.putCoords(496444.663 ,3417347.189);
			
			Point point3 = new Point();
			point3.putCoords(496433.592 , 3417369.056);
			
			Point point4 = new Point();
			point4.putCoords(496413.592, 3417369.056);
			polyline.addPoint(point, null, null);
			polyline.addPoint(point2, null, null);
			polyline.addPoint(point3, null, null);
			polyline.addPoint(point4, null, null);
			
	     
	        IFeature feature = featureClass.createFeature();
	        feature.setShapeByRef(polyline);
	        //feature.setShapeByRef(point);
	        //feature.setValue(ShapeIndex,pLine);
	        feature.setValue(F_YEARIndex,"2011");
	        feature.setValue(F_NATIONCODEIndex,201110L);
	        feature.setValue(F_INTERNATIONCODEIndex,0L);
	        feature.setValue(F_CHINESENAMEIndex,"苗柏");
	        feature.setValue(F_ENGNAMEIndex,"MERBOK");
	        feature.setValue(F_CHINEIDIndex,0L);
	        feature.setValue(F_GENERATIONTIMEIndex,0L);
	        feature.setValue(F_LANDINGPLACEIndex,"8月8日14时");
	        feature.setValue(F_LANDINGTIMEIndex,"157.1");
	        feature.setValue(F_ROUTELONGIndex,"35.4");
	        feature.setValue(F_ROUTELATIIndex,"10");
	        feature.setValue(F_TYPHOONCLASSIndex,"980");
	        feature.setValue(F_CENTERATMOSPRESS,"28");
	        feature.setValue(F_BIGGESTWEEDSPEEDIndex,30L);
	        feature.setValue(F_MOVEDIREIndex,"北东");
	        feature.setValue(F_MOVESPEEDIndex,0L);
	        feature.setValue(F_STYLEIndex,0L);
	        feature.setValue(F_TYPHOONRADIUSIndex,0L);
	        feature.setValue(F_NUMCHINEIDIndex,0L);
	        feature.setValue(F_DATEIndex,0L);
	        
	        IFeature features = featureClass.createFeature();
	        //feature.setShapeByRef(point);
	      
	        features.setValue(F_YEARIndex,"2011");
	        features.setValue(F_NATIONCODEIndex,201110L);
	        features.setValue(F_INTERNATIONCODEIndex,0L);
	        features.setValue(F_CHINESENAMEIndex,"苗柏");
	        features.setValue(F_ENGNAMEIndex,"MERBOK");
	        features.setValue(F_CHINEIDIndex,0L);
	        features.setValue(F_GENERATIONTIMEIndex,0L);
	        features.setValue(F_LANDINGPLACEIndex,"8月8日14时");
	        features.setValue(F_LANDINGTIMEIndex,"158.1");
	        features.setValue(F_ROUTELONGIndex,"36.4");
	        features.setValue(F_ROUTELATIIndex,"10");
	        features.setValue(F_TYPHOONCLASSIndex,"980");
	        features.setValue(F_CENTERATMOSPRESS,"28");
	        features.setValue(F_BIGGESTWEEDSPEEDIndex,30L);
	        features.setValue(F_MOVEDIREIndex,"北东");
	        features.setValue(F_MOVESPEEDIndex,0L);
	        features.setValue(F_STYLEIndex,0L);
	        features.setValue(F_TYPHOONRADIUSIndex,0L);
	        features.setValue(F_NUMCHINEIDIndex,0L);
	        features.setValue(F_DATEIndex,0L);
	        
	        feature.store();
	        features.store();
	        
	        //IFeatureBuffer pFeatureBuffer = featureClass.createFeatureBuffer();
			//pFeatureBuffer.setShapeByRef(pLine);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
       
	}
}

 

package com.hjkj.action.infoAna.topicmap;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.geodatabase.Field;
import com.esri.arcgis.geodatabase.Fields;
import com.esri.arcgis.geodatabase.GeometryDef;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geometry.IGeographicCoordinateSystem;
import com.esri.arcgis.geometry.SpatialReferenceEnvironment;
import com.esri.arcgis.geometry.UnknownCoordinateSystem;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.geometry.esriSRGeoCSType;

public class ShapeHeaderName {

	/**
	 * ����˵����������Ҫ������Shape�ļ�ͷ������
	 * 
	 * @return Fields
	 */
	public Fields createHeader() {

		try {

			// ���
			Field F_YEAR = new Field();
			F_YEAR.setLength(50);
			F_YEAR.setName("F_YEAR");
			F_YEAR.setType(esriFieldType.esriFieldTypeString);

			// ̨������̨���
			Field F_NATIONCODE = new Field();
			F_NATIONCODE.setLength(4);
			F_NATIONCODE.setName("F_ZYCODE");
			F_NATIONCODE.setType(esriFieldType.esriFieldTypeInteger);

			// ̨���ʱ��
			Field F_INTERNATIONCODE = new Field();
			F_INTERNATIONCODE.setLength(4);
			F_INTERNATIONCODE.setName("F_GJCODE");
			F_INTERNATIONCODE.setType(esriFieldType.esriFieldTypeInteger);

			// �������
			Field F_CHINESENAME = new Field();
			F_CHINESENAME.setLength(50);
			F_CHINESENAME.setName("F_ZWNAME");
			F_CHINESENAME.setType(esriFieldType.esriFieldTypeString);

			// Ӣ�����
			Field F_ENGNAME = new Field();
			F_ENGNAME.setLength(255);
			F_ENGNAME.setName("F_YWNAME");
			F_ENGNAME.setType(esriFieldType.esriFieldTypeString);

			// ·����ϢID
			Field F_CHINEID = new Field();
			F_CHINEID.setLength(4);
			F_CHINEID.setName("F_LJID");
			F_CHINEID.setType(esriFieldType.esriFieldTypeInteger);

			// ·��λ��_ʱ��������
			Field F_GENERATIONTIME = new Field();
			F_GENERATIONTIME.setLength(255);
			F_GENERATIONTIME.setName("F_LJNYR");
			F_GENERATIONTIME.setType(esriFieldType.esriFieldTypeString);

			// ��¼�ص�
			Field F_LANDINGPLACE = new Field();
			F_LANDINGPLACE.setLength(255);
			F_LANDINGPLACE.setName("F_DLDD");
			F_LANDINGPLACE.setType(esriFieldType.esriFieldTypeString);

			// ��¼ʱ��
			Field F_LANDINGTIME = new Field();
			F_LANDINGTIME.setLength(255);
			F_LANDINGTIME.setName("F_DLSJ");
			F_LANDINGTIME.setType(esriFieldType.esriFieldTypeString);

			// ·��λ��_����
			Field F_ROUTELONG = new Field();
			F_ROUTELONG.setLength(9);
			F_ROUTELONG.setName("F_LJLONG");
			F_ROUTELONG.setType(esriFieldType.esriFieldTypeDouble);

			// ·��λ��_γ��
			Field F_ROUTELATI = new Field();
			F_ROUTELATI.setLength(9);
			F_ROUTELATI.setName("F_LJLATI");
			F_ROUTELATI.setType(esriFieldType.esriFieldTypeDouble);

			// ��ǰ̨�缶��
			Field F_TYPHOONCLASS = new Field();
			F_TYPHOONCLASS.setLength(255);
			F_TYPHOONCLASS.setName("F_TFJB");
			F_TYPHOONCLASS.setType(esriFieldType.esriFieldTypeString);

			// ������ѹ����
			Field F_CENTERATMOSPRESS = new Field();
			F_CENTERATMOSPRESS.setLength(9);
			F_CENTERATMOSPRESS.setName("F_ZXQY");
			F_CENTERATMOSPRESS.setType(esriFieldType.esriFieldTypeDouble);

			// ��������/��
			Field F_BIGGESTWEEDSPEED = new Field();
			F_BIGGESTWEEDSPEED.setLength(4);
			F_BIGGESTWEEDSPEED.setName("F_ZDFS");
			F_BIGGESTWEEDSPEED.setType(esriFieldType.esriFieldTypeInteger);

			// �ƶ�����
			Field F_MOVEDIRE = new Field();
			F_MOVEDIRE.setLength(50);
			F_MOVEDIRE.setName("F_YDFX");
			F_MOVEDIRE.setType(esriFieldType.esriFieldTypeString);

			// �ƶ��ٶȹ���/Сʱ
			Field F_MOVESPEED = new Field();
			F_MOVESPEED.setLength(4);
			F_MOVESPEED.setName("F_YDSD");
			F_MOVESPEED.setType(esriFieldType.esriFieldTypeInteger);

			// ̨������
			Field F_STYLE = new Field();
			F_STYLE.setLength(50);
			F_STYLE.setName("F_TFTYPE");
			F_STYLE.setType(esriFieldType.esriFieldTypeString);

			// ̨��뾶����
			Field F_TYPHOONRADIUS = new Field();
			F_TYPHOONRADIUS.setLength(4);
			F_TYPHOONRADIUS.setName("F_TFBJ");
			F_TYPHOONRADIUS.setType(esriFieldType.esriFieldTypeInteger);

			// ���ID
			Field F_NUMCHINEID = new Field();
			F_NUMCHINEID.setLength(4);
			F_NUMCHINEID.setName("F_SJID");
			F_NUMCHINEID.setType(esriFieldType.esriFieldTypeInteger);

			// ���ʱ��
			Field F_DATE = new Field();
			F_DATE.setLength(50);
			F_DATE.setName("F_SCTIME");
			F_DATE.setType(esriFieldType.esriFieldTypeString);
			
			Field F_XSD = new Field();
			F_XSD.setLength(9);
			F_XSD.setName("F_XSD");
			F_XSD.setType(esriFieldType.esriFieldTypeDouble);
			//
			// Create a Fields object and add the fields to it.
			//
			Fields fields = new Fields();
			fields.addField(DefinitionShapeType());
			fields.addField(F_YEAR);
			fields.addField(F_NATIONCODE);
			fields.addField(F_INTERNATIONCODE);
			fields.addField(F_CHINESENAME);
			fields.addField(F_ENGNAME);
			fields.addField(F_CHINEID);
			fields.addField(F_GENERATIONTIME);
			fields.addField(F_LANDINGPLACE);
			fields.addField(F_LANDINGTIME);
			fields.addField(F_ROUTELONG);
			fields.addField(F_ROUTELATI);
			fields.addField(F_TYPHOONCLASS);
			fields.addField(F_CENTERATMOSPRESS);
			fields.addField(F_BIGGESTWEEDSPEED);
			fields.addField(F_MOVEDIRE);
			fields.addField(F_MOVESPEED);
			fields.addField(F_STYLE);
			fields.addField(F_TYPHOONRADIUS);
			fields.addField(F_NUMCHINEID);
			fields.addField(F_DATE);
			fields.addField(F_XSD);
			return fields;
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * ����˵��������shape�ļ������Ͷ���
	 * 
	 * @return Field
	 */
	public static Field DefinitionShapeType() {
		try {
			GeometryDef geometryDef = new GeometryDef();
			geometryDef.setGeometryType(esriGeometryType.esriGeometryPolyline);
			
			SpatialReferenceEnvironment spatialReferenceEnvironment = new SpatialReferenceEnvironment();
			IGeographicCoordinateSystem geographicCoordinateSystem = spatialReferenceEnvironment
			.createGeographicCoordinateSystem(esriSRGeoCSType.esriSRGeoCS_WGS1984);
			geometryDef.setSpatialReferenceByRef(geographicCoordinateSystem);
			Field geometryShapeField = new Field();
			geometryShapeField.setName("Shape");
			geometryShapeField.setType(esriFieldType.esriFieldTypeGeometry);
			geometryShapeField.setGeometryDefByRef(geometryDef);
			return geometryShapeField;
		} catch (Exception e) {

		}
		return null;

	}
}

 

你可能感兴趣的:(shape)