一、arcengine for java 环境配置
引用arcengine的依赖和初始化arcengine报错的解决
二、生成mxd文件代码
package hwhy.dgzh.arcgis.common.util.arcgis;
import com.esri.arcgis.carto.*;
import com.esri.arcgis.datasourcesGDB.SdeWorkspaceFactory;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.Workspace;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.PropertySet;
import com.esri.arcgis.system.esriLicenseProductCode;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.util.HashMap;
@Slf4j
public class CreateMxdUtil {
/**
* @param tableName postgres中的图层表表名
* @param path 生成的mxd文件存放的位置
* @return
* @throws IOException
*/
public static java.util.Map<String , String> getMxd(String tableName , String path) throws IOException {
java.util.Map<String , String> resultMap = new HashMap<>();
log.info("需要生成的mxd名字为:"+tableName+".mxd");
IMapDocument pMapDocument = null;
try {
initializeArcGISLicenses();
SdeWorkspaceFactory sdeFact = new SdeWorkspaceFactory();
PropertySet propSet = new PropertySet();
//postgres的ip
propSet.setProperty("SERVER", "10.10.1.xxx");
postgres的ip加端口
propSet.setProperty("INSTANCE", "sde:postgresql:10.10.1.xxx,5432");
//postgres的库
propSet.setProperty("DATABASE", "hainan_dgzh");
//postgres的username
propSet.setProperty("USER", "postgres");
//postgres的password
propSet.setProperty("PASSWORD", "123456");
//默认写法
propSet.setProperty("VERSION", "sde.DEFAULT");
log.info("数据源数据库为:10.10.1.178,5432");
Workspace ws = new Workspace(sdeFact.open(propSet, 0));
IFeatureClass pFeatureClass= ws.openFeatureClass("postgres."+tableName);
String cc=pFeatureClass.getAliasName();
log.info("已连接上数据库");
//转换ILayer
IFeatureLayer pFeatureLayer = new FeatureLayer();
pFeatureLayer.setFeatureClassByRef(pFeatureClass);
pFeatureLayer.setName(pFeatureClass.getAliasName());
IMap pMap=new Map();
pMap.addLayer((ILayer) pFeatureLayer);
String mxdName = tableName;
//保存文件 arcgis_server_20200420073349
IMxdContents pMxdC = (IMxdContents) pMap;
pMapDocument = new MapDocument();
pMapDocument.esri_new(path+"\\"+mxdName+".mxd");
IActiveView pActiveView = (IActiveView) pMap;
pMapDocument.replaceContents(pMxdC);
pMapDocument.save(true, true);
log.info("已生成mxd文件");
resultMap.put("mxdName" , mxdName);
resultMap.put("flag" , "0");
return resultMap;
}catch (Exception e){
e.printStackTrace();
resultMap.put("flag" , "1");
return resultMap;
}finally {
pMapDocument.close();
}
}
public static void initializeArcGISLicenses() {
try {
EngineInitializer.initializeEngine();
com.esri.arcgis.system.AoInitialize ao = new com.esri.arcgis.system.AoInitialize();
// ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
ao.initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
} catch (Exception e) {
e.printStackTrace();
}
}
}