示例#1
0
        private void button2_Click(object sender, EventArgs e)
        {
            //工作空间
            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(pFilePath, 0) as IFeatureWorkspace;

            //创建字段集2
            IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass();
            //创建必要字段
            IObjectClassDescription ocDescription = (IObjectClassDescription)fcDescription;

            //必要字段
            IFields pFields = new FieldsClass();

            pFields = ocDescription.RequiredFields;

            //要素类的几何类型、坐标系
            int              shapeFileIndex   = pFields.FindField(fcDescription.ShapeFieldName);
            IField           pField           = pFields.get_Field(shapeFileIndex);
            IGeometryDef     pGeometryDef     = pField.GeometryDef;
            IGeometryDefEdit pGeometryDefEdit = (IGeometryDefEdit)pGeometryDef;

            pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
            ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();

            //更改为可选的坐标系
            IProjectedCoordinateSystem pProjectedCoordinateSystem = pSpatialReferenceFactory.CreateProjectedCoordinateSystem(
                (int)esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_Zone_21);

            pGeometryDefEdit.SpatialReference_2 = pProjectedCoordinateSystem;


            //自定义字段
            IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

            addField(pFieldsEdit, "FID");
            addField(pFieldsEdit, "Row");
            addField(pFieldsEdit, "Column");
            addField(pFieldsEdit, "Number");
            //传入字段
            IFieldChecker   pFieldChecker   = new FieldCheckerClass();
            IEnumFieldError pEnumFieldError = null;
            IFields         validatedFields = null;

            pFieldChecker.ValidateWorkspace = (IWorkspace)pFeatureWorkspace;
            pFieldChecker.Validate(pFields, out pEnumFieldError, out validatedFields);

            //创建要素类
            IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass(pFileName, validatedFields
                                                                               , ocDescription.InstanceCLSID, ocDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDescription.ShapeFieldName, "");

            MessageBox.Show("创建要素类成功");
        }
