private static void WritePolygonRecord(string path, List <Shape> shapes) { using (FileStream fs = new FileStream(path, FileMode.Append)) { FileWriter fw = new FileWriter(fs); Dictionary <int, int> index = new ShxFile(path.Replace(Path.GetExtension(path), "") + ".shx").ReadContent(); int number = 0; foreach (var item in index) { fw.WriteReverseInt(item.Key, ++number); fw.WriteReverseInt(item.Key + 4, item.Value); fw.WriteInt(item.Key + 8, 3); BoundingBox box = BoundingBox.GetBoundingBox(shapes[number - 1]); fw.WriteDouble(item.Key + 12, box.XMin); fw.WriteDouble(item.Key + 20, box.XMax); fw.WriteDouble(item.Key + 28, box.YMin); fw.WriteDouble(item.Key + 36, box.YMax); fw.WriteInt(item.Key + 44, 1); fw.WriteInt(item.Key + 48, shapes[number - 1].Vertexes.Count); fw.WriteInt(item.Key + 52, 0); for (int i = 0; i < shapes[number - 1].Vertexes.Count; i++) { fw.WriteDouble(item.Key + 56 + i * 16, shapes[number - 1].Vertexes[i].X); fw.WriteDouble(item.Key + 64 + i * 16, shapes[number - 1].Vertexes[i].Y); } } } }
/// <summary> /// Initialize Shapefile class using existing .shp and .shx file. /// </summary> public Shapefile(string directory, string name) { this.FilePath = directory + "\\" + name; string shxPath = this.FilePath + ".shx"; ShxFile shxFile = new ShxFile(shxPath); Dictionary <int, int> indexes = shxFile.ReadContent(); string shpPath = this.FilePath + ".shp"; ShpFile shpFile = new ShpFile(shpPath); this.Box = shpFile.ReadBoundingBox(); this.shapes = shpFile.ReadShapes(indexes); }
/// <summary> /// Save the Shapefile as .shp and .shx files. /// </summary> /// <param name="directory"></param> /// <param name="name"></param> /// <returns></returns> public bool Save(string directory, string name) { if (string.IsNullOrEmpty(directory) || string.IsNullOrEmpty(name)) { return(false); } else { this.FilePath = directory + "\\" + name; string shxPath = this.FilePath + ".shx"; ShxFile.Save(shxPath, Box, shapes); string shpPath = this.FilePath + ".shp"; ShpFile.Save(shpPath, Box, shapes); return(true); } }