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); //最后返回一个图层文件 }
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 SelectResult SelectPoint(GISVertex vertex, List <GISFeature> features, GISView view, GISExtent MinSelectExtent) { Double distance = Double.MaxValue; int id = -1; for (int i = 0; i < features.Count; i++) //找最近的feature判断是否有效 { if (MinSelectExtent.IntersectOrNot(features[i].spatialpart.extent) == false) { continue; } GISPoint point = (GISPoint)(features[i].spatialpart); double dist = point.Distance(vertex); if (dist < distance)//每次找到最小的距离并记录id号 { distance = dist; id = i; } } Console.WriteLine("id:" + id.ToString()); //测试id是否存在 Console.WriteLine(features[id].spatialpart.centroid.x.ToString() + "|" + features[id].spatialpart.centroid.y.ToString()); Console.WriteLine("鼠标点到元素点在地图上相距" + distance.ToString()); //此处的distance是features中最近的元素点到鼠标点映射到地图上点的距离 //精选 if (id == -1) //经过遍历 没有与minsextent相交的点则跳出 { SelectedFeature = null; return(SelectResult.TooFar); } else { double screendistance = view.ToScreenDistance(vertex, features[id].spatialpart.centroid); //Console.WriteLine(screendistance); if (screendistance < GISConst.MinScreenDistance) { SelectedFeature = features[id]; return(SelectResult.OK); } else { SelectedFeature = null; return(SelectResult.TooFar);//即使在粗选相交 精选距离也超过了限制 则返回 } } }
public SelectResult SelectLine(GISVertex vertex, List <GISFeature> features, GISView view, GISExtent MinSelectExtent) { Double distance = Double.MaxValue; int id = -1; for (int i = 0; i < features.Count; i++) //找最近的feature判断是否有效 { if (MinSelectExtent.IntersectOrNot(features[i].spatialpart.extent) == false) { continue; } GISLine line = (GISLine)(features[i].spatialpart); double dist = line.Distance(vertex); //Console.WriteLine("dist:" + dist); if (dist < distance)//每次找到最小的距离并记录id号 { distance = dist; id = i; } } Console.WriteLine("id:" + id.ToString());//测试id是否存在 //Console.WriteLine(distance); //精选 if (id == -1)//经过遍历 没有与minsextent相交的点则跳出 { SelectedFeature = null; return(SelectResult.TooFar); } else { double screendistance = view.ToScreenDistance(distance); if (screendistance < GISConst.MinScreenDistance) { SelectedFeature = features[id]; return(SelectResult.OK); } else { SelectedFeature = null; return(SelectResult.TooFar);//即使在粗选相交 精选距离也超过了限制 则返回 } } }
//读取所有的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); } }
//输出图层所有GISFeatrue static void WriteFeatures(GISLayer layer, BinaryWriter bw) { for (int featureindex = 0; featureindex < layer.FeatureCount(); featureindex++) { GISFeature feature = layer.GetFeature(featureindex); if (layer.ShapeType == SHAPETYPE.point) { ((GISPoint)feature.spatialpart).centroid.WriteVertex(bw); } else if (layer.ShapeType == SHAPETYPE.line) { GISLine line = (GISLine)(feature.spatialpart); WriteMultipleVertexes(line.Vertexes, bw); } else if (layer.ShapeType == SHAPETYPE.polygon) { GISPolygon polygon = (GISPolygon)(feature.spatialpart); WriteMultipleVertexes(polygon.Vertexes, bw); } WriteAttributes(feature.attributepart, bw); } }
public void AddFeature(GISFeature feature) { Features.Add(feature); }