赞
踩
1、可以现在自己电脑上的arcgis上连接下sde试试看能不能连上,如果自己的arcgis都连不上sde,代码里肯定也连不上。
2、arcengine连接sde的相关代码如下:
/** * 连接sde 并返回连接sde后的工作空间 * * @return * @throws IOException */ public static Workspace connectSde() throws IOException { initializeArcGISLicenses(); Workspace workspace = null; IPropertySet pPropset = new PropertySet(); // IWorkspaceFactory pWorkspaceFact = new SdeWorkspaceFactory(); SdeWorkspaceFactory pWorkspaceFact = new SdeWorkspaceFactory(); // Populate the property set with the connection parameters pPropset.setProperty("SERVER", "10.10.1.161"); // propSet.setProperty("INSTANCE", "sde:postgresql:10.10.1.178,5432"); pPropset.setProperty("INSTANCE", "sde:oracle11g:10.10.1.258/ORCL"); pPropset.setProperty("DATABASE", "orcl"); pPropset.setProperty("USER", "data_check"); pPropset.setProperty("PASSWORD", "123456"); pPropset.setProperty("VERSION", "sde.DEFAULT"); log.info("数据源数据库为:10.10.1.258,1521"); // Open the ArcSDE workspace using the connection PropertySet // workspace = pWorkspaceFact.open(pPropset, 0); workspace = new Workspace(pWorkspaceFact.open(pPropset, 0)); return workspace; }
/** * 在 sde 里创建 table * * @param gdbPath * @param FcName 图层名 * @param pWorkspace 连接sde的工作空间 * @param tableName 生成的表明 */ public static void createTable(String gdbPath, String FcName, IWorkspace pWorkspace, String tableName) throws IOException { initializeArcGISLicenses(); try { //获取gdb里的featureClass //创建 gdb工作空间 FileGDBWorkspaceFactory pFileGDBWorkspaceFactoryClass = new FileGDBWorkspaceFactory(); IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pFileGDBWorkspaceFactoryClass.openFromFile(gdbPath, 0); //获取 gdb中某个图层的 FeatureClass对象 IFeatureClass pFeatureClass = pFeatureWorkspace.openFeatureClass(FcName); // IGeoDataset pGeoDataset = (IGeoDataset) pFeatureClass; //featureClass的字段 IFields fields = pFeatureClass.getFields(); //连接sde库 if (pWorkspace != null) { IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)pWorkspace; //创建新featureclass IFeatureClass pFCls = featureWorkspace.createFeatureClass(tableName, fields, null, null, pFeatureClass.getFeatureType(), pFeatureClass.getShapeFieldName(), ""); } } catch (Exception ex) { ex.printStackTrace(); } }
/** * 将gdb里某个图层的数据全部存入sde的某个table中 * @param gdbPath * @param layerCode gdb里图层名称 * @param tableName sde里的表名称 * @param startId sde表开始存的id * @param primaryKey sde表的主键 * @throws IOException */ public static void dataToTable(String gdbPath, String layerCode, String tableName , Integer startId , String primaryKey) throws IOException { initializeArcGISLicenses(); //获取 连接sde的 IFeatureClass 对象 Workspace workspace = connectSde(); IFeatureClass iFeatureClass = workspace.openFeatureClass("data_check." + tableName); //获取 gdb里图层的 featureClass 对象 FeatureVo fcAndF = getFCAndF(gdbPath, layerCode); IFeatureClass featureClass = fcAndF.getFeatureClass(); // IFeatureCursor featureCursor = featureClass.IFeatureClass_insert(true); IQueryFilter queryFilter = new QueryFilter(); IFeatureCursor search = featureClass.search(queryFilter, true); //获取 IFeature对象 IFeature pFeature = null; while ((pFeature = search.nextFeature()) != null) { //将数据一条一条存进sde gdbToSde(iFeatureClass, pFeature , startId , primaryKey); } search.flush(); } /** * 将 gdb文件里的一条数据写进sde的table里(sde中的table已存在) * * @param pFeatureClass 连接数据库里的table对象 * @param pFeature 单条数据,也就是单个图版 * @param startId sde表开始存的id * @param primaryKey sde表的主键 * @return * @throws IOException */ public static void gdbToSde(IFeatureClass pFeatureClass, IFeature pFeature , Integer startId , String primaryKey) throws IOException { initializeArcGISLicenses(); // IFeatureClass rFeatureClass = Erase(pFeatureClass, pFeature); IFeatureCursor featureCursor = pFeatureClass.IFeatureClass_insert(true); IFeatureBuffer feaBuffer = pFeatureClass.createFeatureBuffer(); IField fld = new Field(); IFields flds = pFeature.getFields(); if(startId == 0){ for (int i = 0; i < flds.getFieldCount(); i++) { fld = flds.getField(i); String fldname = fld.getName(); int index = feaBuffer.getFields().findField(fldname); if (index != -1 && index != 0) { feaBuffer.setValue(index, pFeature.getValue(i)); } } feaBuffer.setShapeByRef(pFeature.getShape()); featureCursor.insertFeature(feaBuffer); featureCursor.flush(); }else { for (int i = 0; i < flds.getFieldCount(); i++) { fld = flds.getField(i); String fldname = fld.getName(); int index = feaBuffer.getFields().findField(fldname); if (index != -1 && index != 0) { if(fldname.equals(primaryKey)){ feaBuffer.setValue(index, ++startId); }else { feaBuffer.setValue(index, pFeature.getValue(i)); } } } feaBuffer.setShapeByRef(pFeature.getShape()); featureCursor.insertFeature(feaBuffer); // featureCursor.flush(); } } /** * 获取 IFeatureWorkspace 和 IFeatureClass 和 IFeature 对象 * * @param gdbPath * @param layerCode * @return */ public static FeatureVo getFCAndF(String gdbPath, String layerCode) throws IOException { initializeArcGISLicenses(); //featureVo 就是个实体类,封装了IFeatureWorkspace 与 IFeatureClass FeatureVo featureVo = new FeatureVo(); //创建 gdb工作空间 FileGDBWorkspaceFactory pFileGDBWorkspaceFactoryClass = new FileGDBWorkspaceFactory(); IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pFileGDBWorkspaceFactoryClass.openFromFile(gdbPath, 0); //获取 gdb中某个图层的 FeatureClass对象 IFeatureClass pFeatureClass = pFeatureWorkspace.openFeatureClass(layerCode); featureVo.setFeatureWorkspace(pFeatureWorkspace); featureVo.setFeatureClass(pFeatureClass); return featureVo; }
dataToTable这个方法里有个参数是gdb图层名称,我上一篇文章有如何获取gdb里图层名称的方法
https://blog.csdn.net/qq_45697944/article/details/107243806
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。