コード例 #1
0
        /// <summary>
        /// converte la feature class in csv. Se non sono presenti le coordinate il dato non viene inserito
        /// </summary>
        /// <param name="sourceWorkspace">workspace con la feature class</param>
        /// <param name="outputName">nome della feature class e del csv di output</param>
        /// <param name="targetPath">percorso dove salvare il file di output</param>
        /// <param name="delimitator">delimitatore utilizzato nel csv</param>
        private void ConvertFeatureClassPointToCsv(IWorkspace sourceWorkspace, string outputName, string targetPath, string delimitator = ";")
        {
            IFeatureCursor featureCursor = null;

            try
            {
                IFeatureWorkspace featureWorkspace = sourceWorkspace as IFeatureWorkspace;
                IFeatureClass     featureClass     = featureWorkspace.OpenFeatureClass(outputName);
                if (featureClass.ShapeType != esriGeometryType.esriGeometryPoint)
                {
                    throw new Exception($"Per esportare in {Enum.GetName(typeof(FileExtension), FileExtension.csv)} occorre una feature class di tipo {Enum.GetName(typeof(esriGeometryType), esriGeometryType.esriGeometryPoint)}!");
                }

                featureCursor = featureClass.Search(null, true);

                CsvExport csvExport = new CsvExport();
                csvExport.Delimiter = delimitator;

                Dictionary <int, string> fields = new Dictionary <int, string>();
                IField field = null;
                for (int i = 0; i < featureCursor.Fields.FieldCount; i++)
                {
                    field = featureCursor.Fields.Field[i];
                    if ((field.Type == esriFieldType.esriFieldTypeBlob) || (field.Type == esriFieldType.esriFieldTypeGeometry) ||
                        (field.Type == esriFieldType.esriFieldTypeGlobalID) || (field.Type == esriFieldType.esriFieldTypeGUID) ||
                        (field.Type == esriFieldType.esriFieldTypeRaster) || (field.Type == esriFieldType.esriFieldTypeXML))
                    {
                        continue;
                    }

                    fields.Add(i, field.Name);
                }

                IFeature feature = null;
                while ((feature = featureCursor.NextFeature()) != null)
                {
                    if ((feature.ShapeCopy == null) || (feature.ShapeCopy.IsEmpty))
                    {
                        continue;
                    }

                    IPoint p = feature.ShapeCopy as IPoint;
                    csvExport.AddRow();
                    csvExport["X"] = p.X;
                    csvExport["Y"] = p.Y;

                    foreach (int i in fields.Keys)
                    {
                        csvExport[fields[i]] = feature.get_Value(i);
                    }
                }

                csvExport.ExportToFile(System.IO.Path.Combine(targetPath, System.IO.Path.ChangeExtension(outputName, Enum.GetName(typeof(FileExtension), FileExtension.csv))));
            }
            catch
            {
                throw;
            }
            finally
            {
                if (featureCursor != null)
                {
                    Marshal.FinalReleaseComObject(featureCursor);
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Append an object csvExport in a current csvExport
 /// </summary>
 /// <param name="csvExport">object csvExport</param>
 public void Append(CsvExport csvExport)
 {
     this.rows.AddRange(csvExport.rows);
 }