Ejemplo n.º 1
0
        private DataTable CreateDataTable(ITable table, string[] fieldsName)
        {
            //创建一个DataTable表
            DataTable pDataTable = new DataTable();
            //取得ITable接口
            IField     pField = null;
            DataColumn pDataColumn;

            IFields fields = new FieldsClass();

            if (fieldsName == null)
            {
                fields = table.Fields;
            }
            else
            {
                for (int i = 0; i < fieldsName.Length; i++)
                {
                    int index = table.Fields.FindField(fieldsName[i]);
                    if (index != -1)
                    {
                        IField field = table.Fields.get_Field(index);
                        (fields as IFieldsEdit).AddField(field);
                    }
                }
            }

            //根据每个字段的属性建立DataColumn对象
            for (int i = 0; i < fields.FieldCount; i++)
            {
                pField = fields.get_Field(i);
                //新建一个DataColumn并设置其属性
                pDataColumn = new DataColumn(pField.Name);
                //if (pField.Name == table.OIDFieldName)
                //{
                //    pDataColumn.Unique = true;//字段值是否唯一
                //}
                //字段值是否允许为空
                pDataColumn.AllowDBNull = pField.IsNullable;
                //字段别名
                pDataColumn.Caption = pField.AliasName;
                //字段数据类型
                pDataColumn.DataType = System.Type.GetType(CovertFieldType(pField.Type));
                //字段默认值
                pDataColumn.DefaultValue = pField.DefaultValue;
                //当字段为String类型是设置字段长度
                //if (pField.VarType == 8 && pDataColumn.MaxLength < pField.Length)
                //{
                //    if (pField.Name == "FID_1")
                //        pDataColumn.MaxLength = pField.Length;
                //}

                //字段添加到表中
                pDataTable.Columns.Add(pDataColumn);
                pField      = null;
                pDataColumn = null;
            }

            return(pDataTable);
        }
Ejemplo n.º 2
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("创建要素类成功");
        }
Ejemplo n.º 3
0
        public IFeatureClass CreateFeatureClass(string featureClassName, esriGeometryType geometryType, ISpatialReference spatialReference)
        {
            IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetTempPath(), 0);
            IFields           fields           = new FieldsClass();
            IFieldsEdit       fieldsEdit       = (IFieldsEdit)fields;

            fieldsEdit.FieldCount_2 = 2;
            //Create the Object ID field.
            IField     fieldUserDefined = new Field();
            IFieldEdit fieldEdit        = (IFieldEdit)fieldUserDefined;

            fieldEdit.Name_2      = "FID";
            fieldEdit.AliasName_2 = "OBJECT ID";
            fieldEdit.Type_2      = esriFieldType.esriFieldTypeOID;
            fieldsEdit.set_Field(0, fieldUserDefined);
            // Create the Shape field.
            fieldUserDefined = new Field();
            fieldEdit        = (IFieldEdit)fieldUserDefined;
            // Set up the geometry definition for the Shape field.
            IGeometryDef     geometryDef     = new GeometryDefClass();
            IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;

            switch (geometryType)
            {
            case esriGeometryType.esriGeometryPoint:
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
                break;

            case esriGeometryType.esriGeometryPolyline:
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
                break;

            case esriGeometryType.esriGeometryPolygon:
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                break;

            default:
                break;
            }
            geometryDefEdit.GridCount_2 = 1;
            geometryDefEdit.set_GridSize(0, 0);
            geometryDefEdit.HasM_2 = false;
            geometryDefEdit.HasZ_2 = false;
            if (spatialReference != null)
            {
                geometryDefEdit.SpatialReference_2 = spatialReference;
            }
            // Set standard field properties.
            fieldEdit.Name_2        = "SHAPE";
            fieldEdit.Type_2        = esriFieldType.esriFieldTypeGeometry;
            fieldEdit.GeometryDef_2 = geometryDef;
            fieldEdit.IsNullable_2  = true;
            fieldEdit.Required_2    = true;
            fieldsEdit.set_Field(1, fieldUserDefined);

            UID CLSID = new UIDClass();

            CLSID.Value = "esriGeoDatabase.Feature";

            return(featureWorkspace.CreateFeatureClass(featureClassName, fields, CLSID, null, esriFeatureType.esriFTSimple, fields.get_Field(1).Name, ""));
        }
