//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); }
public static async Task <IEsriShapeCollection> Read(string shpFileName, IRI.Msh.Common.Primitives.BoundingBox boundingBox) { var indexFileName = GetIndexFileName(shpFileName); if (!CheckAllNeededFilesExists(shpFileName) || !System.IO.File.Exists(indexFileName)) { throw new NotImplementedException(); } var shpIndexes = await Indexing.IndexIO.Read(indexFileName); var filtered = shpIndexes.Where(i => i.MinimumBoundingBox.Intersects(boundingBox)).ToList(); var shxFile = new ShxReader(GetShxFileName(shpFileName)); int srid = TryGetSrid(shpFileName); using (System.IO.FileStream shpStream = new System.IO.FileStream(shpFileName, System.IO.FileMode.Open)) { using (var shpReader = new System.IO.BinaryReader(shpStream)) { switch (shxFile.MainHeader.ShapeType) { case EsriShapeType.EsriPoint: case EsriShapeType.EsriPointZ: case EsriShapeType.EsriPointM: return(ExtractPoints(shpReader, shxFile, filtered, srid)); case EsriShapeType.EsriMultiPoint: case EsriShapeType.EsriMultiPointZ: case EsriShapeType.EsriMultiPointM: return(ExtractMultiPoints(shpReader, shxFile, filtered, srid)); case EsriShapeType.EsriPolyLine: case EsriShapeType.EsriPolyLineZ: case EsriShapeType.EsriPolyLineM: return(ExtractPolyLines(shpReader, shxFile, filtered, srid)); case EsriShapeType.EsriPolygon: case EsriShapeType.EsriPolygonZ: case EsriShapeType.EsriPolygonM: return(ExtractPolygons(shpReader, shxFile, filtered, srid)); case EsriShapeType.NullShape: case EsriShapeType.EsriMultiPatch: default: throw new NotImplementedException(); } } } }
private static IShapeCollection ExtractPolygons(System.IO.BinaryReader shpReader, ShxReader shxReader, List <Indexing.ShpIndex> indexes) { List <Polygon> geometries = new List <Polygon>(indexes.Count); for (int i = 0; i < indexes.Count; i++) { int offset, contentLength; shxReader.GetRecord(indexes[i].RecordNumber, out offset, out contentLength); geometries[i] = PolygonReader.Read(shpReader, offset, contentLength); } return(new ShapeCollection <Polygon>(geometries)); }
private static IEsriShapeCollection ExtractPolyLines(System.IO.BinaryReader shpReader, ShxReader shxReader, List <Indexing.ShpIndex> indexes, int srid) { List <EsriPolyline> geometries = new List <EsriPolyline>(indexes.Count); for (int i = 0; i < indexes.Count; i++) { int offset, contentLength; shxReader.GetRecord(indexes[i].RecordNumber, out offset, out contentLength); geometries[i] = PolyLineReader.Read(shpReader, offset, contentLength, srid); } return(new EsriShapeCollection <EsriPolyline>(geometries)); }