arcengine for java将postgres中的图层表生成mxd文件

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值