Ejemplo n.º 4
0
        //创建要素类
        private bool CreateFeatureLayer(string FName)
        {
            try
            {
                IFields     fields     = new FieldsClass();
                IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
                fieldsEdit.FieldCount_2 = 2;
                IField     fieldUserDefined = new Field();
                IFieldEdit fieldEdit        = (IFieldEdit)fieldUserDefined;
                fieldEdit.Name_2      = "FID";
                fieldEdit.AliasName_2 = "OBJECT ID";
                fieldEdit.Type_2      = esriFieldType.esriFieldTypeOID;
                fieldsEdit.set_Field(0, fieldUserDefined);

                fieldUserDefined = new Field();
                fieldEdit        = (IFieldEdit)fieldUserDefined;

                IGeometryDef     geometryDef     = new GeometryDefClass();
                IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                geometryDefEdit.GridCount_2    = 1;
                geometryDefEdit.set_GridSize(0, 0);
                geometryDefEdit.HasM_2 = false;
                geometryDefEdit.HasZ_2 = false;

                if (spatialReference != null)
                {
                    geometryDefEdit.SpatialReference_2 = spatialReference;
                }

                fieldEdit.Name_2        = "SHAPE";
                fieldEdit.Type_2        = esriFieldType.esriFieldTypeGeometry;
                fieldEdit.GeometryDef_2 = geometryDef;
                fieldEdit.IsNullable_2  = true;
                fieldEdit.Required_2    = true;
                fieldsEdit.set_Field(1, fieldUserDefined);

                UID CLSID = new UIDClass
                {
                    Value = "esriGeoDatabase.Feature"
                };
                Thread.Sleep(1000);
                string        tmpLyrName   = DateTime.Now.ToString("yyyyMMddHHmmss");
                IFeatureClass featureClass = featureWorkspace.CreateFeatureClass(tmpLyrName, fields, CLSID, null, esriFeatureType.esriFTSimple, fields.get_Field(1).Name, "");
                ROIFileNames.Add(tmpLyrName);
                IFeatureLayer featureLayer = new FeatureLayerClass();
                featureLayer.FeatureClass = featureClass;
                featureLayer.Name         = FName;
                featureLayer.Visible      = true;

                ILayerEffects layerEffects = featureLayer as ILayerEffects;
                layerEffects.Transparency = 55;

                m_mapControl.ActiveView.FocusMap.AddLayer(featureLayer);
                m_mapControl.ActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeography, null, null);

                return(true);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
                return(false);
            }
        }
