Exemple #1
0
        public static void Save(string shpFileName, IEnumerable <IEsriShape> shapes, bool createDbf = false, bool overwrite = false, SrsBase srs = null)
        {
            if (shapes.IsNullOrEmpty())
            {
                return;
            }

            var directory = System.IO.Path.GetDirectoryName(shpFileName);

            if (!System.IO.Directory.Exists(directory) && !string.IsNullOrEmpty(directory))
            {
                System.IO.Directory.CreateDirectory(directory);
            }

            IEsriShapeCollection collection = new EsriShapeCollection <IEsriShape>(shapes);

            EsriShapeType shapeType = shapes.First().Type;

            using (System.IO.MemoryStream featureWriter = new System.IO.MemoryStream())
            {
                int recordNumber = 0;

                foreach (IEsriShape item in shapes)
                {
                    featureWriter.Write(ShpWriter.WriteHeaderToByte(++recordNumber, item), 0, 2 * ShapeConstants.IntegerSize);

                    featureWriter.Write(item.WriteContentsToByte(), 0, 2 * item.ContentLength);
                }

                using (System.IO.MemoryStream shpWriter = new System.IO.MemoryStream())
                {
                    int fileLength = (int)featureWriter.Length / 2 + 50;

                    shpWriter.Write(ShpWriter.WriteMainHeader(collection, fileLength, shapeType), 0, 100);

                    shpWriter.Write(featureWriter.ToArray(), 0, (int)featureWriter.Length);

                    //var mode = overwrite ? System.IO.FileMode.Create : System.IO.FileMode.CreateNew;
                    var mode = Shapefile.GetMode(shpFileName, overwrite);

                    System.IO.FileStream stream = new System.IO.FileStream(shpFileName, mode);

                    shpWriter.WriteTo(stream);

                    stream.Close();

                    shpWriter.Close();

                    featureWriter.Close();
                }
            }

            ShxWriter.Write(Shapefile.GetShxFileName(shpFileName), collection, shapeType, overwrite);

            if (createDbf)
            {
                Dbf.DbfFile.WriteDefault(Shapefile.GetDbfFileName(shpFileName), collection.Count, overwrite);
            }

            //try to automatically find srs
            if (srs == null)
            {
                var srid = shapes.First()?.Srid ?? 0;

                srs = SridHelper.AsSrsBase(srid);
            }

            if (srs != null)
            {
                SaveAsPrj(shpFileName, srs, overwrite);
            }
        }
Exemple #2
0
        protected ShpReader(string fileName, EsriShapeType type, int srid)
        {
            this._srid = srid;
            //this.directoryName = System.IO.Path.GetDirectoryName(fileName);

            //this.fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fileName);

            if (!Shapefile.CheckAllNeededFilesExists(fileName))
            {
                throw new NotImplementedException();
            }

            ShxReader shxFile = new ShxReader(Shapefile.GetShxFileName(fileName));

            using (System.IO.FileStream shpStream = new System.IO.FileStream(fileName, System.IO.FileMode.Open))
            {
                using (this.shpReader = new System.IO.BinaryReader(shpStream))
                {
                    this.MainHeader = new MainFileHeader(this.shpReader.ReadBytes(ShapeConstants.MainHeaderLengthInBytes));

                    if ((EsriShapeType)this.MainHeader.ShapeType != type)
                    {
                        throw new NotImplementedException();
                    }

                    if (!MainFileHeader.CanBeForTheSameShapefile(this.MainHeader, shxFile.MainHeader))
                    {
                        throw new NotImplementedException();
                    }

                    this.elements = new EsriShapeCollection <T>(this.MainHeader);

                    int numberOfRecords = shxFile.NumberOfRecords;

                    for (int i = 0; i < numberOfRecords; i++)
                    {
                        int offset, contentLength02;

                        shxFile.GetRecord(i, out offset, out contentLength02);

                        shpReader.BaseStream.Position = offset * 2;

                        int recordNumber, contentLength;

                        ReadRecordHeader(out recordNumber, out contentLength);

                        if (contentLength != contentLength02)
                        {
                            System.Diagnostics.Trace.WriteLine("ERROR at contentLength != contentLength02: i=" + i.ToString());
                            //throw new NotImplementedException();
                        }

                        if (contentLength == 2)
                        {
                            continue;
                        }

                        elements.Add(ReadElement());

                        //4:                length of the record header in words
                        //contentLength:    length of the contents in words
                        //this.tempPosition = this.tempPosition - (ShapeConstants.RecordHeaderLengthInWords + contentLength);
                    }
                }
            }
        }