Exemplo n.º 1
0
        private void 增加字段ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddFieldForm addFieldForm = new AddFieldForm();

            if (DialogResult.OK == addFieldForm.ShowDialog(this))
            {
                curFeaClass.AddAttributeField(addFieldForm.FieldName, addFieldForm.FieldType);
                attributeView.Refresh();
            }
            addFieldForm.Dispose();
        }
Exemplo n.º 2
0
        /// <summary>
        /// 从数据库和文件中加载要素类至内存
        /// </summary>
        /// <param name="id">唯一标识id</param>
        /// <returns></returns>
        public MLFeatureClass LoadFeatureClass(uint id)
        {
            string      featureClassName = "";
            FeatureType featureClassType = FeatureType.POINT;

            double[] featureClassMbr = new double[4];
            uint     featureCount;
            string   shpFilePath;

            foreach (MLRecord curRecord in records)
            {
                if (curRecord.ID == id)
                {
                    featureClassName = curRecord.Name;
                    featureClassType = curRecord.Type;
                    break;
                }
            }
            string       sql = "select * from header where ID=" + id.ToString();
            MySqlCommand cmd = new MySqlCommand(sql, connection);

            using (MySqlDataReader reader = cmd.ExecuteReader())
            {
                reader.Read();
                featureCount       = reader.GetUInt32("Count");
                featureClassMbr[0] = reader.GetDouble("Xmin");
                featureClassMbr[1] = reader.GetDouble("Xmax");
                featureClassMbr[2] = reader.GetDouble("Ymin");
                featureClassMbr[3] = reader.GetDouble("Ymax");
                shpFilePath        = reader.GetString("FilePath");
            }
            MLFeatureClass curFeaClass = new MLFeatureClass(id, featureClassName, featureClassType, featureClassMbr);

            using (FileStream shp = new FileStream(shpFilePath, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(shp))
                {
                    sql = "select * from `" + id.ToString() + "`";
                    cmd = new MySqlCommand(sql, connection);
                    using (MySqlDataReader reader = cmd.ExecuteReader())
                    {
                        curFeaClass.AddAttributeField("ID", typeof(uint));
                        curFeaClass.AddAttributeField("Geometry", typeof(FeatureType));
                        for (int i = 3; i < reader.FieldCount; ++i)
                        {
                            curFeaClass.AddAttributeField(reader.GetName(i), reader.GetFieldType(i));
                        }
                        for (int i = 0; i < featureCount; ++i)
                        {
                            reader.Read();
                            object[] curRow = new object[reader.FieldCount];
                            reader.GetValues(curRow);
                            br.BaseStream.Seek((long)reader.GetUInt32("FileBias"), SeekOrigin.Begin);
                            MLFeature curFeature;
                            switch (featureClassType)
                            {
                            case FeatureType.POINT:
                                curFeature = new MLPoint(br);
                                break;

                            case FeatureType.POLYLINE:
                                curFeature = new MLPolyline(br);
                                break;

                            case FeatureType.POLYGON:
                                curFeature = new MLPolygon(br);
                                break;

                            case FeatureType.MULTIPOINT:
                                curFeature = new MLMultiPoint(br);
                                break;

                            default:
                                curFeature = new MLPoint(br);    //
                                break;
                            }
                            curFeaClass.AddFeaure(curFeature, curRow);
                        }
                    }
                }
            }

            return(curFeaClass);
        }
