示例#1
0
        private void ReadHeader()
        {
            //FileCode
            int fileCode = ToLocalEndian.ToInt32FromBig(_brMainFile.ReadBytes(4));

            if (fileCode != 9994)
            {
                throw new Exception("文件不是ESRI Shape Files文件头!");
            }
            //Skip 20 bytes unused (5 integer)
            _brMainFile.ReadBytes(20);
            //File length
            _fileLength = ToLocalEndian.ToInt32FromBig(_brMainFile.ReadBytes(4)) * 2;
            if (_fileLength != _fsMainFile.Length)
            {
                throw new Exception("ESRI Shape Files文件未正确结束!");
            }
            //Version
            int Version = ToLocalEndian.ToInt32FromLittle(_brMainFile.ReadBytes(4));
            //Shape Type
            int ShapeType = ToLocalEndian.ToInt32FromLittle(_brMainFile.ReadBytes(4));

            _shapeType     = (enumShapeType)ShapeType;
            _envelope.MinX = ToLocalEndian.ToDouble64FromLittle(_brMainFile.ReadBytes(8));
            _envelope.MinY = ToLocalEndian.ToDouble64FromLittle(_brMainFile.ReadBytes(8));
            _envelope.MaxX = ToLocalEndian.ToDouble64FromLittle(_brMainFile.ReadBytes(8));
            _envelope.MaxY = ToLocalEndian.ToDouble64FromLittle(_brMainFile.ReadBytes(8));
            AdjustZeroWidthEnvelope(_envelope);
            //Skip minZ,maxZ,minM,maxM
            _brMainFile.ReadBytes(8 * 4);
            _fileHeaderSize = _fsMainFile.Position;
        }
        private long GetHeaderSizeAndFileLength(FileStream fsMainFile, BinaryReader brMainFile, out int fileLength)
        {
            //FileCode
            int fileCode = ToLocalEndian.ToInt32FromBig(brMainFile.ReadBytes(4));

            if (fileCode != 9994)
            {
                throw new Exception("文件不是ESRI Shape Files文件头!");
            }
            //Skip 20 bytes unused (5 integer)
            brMainFile.ReadBytes(20);
            //File length
            fileLength = ToLocalEndian.ToInt32FromBig(brMainFile.ReadBytes(4)) * 2;
            if (fileLength != fsMainFile.Length)
            {
                throw new Exception("ESRI Shape Files文件未正确结束!");
            }
            //Version
            int Version = ToLocalEndian.ToInt32FromLittle(brMainFile.ReadBytes(4));
            //Shape Type
            int ShapeType = ToLocalEndian.ToInt32FromLittle(brMainFile.ReadBytes(4));
            //
            Envelope evp = new Envelope();

            evp.MinX = ToLocalEndian.ToDouble64FromLittle(brMainFile.ReadBytes(8));
            evp.MinY = ToLocalEndian.ToDouble64FromLittle(brMainFile.ReadBytes(8));
            evp.MaxX = ToLocalEndian.ToDouble64FromLittle(brMainFile.ReadBytes(8));
            evp.MaxY = ToLocalEndian.ToDouble64FromLittle(brMainFile.ReadBytes(8));
            //Skip minZ,maxZ,minM,maxM
            brMainFile.ReadBytes(8 * 4);
            return(fsMainFile.Position);
        }
示例#3
0
        private void WriteShapeMultiPointRecords(Feature[] vfs)
        {
            //the fist record offset in 16-bit word
            byte[]          byteArray;
            ShapeMultiPoint mulPoint;
            Int32           contentLength;

            if (_currentShxRecondPosition != 0)
            {
                _fsShxFile.Seek(_currentShxRecondPosition, SeekOrigin.Begin);
            }
            if (_currentShpRecondPosition != 0)
            {
                _fsMainFile.Seek(_currentShpRecondPosition, SeekOrigin.Begin);
            }
            for (int i = 0; i < vfs.Length; i++)
            {
                mulPoint      = vfs[i].Geometry as ShapeMultiPoint;
                contentLength = (mulPoint.Points.Length * 16 + 48) / 2;

                byteArray = BitConverter.GetBytes(_featuresCount + i + 1);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                byteArray = BitConverter.GetBytes(contentLength);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                byteArray = BitConverter.GetBytes(_offset);
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                _offset += contentLength;

                byteArray = BitConverter.GetBytes((int)_shapeType);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(mulPoint.Envelope.MinX);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(mulPoint.Envelope.MinY);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(mulPoint.Envelope.MaxX);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(mulPoint.Envelope.MaxY);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(mulPoint.Points.Length);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

                foreach (ShapePoint sp in mulPoint.Points)
                {
                    byteArray = BitConverter.GetBytes(sp.X);
                    _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
                    byteArray = BitConverter.GetBytes(sp.Y);
                    _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
                }
            }
            _currentShpRecondPosition = _fsMainFile.Position;
            _currentShxRecondPosition = _fsShxFile.Position;
        }
