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();
}
}
}