Exemplo n.º 1
0
        ///格式为

        /*
         * ; name type index formula format
         * "Date" RA -1 F1 "%s,"
         * "Time" RA -1 F0 "%s,"
         * "LATITU" RA -1 F1101 "%9.5f,"
         * "LONGITU" RA -1 F1102 "%9.5f,"
         */
        /// <summary>
        /// 根据header 生成字段
        /// 这里
        /// </summary>
        /// <param name="header"></param>
        /// <returns>AscField </returns>
        public static AscField ParseHeader(String[] header)
        {
            AscField temp = new AscField();

            //这里是hardcode !! reuse 要重写,或像java 有profile,去解析他??
            temp.FieldName = header[0];
            String formula = header[header.Length - 2];

            temp.Formula = formula;
            String t = header[header.Length - 1];

            temp.Type = ASCUtil.FormmaterString2GetType(t);

            if (temp.Type == null)
            {
                return(null);
            }
            else
            {
                return(temp);
            }
        }
Exemplo n.º 2
0
        public static void CreateTable(String csvfilepath, String renamefilepath, String tableName)
        {
            //1 read csv
            //2 预处理字段名 ,将空格去掉,-改为_
            //3 检测重名, 重名的采用 先加_filename ,如果在重复 加_i
            //4 没有重名后,写入 文件,原来 以及改完后的,对于那4个30行,需要处理
            //5 拼接sql,并执行

            //1
            List <String[]> headers = CSVUtil.ReadCsv(csvfilepath);

            //2
            List <String[]> renameHeaders = PerProcessingFieldName(headers);

            //3
            #region 检测重名, 重名的采用 先加_filename ,如果在重复 加_i


            ISet <String> set = new HashSet <String>();
            foreach (String[] strs in renameHeaders)
            {
                String fieldName = strs[0];
                if (set.Contains(fieldName.ToLower()))
                {
                    //rename
                    fieldName += "_" + strs[3];

                    int    i    = 0;
                    String temp = null;
                    do
                    {
                        i++;
                        temp = fieldName + "_" + i;
                    } while (set.Contains(temp.ToLower()));
                    fieldName = temp;

                    strs[0] = fieldName;
                }
                set.Add(fieldName.ToLower());
            }
            #endregion
            //4 没有重名后,写入 文件,原来 以及改完后的,对于那4个30行,需要处理
            StringBuilder stringBuilder = new StringBuilder();
            using (StreamWriter sw = new StreamWriter(renamefilepath))
            {
                for (int i = 0; i < headers.Count(); ++i)
                {
                    foreach (String str in headers[i])
                    {
                        stringBuilder.Append(str + " ");
                    }
                    foreach (String str in renameHeaders[i])
                    {
                        stringBuilder.Append(str + " ");
                    }
                    sw.WriteLine(stringBuilder.ToString());
                    stringBuilder.Clear();
                }
            }
            //5
            List <AscField> fieldHeaders = new List <AscField>();
            foreach (String[] strs in renameHeaders)
            {
                AscField field = new AscField();
                //special
                if (strs[2] == "F10039" || strs[2] == "F10040" || strs[2] == "F10041" || strs[2] == "F10043" || strs[2] == "F10044")
                {
                    String formula = strs[2];
                    String type    = null;

                    if (formula == "F10039")
                    {
                        type = "DOUBLE(12,6)";
                    }
                    else
                    {
                        type = "FLOAT(12,6)";
                    }

                    for (int i = 0; i < 30; ++i)
                    {
                        field           = new AscField();
                        field.FieldName = strs[0] + "_" + (i + 1);
                        field.Type      = type;
                        field.Formula   = strs[2];
                        fieldHeaders.Add(field);
                    }
                }
                else
                {
                    //ordinary
                    field.FieldName = strs[0];
                    field.Type      = strs[1].Replace("\"", "");
                    field.Formula   = strs[2];
                    fieldHeaders.Add(field);
                }
            }
            //output last rename field -> .last file
            using (StreamWriter sw = new StreamWriter(renamefilepath + ".last"))
            {
                foreach (AscField f in fieldHeaders)
                {
                    stringBuilder.Append(f.FieldName + " " + f.Type + " " + f.Formula);
                    sw.WriteLine(stringBuilder.ToString());
                    stringBuilder.Clear();
                }
            }

            String strSql = GenerateCreateTableSql(tableName, fieldHeaders.ToArray());
            Console.WriteLine("创建表sql语句\n" + strSql);

            MySqlDatabaseUtil.ExecuteNoQuery(strSql);
        }
Exemplo n.º 3
0
        //处理单个 asc文件-》.asc.parse 文件,包含 Field的属性,以及每个字段出现的文件名
        public static void  ProcessOneAscFile(String ascfilepath, String destFolder)
        {
            //自动创建目录
            if (!Directory.Exists(destFolder))
            {
                Directory.CreateDirectory(destFolder);
            }

            List <String>   fileHeader  = null;
            List <String[]> dataheaders = ASCUtil.ParseAsc(ascfilepath, out fileHeader);
            //process header, 生成Field
            List <AscField> fields = new List <AscField>();

            foreach (String[] strs in dataheaders)
            {
                AscField temp = ASCUtil.ParseHeader(strs);
                if (temp != null)
                {
                    fields.Add(temp);
                }
            }
            List <String[]>    headers      = FML300Util.ParseThirdHundredFile(ascfilepath + @"\fml.300");
            List <FML300Field> fml300fields = FML300Util.ParsethirdHundred(headers);
            //decorate to find fast,assume key is not duplicate

            Dictionary <String, int> dict = new Dictionary <string, int>();

            foreach (FML300Field fml300field in fml300fields)
            {
                if (dict.ContainsKey(fml300field.formula))
                {
                    Console.WriteLine("fml300Fields formula is duplicate {0} {1} {2} ", fml300field.formula, fml300field.number, dict[fml300field.formula]);
                }
                else
                {
                    dict.Add(fml300field.formula, fml300field.number);
                }
            }



            //利用fml.300文件,AscField ,生成AdvancedAscField
            List <AdvancedAscField> advancedfields = new List <AdvancedAscField>();

            foreach (AscField field in fields)
            {
                AdvancedAscField advancedAscField = new AdvancedAscField
                {
                    FieldName = field.FieldName,
                    Formula   = field.Formula,
                    Type      = field.Type
                };
                if (dict.ContainsKey(field.Formula))
                {
                    advancedAscField.FieldNumber = dict[field.Formula];
                }
                else
                {
                    Console.WriteLine("field.Formula {0} is not found", field.Formula);
                    advancedAscField.FieldNumber = 1;
                }

                advancedfields.Add(advancedAscField);
            }

            FileInfo fileInfo = new FileInfo(ascfilepath);
            String   fileName = fileInfo.Name;
            //output result
            String destpath = destFolder + "//" + fileName.Replace(".asc", "") + ".parse";

            using (StreamWriter sw = new StreamWriter(destpath))
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (AdvancedAscField field in advancedfields)
                {
                    stringBuilder.Append(field.FieldName + " ");
                    stringBuilder.Append(field.Type + " ");
                    stringBuilder.Append(field.Formula + " ");
                    stringBuilder.Append(field.FieldNumber + " ");
                    stringBuilder.Append(fileName.Replace(".asc", ""));
                    sw.WriteLine(stringBuilder.ToString());
                    stringBuilder.Clear();
                }
            }
        }