本人将shp文件导入到空间数据库,发生错误,一直没解决,请各位帮助。
private void IFeatureDataConverter_ConvertFeatureClass_Example(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, String nameOfSourceFeatureClass, String nameOfTargetFeatureClass){
try{
//create source workspace name
IFeatureDataset pFeatureDataset=((IFeatureWorkspace) targetWorkspace).openFeatureDataset("TestArcGisFile"); //存在数据库的要素数据集;
IFeatureDatasetName pOutFeatureDSName = (IFeatureDatasetName)new FeatureDatasetName();
pOutFeatureDSName = (IFeatureDatasetName)pFeatureDataset.getFullName();
IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
IName sourceWorkspaceDatasetName = sourceWorkspaceDataset.getFullName();
IName targetWorkspaceDatasetName = targetWorkspaceDataset.getFullName();
IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDatasetName;
sourceWorkspaceName.setWorkspaceFactoryProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
sourceWorkspaceName.setPathName(shapeFileFolder);
IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDatasetName;
//create source dataset name
IFeatureClassName sourceFeatureClassName = new FeatureClassName();
IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;
sourceDatasetName.setWorkspaceNameByRef(sourceWorkspaceName);
sourceDatasetName.setName(nameOfSourceFeatureClass);
//create target dataset name
IFeatureClassName targetFeatureClassName = new FeatureClassName();
IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName;
targetDatasetName.setWorkspaceNameByRef(targetWorkspaceName);
targetDatasetName.setName(nameOfTargetFeatureClass);
//Open input Featureclass to get field definitions.
IName sourceName = (IName)sourceWorkspaceName;
System.out.println("sourceName.open()==="+sourceName.open());
IFeatureClass sourceFeatureClass =(IFeatureClass)sourceName.open(); ///出现异常
//Validate the field names because you are converting between different workspace types.
IFieldChecker fieldChecker = new FieldChecker();
IFields targetFeatureClassFields[] = new IFields[1];
IFields sourceFeatureClassFields = sourceFeatureClass.getFields();
IEnumFieldError[] enumFieldError = new IEnumFieldError[1];
// Most importantly set the input and validate workspaces!
fieldChecker.setInputWorkspace(sourceWorkspace);
fieldChecker.setValidateWorkspaceByRef(targetWorkspace);
fieldChecker.validate(sourceFeatureClassFields,enumFieldError,targetFeatureClassFields);
if (enumFieldError != null){
System.out.println("Errors were encountered during field validation.");
}
String shapeFieldName = sourceFeatureClass.getShapeFieldName();
int shapeFieldIndex = sourceFeatureClass.findField(shapeFieldName);
IField shapeField = sourceFeatureClassFields.getField(shapeFieldIndex);
// Get the geometry definition from the shape field and clone it.
IGeometryDef geometryDef = shapeField.getGeometryDef();
IClone geometryDefClone = (IClone)geometryDef;
IClone targetGeometryDefClone = geometryDefClone.esri_clone();
IGeometryDef targetGeometryDef = (IGeometryDef)targetGeometryDefClone;
// Cast the IGeometryDef to the IGeometryDefEdit interface.
IGeometryDefEdit targetGeometryDefEdit = (IGeometryDefEdit)
targetGeometryDef;
// Set the IGeometryDefEdit properties.
targetGeometryDefEdit.setGridCount(1);
targetGeometryDefEdit.setGridSize(0, 0.75);
IQueryFilter queryFilter = new QueryFilter();
queryFilter.setWhereClause("");
IFeatureDataConverter featureDataConverter = new FeatureDataConverter();
IEnumInvalidObject enumInvalidObject=featureDataConverter.convertFeatureClass(sourceFeatureClassName,
queryFilter, pOutFeatureDSName, targetFeatureClassName, targetGeometryDef,targetFeatureClassFields[0], "", 1000, 0);
// Check for errors.
IInvalidObjectInfo invalidObjectInfo = null;
enumInvalidObject.reset();
while ((invalidObjectInfo = enumInvalidObject.next()) != null){
System.out.println("Errors occurred for the following feature: " +invalidObjectInfo.getInvalidObjectID());
}
System.out.println("导入成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("导入失败");
}
}
错误异常为:
java.lang.ClassCastException: com.esri.arcgis.interop.NativeObjRef cannot be cast to com.esri.arcgis.geodatabase.IFeatureClass
at com.trendcom.shapeFile.ShapeFileOpration.IFeatureDataConverter_ConvertFeatureClass_Example(ShapeFileOpration.java:123)
at com.trendcom.shapeFile.ShapeFileOpration.ConvertShapeFile2GDB(ShapeFileOpration.java:51)
at com.trendcom.shapeFile.ShapeFileOpration.main(ShapeFileOpration.java:45)