public static GISLayer ReadShapefile(string shpfilename) { FileStream fsr = new FileStream(shpfilename, FileMode.Open); //打开shp文件 BinaryReader br = new BinaryReader(fsr); //获取文件流后用二进制读取工具 ShapefileHeader sfh = ReadFileHeader(br); //调用之前的函数 获取头文件 SHAPETYPE ShapeType = (SHAPETYPE)Enum.Parse( //类型整数变对应的枚举值 typeof(SHAPETYPE), sfh.ShapeType.ToString()); GISExtent extent = new GISExtent(sfh.Xmax, sfh.Xmin, sfh.Ymax, sfh.Ymin); string dbffilename = shpfilename.Replace(".shp", ".dbf"); //更改后缀 DataTable table = ReadDBF(dbffilename); GISLayer layer = new GISLayer(shpfilename, ShapeType, extent, ReadFields(table)); //gislayer的构造参数分别是名字 图层类型 范围 *GISField的泛型 int rowindex = 0; //当前读取的记录位置 while (br.PeekChar() != -1) { RecordHeader rh = ReadRecordHeader(br); int RecordLength = FromBigToLittle(rh.RecordLength) * 2 - 4; byte[] RecordContent = br.ReadBytes(RecordLength);//将记录内容读入字节数组 if (ShapeType == SHAPETYPE.point) { GISPoint onepoint = ReadPoint(RecordContent); GISFeature onefeature = new GISFeature(onepoint, ReadAttribute(table, rowindex)); layer.AddFeature(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.AddFeature(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.AddFeature(onefeature); } } rowindex++; } br.Close(); fsr.Close(); //归还文件权限于操作系统 return(layer); //最后返回一个图层文件 }
public GISLayer ReadShapefile(string shpfilename) { FileStream fsr = new FileStream(shpfilename, FileMode.Open); //打开shp文件 BinaryReader br = new BinaryReader(fsr); //获取文件流后用二进制读取工具 ShapefileHeader sfh = ReadFileHeader(br); //调用之前的函数 获取头文件 SHAPETYPE ShapeType = (SHAPETYPE)Enum.Parse( //类型整数变对应的枚举值 typeof(SHAPETYPE), sfh.ShapeType.ToString()); GISExtent extent = new GISExtent(sfh.Xmax, sfh.Xmin, sfh.Ymax, sfh.Ymin); GISLayer layer = new GISLayer(shpfilename, ShapeType, extent); //gislayer的构造参数分别是名字 图层类型 范围 while (br.PeekChar() != -1) { RecordHeader rh = ReadRecordHeader(br); int RecordLength = FromBigToLittle(rh.RecordLength) * 2 - 4; byte[] RecordContent = br.ReadBytes(RecordLength);//将记录内容读入字节数组 if (ShapeType == SHAPETYPE.point) { GISPoint onepoint = ReadPoint(RecordContent); GISFeature onefeature = new GISFeature(onepoint, new GISAttribute()); layer.AddFeature(onefeature); } } br.Close(); fsr.Close(); //归还文件权限于操作系统 return(layer); //最后返回一个图层文件 }
//读取所有的gisfeature的空间信息和属性值 static void ReadFeatures(GISLayer layer, BinaryReader br, int FeatureCount) { for (int featureindex = 0; featureindex < FeatureCount; featureindex++) { GISFeature feature = new GISFeature(null, null); if (layer.ShapeType == SHAPETYPE.point) { feature.spatialpart = new GISPoint(new GISVertex(br)); } else if (layer.ShapeType == SHAPETYPE.line) { feature.spatialpart = new GISLine(ReadMultipleVertexes(br)); } else if (layer.ShapeType == SHAPETYPE.polygon) { feature.spatialpart = new GISPolygon(ReadMultipleVertexes(br)); } feature.attributepart = ReadAttributes(layer.Fields, br); layer.AddFeature(feature); } }