/// <summary> /// Reads the .shp and .dbf file. Both files have to be in the same folder. /// </summary> /// <param name="fileName">Full path for the shapefile.</param> public ShapeFile(string fileName) { fileHeader = new FileHeader(); fName = fileName; if (!File.Exists(fileName)) { throw new FileNotFoundException("Shapefile cannot be found. Make sure the file exists, path is correct and you have access rights to the file.", fileName); } try { var fs = new FileStream(fileName, FileMode.Open); var fileLength = fs.Length; data = new Byte[fileLength]; fs.Read(data, 0, (int)fileLength); fs.Close(); } catch (Exception) { throw; } }
private FileHeader ReadFileHeader() { var data = this.data; var fh = new FileHeader(); fh.FileCode = ReadIntBigEndian(data, 0); fh.FileLength = ReadIntBigEndian(data, 24); fh.Version = ReadIntLittleEndian(data, 28); fh.ShapeType = ReadIntLittleEndian(data, 32); fh.XMin = ReadDoubleLittleEndian(data, 36); fh.YMin = ReadDoubleLittleEndian(data, 44); fh.XMax = ReadDoubleLittleEndian(data, 52); fh.YMax = ReadDoubleLittleEndian(data, 60); # region optionalContent // Unused, with value 0.0, if not Measured or Z type fh.ZMin = ReadDoubleLittleEndian(data, 68); fh.ZMax = ReadDoubleLittleEndian(data, 76); fh.MMin = ReadDoubleLittleEndian(data, 84); fh.MMax = ReadDoubleLittleEndian(data, 92); # endregion if (fh.FileCode != FileHeader.shapefileDefaultCode) { throw new Exception("Unrecognized file format."); } return fh; }
public void Read() { fileHeader = ReadFileHeader(); // Spatial reference system parameters are stored in a file with the same name but a different (.prj) extension. var prjFile = fName.Replace(".shp", ".prj"); prjFile = prjFile.Replace(".SHP", ".PRJ"); ReadSRSInfo(prjFile); // Read geometry Shapes = new List<Shape>(); ReadShapes(); // dBase file has the same name but a different (.dbf) extension. var dbaseFile = fName.Replace(".shp", ".dbf"); dbaseFile = dbaseFile.Replace(".SHP", ".DBF"); ReadDBaseFile(dbaseFile); if (fileHeader.ProjectionInfo != null) { if (fileHeader.ProjectionInfo.Name.ToLower().Contains("stereographic")) { if (fileHeader.ProjectionInfo.Name.ToLower().Contains("south")) { Projection = Projections.PolarStereo; } else { Projection = Projections.PolarStereo; } } } }