Exemplo n.º 1
0
        public static void MergeManyShpFile(string[] filenames, string TargetFile)
        {
            BinaryReader binaryReader1 = null;
            FileStream   fileStream1   = null;

            fileStream1   = new FileStream(filenames[0], FileMode.Open, FileAccess.Read, FileShare.None);
            binaryReader1 = new BinaryReader(fileStream1);
            ShpHeader newShpHeader1 = new ShpHeader();//定义header

            newShpHeader1 = ShpFunction.ReadShpHeader(binaryReader1);
            ShpFileBody newShpBody1 = new ShpFileBody();

            FileStream   fileStream2   = null;
            BinaryWriter binaryWriter1 = null;

            // 首先判断,文件是否已经存在
            if (File.Exists(TargetFile))
            {
                File.Delete(TargetFile);// 如果文件已经存在,那么删除掉.
            }
            fileStream2   = new FileStream(TargetFile, FileMode.Create, FileAccess.Write, FileShare.None);
            binaryWriter1 = new BinaryWriter(fileStream2);
            //大端格式写入fileCode;
            ShpFunction.WriteShpHeader(binaryWriter1, newShpHeader1);
            //先将第一个文件的shpheader写入新文件中
            while (binaryReader1.BaseStream.Position < binaryReader1.BaseStream.Length)
            {
                newShpBody1 = ShpFunction.ReadShpFileBody(binaryReader1);
                ShpFunction.WriteShpBody(binaryWriter1, newShpBody1);
            }
            int k = newShpBody1.recordNo;//记录第一个文件记录的编号
            //读入并写入第一个文件

            BinaryReader binaryReader = null;
            FileStream   fileStream   = null;
            ShpHeader    newShpHeader = new ShpHeader();//定义header
            ShpFileBody  newShpBody   = new ShpFileBody();

            for (int i = 1; i < filenames.Length; i++)
            {
                fileStream    = new FileStream(filenames[i], FileMode.Open, FileAccess.Read, FileShare.None);
                binaryReader  = new BinaryReader(fileStream);
                newShpHeader  = ShpFunction.ReadShpHeader(binaryReader);
                newShpHeader1 = ShpFunction.MergeShpHeader(newShpHeader, newShpHeader1);
                //合并文件头
                while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
                {
                    newShpBody          = ShpFunction.ReadShpFileBody(binaryReader);
                    newShpBody.recordNo = ++k;//通过循环将记录不断增加1
                    ShpFunction.WriteShpBody(binaryWriter1, newShpBody);
                }
            }//读取文件头和文件记录,将文件头合并,将文件记录读入
            fileStream2.Position = 0;
            ShpFunction.WriteShpHeader(binaryWriter1, newShpHeader1);
            //后面的操作是将指针回到顶点,然后将合并后的shp头写入到新文件中
        }
Exemplo n.º 2
0
 public static void WriteShpHeader(BinaryWriter binaryWriter, ShpHeader shpHeader)
 {
     ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.fileCode);
     ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.unUse1);
     ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.unUse2);
     ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.unUse3);
     ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.unUse4);
     ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.unUse5);
     ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.fileLength);
     binaryWriter.Write(shpHeader.fileVersion);
     binaryWriter.Write(shpHeader.shapeType);
     ShpFunction.WriteBoundingBox(binaryWriter, shpHeader.boundingBox);
     //写header逻辑
 }
