コード例 #1
0
ファイル: MLDataFrame.cs プロジェクト: GIS-Designation/HW1
            public Layer(FeatureType type, int _index, uint id = uint.MaxValue)
            {
                index = _index;
                sign  = new Label();
                name  = new Label();
                switch (type)
                {
                case FeatureType.POINT:
                    sign.Text = "·";
                    name.Text = "新建点图层";
                    break;

                case FeatureType.MULTIPOINT:
                    sign.Text = "·";
                    name.Text = "新建多点图层";
                    break;

                case FeatureType.POLYLINE:
                    sign.Text = "—";
                    name.Text = "新建线图层";
                    break;

                case FeatureType.POLYGON:
                    sign.Text = "■";
                    name.Text = "新建面图层";
                    break;
                }

                LineWidth    = 1;              //轮廓宽度为1
                LineStyle    = "Solid";        //实线
                PointSign    = "FilledCircle"; //初始化默认点符号类型为实心圆
                PointSize    = 2;              //点符号大小初始为2
                renderMethod = 0;              //初始默认使用单一符号法渲染
                if (id == uint.MaxValue)
                {
                    featureClass = new MLFeatureClass(MLMainForm.FeatureProcessor, name.Text, type);  //新建一个要素类
                }
                else
                {
                    featureClass = MLMainForm.FeatureProcessor.LoadFeatureClass(id);
                    name.Text    = featureClass.Name;
                }

                sign.Width = 12;  //长宽固定
                name.Width = 78;

                int y = 25 * index + 30;  //新图层的显示位置可以推算

                sign.Location = new Point(0, y);
                name.Location = new Point(12, y);
            }
コード例 #2
0
ファイル: MLDataFrame.cs プロジェクト: GIS-Designation/HW1
            public Layer(MLFeatureClass fc, int _index)
            {
                index = _index;
                sign  = new Label();
                name  = new Label();
                switch (fc.featureType)
                {
                case FeatureType.POINT:
                    sign.Text = "·";
                    break;

                case FeatureType.MULTIPOINT:
                    sign.Text = "·";
                    break;

                case FeatureType.POLYLINE:
                    sign.Text = "—";
                    break;

                case FeatureType.POLYGON:
                    sign.Text = "■";
                    break;
                }

                LineWidth    = 1;              //轮廓宽度为1
                LineStyle    = "Solid";        //实线
                PointSign    = "FilledCircle"; //初始化默认点符号类型为实心圆
                PointSize    = 2;              //点符号大小初始为2
                featureClass = fc;
                name.Text    = fc.Name;

                sign.Width = 12;  //长宽固定
                name.Width = 78;

                int y = 25 * index + 30;  //新图层的显示位置可以推算

                sign.Location = new Point(0, y);
                name.Location = new Point(12, y);
            }
コード例 #3
0
 public void BindData(MLFeatureClass _curFeaClass)
 {
     curFeaClass = _curFeaClass;
     attributeView.DataSource = curFeaClass.AttributeData;
 }
コード例 #4
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);
        }