Exemplo n.º 3
0
        public MLFeatureClass LoadFeatureClassFromShapefile(string filePath)
        {
            string shpName = filePath.Substring(filePath.LastIndexOf(@"\") + 1);

            shpName = shpName.Substring(0, shpName.IndexOf(".shp"));

            MLFeatureClass curFeaClass;

            int[] feaBias;
            int[] feaLength;
            using (FileStream shx = new FileStream(filePath.Substring(0, filePath.IndexOf(".shp")) + ".shx", FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(shx))
                {
                    br.BaseStream.Seek(100, SeekOrigin.Begin);
                    long count = (br.BaseStream.Length - 100) / 8;
                    feaBias   = new int[count];
                    feaLength = new int[count];
                    byte[] temp;
                    for (long i = 0; i < count; ++i)
                    {
                        temp         = br.ReadBytes(8).Reverse().ToArray();
                        feaBias[i]   = 2 * BitConverter.ToInt32(temp, 4);
                        feaLength[i] = 2 * BitConverter.ToInt32(temp, 0);
                    }
                }
            }
            using (FileStream shp = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(shp))
                {
                    using (FileStream dbf = new FileStream(filePath.Substring(0, filePath.IndexOf(".shp")) + ".dbf", FileMode.Open, FileAccess.Read))
                    {
                        using (BinaryReader br_dbf = new BinaryReader(dbf))
                        {
                            br.BaseStream.Seek(32, SeekOrigin.Begin);
                            int         typeInt = br.ReadInt32();
                            FeatureType fcType;
                            switch (typeInt)
                            {
                            case 1:
                                fcType = FeatureType.POINT;
                                break;

                            case 3:
                                fcType = FeatureType.POLYLINE;
                                break;

                            case 5:
                                fcType = FeatureType.POLYGON;
                                break;

                            case 8:
                                fcType = FeatureType.MULTIPOINT;
                                break;

                            default:
                                fcType = FeatureType.POINT;
                                break;
                            }
                            double[] mbr = new double[4];
                            mbr[0]      = br.ReadDouble();
                            mbr[2]      = br.ReadDouble();
                            mbr[1]      = br.ReadDouble();
                            mbr[3]      = br.ReadDouble();
                            curFeaClass = new MLFeatureClass(NextFeaClassId, shpName, fcType, mbr);
                            curFeaClass.AddAttributeField("ID", typeof(uint));
                            curFeaClass.AddAttributeField("Geometry", typeof(FeatureType));
                            br_dbf.BaseStream.Seek(4, SeekOrigin.Begin);
                            int    rowCount       = br_dbf.ReadInt32();
                            short  firstRow       = br_dbf.ReadInt16();
                            short  rowLength      = br_dbf.ReadInt16();
                            int    attributeCount = (firstRow - 33) / 32;
                            byte[] fieldLength    = new byte[attributeCount];
                            string fieldName;
                            Type[] fieldTypes;
                            fieldTypes = new Type[attributeCount];
                            char ch;
                            for (int i = 0; i < attributeCount; ++i)
                            {
                                br_dbf.BaseStream.Seek(i * 32 + 32, SeekOrigin.Begin);
                                fieldName = Encoding.UTF8.GetString(br_dbf.ReadBytes(11)).Trim('\0');
                                switch (ch = br_dbf.ReadChar())
                                {
                                case 'I':
                                case 'L':
                                    fieldTypes[i] = typeof(int);
                                    break;

                                case 'N':
                                case 'F':
                                case 'B':
                                    fieldTypes[i] = typeof(double);
                                    break;

                                default:
                                    fieldTypes[i] = typeof(string);
                                    break;
                                }
                                br_dbf.BaseStream.Seek(4, SeekOrigin.Current);
                                fieldLength[i] = br_dbf.ReadByte();
                                curFeaClass.AddAttributeField(fieldName, fieldTypes[i]);
                            }
                            br_dbf.BaseStream.Seek(firstRow, SeekOrigin.Begin);
                            for (int i = 0; i < rowCount; ++i)
                            {
                                br.BaseStream.Seek(feaBias[i], SeekOrigin.Begin);
                                MLFeature curFeature;
                                switch (fcType)
                                {
                                case FeatureType.POINT:
                                    curFeature = new MLPoint(br);
                                    break;

                                case FeatureType.POLYLINE:
                                    curFeature = new MLPolyline(br);
                                    break;

                                case FeatureType.POLYGON:
                                    curFeature = new MLPolygon(br);
                                    break;

                                case FeatureType.MULTIPOINT:
                                    curFeature = new MLMultiPoint(br);
                                    break;

                                default:
                                    curFeature = new MLPoint(br);    //
                                    break;
                                }
                                object[] values = new object[attributeCount + 3];
                                values[0] = (uint)i;
                                string temp;
                                br_dbf.BaseStream.Seek(1, SeekOrigin.Current);
                                for (int j = 0; j < attributeCount; ++j)
                                {
                                    temp = Encoding.GetEncoding("GBK").GetString(br_dbf.ReadBytes(fieldLength[j])).Trim((char)0x20);
                                    if (fieldTypes[j] == typeof(int))
                                    {
                                        if (temp.Equals(""))
                                        {
                                            values[j + 2] = 0;
                                        }
                                        else
                                        {
                                            values[j + 3] = int.Parse(temp);
                                        }
                                    }
                                    if (fieldTypes[j] == typeof(double))
                                    {
                                        if (temp.Equals(""))
                                        {
                                            values[j + 2] = 0;
                                        }
                                        else
                                        {
                                            values[j + 3] = double.Parse(temp);
                                        }
                                    }
                                    if (fieldTypes[j] == typeof(string))
                                    {
                                        values[j + 3] = temp;
                                    }
                                }
                                curFeaClass.AddFeaure(curFeature, values);
                            }
                        }
                    }
                }
            }
            return(curFeaClass);
        }