示例#2
0
        //创建featureclass
        private IFeatureClass CreateFeatureClass(string name, IFeatureClass pFeaCls, IFeatureDataset pFeaDataset, IFeatureWorkspace pWks)
        {
            UID uidCLSID  = null;
            UID uidCLSEXT = null;


            try
            {
                IObjectClassDescription pObjCls = null;
                if (uidCLSID == null)
                {
                    //esriGeometryType GeometryType;
                    uidCLSID = new UIDClass();
                    switch (pFeaCls.FeatureType)
                    {
                    case (esriFeatureType.esriFTSimple):
                        uidCLSID.Value = "{52353152-891A-11D0-BEC6-00805F7C4268}";
                        break;

                    case (esriFeatureType.esriFTSimpleJunction):
                        //GeometryType = esriGeometryType.esriGeometryPoint;
                        uidCLSID.Value = "{CEE8D6B8-55FE-11D1-AE55-0000F80372B4}";
                        break;

                    case (esriFeatureType.esriFTComplexJunction):
                        uidCLSID.Value = "{DF9D71F4-DA32-11D1-AEBA-0000F80372B4}";
                        break;

                    case (esriFeatureType.esriFTSimpleEdge):
                        //GeometryType = esriGeometryType.esriGeometryPolyline;
                        uidCLSID.Value = "{E7031C90-55FE-11D1-AE55-0000F80372B4}";
                        break;

                    case (esriFeatureType.esriFTComplexEdge):
                        //GeometryType = esriGeometryType.esriGeometryPolyline;
                        uidCLSID.Value = "{A30E8A2A-C50B-11D1-AEA9-0000F80372B4}";
                        break;

                    case (esriFeatureType.esriFTAnnotation):
                        //GeometryType = esriGeometryType.esriGeometryPolygon;
                        uidCLSID.Value = "{E3676993-C682-11D2-8A2A-006097AFF44E}";
                        break;

                    case (esriFeatureType.esriFTDimension):
                        //GeometryType = esriGeometryType.esriGeometryPolygon;
                        uidCLSID.Value = "{496764FC-E0C9-11D3-80CE-00C04F601565}";
                        break;
                    }
                }

                // 设置 uidCLSEXT (if Null)
                if (uidCLSEXT == null)
                {
                    switch (pFeaCls.FeatureType)
                    {
                    case (esriFeatureType.esriFTAnnotation):
                        uidCLSEXT       = new UIDClass();
                        uidCLSEXT.Value = "{24429589-D711-11D2-9F41-00C04F6BC6A5}";
                        pObjCls         = new AnnotationFeatureClassDescriptionClass();
                        break;

                    case (esriFeatureType.esriFTDimension):
                        uidCLSEXT       = new UIDClass();
                        uidCLSEXT.Value = "{48F935E2-DA66-11D3-80CE-00C04F601565}";
                        break;
                    }
                }
                //IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
                IFieldChecker fdCheker = new FieldCheckerClass();//yjl20110804 add
                pEnumFieldError            = null;
                pFixedField                = null;
                fdCheker.ValidateWorkspace = pWks as IWorkspace;
                fdCheker.Validate(pFeaCls.Fields, out pEnumFieldError, out pFixedField);

                //string strShapeFieldName = pfeaturelayer.FeatureClass.ShapeFieldName;//geometry字段名
                //string[] strShapeNames = strShapeFieldName.Split('.');
                //strShapeFieldName = strShapeNames[strShapeNames.GetLength(0) - 1];

                IFields pFields = new FieldsClass();
                if (pObjCls != null)
                {
                    IFeatureClassDescription pClsDes = pObjCls as IFeatureClassDescription;
                    pFields = pObjCls.RequiredFields;
                }

                IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;

                for (int i = 0; i < pFeaCls.Fields.FieldCount; i++)
                {
                    IField   pf            = pFeaCls.Fields.get_Field(i);
                    string   strFieldName  = pf.Name;
                    string[] strFieldNames = strFieldName.Split('.');

                    bool blnfind = false;
                    for (int j = 0; j < pFields.FieldCount; j++)
                    {
                        IField   pf2        = pFields.get_Field(j);
                        string[] strfields2 = pf2.Name.Split('.');
                        if (strfields2[strfields2.GetLength(0) - 1].ToUpper() == strFieldNames[strFieldNames.GetLength(0) - 1].ToUpper())
                        {
                            blnfind = true;
                            break;
                        }
                    }

                    if (blnfind)
                    {
                        continue;
                    }

                    if (pFeaCls.FeatureType == esriFeatureType.esriFTAnnotation)
                    {
                        if (pFeaCls.ShapeFieldName == pf.Name)
                        {
                            continue;
                        }
                    }

                    if (pFeaCls.LengthField != null)
                    {
                        if (pFeaCls.LengthField.Name == pf.Name)
                        {
                            continue;
                        }
                    }
                    if (pFeaCls.AreaField != null)
                    {
                        if (pFeaCls.AreaField.Name == pf.Name)
                        {
                            continue;
                        }
                    }

                    IClone     pClone         = pFeaCls.Fields.get_Field(i) as IClone;
                    IField     pTempField     = pClone.Clone() as IField;
                    IFieldEdit pTempFieldEdit = pTempField as IFieldEdit;

                    if (pFieldsEdit.FindField(strFieldNames[strFieldNames.GetLength(0) - 1]) > -1)
                    {
                        continue;
                    }

                    pTempFieldEdit.Name_2 = strFieldNames[strFieldNames.GetLength(0) - 1];
                    pFieldsEdit.AddField(pTempField);
                }

                string   strShapeFieldName = pFeaCls.ShapeFieldName;
                string[] strShapeNames     = strShapeFieldName.Split('.');
                strShapeFieldName = strShapeNames[strShapeNames.GetLength(0) - 1];

                //修改geometrydef
                IField pFieldShape = pFeaCls.Fields.get_Field(pFeaCls.Fields.FindField(pFeaCls.ShapeFieldName));

                if (pFieldShape != null)
                {
                    IFieldEdit pFieldShapeEdit = pFields.get_Field(pFields.FindField(strShapeFieldName)) as IFieldEdit;
                    pFieldShapeEdit.GeometryDef_2 = pFieldShape.GeometryDef;
                }

                IGeometryDef pGeoDef  = pFieldShape.GeometryDef;
                double       dblIndex = pGeoDef.get_GridSize(0);

                //添加两个字段一个时间 一个名称
                if (pFields.FindField("ImportTime") < 0)
                {
                    IField     pNewField1 = new FieldClass();
                    IFieldEdit pNewEdit1  = pNewField1 as IFieldEdit;
                    pNewEdit1.Name_2      = "ImportTime";
                    pNewEdit1.AliasName_2 = "入库时间";
                    pNewEdit1.Type_2      = esriFieldType.esriFieldTypeDate;
                    pFieldsEdit.AddField(pNewField1);
                }

                if (pFields.FindField("ImportUser") < 0)
                {
                    IField     pNewField2 = new FieldClass();
                    IFieldEdit pNewEdit2  = pNewField2 as IFieldEdit;
                    pNewEdit2.Name_2      = "ImportUser";
                    pNewEdit2.AliasName_2 = "入库人";
                    pNewEdit2.Type_2      = esriFieldType.esriFieldTypeString;
                    pFieldsEdit.AddField(pNewField2);
                }

                IFeatureClass targetFeatureclass = null;


                if (pFeaCls.FeatureType == esriFeatureType.esriFTAnnotation)
                {
                    IAnnoClass pAnno = pFeaCls.Extension as IAnnoClass;

                    IFeatureWorkspaceAnno pWksAnno = pWks as IFeatureWorkspaceAnno;
                    IGraphicsLayerScale   pGl      = new GraphicsLayerScaleClass();
                    pGl.ReferenceScale = pAnno.ReferenceScale;
                    pGl.Units          = pAnno.ReferenceScaleUnits;
                    targetFeatureclass = pWksAnno.CreateAnnotationClass(name, pFields, pFeaCls.CLSID, pFeaCls.EXTCLSID, strShapeFieldName, "", pFeaDataset, null, pAnno.AnnoProperties, pGl, pAnno.SymbolCollection, false);
                }
                else
                {
                    if (pFeaDataset != null)
                    {
                        targetFeatureclass = pFeaDataset.CreateFeatureClass(name, pFixedField, uidCLSID, uidCLSEXT, pFeaCls.FeatureType, strShapeFieldName, "");
                    }
                    else
                    {
                        targetFeatureclass = pWks.CreateFeatureClass(name, pFixedField, uidCLSID, uidCLSEXT, pFeaCls.FeatureType, strShapeFieldName, "");
                    }
                }

                return(targetFeatureclass);
            }
            catch (Exception ex)
            {
                if (ex.Message == "Cannot create a low precision dataset in a high precision database.")
                {
                    MessageBox.Show("数据必须是ArcGis9.2的数据,请将数据处理成ArcGis9.2的数据!");
                }
            }
            IFeatureClass featureclass = null;

            return(featureclass);
        }
