Exemple #1
0
        public static IEnumerable<AddressPartIndexWrite> ParseItems(Stream ms)
        {
            DbfFile dbfFile = new DbfFile(Encoding.GetEncoding(866));
            dbfFile.Open(ms);
            try
            {
                DbfHeader header = dbfFile.Header;
                dynamic headerObj = new
                {
                    NAME = FindColumnOrThrow(header, "NAME", maxLen: 40),
                    SOCR = FindColumnOrThrow(header, "SOCR", maxLen: 10),
                    CODE = FindColumnOrThrow(header, "CODE", maxLen: 17),
                    INDEX = FindColumnOrThrow(header, "INDEX", maxLen: 6),
            //                    GNINMB = FindColumnOrThrow(header, "GNINMB", maxLen: 4),
            //                    UNO = FindColumnOrThrow(header, "UNO", maxLen: 4),
            //                    OCATD = FindColumnOrThrow(header, "OCATD", maxLen: 11),
                };

                IEnumerable<DbfRecord> records = EnumRecords(dbfFile, header);

                foreach (var record in records)
                {
                    AddressPartIndexWrite d = EnumRecord(headerObj, record);
                    if (d != null) yield return d;
                }
            }
            finally
            {
                dbfFile.Close();
            }
        }
Exemple #2
0
        public static IEnumerable<AddressPartReduction> ParseItems(Stream ms)
        {
            DbfFile dbfFile = new DbfFile(Encoding.GetEncoding(866));
            dbfFile.Open(ms);
            try
            {
                DbfHeader header = dbfFile.Header;
                dynamic headerObj = new
                {
                    LEVEL = FindColumnOrThrow(header, "LEVEL", maxLen: 5),
                    SCNAME = FindColumnOrThrow(header, "SCNAME", maxLen: 10),
                    SOCRNAME = FindColumnOrThrow(header, "SOCRNAME", maxLen: 29),
            //                    KOD_T_ST = FindColumnOrThrow(header, "KOD_T_ST", maxLen: 3),
                };

                IEnumerable<DbfRecord> records = EnumRecords(dbfFile, header);

                foreach (var record in records)
                {
                    var item = new AddressPartReduction()
                    {
                        Level = int.Parse(record[headerObj.LEVEL].Trim()),
                        Short = record[headerObj.SCNAME].Trim(),
                        Name = record[headerObj.SOCRNAME].Trim(),
            //                        KOD_T_ST = record[headerObj.KOD_T_ST].Trim(),
                    };
                    yield return item;
                }
            }
            finally
            {
                dbfFile.Close();
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            if(args.Length < 2)
              {
            //print help
            Console.WriteLine("\n\n");
            Console.WriteLine("Welcome to Social Explorer DBF 2 CSV Utility");
            Console.WriteLine("-------------------------------------------------");
            Console.WriteLine("\nParameters:");
            Console.WriteLine("1. input DBF file");
            Console.WriteLine("2. output CSV file");

            Console.WriteLine("\nOptional switches:");
            Console.WriteLine("/F  - format numbers so 5.5000 comes out as 5.5");
            Console.WriteLine("/P  - padded output, fixed width (/P trumps /F)");
            Console.WriteLine("/Q  - only output quotes when comma appears in data");

            Console.WriteLine("\n\nExample: dbf2csv \"in.dbf\" \"out.csv\" /P /Q");

              }
              else
              {

            //check if input DBF file exists...
            if(!File.Exists(args[0]))
            {
              Console.WriteLine("Input file '" + args[0] + "' does not exist!");
              return;
            }

            //create output csv file overwrite if already exists.
            if(File.Exists(args[1]))
            {
              //ask to overwrite:
              Console.WriteLine("Output CSV file '" + args[1] + "' already exists.");
              Console.WriteLine("Would you like to overwrite it? Press 'Y' for yes: ");
              if(Console.ReadKey().KeyChar.ToString().ToUpper() != "Y")
            return;
            }

            bool bSwitchF = false;
            bool bSwitchP = false;
            bool bSwitchQ = false;

            for(int i=0;i<args.Length;i++)
              if(args[i] == "/F")
            bSwitchF = true;

            for (int i = 0; i < args.Length; i++)
              if (args[i] == "/P")
            bSwitchP = true;

            for (int i = 0; i < args.Length; i++)
              if (args[i] == "/Q")
            bSwitchQ = true;

            //open DBF file and create CSV output file...
            StreamWriter swcsv = new StreamWriter(args[1], false, Encoding.Default);
            DbfFile dbf = new DbfFile(Encoding.UTF8);
            dbf.Open(args[0], FileMode.Open);

            //output column names
            for (int i = 0; i < dbf.Header.ColumnCount; i++)
            {
              if(dbf.Header[i].ColumnType != DbfColumn.DbfColumnType.Binary &&
             dbf.Header[i].ColumnType != DbfColumn.DbfColumnType.Memo)
            swcsv.Write((i == 0 ? "": ",") + dbf.Header[i].Name);
              else
            Console.WriteLine("WARNING: Excluding Binary/Memo field '" + dbf.Header[i].Name + "'");

            }

            swcsv.WriteLine();

            //output values for all but binary and memo...
            DbfRecord orec = new DbfRecord(dbf.Header);
            while(dbf.ReadNext(orec))
            {
              //output column values...
              if (!orec.IsDeleted)
              {
            for (int i = 0; i < orec.ColumnCount; i++)
            {
              if(orec.Column(i).ColumnType == DbfColumn.DbfColumnType.Character)
              {
                //string values: trim, enclose in quotes and escape quotes with double quotes
                string sval = orec[i];

                if(!bSwitchP)
                  sval = orec[i].Trim();

                if(!bSwitchQ || sval.IndexOf('"') > -1)
                  sval = ("\"" + sval.Replace("\"", "\"\"") + "\"");

                swcsv.Write(sval);

              }
              else if(orec.Column(i).ColumnType == DbfColumn.DbfColumnType.Date)
                swcsv.Write(orec.GetDateValue(i).ToString("MM-dd-yyyy"));
              else
              {
                if (bSwitchP)
                  swcsv.Write(orec[i]);
                else if(bSwitchF)
                  swcsv.Write(FormatNumber(orec[i].Trim()));
                else
                  swcsv.Write(orec[i].Trim());
              }

              //end record with a linefeed or end column with a comma.
              if(i < orec.ColumnCount-1)
                swcsv.Write(",");

            }

            //write line...
            swcsv.WriteLine();

              }

            }

            //close files...
            swcsv.Flush();
            swcsv.Close();
            dbf.Close();

              }
        }
        /// <summary>
        /// Write data in given time range as ArcSWAT dbf file
        /// </summary>
        /// <param name="startYear"></param>
        /// <param name="endYear"></param>
        /// <param name="destinationFolder"></param>
        /// <returns></returns>
        private bool save2ArcSWATdbf(int startYear, int endYear, string destinationFolder)
        {
            string timeAffix = getTimeAffix();
            string pFile = string.Format("{0}\\P{1}{2}.dbf", Path.GetFullPath(destinationFolder), _id, timeAffix);  //precipitation
            string tFile = string.Format("{0}\\T{1}{2}.dbf", Path.GetFullPath(destinationFolder), _id, timeAffix);  //temperature

            this.setProgress(0,string.Format("Processing station {0}", _id));
            this.setProgress(0, pFile);
            this.setProgress(0, tFile);

            //create the dbf structure based on ArcSWAT document
            DbfFile pDBF = new DbfFile();
            pDBF.Open(pFile, FileMode.Create);
            pDBF.Header.AddColumn(new DbfColumn("DATE", DbfColumn.DbfColumnType.Date));
            pDBF.Header.AddColumn(new DbfColumn("PCP", DbfColumn.DbfColumnType.Number, 5, 1));

            DbfFile tDBF = new DbfFile();
            tDBF.Open(tFile, FileMode.Create);
            tDBF.Header.AddColumn(new DbfColumn("DATE", DbfColumn.DbfColumnType.Date));
            tDBF.Header.AddColumn(new DbfColumn("MAX", DbfColumn.DbfColumnType.Number, 5, 1));
            tDBF.Header.AddColumn(new DbfColumn("MIN", DbfColumn.DbfColumnType.Number, 5, 1));

            DbfRecord pRec = new DbfRecord(pDBF.Header);
            DbfRecord tRec = new DbfRecord(tDBF.Header);

            int processPercent = 0;
            bool hasResults = false;
            clearFailureYears();
            clearUncompletedYears();
            for (int i = startYear; i <= endYear; i++)
            {
                setProgress(processPercent, string.Format("Downloading data for station: {0}, year: {1}", _id, i));
                string resultsForOneYear = this.retrieveAnnualDailyClimateData(i, true);
                if (resultsForOneYear.Length == 0)
                {
                    addFailureYear(i);
                    continue;
                }

                processPercent += 1;
                setProgress(processPercent, "Writing data");

                using (CachedCsvReader csv = new CachedCsvReader(new StringReader(resultsForOneYear), true))
                {
                    if (csv.FieldCount >= 27)
                    {
                        hasResults = true;

                        string date = "";
                        while (csv.ReadNextRecord())
                        {
                            date = csv[0];
                            double p = ClimateString2Double(csv[TOTAL_PRECIPITATION_COL_INDEX]);
                            pRec[0] = date;
                            pRec[1] = p.ToString();
                            pDBF.Write(pRec, true);

                            double t_max = ClimateString2Double(csv[MAX_T_COL_INDEX]);
                            double t_min = ClimateString2Double(csv[MIN_T_COL_INDEX]);
                            tRec[0] = date;
                            tRec[1] = t_max.ToString();
                            tRec[2] = t_min.ToString();
                            tDBF.Write(tRec, true);
                        }
                        checkLastDayofYear(date);
                    }
                }
                processPercent += 1;
            }
            pDBF.Close();
            tDBF.Close();

            return hasResults;
        }
Exemple #5
0
        private static void TestWriteNewDbf()
        {
            //create a simple DBF file and output to args[0]
            var odbf = new DbfFile(Encoding.GetEncoding(1252));
            odbf.Open(Path.Combine(TestPath,"TestNew2.dbf"), FileMode.Create);

            //create a header
            odbf.Header.AddColumn(new DbfColumn("StrCol", DbfColumn.DbfColumnType.Character, 20, 0));
            odbf.Header.AddColumn(new DbfColumn("DecCol1", DbfColumn.DbfColumnType.Number, 5, 1));
            odbf.Header.AddColumn(new DbfColumn("DecCol2", DbfColumn.DbfColumnType.Number, 5, 2));
            odbf.Header.AddColumn(new DbfColumn("DecCol3", DbfColumn.DbfColumnType.Number, 5, 3));
            odbf.Header.AddColumn(new DbfColumn("DecCol4", DbfColumn.DbfColumnType.Number, 15, 5));
            odbf.Header.AddColumn(new DbfColumn("NumCol1", DbfColumn.DbfColumnType.Number, 5, 0));
            odbf.Header.AddColumn(new DbfColumn("NumCol2", DbfColumn.DbfColumnType.Number, 10, 0));
            odbf.Header.AddColumn(new DbfColumn("DateCol1", DbfColumn.DbfColumnType.Date));
            odbf.Header.AddColumn(new DbfColumn("BoolCol1", DbfColumn.DbfColumnType.Boolean));

            //add some records...
            var orec = new DbfRecord(odbf.Header) {AllowDecimalTruncate = true};
            orec[0] = "Ahmed Test";
            orec[1] = "123.5";
            orec[2] = "12.35";
            orec[3] = "1.235";
            orec[4] = "1235.123456";
            orec[5] = "1235";
            orec[6] = "123567890";
            orec[7] = "11/07/2007";
            orec[8] = "f";
            odbf.Write(orec, true);

            orec[0] = "Stéfanié Singer";
            orec[1] = "-1.5";
            orec[2] = "-1.35";
            orec[3] = "1.235";
            orec[4] = "-1235.123";
            orec[5] = "15";
            orec[6] = "12345"; //put a decimal in integer, we won't throw an exception beacuse we do not test for that.
            orec[7] = "2008-12-21";
            orec[8] = "f";
            odbf.Write(orec, true);

            orec[0] = "Stéfanié Singer longer than fits in the DBF record!";
            orec[1] = "0.1";
            orec[2] = ".12";
            orec[3] = ".1";
            orec[4] = "";
            orec[5] = "-15";
            orec[6] = "-12345"; //put a decimal in integer, we won't throw an exception beacuse we do not test for that.
            orec[7] = "";
            orec[8] = "no";
            odbf.Write(orec);

            //overwrite first record with last record's data...
            orec.RecordIndex = 0;
            odbf.Write(orec);

            //odbf.Header.RecordCount = 50;
            odbf.WriteHeader();

            odbf.Close();

            //open the same DBF file we just output, and append a few records to it...
            odbf.Open(Path.Combine(TestPath,"TestNew2.dbf"), FileMode.Open);

            orec.Clear();
            orec[0] = "New record added!";
            orec[6] = "100";
            orec[8] = "t";
            odbf.Write(orec, true);

            orec[0] = "New record 2";
            orec[6] = "104";
            orec[8] = "y";
            odbf.Write(orec, true);

            orec[0] = "New record 3";
            orec[6] = "104";
            orec[8] = "TRUE";
            odbf.Write(orec, true);

            if (odbf.Read(0, orec))
            {
                orec[0] = "modified first record";
                odbf.Write(orec, true);
            }

            //read 3rd record and output to console...
            Console.WriteLine(odbf.Read(2).ToString());

            //now add a new record, forcing seek to end of file...
            orec.Clear();
            orec[0] = "New record 4";
            orec[6] = "500";
            orec[8] = "FALSE";
            odbf.Write(orec, true);

            odbf.Close();

            Console.ReadKey();
        }
Exemple #6
0
        private static void TestReadHttpConn()
        {
            //Open a web file...
            //-------------------------------------
            var oWebDBF = new DbfFile(Encoding.GetEncoding(1252));
            var oWebFile = new WebClient();

            oWebDBF.Open(oWebFile.OpenRead("http://private.socialexplorer.com/State_vars1.dbf"));

            //read and print records to screen...
            var orecWeb = new DbfRecord(oWebDBF.Header);

            var ofs2 = new FileStream(Path.Combine(TestPath,"Webfile.txt"), FileMode.Create);
            var osw2 = new StreamWriter(ofs2);

            bool bIsForwardOnly = oWebDBF.IsForwardOnly;
            bool bIsReadOnly = oWebDBF.IsReadOnly;

            while (oWebDBF.ReadNext(orecWeb))
                osw2.WriteLine("index: " + orecWeb.RecordIndex + ": " + orecWeb);

            osw2.Flush();
            osw2.Close();

            oWebDBF.Close();
        }