示例#4
0
        private void WriteShapePointRecords(Feature[] vfs)
        {
            //the fist record offset in 16-bit word
            Int32 Offset = 50;

            byte[]     byteArray;
            ShapePoint point;


            for (int i = 0; i < vfs.Length; i++)
            {
                point = vfs[i].Geometry as ShapePoint;

                byteArray = BitConverter.GetBytes(i + 1);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                byteArray = BitConverter.GetBytes(14);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                ////
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                byteArray = BitConverter.GetBytes(Offset);
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                Offset += 14;
                ////
                byteArray = BitConverter.GetBytes((int)_shapeType);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(point.X);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(point.Y);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
            }
        }
        private float ComputeSizeAndCount(string mainFile, string shxFile, out int featureCount)
        {
            featureCount = 0;
            int          pointCount = 0;
            FileStream   fsMainFile = null, fsShxFile = null;
            BinaryReader brMainFile = null, brShxFile = null;

            try
            {
                fsMainFile = new FileStream(mainFile, FileMode.Open, FileAccess.Read);
                brMainFile = new BinaryReader(fsMainFile);
                fsShxFile  = new FileStream(shxFile, FileMode.Open, FileAccess.Read);
                brShxFile  = new BinaryReader(fsShxFile);
                //
                int  fileLength = 0;
                long headerSize = GetHeaderSizeAndFileLength(fsMainFile, brMainFile, out fileLength);
                //
                featureCount = 0;
                fsMainFile.Seek(headerSize, SeekOrigin.Begin);
                while (fsMainFile.Position < fileLength)
                {
                    //Record header
                    int    oid          = ToLocalEndian.ToInt32FromBig(brMainFile.ReadBytes(4));
                    int    contentSize  = ToLocalEndian.ToInt32FromBig(brMainFile.ReadBytes(4));//16bit 字为单位
                    byte[] contentBytes = brMainFile.ReadBytes(contentSize * 2);
                    pointCount += GetPointCount(contentBytes, oid, fsMainFile);
                    //
                    featureCount++;
                }
                //
                float memorySize = pointCount * 8f * 2f; //x double, y double
                memorySize += featureCount * 100;        //100 bytes per feature
                memorySize += 1024;                      //1024 bytes  per feature layer
                return(memorySize);
            }
            finally
            {
                if (brMainFile != null)
                {
                    brMainFile.Dispose();
                }
                if (fsMainFile != null)
                {
                    fsMainFile.Dispose();
                }
                if (fsShxFile != null)
                {
                    fsShxFile.Dispose();
                }
                if (brShxFile != null)
                {
                    brShxFile.Dispose();
                }
            }
        }
示例#6
0
        private void WriteShapeMultiPointRecords(Feature[] vfs)
        {
            //the fist record offset in 16-bit word
            Int32 Offset = 50;

            byte[]          byteArray;
            ShapeMultiPoint mulPoint;
            Int32           contentLength;

            for (int i = 0; i < vfs.Length; i++)
            {
                mulPoint      = vfs[i].Geometry as ShapeMultiPoint;
                contentLength = (mulPoint.Points.Length * 16 + 48) / 2;

                byteArray = BitConverter.GetBytes(i + 1);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                byteArray = BitConverter.GetBytes(contentLength);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                ////
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                byteArray = BitConverter.GetBytes(Offset);
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                Offset += contentLength;
                ////

                byteArray = BitConverter.GetBytes((int)_shapeType);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(mulPoint.Envelope.MinX);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(mulPoint.Envelope.MinY);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(mulPoint.Envelope.MaxX);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(mulPoint.Envelope.MaxY);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(mulPoint.Points.Length);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

                foreach (ShapePoint sp in mulPoint.Points)
                {
                    byteArray = BitConverter.GetBytes(sp.X);
                    _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                    byteArray = BitConverter.GetBytes(sp.Y);
                    _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
                }
            }
        }