Ejemplo n.º 5
0
        public bool StartImport()
        {
            int    kk;
            string pFieldName;
            int    pFieldIndex;
            bool   flag;

            DataRow[] dataRowArray;
            int       l;
            var       xyzTableName = string.Concat("XYZ_", Basename);
            IFields   pFields      = new FieldsClass();
            var       pFieldsEdit  = pFields as IFieldsEdit;
            var       xyzTable     = new DataTable("XYZ");

            xyzTable.Columns.Add(new DataColumn("SurveyNO", typeof(string)));
            xyzTable.Columns.Add(new DataColumn("CX", typeof(double)));
            xyzTable.Columns.Add(new DataColumn("CY", typeof(double)));
            xyzTable.Columns.Add(new DataColumn("CZ", typeof(double)));
            xyzTable.Columns.Add(new DataColumn("PNTCODE", typeof(string)));
            IFieldEdit pFieldEdit = new FieldClass();

            pFieldEdit.Name_2       = "SurveyNO";
            pFieldEdit.Type_2       = esriFieldType.esriFieldTypeString;
            pFieldEdit.IsNullable_2 = true;
            pFieldEdit.Length_2     = 10;
            pFieldsEdit.AddField(pFieldEdit);
            pFieldEdit              = new FieldClass();
            pFieldEdit.Name_2       = "CX";
            pFieldEdit.Type_2       = esriFieldType.esriFieldTypeDouble;
            pFieldEdit.IsNullable_2 = true;
            pFieldEdit.Length_2     = 10;
            pFieldsEdit.AddField(pFieldEdit);
            pFieldEdit              = new FieldClass();
            pFieldEdit.Name_2       = "CY";
            pFieldEdit.Type_2       = esriFieldType.esriFieldTypeDouble;
            pFieldEdit.IsNullable_2 = true;
            pFieldEdit.Length_2     = 10;
            pFieldsEdit.AddField(pFieldEdit);
            pFieldEdit              = new FieldClass();
            pFieldEdit.Name_2       = "CZ";
            pFieldEdit.Type_2       = esriFieldType.esriFieldTypeDouble;
            pFieldEdit.IsNullable_2 = true;
            pFieldEdit.Length_2     = 10;
            pFieldsEdit.AddField(pFieldEdit);
            pFieldEdit              = new FieldClass();
            pFieldEdit.Name_2       = "PNTCODE";
            pFieldEdit.Type_2       = esriFieldType.esriFieldTypeString;
            pFieldEdit.IsNullable_2 = true;
            pFieldEdit.Length_2     = 10;
            pFieldsEdit.AddField(pFieldEdit);
            pFieldEdit              = new FieldClass();
            pFieldEdit.Name_2       = "Surveyer";
            pFieldEdit.Type_2       = esriFieldType.esriFieldTypeString;
            pFieldEdit.IsNullable_2 = true;
            pFieldEdit.Length_2     = 10;
            pFieldsEdit.AddField(pFieldEdit);
            pFieldEdit              = new FieldClass();
            pFieldEdit.Name_2       = "PNTCODE";
            pFieldEdit.Type_2       = esriFieldType.esriFieldTypeDate;
            pFieldEdit.IsNullable_2 = true;
            pFieldEdit.Length_2     = 10;
            pFieldsEdit.AddField(pFieldEdit);
            if (new FileInfo(Xyzfile).Exists)
            {
                var     sr    = new StreamReader(Xyzfile, Encoding.GetEncoding("gb2312"));
                var     pLine = sr.ReadLine().Trim();
                DataRow pRow;
                while (true)
                {
                    var str = sr.ReadLine();
                    pLine = str;
                    if (str == null)
                    {
                        break;
                    }
                    if (pLine == null ? false : pLine.Trim().Length != 0)
                    {
                        pLine = pLine.Trim();
                        var pSubs = pLine.Split(',');
                        pRow             = xyzTable.NewRow();
                        pRow["SurveyNo"] = pSubs[0];
                        switch (CoordType)
                        {
                        case SurveyCoordType.NOXY:
                        {
                            pRow["CX"] = double.Parse(pSubs[1]);
                            pRow["CY"] = double.Parse(pSubs[2]);
                            break;
                        }

                        case SurveyCoordType.NOYX:
                        {
                            pRow["CX"] = double.Parse(pSubs[2]);
                            pRow["CY"] = double.Parse(pSubs[1]);
                            break;
                        }

                        case SurveyCoordType.NOCODEXY:
                        {
                            pRow["CX"]      = double.Parse(pSubs[2]);
                            pRow["CY"]      = double.Parse(pSubs[3]);
                            pRow["PNTCODE"] = pSubs[1];
                            break;
                        }

                        case SurveyCoordType.NOCODEYX:
                        {
                            pRow["CX"]      = double.Parse(pSubs[3]);
                            pRow["CY"]      = double.Parse(pSubs[2]);
                            pRow["PNTCODE"] = pSubs[1];
                            break;
                        }

                        case SurveyCoordType.NOXYZ:
                        {
                            pRow["CX"] = double.Parse(pSubs[1]);
                            pRow["CY"] = double.Parse(pSubs[2]);
                            pRow["CZ"] = double.Parse(pSubs[3]);
                            break;
                        }

                        case SurveyCoordType.NOYXZ:
                        {
                            pRow["CX"] = double.Parse(pSubs[2]);
                            pRow["CY"] = double.Parse(pSubs[1]);
                            pRow["CZ"] = double.Parse(pSubs[3]);
                            break;
                        }

                        case SurveyCoordType.NOCODEXYZ:
                        {
                            pRow["PNTCODE"] = pSubs[1];
                            pRow["CX"]      = double.Parse(pSubs[2]);
                            pRow["CY"]      = double.Parse(pSubs[3]);
                            pRow["CZ"]      = double.Parse(pSubs[4]);
                            break;
                        }

                        case SurveyCoordType.NOCODEYXZ:
                        {
                            pRow["PNTCODE"] = pSubs[1];
                            pRow["CX"]      = double.Parse(pSubs[3]);
                            pRow["CY"]      = double.Parse(pSubs[2]);
                            pRow["CZ"]      = double.Parse(pSubs[4]);
                            break;
                        }

                        default:
                        {
                            flag = false;
                            return(flag);
                        }
                        }
                        xyzTable.Rows.Add(pRow);
                    }
                }
                sr.Close();
                var xlsxTable    = new DataTable("XLSXTABLE");
                var ws           = new XLWorkbook(Xlsxfile).Worksheet("Data");
                var firstRowUsed = ws.FirstRowUsed();
                ws.LastRowUsed();
                var firstColumnUsed = ws.FirstColumnUsed();
                ws.LastColumnUsed();
                firstColumnUsed.CellCount();
                var compcodeIndex = 0;
                var k             = 0;
                foreach (var xlCell in firstRowUsed.Cells())
                {
                    var columnName = xlCell.GetValue <string>().Trim();
                    columnName = columnName.Replace(" ", "");
                    var column = new DataColumn(columnName, typeof(string))
                    {
                        AllowDBNull = true
                    };
                    xlsxTable.Columns.Add(column);
                    if (columnName == "CompCode")
                    {
                        compcodeIndex = k;
                    }
                    k++;
                    if (!(columnName == "SurveyNO"))
                    {
                        pFieldEdit        = new FieldClass();
                        pFieldEdit.Name_2 = columnName;
                        pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                        pFieldsEdit.AddField(pFieldEdit);
                    }
                }
                var firstvalueAddress = firstRowUsed.RowBelow().FirstCell().Address;
                var lastvalueAddress  = ws.LastCellUsed().Address;
                int j;
                foreach (
                    var xlRangeRow in ws.Range(firstvalueAddress, lastvalueAddress).Rows((Func <IXLRangeRow, bool>)null)
                    )
                {
                    j    = 0;
                    pRow = xlsxTable.NewRow();
                    foreach (var xLCell in xlRangeRow.Cells())
                    {
                        if (j == compcodeIndex)
                        {
                            pRow[j] = xLCell.GetString().PadLeft(4, '0');
                        }
                        else
                        {
                            pRow[j] = xLCell.GetString();
                        }
                        j++;
                    }
                    xlsxTable.Rows.Add(pRow);
                }
                var geoFields     = new FeatureClassDescriptionClass().RequiredFields;
                var geoFieldsEdit = geoFields as IFieldsEdit;
                var xyFields      = new ObjectClassDescriptionClass().RequiredFields;
                var xyFieldsEdit  = xyFields as IFieldsEdit;
                for (var ii = 0; ii < pFields.FieldCount; ii++)
                {
                    xyFieldsEdit.AddField(pFields.get_Field(ii));
                    geoFieldsEdit.AddField(pFields.get_Field(ii));
                }
                var esriTable = GSCSUtilsGen.Instance.CreateTable((IWorkspace2)TargetWks, xyzTableName, xyFields);
                var pointFC   = GSCSUtilsGen.Instance.CreateFeatureClass((IWorkspace2)TargetWks, null,
                                                                         string.Concat("PNT_", Basename), geoFields, null, null, "", esriGeometryType.esriGeometryPoint);
                var lineFC = GSCSUtilsGen.Instance.CreateFeatureClass((IWorkspace2)TargetWks, null,
                                                                      string.Concat("LINE_", Basename), geoFields, null, null, "", esriGeometryType.esriGeometryPolyline);
                var areaFC = GSCSUtilsGen.Instance.CreateFeatureClass((IWorkspace2)TargetWks, null,
                                                                      string.Concat("AREA_", Basename), geoFields, null, null, "", esriGeometryType.esriGeometryPolygon);
                var dataTable  = xlsxTable;
                var dataTable1 = xyzTable;
                Func <DataRow, DataRow, bool>[] funcArray =
                {
                    (rowA, rowB) => rowA.Field <string>("SurveyNO") == rowB.Field <string>("SurveyNO")
                };
                var vResult        = JoinDataTables(dataTable, dataTable1, funcArray);
                var pWorkspaceEdit = TargetWks as IWorkspaceEdit;
                pWorkspaceEdit.StartEditing(false);
                pWorkspaceEdit.StartEditOperation();
                var pCount             = vResult.Rows.Count;
                var pColCount          = vResult.Columns.Count;
                var pRowIndexes        = new int[pColCount];
                var pPntFieldIndexes   = new int[pColCount];
                var pLineFieldIndexes  = new int[pColCount];
                var pAreaFieldIndexes  = new int[pColCount];
                var surveyIndex        = esriTable.FindField("Surveyer");
                var surveyDateIndex    = esriTable.FindField("SurveyDate");
                var surveyPntIndex     = pointFC.FindField("Surveyer");
                var surveyDatePntIndex = pointFC.FindField("SurveyDate");
                int i;
                for (i = 0; i < vResult.Columns.Count; i++)
                {
                    pRowIndexes[i]       = esriTable.FindField(vResult.Columns[i].ColumnName);
                    pPntFieldIndexes[i]  = pointFC.FindField(vResult.Columns[i].ColumnName);
                    pLineFieldIndexes[i] = lineFC.FindField(vResult.Columns[i].ColumnName);
                    pAreaFieldIndexes[i] = areaFC.FindField(vResult.Columns[i].ColumnName);
                }
                DataRow dataRow;
                for (i = 0; i < pCount; i++)
                {
                    dataRow = vResult.Rows[i];
                    var    esriRow  = esriTable.CreateRow();
                    var    pFeature = pointFC.CreateFeature();
                    IPoint pnt      = new PointClass();
                    pnt.PutCoords((double)dataRow["CX"], (double)dataRow["CY"]);
                    pFeature.Shape = pnt;
                    for (j = 0; j < pColCount; j++)
                    {
                        if (pRowIndexes[j] >= 0)
                        {
                            esriRow.set_Value(pRowIndexes[j], dataRow[j]);
                            pFeature.set_Value(pPntFieldIndexes[j], dataRow[j]);
                        }
                    }
                    esriRow.set_Value(surveyIndex, Surveyer);
                    esriRow.set_Value(surveyDateIndex, SurveyDate);
                    pFeature.set_Value(surveyPntIndex, Surveyer);
                    pFeature.set_Value(surveyDatePntIndex, SurveyDate);
                    esriRow.Store();
                    pFeature.Store();
                }
                pWorkspaceEdit.StopEditOperation();
                pWorkspaceEdit.StopEditing(true);
                pWorkspaceEdit.StartEditing(false);
                pWorkspaceEdit.StartEditOperation();
                var rows     = vResult.Select("PointType='LinePoint' AND LinkedSurveyNO =''", "RecordID");
                var lineRows = vResult.Select("PointType='LinePoint'");
                IPointCollection pnts;
                IPoint           pPnt;
                IPointCollection pPnts;
                IFeature         pLineFeature;
                if (rows == null ? false : rows.Length > 0)
                {
                    dataRowArray = rows;
                    for (l = 0; l < dataRowArray.Length; l++)
                    {
                        dataRow = dataRowArray[l];
                        pnts    = new PolylineClass();
                        pPnt    = new PointClass();
                        pPnt.PutCoords((double)dataRow["CX"], (double)dataRow["CY"]);
                        object r__ComRefCallLocal0 = Missing.Value;
                        object r__ComRefCallLocal1 = Missing.Value;
                        pnts.AddPoint(pPnt, ref r__ComRefCallLocal0, ref r__ComRefCallLocal1);
                        pPnts = CreateLine(pnts, dataRow, lineRows);
                        if (pPnts == null ? false : pPnts.PointCount > 1)
                        {
                            pLineFeature = lineFC.CreateFeature();
                            for (kk = 0; kk < vResult.Columns.Count; kk++)
                            {
                                pFieldName  = vResult.Columns[kk].ColumnName;
                                pFieldIndex = pLineFeature.Fields.FindField(pFieldName);
                                if (pFieldIndex > 0)
                                {
                                    pLineFeature.set_Value(pFieldIndex, dataRow[kk]);
                                }
                            }
                            pLineFeature.Shape = pPnts as IGeometry;
                            pLineFeature.Store();
                        }
                    }
                }
                pWorkspaceEdit.StopEditOperation();
                pWorkspaceEdit.StopEditing(true);
                pWorkspaceEdit.StartEditing(false);
                pWorkspaceEdit.StartEditOperation();
                rows     = vResult.Select("PointType='AreaPoint' AND LinkedSurveyNO =''", "RecordID");
                lineRows = vResult.Select("PointType='AreaPoint'");
                if (rows == null ? false : rows.Length > 0)
                {
                    dataRowArray = rows;
                    for (l = 0; l < dataRowArray.Length; l++)
                    {
                        dataRow = dataRowArray[l];
                        pnts    = new PolygonClass();
                        pPnt    = new PointClass();
                        pPnt.PutCoords((double)dataRow["CX"], (double)dataRow["CY"]);
                        object r__ComRefCallLocal2 = Missing.Value;
                        object r__ComRefCallLocal3 = Missing.Value;
                        pnts.AddPoint(pPnt, ref r__ComRefCallLocal2, ref r__ComRefCallLocal3);
                        pPnts = CreateLine(pnts, dataRow, lineRows);
                        if (pPnts == null ? false : pPnts.PointCount > 2)
                        {
                            pLineFeature = areaFC.CreateFeature();
                            for (kk = 0; kk < vResult.Columns.Count; kk++)
                            {
                                pFieldName  = vResult.Columns[kk].ColumnName;
                                pFieldIndex = pLineFeature.Fields.FindField(pFieldName);
                                if (pFieldIndex > 0)
                                {
                                    pLineFeature.set_Value(pFieldIndex, dataRow[kk]);
                                }
                            }
                            pPnt = new PointClass();
                            pPnt.PutCoords((double)dataRow["CX"], (double)dataRow["CY"]);
                            object r__ComRefCallLocal4 = Missing.Value;
                            object r__ComRefCallLocal5 = Missing.Value;
                            pnts.AddPoint(pPnt, ref r__ComRefCallLocal4, ref r__ComRefCallLocal5);
                            pLineFeature.Shape = (IGeometry)pPnts;
                            pLineFeature.Store();
                        }
                    }
                }
                pWorkspaceEdit.StopEditOperation();
                pWorkspaceEdit.StopEditing(true);
                flag = true;
            }
            else
            {
                flag = false;
            }
            return(flag);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        /////////////////////////////////////////////////////////////////////////////////////////////
        // create a shapefile
        public IFeatureClass CreateShapefile(string strShapeFolder, FeatureSource selLayer, string shpFileName)
        {
            Console.WriteLine(shpFileName);

            // prepared the shapefile fodler
            try
            {
                if (!System.IO.Directory.Exists(strShapeFolder))
                    System.IO.Directory.CreateDirectory(strShapeFolder);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            // if shapefile already exists, overwrite?
            string shpfileName = strShapeFolder + "\\" + shpFileName + ".shp";
            string dbffileName = strShapeFolder + "\\" + shpFileName + ".dbf";
            string shxfileName = strShapeFolder + "\\" + shpFileName + ".shx";
            if (File.Exists(shpfileName) || File.Exists(dbffileName) || File.Exists(shxfileName))
            {
                if (MessageBox.Show("shape file: " + strShapeFolder + "\\" + selLayer.Name + " already exists", "overwrite?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    return null;
                else
                {
                    File.Delete(shpfileName);
                    File.Delete(dbffileName);
                    File.Delete(shxfileName);
                }
            }

            // create workspace
            IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace pWS = (IFeatureWorkspace)pWSF.OpenFromFile(strShapeFolder, 0);

            // get the feature table
            FeatureDataTable ftable = selLayer.GetDataTable();
            int rowCount = ftable.Rows.Count;
            int colCount = ftable.Columns.Count;
            int geometryColIndex = ftable.GeometryColumnIndex;

            // create fields
            //fields
            ESRI.ArcGIS.Geodatabase.IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;
            pFieldsEdit.FieldCount_2 = colCount;
            Console.WriteLine("Colume Counts = " + colCount.ToString());

            //field
            IField pField;
            IFieldEdit pFieldEdit;
            string shpFldName = "";

            for (int col = 0; col < colCount; col++)
            {
                DataColumn dc = ftable.Columns[col];
                pField = new FieldClass();
                pFieldEdit = (IFieldEdit)pField;

                String colname = dc.ColumnName;
                if (colname.Length > 10)
                {
                    colname = colname.Substring(0, 8) + col.ToString();
                }

                pFieldEdit.AliasName_2 = colname;
                pFieldEdit.Name_2 = colname;
                pFieldEdit.IsNullable_2 = true;
                pFieldEdit.Editable_2 = true;

                Type dt = dc.DataType;
                Console.WriteLine(dc.ColumnName + "\t\t" + colname + "\t\t" + dt.ToString());

                if (dt == typeof(String))
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;

                else if ((dt == typeof(Boolean)))
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger;
                else if ((dt == typeof(Int16)) || (dt == typeof(Int32)) || (dt == typeof(Int64)))
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
                else if (dt == typeof(Double))
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
                else if (dt == typeof(Single))
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeSingle;
                else if (dt == typeof(DateTime))
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate;
                else if (dt == typeof(Guid))
                    //pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGUID;    // not supported by shapefile
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                else if (dt == typeof(ESRI.ArcGIS.Mobile.GlobalId))
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                //pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGlobalID;  // not supported by shapefile

                else if (dt == typeof(Bitmap))   // raster or blob, not supported by shapefile,
                {
                    Console.WriteLine(dc.ColumnName + ": raster field found\n");
                    string folder = strShapeFolder + "\\" + dc.ColumnName;
                    try
                    {
                        if (!Directory.Exists(folder))
                            Directory.CreateDirectory(folder);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return null;
                    }
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                }
                else if (dt == typeof(System.Byte[]))   // raster or blob, not supported by shapefile,
                {
                    Console.WriteLine(dc.ColumnName + ": blob field found\n");
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                }
                else if (dt == typeof(Bitmap))   // raster or blob, not supported by shapefile,
                {
                    Console.WriteLine(dc.ColumnName + ": raster field found\n");
                    string folder = strShapeFolder + "\\" + dc.ColumnName;
                    try
                    {
                        if (!Directory.Exists(folder))
                            Directory.CreateDirectory(folder);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return null;
                    }

                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                }
                else if (dt == typeof(System.Byte[]))   // raster or blob, not supported by shapefile,
                {
                    Console.WriteLine(dc.ColumnName + ": blob field found\n");
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                }
                else if (dt == typeof(ESRI.ArcGIS.Mobile.Geometries.Geometry))
                {
                    Console.WriteLine("geometry field found\n");
                    shpFldName = dc.ColumnName;
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                    IGeometryDef pGeoDef = new GeometryDefClass();
                    IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;
                    pGeoDefEdit.GeometryType_2 = (ESRI.ArcGIS.Geometry.esriGeometryType)selLayer.GeometryType;
                    pGeoDefEdit.SpatialReference_2 = new ESRI.ArcGIS.Geometry.UnknownCoordinateSystemClass();
                    pFieldEdit.GeometryDef_2 = pGeoDef;
                }

                else
                {
                    throw (new Exception("different data type found, modify the code"));
                }

                // add the field to fields
                pFieldsEdit.set_Field(col, pFieldEdit);
            }

            for (int ii = 0; ii < pFields.FieldCount; ii++)
            {
                Console.WriteLine(pFields.get_Field(ii).Name + "\t\t" + pFields.get_Field(ii).Type.ToString());
            }

            //create shapefile
            IFeatureClass fc = null;
            try
            {
                fc = pWS.CreateFeatureClass(shpFileName, pFields, null, null, esriFeatureType.esriFTSimple, shpFldName, "");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            if (fc != null)
                return fc;
            else
                return null;
        }
Ejemplo n.º 8
0
        public IFeatureClass CreateFeatureClass(string geometryType, ISpatialReference spatialReference)
        {
            //IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
            //string gdbName = "FGISTemp" + ".gdb";
            //string gdbFullPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), gdbName);
            //IWorkspace workspace;
            //IFeatureWorkspace featureWorkspace;

            //if (System.IO.Directory.Exists(gdbFullPath))
            //{
            //    featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(gdbFullPath, 0);
            //}
            //else
            //{
            //    IWorkspaceName workspaceName = workspaceFactory.Create(System.IO.Path.GetTempPath(), gdbName, null, 0);
            //    IName name = (ESRI.ArcGIS.esriSystem.IName)workspaceName;
            //    workspace = (IWorkspace)name.Open();
            //    featureWorkspace = workspace as IFeatureWorkspace;
            //}

            IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetTempPath(), 0);
            IFields           fields           = new FieldsClass();
            IFieldsEdit       fieldsEdit       = (IFieldsEdit)fields;

            fieldsEdit.FieldCount_2 = 2;
            //Create the Object ID field.
            IField     fieldUserDefined = new Field();
            IFieldEdit fieldEdit        = (IFieldEdit)fieldUserDefined;

            fieldEdit.Name_2      = "FID";
            fieldEdit.AliasName_2 = "OBJECT ID";
            fieldEdit.Type_2      = esriFieldType.esriFieldTypeOID;
            fieldsEdit.set_Field(0, fieldUserDefined);
            // Create the Shape field.
            fieldUserDefined = new Field();
            fieldEdit        = (IFieldEdit)fieldUserDefined;
            // Set up the geometry definition for the Shape field.
            IGeometryDef     geometryDef     = new GeometryDefClass();
            IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;

            switch (geometryType)
            {
            case "点":
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
                break;

            case "线":
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
                break;

            case "多边形":
            case "矩形":
            case "圆":
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                break;

            default:
                break;
            }
            // By setting the grid size to 0, you're allowing ArcGIS to determine the appropriate grid sizes for the feature class.
            // If in a personal geodatabase, the grid size will be 1000. If in a file or ArcSDE geodatabase, the grid size
            // will be based on the initial loading or inserting of features.
            geometryDefEdit.GridCount_2 = 1;
            geometryDefEdit.set_GridSize(0, 0);
            geometryDefEdit.HasM_2 = false;
            geometryDefEdit.HasZ_2 = false;
            //Assign the spatial reference that was passed in, possibly from
            //IGeodatabase.SpatialReference for the containing feature dataset.
            if (spatialReference != null)
            {
                geometryDefEdit.SpatialReference_2 = spatialReference;
            }
            // Set standard field properties.
            fieldEdit.Name_2        = "SHAPE";
            fieldEdit.Type_2        = esriFieldType.esriFieldTypeGeometry;
            fieldEdit.GeometryDef_2 = geometryDef;
            fieldEdit.IsNullable_2  = true;
            fieldEdit.Required_2    = true;
            fieldsEdit.set_Field(1, fieldUserDefined);

            UID CLSID = new UIDClass();

            CLSID.Value = "esriGeoDatabase.Feature";

            return(featureWorkspace.CreateFeatureClass(tempFeatureLayerName, fields, CLSID, null, esriFeatureType.esriFTSimple, fields.get_Field(1).Name, ""));
        }