Example #1
0
        /// <summary>
        /// 将FeatureSet保存为shapefile
        /// </summary>
        /// <param name="iFeatureSet"></param>
        /// <param name="shpFilePath"></param>
        public static void SaveFeatureSet(IFeatureSet iFeatureSet,
                                          string shpFilePath)
        {
            var dir = Path.GetDirectoryName(shpFilePath);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir ?? throw new InvalidOperationException(nameof(shpFilePath)));
            }

            ShpUtil.DeleteShp(shpFilePath);
            var(shxFilePath, dbfFilePath, prjFilePath) = ShpUtil.GetSubFileName(shpFilePath);

            using var dbfWriter = new DbfWriter(iFeatureSet.AttrTable,
                                                dbfFilePath, DbfEncodingUtil.DefaultEncoding);
            dbfWriter.Write();

            var featureSet = (FeatureSet)iFeatureSet;
            var header     = featureSet.GetHeader();

            using ShpWriter shpWriter = new ShpWriter(shpFilePath);
            using ShxWriter shxWriter = new ShxWriter(shxFilePath);
            shpWriter.WriterHeader(header);
            shxWriter.WriterHeader(header);

            int offset = 100;

            foreach (var feature in featureSet.Features)
            {
                var contentByteLength = shpWriter.WriterRecord(feature);
                shxWriter.WriterRecord(offset, contentByteLength);

                offset += contentByteLength;
            }

            shpWriter.WriteFileLength();;
            shxWriter.WriteFileLength();
            if (featureSet.Crs != null)
            {
                using var fsStream = new FileStream(prjFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
                using var sw       = new StreamWriter(fsStream);
                sw.Write(featureSet.Crs.Wkt);
            }
        }
Example #2
0
        public static ShapeFileBytes GetShapeFileStreams(IFeatureSet iFeatureSet)
        {
            using var dbfWriter = new DbfWriter(iFeatureSet.AttrTable,
                                                "", DbfEncodingUtil.DefaultEncoding);
            dbfWriter.Write();

            var featureSet = (FeatureSet)iFeatureSet;
            var header     = featureSet.GetHeader();

            using ShpWriter shpWriter = new ShpWriter();
            using ShxWriter shxWriter = new ShxWriter();
            shpWriter.WriterHeader(header);
            shxWriter.WriterHeader(header);

            int offset = 100;

            foreach (var feature in featureSet.Features)
            {
                var contentByteLength = shpWriter.WriterRecord(feature);
                shxWriter.WriterRecord(offset, contentByteLength);

                offset += contentByteLength;
            }

            shpWriter.WriteFileLength();
            shxWriter.WriteFileLength();

            var fileBytes = new ShapeFileBytes
            {
                DbfBytes = ((MemoryStream)dbfWriter.Stream).ToArray(),
                ShpBytes = ((MemoryStream)shpWriter.Stream).ToArray(),
                ShxBytes = ((MemoryStream)shxWriter.Stream).ToArray()
            };

            if (featureSet.Crs != null)
            {
                var bytes = Encoding.UTF8.GetBytes(featureSet.Crs.Wkt);
                fileBytes.PrjBytes = bytes;
            }
            return(fileBytes);
        }