示例#7
0
        public void ReadRecords(IProgressTracker tracker)
        {
            lock (_fsMainFile)
            {
                if (_isLoaded)
                {
                    return;
                }
                try
                {
                    _dbfReader    = new DbaseReader(_dbffile);
                    _featureCount = 0;
                    _features     = new List <Feature>();
                    while (_fsMainFile.Position < _fileLength)
                    {
                        //Record header
                        int oid         = ToLocalEndian.ToInt32FromBig(_brMainFile.ReadBytes(4));
                        int contentSize = ToLocalEndian.ToInt32FromBig(_brMainFile.ReadBytes(4));//16bit 字为单位
                        //if (contentSize < 0)
                        //    Console.WriteLine("");
                        byte[] contentBytes = _brMainFile.ReadBytes(contentSize * 2);
                        object obj          = BytesToFeature(contentBytes, oid);
                        _features.Add(obj as Feature);
                        //
                        _featureCount++;
                    }

                    if (_argOfLeveling != null && _argOfLeveling.Enabled && _shapeType == enumShapeType.Point)
                    {
                        //int bTime = Environment.TickCount;
                        using (LevelAdjuster set = new LevelAdjuster())
                        {
                            set.BeginLevel = _argOfLeveling.BeginLevel;
                            set.GridSize   = _argOfLeveling.GridSize;
                            set.Features   = _features.ToArray();
                            set.Do();
                        }
                        //int eTime = Environment.TickCount - bTime;
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    _isLoaded = true;
                }
            }
        }
示例#8
0
 public void FetchFeatures(Action <Feature> action)
 {
     using (_dbfReader = new DbaseReader(_dbffile))
     {
         _fsMainFile.Seek(_fileHeaderSize, SeekOrigin.Begin);
         while (_fsMainFile.Position < _fileLength)
         {
             int    oid          = ToLocalEndian.ToInt32FromBig(_brMainFile.ReadBytes(4));
             int    contentSize  = ToLocalEndian.ToInt32FromBig(_brMainFile.ReadBytes(4));//16bit 字为单位
             byte[] contentBytes = _brMainFile.ReadBytes(contentSize * 2);
             object obj          = BytesToFeature(contentBytes, oid);
             action(obj as Feature);
         }
     }
 }
示例#9
0
 private Feature ReadFirstFromFile()
 {
     using (_dbfReader = new DbaseReader(_dbffile))
     {
         _fsMainFile.Seek(_fileHeaderSize, SeekOrigin.Begin);
         while (_fsMainFile.Position < _fileLength)
         {
             int    oid          = ToLocalEndian.ToInt32FromBig(_brMainFile.ReadBytes(4));
             int    contentSize  = ToLocalEndian.ToInt32FromBig(_brMainFile.ReadBytes(4));//16bit 字为单位
             byte[] contentBytes = _brMainFile.ReadBytes(contentSize * 2);
             object obj          = BytesToFeature(contentBytes, oid);
             return(obj as Feature);
         }
         return(null);
     }
 }
示例#10
0
 public Feature[] FetchFeatures()
 {
     using (_dbfReader = new DbaseReader(_dbffile))
     {
         _fsMainFile.Seek(_fileHeaderSize, SeekOrigin.Begin);
         List <Feature> retFeatures = new List <Feature>();
         while (_fsMainFile.Position < _fileLength)
         {
             int    oid          = ToLocalEndian.ToInt32FromBig(_brMainFile.ReadBytes(4));
             int    contentSize  = ToLocalEndian.ToInt32FromBig(_brMainFile.ReadBytes(4));//16bit 字为单位
             byte[] contentBytes = _brMainFile.ReadBytes(contentSize * 2);
             object obj          = BytesToFeature(contentBytes, oid);
             retFeatures.Add(obj as Feature);
         }
         return(retFeatures.Count > 0 ? retFeatures.ToArray() : null);
     }
 }
示例#11
0
        private void WriteShapePointRecords(Feature[] vfs)
        {
            //the fist record offset in 16-bit word
            byte[]     byteArray;
            ShapePoint point;

            if (_currentShxRecondPosition != 0)
            {
                _fsShxFile.Seek(_currentShxRecondPosition, SeekOrigin.Begin);
            }
            if (_currentShpRecondPosition != 0)
            {
                _fsMainFile.Seek(_currentShpRecondPosition, SeekOrigin.Begin);
            }
            for (int i = 0; i < vfs.Length; i++)
            {
                point = vfs[i].Geometry as ShapePoint;

                byteArray = BitConverter.GetBytes(_featuresCount + i + 1);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                byteArray = BitConverter.GetBytes(10);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                byteArray = BitConverter.GetBytes(_offset);
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                _offset += 14;

                byteArray = BitConverter.GetBytes((int)_shapeType);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(point.X);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(point.Y);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
            }
            _currentShpRecondPosition = _fsMainFile.Position;
            _currentShxRecondPosition = _fsShxFile.Position;
        }
示例#12
0
        private void WriteFileHeader(Feature[] features)
        {
            if (_isFirstWrite)
            {
                byte[]   byteArray;
                Envelope env = features[0].Geometry.Envelope;
                for (int i = 1; i < features.Length; i++)
                {
                    env.UnionWith(features[i].Geometry.Envelope);
                }
                _currentEnvelope = env;

                byteArray = BitConverter.GetBytes(9994);
                int byteArrayValue = ToLocalEndian.ToInt32FromBig(byteArray);
                _bwMainFile.Write(byteArrayValue);
                _bwShxFile.Write(byteArrayValue);

                byteArray = new byte[20];
                _bwMainFile.Write(byteArray);
                _bwShxFile.Write(byteArray);
                //
                byteArray = BitConverter.GetBytes(CalculateMainfileLengthInByte(features) / 2);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                byteArray = BitConverter.GetBytes(CalculateShxfileLenthInByte(features) / 2);
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                //
                _bwMainFile.Write(1000);
                _bwShxFile.Write(1000);

                _bwMainFile.Write((int)_shapeType);
                _bwShxFile.Write((int)_shapeType);

                byteArray = BitConverter.GetBytes(env.MinX);
                _bwMainFile.Write(byteArray);
                _bwShxFile.Write(byteArray);

                byteArray = BitConverter.GetBytes(env.MinY);
                _bwMainFile.Write(byteArray);
                _bwShxFile.Write(byteArray);

                byteArray = BitConverter.GetBytes(env.MaxX);
                _bwMainFile.Write(byteArray);
                _bwShxFile.Write(byteArray);

                byteArray = BitConverter.GetBytes(env.MaxY);
                _bwMainFile.Write(byteArray);
                _bwShxFile.Write(byteArray);

                byteArray = new byte[32];
                _bwMainFile.Write(byteArray);
                _bwShxFile.Write(byteArray);
                _isFirstWrite = false;
            }
            else
            {
                byte[]   byteArray;
                Envelope env = features[0].Geometry.Envelope;
                for (int i = 1; i < features.Length; i++)
                {
                    env.UnionWith(features[i].Geometry.Envelope);
                }
                if (_currentEnvelope != null)
                {
                    env.UnionWith(_currentEnvelope);
                }
                _currentEnvelope = env;
                _fsMainFile.Seek(24, SeekOrigin.Begin);
                _fsShxFile.Seek(24, SeekOrigin.Begin);

                byteArray = BitConverter.GetBytes(CalculateMainfileLengthInByte(features) / 2);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                byteArray = BitConverter.GetBytes(CalculateShxfileLenthInByte(features) / 2);
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                ////
                _fsMainFile.Seek(8, SeekOrigin.Current);
                _fsShxFile.Seek(8, SeekOrigin.Current);

                byteArray = BitConverter.GetBytes(env.MinX);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
                _bwShxFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(env.MinY);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
                _bwShxFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(env.MaxX);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
                _bwShxFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(env.MaxY);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
                _bwShxFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
            }
        }
示例#13
0
        private void WriteShapePolygonRecords(Feature[] vfs)
        {
            //the fist record offset in 16-bit word
            byte[]       byteArray;
            ShapePolygon polygon;
            Int32        contentLength;
            Int32        pointsNum = 0;
            Int32        index     = 0;

            if (_currentShxRecondPosition != 0)
            {
                _fsShxFile.Seek(_currentShxRecondPosition, SeekOrigin.Begin);
            }
            if (_currentShpRecondPosition != 0)
            {
                _fsMainFile.Seek(_currentShpRecondPosition, SeekOrigin.Begin);
            }
            for (int i = 0; i < vfs.Length; i++)
            {
                polygon = vfs[i].Geometry as ShapePolygon;

                pointsNum     = 0;
                index         = 0;
                contentLength = (44 + polygon.Rings.Length * 4) / 2;
                foreach (ShapeRing part in polygon.Rings)
                {
                    contentLength += part.Points.Length * 8;
                    pointsNum     += part.Points.Length;
                }
                byteArray = BitConverter.GetBytes(_featuresCount + i + 1);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                byteArray = BitConverter.GetBytes(_offset);
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                _offset += contentLength + 4;

                byteArray = BitConverter.GetBytes(contentLength);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                byteArray = BitConverter.GetBytes((int)_shapeType);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(polygon.Envelope.MinX);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(polygon.Envelope.MinY);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(polygon.Envelope.MaxX);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(polygon.Envelope.MaxY);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(polygon.Rings.Length);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(pointsNum);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

                foreach (ShapeRing sr in polygon.Rings)
                {
                    byteArray = BitConverter.GetBytes(index);
                    _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));
                    index += sr.Points.Length;
                }
                foreach (ShapeRing sr in polygon.Rings)
                {
                    foreach (ShapePoint sp in sr.Points)
                    {
                        byteArray = BitConverter.GetBytes(sp.X);
                        _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                        byteArray = BitConverter.GetBytes(sp.Y);
                        _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
                    }
                }
            }
            _currentShpRecondPosition = _fsMainFile.Position;
            _currentShxRecondPosition = _fsShxFile.Position;
        }
