public static void WriteFile(GISLayer layer, string filename) { FileStream fsr = new FileStream(filename, FileMode.Create); BinaryWriter bw = new BinaryWriter(fsr); WriteFileHeader(layer, bw); WriteString(layer.Name, bw); WriteFields(layer.Fields, bw); WriteFeatures(layer, bw); //其它写入内容 bw.Close(); fsr.Close(); }
static void WriteFileHeader(GISLayer layer, BinaryWriter bw) { MyFileHeader mfh = new MyFileHeader(); mfh.MinX = layer.Extent.MinX; mfh.MinY = layer.Extent.MinY; mfh.MaxX = layer.Extent.MaxX; mfh.MaxY = layer.Extent.MaxY; mfh.FeatureCount = layer.Features.Count; mfh.DrawAttributeOrNot = layer.DrawAttributeOrNot ? 1 : 0; mfh.ShapeType = ShapeTypeToInt(layer.ShapeType); mfh.FieldCount = layer.Fields.Count; bw.Write(ToBytes(mfh)); }
static void WriteFeatures(GISLayer layer, BinaryWriter bw) { for (int featureindex = 0; featureindex < layer.Features.Count; featureindex++) { GISFeature feature = layer.Features[featureindex]; if (layer.ShapeType == SHAPETYPE.POINT) { WriteVertex(((GISPoint)feature.spatialpart).Location, bw); } else if (layer.ShapeType == SHAPETYPE.LINE) { GISLine line = (GISLine)(feature.spatialpart); bw.Write(line.Vertexes.Count); for (int vc = 0; vc < line.Vertexes.Count; vc++) { WriteVertex(line.Vertexes[vc], bw); } } else if (layer.ShapeType == SHAPETYPE.POLYGON) { GISPolygon polygon = (GISPolygon)(feature.spatialpart); bw.Write(polygon.Vertexes.Count); for (int vc = 0; vc < polygon.Vertexes.Count; vc++) { WriteVertex(polygon.Vertexes[vc], bw); } } for (int fieldindex = 0; fieldindex < layer.Fields.Count; fieldindex++) { GISField field = layer.Fields[fieldindex]; int tint = TypeToInt(field.datatype); if (tint == 1) { bw.Write((int)(feature.getAttributeValue(fieldindex))); } else if (tint == 2) { bw.Write(Convert.ToDouble(feature.getAttributeValue(fieldindex))); } else if (tint == 3) { WriteString(feature.getAttributeValue(fieldindex).ToString(), bw); } } } }
public static GISLayer ReadFile(string filename) { FileStream fsr = new FileStream(filename, FileMode.Open); BinaryReader br = new BinaryReader(fsr); MyFileHeader mfh = (MyFileHeader)(FromBytes(br, typeof(MyFileHeader))); SHAPETYPE ShapeType = IntToShapeType(mfh.ShapeType); GISExtent Extent = new GISExtent(new GISVertex(mfh.MinX, mfh.MinY), new GISVertex(mfh.MaxX, mfh.MaxY)); string layername = ReadString(br); List <GISField> Fields = ReadFields(br, mfh.FieldCount); GISLayer layer = new GISLayer(layername, ShapeType, Extent, Fields); layer.DrawAttributeOrNot = (mfh.DrawAttributeOrNot == 1); layer.Features = ReadFeatures(br, ShapeType, Fields, mfh.FeatureCount); br.Close(); fsr.Close(); return(layer); }
public void RemoveLayer(GISLayer layer) { layers.Remove(layer); UpdateExtent(); }
public void AddLayer(GISLayer layer) { layers.Add(layer); UpdateExtent(); }
public static GISLayer ReadShapeFile(string shpfilename) { FileStream fsr = new FileStream(shpfilename, FileMode.Open); BinaryReader br = new BinaryReader(fsr); ShapeFileHeader sfh = ReadFileHeader(br); SHAPETYPE ShapeType = SHAPETYPE.POINT; if (sfh.ShapeType == 1) { ShapeType = SHAPETYPE.POINT; } else if (sfh.ShapeType == 3) { ShapeType = SHAPETYPE.LINE; } else if (sfh.ShapeType == 5) { ShapeType = SHAPETYPE.POLYGON; } else { return(null); } GISExtent extent = new GISExtent(new GISVertex(sfh.Xmin, sfh.Ymin), new GISVertex(sfh.Xmax, sfh.Ymax)); string dbffilename = shpfilename.Replace(".shp", ".dbf"); DataTable table = ReadDBF(dbffilename); GISLayer layer = new GISLayer(shpfilename, ShapeType, extent, ReadFields(table)); int rowindex = 0; while (br.PeekChar() != -1) { RecordHeader rh = ReadRecordHeader(br); int RecordLength = FromBigToLittle(rh.RecordLength); byte[] RecordContent = br.ReadBytes(RecordLength * 2 - 4); if (ShapeType == SHAPETYPE.POINT) { GISPoint onepoint = ReadPoint(RecordContent); GISFeature onefeature = new GISFeature(onepoint, ReadAttribute(table, rowindex)); layer.Features.Add(onefeature); } if (ShapeType == SHAPETYPE.LINE) { List <GISLine> lines = ReadLines(RecordContent); for (int i = 0; i < lines.Count; i++) { GISFeature onefeature = new GISFeature(lines[i], ReadAttribute(table, rowindex)); layer.Features.Add(onefeature); } } if (ShapeType == SHAPETYPE.POLYGON) { List <GISPolygon> polygons = ReadPolygons(RecordContent); for (int i = 0; i < polygons.Count; i++) { GISFeature onefeature = new GISFeature(polygons[i], ReadAttribute(table, rowindex)); layer.Features.Add(onefeature); } } rowindex++; //其它代码,用于处理RecordContent } br.Close(); fsr.Close(); return(layer); }