コード例 #5
0
        public void SaveFeatureClass(MLFeatureClass curFeaClass)
        {
            string       shpPath;
            string       sql = "select * from header where ID=" + curFeaClass.ID.ToString();
            MySqlCommand cmd = new MySqlCommand(sql, connection);
            bool         toUpdate;

            using (MySqlDataReader reader = cmd.ExecuteReader())
            {
                reader.Read();
                if (reader.HasRows)
                {
                    toUpdate = true;
                    shpPath  = reader.GetString("FilePath");
                }
                else
                {
                    toUpdate = false;
                    shpPath  = defaultShpPath + curFeaClass.ID.ToString() + ".shp";
                }
            }
            if (toUpdate)
            {
                //update
                sql = "update header set Name='" + curFeaClass.Name
                      + "',Count=" + curFeaClass.Count.ToString()
                      + ",Xmin=" + curFeaClass.XMin.ToString() + ",Xmax=" + curFeaClass.XMax.ToString()
                      + ",Ymin=" + curFeaClass.YMin.ToString() + ",Ymax=" + curFeaClass.YMax.ToString()
                      + " where ID=" + curFeaClass.ID.ToString();
                MySqlCommand update = new MySqlCommand(sql, connection);
                update.ExecuteNonQuery();
                sql = "drop table " + curFeaClass.ID.ToString();
                cmd = new MySqlCommand(sql, connection);
                cmd.ExecuteNonQuery();
            }
            else
            {
                //insert
                sql = "insert into header values(" + curFeaClass.ID.ToString() + ",'"
                      + curFeaClass.Type.ToString("") + "','" + curFeaClass.Name + "',"
                      + curFeaClass.Count.ToString() + ",'" + shpPath + "',"
                      + curFeaClass.XMin.ToString() + "," + curFeaClass.XMax.ToString() + ","
                      + curFeaClass.YMin.ToString() + "," + curFeaClass.YMax.ToString() + ")";
                MySqlCommand insert = new MySqlCommand(sql, connection);
                insert.ExecuteNonQuery();
            }

            sql = "create table `" + curFeaClass.ID.ToString() + "` (ID int,FileBias int,FileLength int";
            for (int i = 2; i < curFeaClass.FieldCount; ++i)
            {
                switch (curFeaClass.GetFieldType(i).Name)
                {
                case "Int32":
                    sql += "," + curFeaClass.GetFieldName(i) + " int";
                    break;

                case "Double":
                    sql += "," + curFeaClass.GetFieldName(i) + " double";
                    break;

                default:
                    sql += "," + curFeaClass.GetFieldName(i) + " varchar(100)";
                    break;
                }
            }
            sql += ")";
            cmd  = new MySqlCommand(sql, connection);
            cmd.ExecuteNonQuery();
            using (FileStream shp = new FileStream(shpPath, FileMode.Create, FileAccess.Write))
            {
                using (BinaryWriter bw = new BinaryWriter(shp))
                {
                    int fileSize = 100;
                    bw.Write(BitConverter.GetBytes(curFeaClass.ID).Reverse().ToArray());
                    bw.Write(new byte[24]);
                    bw.Write(BitConverter.GetBytes(1000).Reverse().ToArray());
                    int[] typeCode = { 1, 3, 5, 8 };
                    bw.Write(typeCode[(int)curFeaClass.Type]);
                    bw.Write(curFeaClass.XMin);
                    bw.Write(curFeaClass.YMin);
                    bw.Write(curFeaClass.XMax);
                    bw.Write(curFeaClass.YMax);
                    bw.Write(new byte[32]);

                    for (int i = 0; i < curFeaClass.Count; ++i)
                    {
                        long bias = (int)bw.BaseStream.Position;
                        bw.Write(BitConverter.GetBytes(i).Reverse().ToArray());
                        byte[] featureBytes = curFeaClass.GetFeature(i).ToBytes();
                        bw.Write(BitConverter.GetBytes(featureBytes.Length).Reverse().ToArray());
                        bw.Write(featureBytes);
                        long length = bw.BaseStream.Position - bias;
                        sql = "insert into `" + curFeaClass.ID.ToString() + "` values("
                              + i.ToString() + "," + bias.ToString() + "," + length.ToString();
                        string temp;
                        for (int j = 2; j < curFeaClass.FieldCount; ++j)
                        {
                            temp = curFeaClass.GetAttributeCell(i, j).ToString();
                            if (curFeaClass.GetFieldType(j) == typeof(string))
                            {
                                temp = "'" + temp + "'";
                            }
                            sql += "," + temp;
                        }
                        sql += ")";
                        MySqlCommand insert = new MySqlCommand(sql, connection);
                        insert.ExecuteNonQuery();
                    }
                }
            }
            RefreshRecords();
        }
コード例 #6
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);
        }