示例#14
0
        private void WriteShapePolylineRecords(Feature[] vfs)
        {
            byte[]        byteArray;
            ShapePolyline polyLine;
            Int32         contentLength;

            if (_currentShxRecondPosition != 0)
            {
                _fsShxFile.Seek(_currentShxRecondPosition, SeekOrigin.Begin);
            }
            if (_currentShpRecondPosition != 0)
            {
                _fsMainFile.Seek(_currentShpRecondPosition, SeekOrigin.Begin);
            }
            for (int i = 0; i < vfs.Length; i++)
            {
                polyLine      = vfs[i].Geometry as ShapePolyline;
                contentLength = (44 + polyLine.Parts.Length * 4) / 2;
                int pointsNum = 0;
                int index     = 0;
                foreach (ShapeLineString part in polyLine.Parts)
                {
                    contentLength += part.Points.Length * 8;
                    pointsNum     += part.Points.Length;
                }
                byteArray = BitConverter.GetBytes(_featuresCount + i + 1);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                byteArray = BitConverter.GetBytes(_offset);
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                _offset += contentLength + 4;

                byteArray = BitConverter.GetBytes(contentLength);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                _bwMainFile.Write((int)_shapeType);

                _bwMainFile.Write(polyLine.Envelope.MinX);

                _bwMainFile.Write(polyLine.Envelope.MinY);

                _bwMainFile.Write(polyLine.Envelope.MaxX);

                _bwMainFile.Write(polyLine.Envelope.MaxY);

                _bwMainFile.Write(polyLine.Parts.Length);

                _bwMainFile.Write(pointsNum);

                foreach (ShapeLineString part in polyLine.Parts)
                {
                    _bwMainFile.Write(index);
                    index += part.Points.Length;
                }
                foreach (ShapeLineString part in polyLine.Parts)
                {
                    foreach (ShapePoint sp in part.Points)
                    {
                        _bwMainFile.Write(sp.X);
                        _bwMainFile.Write(sp.Y);
                    }
                }
            }
            _currentShpRecondPosition = _fsMainFile.Position;
            _currentShxRecondPosition = _fsShxFile.Position;
        }
