Пример #1
0
        //Indexing
        public static async Task CreateIndex(string shpFileName)
        {
            ShxReader shxFile = new ShxReader(Shapefile.GetShxFileName(shpFileName));

            var shapes = await ReadAsync(shpFileName);

            int numberOfRecords = shxFile.NumberOfRecords;

            List <Indexing.ShpIndex> indexes = new List <Indexing.ShpIndex>(numberOfRecords);

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

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

                if (contentLength == 2)
                {
                    continue;
                }

                indexes.Add(new Indexing.ShpIndex()
                {
                    RecordNumber       = i,
                    MinimumBoundingBox = shapes[indexes.Count].MinimumBoundingBox
                });
            }

            Indexing.IndexIO.Write(GetIndexFileName(shpFileName), indexes);
        }
Пример #2
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);
            }
        }