arcengine for java相关环境配置参见我上一篇文章
大概的思路是1、使用gp工具在指定路径创建gdb文件。2、连接上sde(我连接的是oracle)。3、将sde里的表数据导入gdb中。具体代码如下:
/**
* 创建gdb文件并将sde里的图层数据导入到gdb中
* @param gdbPath_ gdb文件的路径,路径不包括 xx.gdb文件
* @param gdbName gdb文件的名称,名称包括.gdb后缀
* @param sdeTableName sde里表的名称
* @param attribute sde里表的数据导入到gdb的限制条件(如 FID=1,多个条件为FID=1 or FID=2 或者是 FID=1 and area=100)
* ,没有就为null(为null的话就将整个图层数据导入gdb)
*/
public static void sdeToGDB(String gdbPath_ , String gdbName , String sdeTableName , String attribute) throws IOException {
//初始换AE
initializeArcGISLicenses();
try {
/**
* 使用gp工具创建gdb文件
*/
GeoProcessor geoProcessor = new GeoProcessor();
CreateFileGDB createFileGDB = new CreateFileGDB(gdbPath_ , gdbName);
geoProcessor.execute(createFileGDB , null);
String gdbPath = gdbPath_+ File.separator +gdbName;
/**
* 将sde里的表数据导入gdb中
*/
Workspace workspace = connectSde();
//指定sde要素名称
workspace.openFeatureClass(sdeTableName);
//指定导出的gdb路径
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
//创建导出数据集所需要的参数
IWorkspace iWorkspace = workspaceFactory.openFromFile(gdbPath, 0);
IWorkspaceName workspaceNameParameter = (IWorkspaceName) workspace.getFullName();
//创建要导出的数据集
IFeatureClassName featureClassName = new FeatureClassName();
IDatasetName datasetName = (IDatasetName) featureClassName;
datasetName.setWorkspaceNameByRef(workspaceNameParameter);
datasetName.setName(sdeTableName);
IDataset dataset = (IDataset) iWorkspace;
IName fullName = dataset.getFullName();
IWorkspaceName workspaceNameTarget = (IWorkspaceName) fullName;
//创建要导入的数据集
IFeatureClassName featureClassNameTarget = new FeatureClassName();
IDatasetName datasetNameTarget = (IDatasetName) featureClassNameTarget;
datasetNameTarget.setWorkspaceNameByRef(workspaceNameTarget);
datasetNameTarget.setName(sdeTableName);
//导入时符合的条件,
IQueryFilter queryFilter = new QueryFilter();
queryFilter.setWhereClause(attribute);
//将sde的数据导入gdb中
IFeatureDataConverter featureDataConverter = new FeatureDataConverter();
featureDataConverter.convertFeatureClass(featureClassName , queryFilter , null ,
featureClassNameTarget , null , null , "" , 1000 , 0);
log.info("添加成功");
}catch (Exception e){
log.info("添加失败");
e.printStackTrace();
}
}
/**
* arcengine创建相关类前的初始化
*/
public static void initializeArcGISLicenses() {
try {
EngineInitializer.initializeEngine();
com.esri.arcgis.system.AoInitialize ao = new com.esri.arcgis.system.AoInitialize();
ao.initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 连接sde 并返回连接sde后的工作空间
*
* @return
* @throws IOException
*/
public static Workspace connectSde() throws IOException {
initializeArcGISLicenses();
Workspace workspace = null;
IPropertySet pPropset = new PropertySet();
SdeWorkspaceFactory pWorkspaceFact = new SdeWorkspaceFactory();
pPropset.setProperty("SERVER", "10.10.1.161");
pPropset.setProperty("INSTANCE", "sde:oracle11g:10.11.11.xxx/ORCL");
pPropset.setProperty("DATABASE", "orcl");
pPropset.setProperty("USER", "data_xxx");
pPropset.setProperty("PASSWORD", "xxx");
pPropset.setProperty("VERSION", "sde.DEFAULT");
log.info("数据源数据库为:10.11.11.xxx");
workspace = new Workspace(pWorkspaceFact.open(pPropset, 0));
return workspace;
}