/// <summary> /// 读取shx索引信息 /// </summary> /// <param name="stream"></param> /// <returns></returns> private static List <ShxRecord> GetAllIndexRecords(Stream stream) { List <ShxRecord> records = new List <ShxRecord>(); try { stream.Position = 100; byte[] streamBuffer; do { streamBuffer = ShpUtil.ReadBytesFromStream(stream, 8); if (streamBuffer.Length < 8) { break; } else { ShxRecord myRecord = new ShxRecord(streamBuffer); records.Add(myRecord); } } while (streamBuffer.Length > 0); } catch { records.Clear(); } return(records); }
/// <summary> /// 从shapefile生成FeatureSet /// </summary> /// <param name="shpFilePath"></param> /// <returns></returns> public static FeatureSet CreateFeatureSet(string shpFilePath) { if (!ShpUtil.VerificationShp(shpFilePath, out var subFiles)) { throw new IOException("该shapefile文件不存在或者主文件缺失!"); } //else using var dbfReader = new DbfReader(subFiles.Item2); var shxIndexs = new ShxReader().ReadShx(subFiles.Item1); var recordNum = shxIndexs.Count; using var shpReader = new ShpReader(shpFilePath, shxIndexs); var fs = CreateFeatureSet(shpReader, dbfReader, recordNum); if (File.Exists(subFiles.Item3)) { string prjWkt = ""; using (var sr = new StreamReader(subFiles.Item3, Encoding.UTF8)) { prjWkt = sr.ReadToEnd(); } var proj = Crs.CreateFromWkt(prjWkt); fs.Crs = proj; } return(fs); }
public ShpReader(string shpFilePath) { string shxFilePath = shpFilePath.Substring(0, shpFilePath.Length - 3) + ".shx"; _shxRecords = new ShxReader().ReadShx(shxFilePath); _readStream = new FileStream(shpFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); this.ShpHeader = ShpUtil.GetHeader(_readStream); }
/// <summary> /// 根据ShpReader和DbfReader创建FeatureSet /// </summary> /// <param name="shpReader"></param> /// <param name="dbfReader"></param> /// <param name="recordNum"></param> /// <returns></returns> private static FeatureSet CreateFeatureSet(ShpReader shpReader, DbfReader dbfReader, int recordNum) { GeometryType geometryType = (GeometryType)shpReader.ShpHeader.ShapeType; var featureType = ShpUtil .GeometryType2FeatureType(geometryType); FeatureSet fs = new FeatureSet(featureType); IFeatureList features = new FeatureList(fs); for (int i = 0; i < recordNum; i++) { var spatialBytes = shpReader.GetNextRecord(); dbfReader.GetNextRow(); IGeometry geometry; switch (geometryType) { case GeometryType.Point: geometry = BytesToGeometry.CreatePoint(spatialBytes); break; case GeometryType.MultiPoint: geometry = BytesToGeometry.CreateMultipoint(spatialBytes); break; case GeometryType.PolyLine: geometry = BytesToGeometry.CreatePolyLine(spatialBytes); break; case GeometryType.Polygon: geometry = BytesToGeometry.CreatePolygon(spatialBytes); break; default: geometry = null; break; } IFeature feature = new Feature(geometry); features.Add(feature); } fs.Features.Set(features); fs.AttrTable = dbfReader.DbfTable; var header = shpReader.ShpHeader; //fs.Envelope = new Envelope(header.XMin, header.YMin, // header.XMax, header.YMax, // header.ZMin, header.ZMax); return(fs); }
/// <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); } }
/// <summary> /// 当前feature set对应的文件头 /// </summary> /// <returns></returns> internal byte[] GetHeader() { return(_header ??= ShpUtil.BuildHeader(this)); }
public ShpReader(Stream shpFileStream, List <ShxRecord> shxRecords) { _shxRecords = shxRecords; _readStream = shpFileStream; this.ShpHeader = ShpUtil.GetHeader(_readStream); }