public XLayer ReadFile(string filename) { FileStream fsr = new FileStream(filename, FileMode.Open); BinaryReader br = new BinaryReader(fsr); //读文件头 MyFileHeader mfh = (MyFileHeader)XTools.FromBytes(br, typeof(MyFileHeader)); //读图层名称 string name = XTools.ReadString(br); //读属性字段结构 List <XField> fields = ReadFields(mfh.FieldCount, br); //定义图层 SHAPETYPE ShapeType = (SHAPETYPE)Enum.Parse(typeof(SHAPETYPE), mfh.ShapeType.ToString()); XExtent extent = new XExtent( new XVertex(mfh.MinX, mfh.MinY), new XVertex(mfh.MaxX, mfh.MaxY)); XLayer layer = new XLayer(name, ShapeType, extent, fields); //读空间对象类型 for (int i = 0; i < mfh.FeatureCount; i++) { XSpatial spatial = ReadSpatial(ShapeType, br); XAttribute attribute = ReadAttribute(br, fields); layer.AddFeature(new XFeature(spatial, attribute)); } //关闭文件并返回结果 br.Close(); fsr.Close(); return(layer); }
public XLayer ReadShapefile(string shpfilename) { //打开文件和读取工具 FileStream fsr = new FileStream(shpfilename, FileMode.Open); BinaryReader br = new BinaryReader(fsr); //读取文件头 ShapefileHeader sfh = (ShapefileHeader) XTools.FromBytes(br, typeof(ShapefileHeader)); //获得空间对象类型 SHAPETYPE ShapeType = (SHAPETYPE)Enum.Parse(typeof(SHAPETYPE), sfh.ShapeType.ToString()); //获得空间范围 XExtent extent = new XExtent(new XVertex(sfh.Xmin, sfh.Ymin), new XVertex(sfh.Xmax, sfh.Ymax)); //读dbf string dbfFileName = shpfilename.Replace(".shp", ".dbf"); List <XField> fields = new List <XField>(); List <XAttribute> attributes = new List <XAttribute>(); ReadDBFFile(dbfFileName, fields, attributes); //构建图层 XLayer layer = new XLayer(shpfilename, ShapeType, extent, fields); int index = 0; while (br.PeekChar() != -1) { //读记录头 RecordHeader rh = (RecordHeader) XTools.FromBytes(br, typeof(RecordHeader)); int RecordLength = FromBigToLittle(rh.RecordLength) * 2 - 4; byte[] RecordContent = br.ReadBytes(RecordLength); //开始读实际的空间数据 if (ShapeType == SHAPETYPE.point) { XPointSpatial point = ReadPoint(RecordContent); layer.AddFeature(new XFeature(point, attributes[index])); } else if (ShapeType == SHAPETYPE.line) { List <XLineSpatial> lines = ReadLines(RecordContent); foreach (XLineSpatial line in lines) { layer.AddFeature(new XFeature(line, new XAttribute(attributes[index]))); } } else if (ShapeType == SHAPETYPE.polygon) { List <XPolygonSpatial> polygons = ReadPolygons(RecordContent); foreach (XPolygonSpatial polygon in polygons) { layer.AddFeature(new XFeature(polygon, new XAttribute(attributes[index]))); } } index++; } //关闭读取工具和文件 br.Close(); fsr.Close(); return(layer); }
public XField(BinaryReader br) { //获得DBF字段 dbfField = (DBFField)XTools.FromBytes(br, typeof(DBFField)); //字段名 byte[] bs = new byte[] { dbfField.b1, dbfField.b2, dbfField.b3, dbfField.b4, dbfField.b5, dbfField.b6, dbfField.b7, dbfField.b8, dbfField.b9, dbfField.b10, dbfField.b11 }; name = XTools.BytesToString(bs).Trim(); //字段类型 switch (dbfField.FieldType) { case 'C': //字符型 允许输入各种字符 datatype = Type.GetType("System.String"); break; case 'D': //日期型 用于区分年、月、日的数字和一个字符,按照YYYYMMDD格式。 datatype = Type.GetType("System.String"); break; case 'N': //数值型 if (dbfField.NumberOfDecimalPlaces == 0) { datatype = Type.GetType("System.Int32"); } else { datatype = Type.GetType("System.Double"); } break; case 'F': datatype = Type.GetType("System.Double"); break; case 'B': //二进制 允许输入各种字符 datatype = Type.GetType("System.String"); break; case 'G': //General or OLE datatype = Type.GetType("System.String"); break; case 'L': //逻辑型,表示没有初始化 datatype = Type.GetType("System.String"); break; case 'M': //Memo datatype = Type.GetType("System.String"); break; default: break; } }
private static void ReadDBFFile(string dbffilename, List <XField> fields, List <XAttribute> attributes) { FileStream fsr = new FileStream(dbffilename, FileMode.Open); BinaryReader br = new BinaryReader(fsr); DBFHeader dh = (DBFHeader)XTools.FromBytes(br, typeof(DBFHeader)); int FieldCount = (dh.HeaderLength - 33) / 32; //读字段结构 fields.Clear(); for (int i = 0; i < FieldCount; i++) { fields.Add(new XField(br)); } byte END = br.ReadByte(); //1个字节作为记录项终止标识。 //读具体数值 attributes.Clear(); for (int i = 0; i < dh.RecordCount; i++) { XAttribute attribute = new XAttribute(); char tempchar = (char)br.ReadByte(); //每个记录的开始都有一个起始字节 for (int j = 0; j < FieldCount; j++) { attribute.AddValue(fields[j].DBFValueToObject(br)); } attributes.Add(attribute); } br.Close(); fsr.Close(); }