Exemple #1
0
        //public static object[][] ReadToObject(string dbfFileName, string tableName)
        //{

        //}

        public static void WriteDefault(string dbfFileName, int numberOfRecords, bool overwrite = false)
        {
            List <int> attributes = Enumerable.Range(0, numberOfRecords).ToList();

            List <ObjectToDbfTypeMap <int> > mapping = new List <ObjectToDbfTypeMap <int> >()
            {
                new ObjectToDbfTypeMap <int>(DbfFieldDescriptors.GetIntegerField("Id"), i => i)
            };

            Write(dbfFileName,
                  attributes,
                  mapping,
                  Encoding.ASCII,
                  overwrite);
        }
Exemple #2
0
        public static void Write(string fileName, int numberOfRecords, bool overwrite = false)
        {
            List <int> attributes = Enumerable.Range(0, numberOfRecords).ToList();

            Write(fileName,
                  attributes,
                  new List <Func <int, object> >()
            {
                i => i
            },
                  new List <DbfFieldDescriptor>()
            {
                DbfFieldDescriptors.GetIntegerField("Id")
            },
                  Encoding.ASCII);
        }
Exemple #3
0
        // 1400.02.03-comment
        //public static List<DbfFieldDescriptor> MakeDbfFields(Dictionary<string, object> dictionary)
        //{
        //    List<DbfFieldDescriptor> result = new List<DbfFieldDescriptor>();

        //    foreach (var item in dictionary)
        //    {
        //        result.Add(new DbfFieldDescriptor(item.Key, 'C', 255, 0));
        //    }

        //    return result;
        //}

        // 1400.02.03-comment
        //public static List<ObjectToDbfTypeMap<T>> MakeDbfFieldsAndMaps<T>(IEnumerable<T> values, Func<T, Dictionary<string, object>> extractAttributeFunc)
        //{
        //    var fields = new List<ObjectToDbfTypeMap<T>>();

        //    if (values.IsNullOrEmpty())
        //        return fields;

        //    return MakeDbfFieldsAndMaps

        //    //var firstProperties = extractAttributeFunc(values.First());

        //    //foreach (var item in firstProperties)
        //    //{
        //    //    var propertyName = item.Key;

        //    //    ObjectToDbfTypeMap<T> typeMap = null;

        //    //    var mapFunc = new Func<T, object>(f => extractAttributeFunc(f)[propertyName]);

        //    //    switch (firstProperties[propertyName])
        //    //    {
        //    //        case string property:
        //    //            // گرفتن بیش‌ترین طول
        //    //            var maxLength = (byte)values.Select(f => extractAttributeFunc(f)[propertyName]?.ToString()).Max(val => val == null ? 0 : val.Length);

        //    //            typeMap = new ObjectToDbfTypeMap<T>(DbfFieldDescriptors.GetStringField(propertyName, maxLength), mapFunc);
        //    //            break;

        //    //        case char charProperty:
        //    //        case bool boolProperty:
        //    //            // 1400.02.03: Shapefile does not support boolean field
        //    //            typeMap = new ObjectToDbfTypeMap<T>(DbfFieldDescriptors.GetStringField(propertyName, 1), mapFunc);
        //    //            break;

        //    //        case int intProperty:
        //    //        case short shortProperty:
        //    //        case byte byteProperty:
        //    //        case uint uintProperty:
        //    //        case ushort ushortProperty:
        //    //        case sbyte sbyteProperty:
        //    //            typeMap = new ObjectToDbfTypeMap<T>(DbfFieldDescriptors.GetIntegerField(propertyName), mapFunc);
        //    //            break;

        //    //        case double doubleProperty:
        //    //        case decimal decimalProperty:
        //    //        case float floatProperty:
        //    //            typeMap = new ObjectToDbfTypeMap<T>(DbfFieldDescriptors.GetFloatField(propertyName), mapFunc);
        //    //            break;

        //    //        case long longProperty:
        //    //        case ulong ulongProperty:
        //    //            typeMap = new ObjectToDbfTypeMap<T>(DbfFieldDescriptors.GetFloatFieldForLong(propertyName), mapFunc);
        //    //            break;

        //    //        case DateTime dateTimeProperty:
        //    //            typeMap = new ObjectToDbfTypeMap<T>(DbfFieldDescriptors.GetDateField(propertyName), mapFunc);
        //    //            break;

        //    //        default:
        //    //            typeMap = new ObjectToDbfTypeMap<T>(DbfFieldDescriptors.GetStringField(propertyName), mapFunc);
        //    //            break;
        //    //            //throw new NotImplementedException();
        //    //    }

        //    //    fields.Add(typeMap);
        //    //}

        //    //return fields;
        //}

        public static List <ObjectToDbfTypeMap <Dictionary <string, object> > > MakeDbfFieldsAndMaps(List <Dictionary <string, object> > dictionaries)
        {
            var fields = new List <ObjectToDbfTypeMap <Dictionary <string, object> > >();

            if (dictionaries.IsNullOrEmpty())
            {
                return(fields);
            }

            var firstProperties = dictionaries.First();

            foreach (var item in firstProperties)
            {
                var propertyName = item.Key;

                ObjectToDbfTypeMap <Dictionary <string, object> > typeMap = null;

                var mapFunc = new Func <Dictionary <string, object>, object>(f => f[propertyName]);

                switch (firstProperties[propertyName])
                {
                case string property:
                    // گرفتن بیش‌ترین طول
                    var maxLength = (byte)dictionaries.Select(f => f[propertyName]?.ToString()).Max(val => val == null ? 0 : val.Length);

                    typeMap = new ObjectToDbfTypeMap <Dictionary <string, object> >(DbfFieldDescriptors.GetStringField(propertyName, maxLength), mapFunc);
                    break;

                case char charProperty:
                case bool boolProperty:
                    // 1400.02.03: Shapefile does not support boolean field
                    typeMap = new ObjectToDbfTypeMap <Dictionary <string, object> >(DbfFieldDescriptors.GetStringField(propertyName, 1), mapFunc);
                    break;

                case int intProperty:
                case short shortProperty:
                case byte byteProperty:
                case uint uintProperty:
                case ushort ushortProperty:
                case sbyte sbyteProperty:
                    typeMap = new ObjectToDbfTypeMap <Dictionary <string, object> >(DbfFieldDescriptors.GetIntegerField(propertyName), mapFunc);
                    break;

                case double doubleProperty:
                case decimal decimalProperty:
                case float floatProperty:
                    typeMap = new ObjectToDbfTypeMap <Dictionary <string, object> >(DbfFieldDescriptors.GetFloatField(propertyName), mapFunc);
                    break;

                case long longProperty:
                case ulong ulongProperty:
                    typeMap = new ObjectToDbfTypeMap <Dictionary <string, object> >(DbfFieldDescriptors.GetFloatFieldForLong(propertyName), mapFunc);
                    break;

                case DateTime dateTimeProperty:
                    typeMap = new ObjectToDbfTypeMap <Dictionary <string, object> >(DbfFieldDescriptors.GetDateField(propertyName), mapFunc);
                    break;

                default:
                    typeMap = new ObjectToDbfTypeMap <Dictionary <string, object> >(DbfFieldDescriptors.GetStringField(propertyName), mapFunc);
                    break;
                    //throw new NotImplementedException();
                }

                fields.Add(typeMap);
            }

            return(fields);
        }