示例#15
0
        private void WriteVectorDataToFiles(Feature[] features)
        {
            byte[] byteArray;

            Envelope env = new Envelope(double.MaxValue, double.MaxValue, double.MinValue, double.MinValue);

            foreach (Feature vf in features)
            {
                env.UnionWith(vf.Geometry.Envelope);
            }

            ////write main file
            //write main file and shx file header
            byteArray = BitConverter.GetBytes(9994);
            _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
            _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

            byteArray = new byte[20];
            _bwMainFile.Write(byteArray);
            _bwShxFile.Write(byteArray);

            ////
            byteArray = BitConverter.GetBytes(CalculateMainfileLengthInByte(features) / 2);
            _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
            byteArray = BitConverter.GetBytes(CalculateShxfileLenthInByte(features) / 2);
            _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
            ////

            byteArray = BitConverter.GetBytes(1000);
            _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));
            _bwShxFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

            byteArray = BitConverter.GetBytes((int)_shapeType);
            _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));
            _bwShxFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));


            byteArray = BitConverter.GetBytes(env.MinX);
            _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
            _bwShxFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

            byteArray = BitConverter.GetBytes(env.MinY);
            _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
            _bwShxFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

            byteArray = BitConverter.GetBytes(env.MaxX);
            _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
            _bwShxFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

            byteArray = BitConverter.GetBytes(env.MaxY);
            _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
            _bwShxFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

            byteArray = new byte[32];
            _bwMainFile.Write(byteArray);
            _bwShxFile.Write(byteArray);

            //write main file and Shx file records

            switch (_shapeType)
            {
            case enumShapeType.Point:
                WriteShapePointRecords(features);
                break;

            case enumShapeType.MultiPoint:
                WriteShapeMultiPointRecords(features);
                break;

            case enumShapeType.Polyline:
                WriteShapePolylineRecords(features);
                break;

            case enumShapeType.Polygon:
                WriteShapePolygonRecords(features);
                break;

            default:
                break;
            }


            ////write prj file,leave it now,no spec

            CloseVectorDataFiles();
        }