Exemplo n.º 3
0
        public static ShpHeader ReadShpHeader(BinaryReader binaryReader)
        {
            ShpHeader shpHeader = new ShpHeader();

            shpHeader.fileCode    = ShpFunction.ReadInt32WithBigEnduim(binaryReader);
            shpHeader.unUse1      = ShpFunction.ReadInt32WithBigEnduim(binaryReader);
            shpHeader.unUse2      = ShpFunction.ReadInt32WithBigEnduim(binaryReader);
            shpHeader.unUse3      = ShpFunction.ReadInt32WithBigEnduim(binaryReader);
            shpHeader.unUse4      = ShpFunction.ReadInt32WithBigEnduim(binaryReader);
            shpHeader.unUse5      = ShpFunction.ReadInt32WithBigEnduim(binaryReader);
            shpHeader.fileLength  = ShpFunction.ReadInt32WithBigEnduim(binaryReader);
            shpHeader.fileVersion = binaryReader.ReadInt32();
            shpHeader.shapeType   = binaryReader.ReadInt32();
            shpHeader.boundingBox = ShpFunction.ReadBoundingBox(binaryReader);
            //读取逻辑
            return(shpHeader);
        }
Exemplo n.º 4
0
        public static ShpHeader MergeShpHeader(ShpHeader shpHeader1, ShpHeader ShpHeader2)
        {
            ShpHeader newShpHeader = new ShpHeader();

            newShpHeader.fileCode    = shpHeader1.fileCode;
            newShpHeader.unUse1      = shpHeader1.unUse1;
            newShpHeader.unUse2      = shpHeader1.unUse2;
            newShpHeader.unUse3      = shpHeader1.unUse3;
            newShpHeader.unUse4      = shpHeader1.unUse4;
            newShpHeader.unUse5      = shpHeader1.unUse5;
            newShpHeader.fileLength  = shpHeader1.fileLength + ShpHeader2.fileLength - 50;
            newShpHeader.fileVersion = shpHeader1.fileVersion;
            newShpHeader.shapeType   = shpHeader1.shapeType;
            newShpHeader.boundingBox = ShpFunction.MergeBoundingBox(shpHeader1.boundingBox, ShpHeader2.boundingBox);
            //合并逻辑
            return(newShpHeader);
        }
Exemplo n.º 5
0
        public static string ShowString(string TextFileName)
        {
            BinaryReader binaryReader = null;
            FileStream   fileStream   = null;
            string       allLine      = null;

            try
            {
                fileStream   = new FileStream(TextFileName, FileMode.Open, FileAccess.Read, FileShare.None);
                binaryReader = new BinaryReader(fileStream);
                ShpHeader newShpHeader = new ShpHeader();//定义header
                newShpHeader = ShpFunction.ReadShpHeader(binaryReader);
                ShpFileBody newShpBody = new ShpFileBody();
                allLine = allLine + ShpFunction.ShpHeader_to_String(newShpHeader);
                //大端格式读出记录编号,长度
                while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
                {
                    newShpBody = ShpFunction.ReadShpFileBody(binaryReader);
                    allLine    = allLine + ShpFunction.ShpBody_to_String(newShpBody);
                }
                ShpFunction.CloseReader(binaryReader, fileStream);
            }

            catch (Exception ex)
            {
                Console.WriteLine("在读取文件的过程中,发生了异常!");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                if (fileStream != null)
                {
                    try
                    {
                        fileStream.Close();
                    }
                    catch
                    {
                        // 最后关闭文件,无视关闭是否会发生错误了.
                    }
                }
            }
            return(allLine);
        }
