public void ProcessDirectory(KeyValuePair <DirectoryInfo, IEnumerable <FileInfo> > directory) { if (!directory.Key.Exists) { throw new ArgumentException("The directory specified does not exist. Unable to process directory.", nameof(directory.Key)); } if (directory.Value == null || !directory.Value.Any()) { throw new ArgumentException("The specified directory contains no files to parse. Unable to process directory", nameof(directory.Value)); } NameDefinitionType nameDefinitionType = (NameDefinitionType)Enum.Parse(typeof(NameDefinitionType), directory.Key.Name); string nameDefinition = Dictionaries.NameDefinitions[nameDefinitionType]; //TODO: TEMPORARY! REMOVE THIS TO PREVENT SKIPPING! //if (nameDefinitionType == NameDefinitionType.ADDR) // return; Console.WriteLine( $"Now processing {directory.Value.Count()} zip files in the {nameDefinitionType} ({nameDefinition}) directory"); RawGISDataEntities db = new RawGISDataEntities(); db.Database.Initialize(true); // Single-Threaded Operation foreach (FileInfo file in directory.Value) { ProcessZipFile(nameDefinitionType, file); } // Multi-Threaded Operation //Parallel.ForEach(directory.Value, value => ProcessZipFile(nameDefinitionType, value)); }
public void ProcessZipFile(NameDefinitionType nameDefinitionType, FileInfo file) { #region File Path Strings string fileName = file.Name.Replace(file.Extension, ""); string outputPath = Path.Combine(OutputDirectoryPath, fileName); string codePageFilePath = Path.Combine(outputPath, $"{fileName}.cpg"); string projectionFilePath = Path.Combine(outputPath, $"{fileName}.prj"); //string dbfMetadataFilePath = Path.Combine(outputPath, $"{fileName}.dbf.xml"); //string shpMetadataFilePath = Path.Combine(outputPath, $"{fileName}.shp.xml"); string attributeFilePath = Path.Combine(outputPath, $"{fileName}.dbf"); string shapeFilePath = Path.Combine(outputPath, $"{fileName}.shp"); #endregion File Path Strings #region File Infos FileInfo codePageFileInfo = new FileInfo(codePageFilePath); FileInfo projectionFileInfo = new FileInfo(projectionFilePath); //FileInfo dbfMetadataFileInfo = new FileInfo(dbfMetadataFilePath); //FileInfo shpMetadataFileInfo = new FileInfo(shpMetadataFilePath); FileInfo attributeFileInfo = new FileInfo(attributeFilePath); FileInfo shapeFileInfo = new FileInfo(shapeFilePath); #endregion File Infos #region Directory Prep if (Directory.Exists(outputPath)) { Console.WriteLine($"Deleting Directory: {outputPath}"); FileSystem.DeleteDirectory(outputPath, UIOption.OnlyErrorDialogs, RecycleOption.DeletePermanently); } #endregion Directory Prep #region File Extraction Console.WriteLine($"\rExtracting {file.Name} to {outputPath}\t\t"); ZipFile.ExtractToDirectory(file.FullName, outputPath); #endregion File Extration #region File Prep Console.WriteLine("Removing XML Files"); foreach (string isoFile in Directory.EnumerateFiles(outputPath, "*.xml")) { FileSystem.DeleteFile(isoFile, UIOption.OnlyErrorDialogs, RecycleOption.DeletePermanently); } if (attributeFileInfo.Exists && attributeFileInfo.Name.Length > 8) { Console.WriteLine("Renaming Attribute File"); string newName = "Attrib.dbf"; FileSystem.RenameFile(attributeFilePath, newName); attributeFilePath = Path.Combine(outputPath, newName); attributeFileInfo = new FileInfo(attributeFilePath); } #endregion File Prep #region File Processing Console.WriteLine("Processing files"); CodePageFile codePageFile = new CodePageFile(codePageFileInfo); ProjectionFile projectionFile = new ProjectionFile(projectionFileInfo); //MetadataFile dbfMetadataFile = new MetadataFile(dbfMetadataFileInfo); //MetadataFile shpMetadataFile = new MetadataFile(shpMetadataFileInfo); //MetadataFile metadataFile = new MetadataFile(dbfMetadataFileInfo.Exists ? dbfMetadataFileInfo : shpMetadataFileInfo); //if (dbfMetadataFile.Exists && shpMetadataFile.Exists) //{ // Console.WriteLine($"{fileName} has both shape and attribute metadata files!"); // Debugger.Break(); //} AttributeFile attributeFile = new AttributeFile(attributeFileInfo); ShapeFile shapeFile = projectionFile.Exists ? new ShapeFile(shapeFileInfo, projectionFile.SRID) : null; #endregion if (shapeFile != null) { DataColumn newColumn = new DataColumn("GEOM", typeof(SqlGeometry)); attributeFile.Records.Tables[0].Columns.Add(newColumn); foreach (Shape shape in shapeFile.Shapes) { attributeFile.Records.Tables[0].Rows[shape.RecordNumber - 1][newColumn.ColumnName] = shape.DTGeometry; } } using (SqlBulkCopy sbc = new SqlBulkCopy(DataHelper.DefaultConnectionString)) { sbc.SqlRowsCopied += Sbc_SqlRowsCopied; sbc.DestinationTableName = Pluralizer.Pluralize(nameDefinitionType.ToString()); sbc.BatchSize = DataHelper.DefaultBatchSize; sbc.BulkCopyTimeout = DataHelper.DefaultTimeoutSeconds; foreach (DataColumn column in attributeFile.Records.Tables[0].Columns) { sbc.ColumnMappings.Add(column.ColumnName, column.ColumnName); } sbc.WriteToServer(attributeFile.Records.Tables[0]); } #region Directory Cleanup Console.WriteLine($"Deleting Directory: {outputPath}"); FileSystem.DeleteDirectory(outputPath, UIOption.OnlyErrorDialogs, RecycleOption.DeletePermanently); #endregion Directory Cleanup }