示例#16
0
        private void WriteShapePolygonRecords(Feature[] vfs)
        {
            //the fist record offset in 16-bit word
            Int32 Offset = 50;

            byte[]       byteArray;
            ShapePolygon polygon;
            Int32        contentLength;
            Int32        pointsNum = 0;
            Int32        index     = 0;

            for (int i = 0; i < vfs.Length; i++)
            {
                polygon = vfs[i].Geometry as ShapePolygon;

                pointsNum     = 0;
                index         = 0;
                contentLength = (44 + polygon.Rings.Length * 4) / 2;
                foreach (ShapeRing part in polygon.Rings)
                {
                    contentLength += part.Points.Length * 8;
                    pointsNum     += part.Points.Length;
                }



                byteArray = BitConverter.GetBytes(i + 1);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                ////
                byteArray = BitConverter.GetBytes(Offset);
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));
                Offset += contentLength + 4;
                ////


                byteArray = BitConverter.GetBytes(contentLength);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                ////
                _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray));

                ////

                byteArray = BitConverter.GetBytes((int)_shapeType);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(polygon.Envelope.MinX);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(polygon.Envelope.MinY);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(polygon.Envelope.MaxX);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(polygon.Envelope.MaxY);
                _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(polygon.Rings.Length);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

                byteArray = BitConverter.GetBytes(pointsNum);
                _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));

                foreach (ShapeRing sr in polygon.Rings)
                {
                    byteArray = BitConverter.GetBytes(index);
                    _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray));
                    index += sr.Points.Length;
                }
                foreach (ShapeRing sr in polygon.Rings)
                {
                    foreach (ShapePoint sp in sr.Points)
                    {
                        byteArray = BitConverter.GetBytes(sp.X);
                        _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));

                        byteArray = BitConverter.GetBytes(sp.Y);
                        _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray));
                    }
                }
            }
        }