示例#3
0
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (Validate(true) == false)
            {
                return;
            }

            object         createLoc;
            IWorkspace2    workspace2 = null;
            IObjectDataset dataset    =
                _database.Datasets.FirstOrDefault(c => c.Name == cmbTemplate.SelectedItem.ToString());

            if (dataset == null)
            {
                return;
            }

            IGxDatabase pDataset  = txtDB.Tag as IGxDatabase;
            IWorkspace  workspace = ((IGxObject)pDataset).InternalObjectName.Open();

            workspace2 = workspace as IWorkspace2;


            string namePre   = txtPreName.Text.Trim();
            string nameNext  = txtNameNext.Text.Trim();
            string aliasPre  = txtAliasPre.Text.Trim();
            string aliasNext = txtAliasNext.Text.Trim();

            if (!chkNamePre.Checked)
            {
                namePre  = "";
                aliasPre = "";
            }
            if (!chkNameNext.Checked)
            {
                nameNext  = "";
                aliasNext = "";
            }

            string dsName = CombineName(dataset.BaseName, namePre, nameNext);


            if (workspace == null)
            {
                return;
            }



            if (workspace2.NameExists[esriDatasetType.esriDTFeatureDataset, dsName])
            {
                MessageService.Current.Warn("该名称已经存在,请重新输入!");
                return;
            }
            ISpatialReference pSpatialReference = _map.SpatialReference;

            IFeatureDataset pNewDataset =
                WorkspaceOperator.CreateFeatureDataSet(workspace, dsName, pSpatialReference) as IFeatureDataset;
            string dsAliasName = CombineName(dataset.AliasName, aliasPre, aliasNext);

            //(pNewDataset as IClassSchemaEdit).AlterAliasName(dsAliasName);
            foreach (int selectedItem in chkFeatureClasses.CheckedIndices)
            {
                IObjectTemplate template    = _templates[selectedItem];
                string          fcName      = CombineName(template.BaseName, namePre, nameNext);
                string          fcAliasName = CombineName(template.AliasName, aliasPre, aliasNext);

                if (template.FeatureType == esriFeatureType.esriFTAnnotation)
                {
                    WorkspaceOperator.CreateAnnoFeatureClass(fcName, pNewDataset, 1000);
                }
                else
                {
                    IFieldsEdit pFieldsEdit = new FieldsClass() as IFieldsEdit;
                    IField      pField      = FieldHelper.CreateOIDField();
                    if (pFieldsEdit.FindField(pField.Name) < 0)
                    {
                        pFieldsEdit.AddField(pField);
                    }

                    pField = FieldHelper.CreateGeometryField(template.GeometryType, _map.SpatialReference);
                    if (pFieldsEdit.FindField(pField.Name) < 0)
                    {
                        pFieldsEdit.AddField(pField);
                    }

                    string keyName = "";
                    foreach (YTField ytField in template.Fields)
                    {
                        pField = ytField.CreateField();
                        if (pFieldsEdit.FindField(pField.Name) < 0)
                        {
                            pFieldsEdit.AddField(pField);
                        }
                        if (ytField.IsKey)
                        {
                            keyName = ytField.Name;
                        }
                    }

                    IFeatureClass pClass = WorkspaceOperator.CreateFeatureClass(pNewDataset, fcName, pSpatialReference, template.FeatureType,
                                                                                template.GeometryType, (IFields)pFieldsEdit, null, null, "");
                    (pClass as IClassSchemaEdit).AlterAliasName(fcAliasName);


                    if (pClass == null)
                    {
                        MessageService.Current.Info(fcName + "创建失败!");
                        continue;
                    }
                }
            }
            MapHelper.AddDataset(_map as IBasicMap, pNewDataset, dsAliasName);
            MessageService.Current.Info("创建成功层!");
            DialogResult = DialogResult.OK;
        }
