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); }
internal void SelectByClick(Point location, XView view) { XVertex v = view.ToMapVertex(location); foreach (XFeature feature in Features) { feature.Selected = false; } if (ShapeType == SHAPETYPE.point) { Double distance = Double.MaxValue; int id = -1; for (int i = 0; i < Features.Count; i++) { XPointSpatial point = (XPointSpatial)(Features[i].Spatial); double dist = point.Distance(v); if (dist < distance) { distance = dist; id = i; } } double scd = view.ToScreenDistance(v, Features[id].Spatial.Centroid);//转为屏幕距离 if (scd <= 5) { Features[id].Selected = true; } else { Console.WriteLine("屏幕距离为" + scd + ",选择无效!!"); } } else if (ShapeType == SHAPETYPE.line) { Double distance = Double.MaxValue; int id = -1; for (int i = 0; i < Features.Count; i++) { XLineSpatial line = (XLineSpatial)(Features[i].Spatial); double dist = line.Distance(v); if (dist < distance) { distance = dist; id = i; } } double scd = view.ToScreenDistance(distance);//转为屏幕距离 if (scd <= 5) { Features[id].Selected = true; } else { Console.WriteLine("屏幕距离为" + scd + ",选择无效!!"); } } else if (ShapeType == SHAPETYPE.polygon) { for (int i = 0; i < Features.Count; i++) { XPolygonSpatial polygon = (XPolygonSpatial)(Features[i].Spatial); if (polygon.Incude(v)) { Features[i].Selected = true; } } } }