Exemplo n.º 6
0
        //string
        public static string ShpHeader_to_String(ShpHeader shpHeader)
        {
            string allLine = "";

            allLine = allLine + "文件的filecode:" + shpHeader.fileCode + "\r\n";
            allLine = allLine + "unUse1:" + shpHeader.unUse1 + "\r\n";
            allLine = allLine + "unUse2:" + shpHeader.unUse2 + "\r\n";
            allLine = allLine + "unUse3:" + shpHeader.unUse3 + "\r\n";
            allLine = allLine + "unUse4:" + shpHeader.unUse4 + "\r\n";
            allLine = allLine + "unUse5:" + shpHeader.unUse5 + "\r\n";
            allLine = allLine + "fileLength:" + shpHeader.fileLength + "字节\r\n";
            allLine = allLine + "fileVersion:" + shpHeader.fileVersion + "\r\n";
            allLine = allLine + "shapeType:" + shpHeader.shapeType + "\r\n";
            allLine = allLine + "xMin:" + shpHeader.boundingBox.xMin + "\r\n";
            allLine = allLine + "yMin:" + shpHeader.boundingBox.yMin + "\r\n";
            allLine = allLine + "xMax:" + shpHeader.boundingBox.xMax + "\r\n";
            allLine = allLine + "yMax:" + shpHeader.boundingBox.yMax + "\r\n";
            allLine = allLine + "zMin:" + shpHeader.boundingBox.zMin + "\r\n";
            allLine = allLine + "zMax:" + shpHeader.boundingBox.zMax + "\r\n";
            allLine = allLine + "mMin:" + shpHeader.boundingBox.mMin + "\r\n";
            allLine = allLine + "mMax:" + shpHeader.boundingBox.mMax + "\r\n";
            return(allLine);
        }
Exemplo n.º 7
0
        public static void MergeShp(String TextFileName, String TextFileName1, String TextFileName2)
        {
            BinaryReader binaryReader  = null;
            BinaryReader binaryReader1 = null;
            FileStream   fileStream    = null;
            FileStream   fileStream1   = null;

            try
            {
                fileStream   = new FileStream(TextFileName, FileMode.Open, FileAccess.Read, FileShare.None);
                binaryReader = new BinaryReader(fileStream);
                ShpHeader newShpHeader = new ShpHeader();//定义header
                newShpHeader = ShpFunction.ReadShpHeader(binaryReader);
                ShpFileBody newShpBody = new ShpFileBody();

                fileStream1   = new FileStream(TextFileName1, FileMode.Open, FileAccess.Read, FileShare.None);
                binaryReader1 = new BinaryReader(fileStream1);
                ShpHeader newShpHeader1 = new ShpHeader();//定义header
                newShpHeader1 = ShpFunction.ReadShpHeader(binaryReader1);
                ShpFileBody newShpBody1 = new ShpFileBody();
                //写入新文件

                FileStream   fileStream2  = null;
                BinaryWriter binaryWriter = null;
                // 首先判断,文件是否已经存在
                if (File.Exists(TextFileName2))
                {
                    File.Delete(TextFileName2);// 如果文件已经存在,那么删除掉.
                }
                fileStream2  = new FileStream(TextFileName2, FileMode.Create, FileAccess.Write, FileShare.None);
                binaryWriter = new BinaryWriter(fileStream2);
                //大端格式写入fileCode;
                newShpHeader = ShpFunction.MergeShpHeader(newShpHeader, newShpHeader1);
                //合并文件头
                ShpFunction.WriteShpHeader(binaryWriter, newShpHeader);
                while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
                {
                    newShpBody = ShpFunction.ReadShpFileBody(binaryReader);
                    ShpFunction.WriteShpBody(binaryWriter, newShpBody);
                }
                while (binaryReader1.BaseStream.Position < binaryReader1.BaseStream.Length)
                {
                    newShpBody1          = ShpFunction.ReadShpFileBody(binaryReader1);
                    newShpBody1.recordNo = newShpBody1.recordNo + newShpBody.recordNo;
                    ShpFunction.WriteShpBody(binaryWriter, newShpBody1);
                }
                //大端格式读出记录编号,长度
                ShpFunction.CloseReader(binaryReader, fileStream);
                ShpFunction.CloseReader(binaryReader1, fileStream1);
                ShpFunction.CloseWriter(binaryWriter, fileStream2);
            }
            catch (Exception ex)
            {
                Console.WriteLine("在读取文件的过程中,发生了异常!");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                if (fileStream != null)
                {
                    try
                    {
                        fileStream.Close();
                    }
                    catch
                    {
                        // 最后关闭文件,无视关闭是否会发生错误了.
                    }
                }
            }
            //合并逻辑
        }