示例#4
0
        public ITable DataTableToAETable(DataTable table, IFeatureWorkspace featureWorkspace, string[] fieldsName, string name)
        {
            if (table != null && featureWorkspace != null)
            {
                IFields     pFileds     = new FieldsClass();
                IFieldsEdit pFieldsEdit = (IFieldsEdit)pFileds;

                Dictionary <string, string> fieldLookUp = new Dictionary <string, string>();
                string[] fName = FieldNameCopy(table, fieldsName, fieldLookUp);

                int count = fName.Length;
                if (count > 50)
                {
                    count = 50;
                }

                for (int i = 0; i < count; i++)
                {
                    esriFieldType type = GetAEFieldType(table.Columns[fieldLookUp[fName[i]]]);
                    if (type == esriFieldType.esriFieldTypeGeometry)
                    {
                        continue;
                    }

                    IField     pField     = new FieldClass();
                    IFieldEdit pFieldEdit = (IFieldEdit)pField;
                    pFieldEdit.Name_2 = fName[i];
                    pFieldEdit.Type_2 = type;
                    //pFieldEdit.Length_2 = 50;

                    pFieldsEdit.AddField(pField);
                }

                ITable pTable = featureWorkspace.CreateTable(name, pFileds, null, null, "");

                try
                {
                    if (count < fName.Length)
                    {
                        for (int i = count; i < fName.Length; i++)
                        {
                            esriFieldType type = GetAEFieldType(table.Columns[i]);

                            if (type == esriFieldType.esriFieldTypeGeometry || fName[i] == "ObjectId" || pFileds.FindField(fName[i]) != -1)
                            {
                                continue;
                            }

                            IField     pField     = new FieldClass();
                            IFieldEdit pFieldEdit = (IFieldEdit)pField;
                            pFieldEdit.Name_2 = fName[i];
                            pFieldEdit.Type_2 = type;
                            pTable.AddField(pField);
                        }
                    }

                    List <string> fNames = new List <string>();
                    List <int>    index  = new List <int>();
                    for (int i = 0; i < fName.Length; i++)
                    {
                        int id = pTable.Fields.FindField(fName[i]);
                        if (id != -1)
                        {
                            index.Add(id);
                            fNames.Add(fName[i]);
                        }
                    }
                    fName = fNames.ToArray();

                    ICursor cursor = pTable.Insert(true);
                    //添加内容
                    for (int j = 0; j < table.Rows.Count; j++)
                    {
                        IRowBuffer rowBuffer = pTable.CreateRowBuffer();
                        DataRow    myRow     = table.Rows[j];
                        for (int i = 0; i < fName.Length; i++)
                        {
                            string dataTableField = fieldLookUp[fName[i]];
                            object obj            = myRow[dataTableField];
                            if (obj.ToString() != "")
                            {
                                rowBuffer.set_Value(index[i], obj);
                            }
                        }
                        cursor.InsertRow(rowBuffer);
                    }
                    if (pFileds != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pFileds);
                    }
                    if (pFieldsEdit != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pFieldsEdit);
                    }
                    if (featureWorkspace != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(featureWorkspace);
                    }
                    return(pTable);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
            return(null);
        }