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 GISLayer(string _name, SHAPETYPE _shapetype, GISExtent _extent, List <GISField> _fields) { Name = _name; ShapeType = _shapetype; Extent = _extent; Fields = _fields; }
public GISLayer(string _name, SHAPETYPE _shapetype, GISExtent _extent) { Name = _name; ShapeType = _shapetype; Extent = _extent; Fields = new List <GISField>(); }
public void Update(GISExtent _extent, Rectangle _rectangle) { CurrentMapExtent = _extent; MapWindowsSize = _rectangle; MapMinX = CurrentMapExtent.getMinX(); MapMinY = CurrentMapExtent.getMinY(); WinW = MapWindowsSize.Width; WinH = MapWindowsSize.Height; MapW = CurrentMapExtent.getWidth(); MapH = CurrentMapExtent.getHeight(); ScaleX = MapW / WinW; ScaleY = MapH / WinH; }
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 static GISLayer ReadFile(string filename) { FileStream fsr = new FileStream(filename, FileMode.Open); BinaryReader br = new BinaryReader(fsr); MyFileHeader mfh = (MyFileHeader)(GISTools.FromBytes(br, typeof(MyFileHeader))); //读取文件头 SHAPETYPE ShapeType = (SHAPETYPE)Enum.Parse(typeof(SHAPETYPE), mfh.Shapetype.ToString()); //获取空间实体类型shapetype和 GISExtent Extent = new GISExtent(mfh.MinX, mfh.MaxX, mfh.MinY, mfh.MaxY); //地图范围extent string layername = GISTools.ReadString(br); //读取图层名 List <GISField> Fields = ReadFields(br, mfh.FieldCount); //读取字段信息 GISLayer layer = new GISLayer(layername, ShapeType, Extent, Fields); ReadFeatures(layer, br, mfh.FeatureCount); 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);//即使在粗选相交 精选距离也超过了限制 则返回 } } }
public SelectResult Select(GISVertex vertex, List <GISFeature> features, SHAPETYPE shapetype, GISView view) { if (features.Count == 0) { return(SelectResult.EmptySet); } GISExtent MinSelectExtent = BuildExtent(vertex, view);//建立最小选择范围 switch (shapetype) { case SHAPETYPE.point: Console.WriteLine("point"); return(SelectPoint(vertex, features, view, MinSelectExtent)); case SHAPETYPE.line: Console.WriteLine("line"); return(SelectLine(vertex, features, view, MinSelectExtent)); case SHAPETYPE.polygon: Console.WriteLine("polygon"); return(SelectPolygon(vertex, features, view, MinSelectExtent)); } return(SelectResult.UnknownType); }
/*显然有公式 * ScreenX = (MapX-MapMinX)/ScaleX * ScreenY = WinH-(MapY-MapMinY)/ScaleY */ public GISView(GISExtent _extent, Rectangle _rectangle) { //CurrentMapExtent = _extent;//需要更新extent类使其提供高、宽信息 //MapWindowsSize = _rectangle; Update(_extent, _rectangle); }
List <GISFeature> Features = new List <GISFeature>(); //私有的 不宜改动 public GISLayer(string _name, SHAPETYPE _shapetype, GISExtent _extent) { Name = _name; ShapeType = _shapetype; Extent = _extent; }
public void UpdateExtent(GISExtent extent) { //CurrentMapExtent.CopyFrom(extent);// 为什么不直接是 CurrentMapExtent = extent CurrentMapExtent = extent; Update(CurrentMapExtent, MapWindowsSize); }
public void CopyFrom(GISExtent extent) { upright.CopyFrom(extent.upright); bottomleft.CopyFrom(extent.bottomleft); }
public void UpdateExtent(GISExtent extent) { CurrentMapExtent.CopyFrom(extent); Update(CurrentMapExtent, MapWindowsSize); }
//判断两个空间对象是否相交 排除所有不相交情况就得到相交 public bool IntersectOrNot(GISExtent extent) { return(!(getMaxX() < extent.getMinX()) || getMinX() > extent.getMaxX() || getMaxY() < extent.getMinY() || getMinY() > extent.getMaxY()); }
public SelectResult SelectPolygon(GISVertex vertex, List <GISFeature> features, GISView view, GISExtent MinSelectExtent) { return(SelectResult.TooFar); }
//public GISVertex Location; **previous code //public string Attribute; public GISPoint(GISVertex onevertex) { centroid = onevertex; extent = new GISExtent(onevertex, onevertex); }
private void GISPanel_MouseUp(object sender, MouseEventArgs e) { if (document.IsEmpty()) { return; } if (MouseOnMap == false) { return; } MouseOnMap = false; switch (MouseCommand) { case MOUSECOMMAND.Select: //如果CTRL没被按住,就清空选择集 按住CTRL键表示选择多个 即向选择集中新增空间对象 if (Control.ModifierKeys != Keys.Control) { document.ClearSelection(); } //初始化选择结果 SelectResult sr = SelectResult.UnknownType; if (e.X == MouseStartX && e.Y == MouseStartY) { //点选 GISVertex v = view.ToMapVertex(e.Location); sr = document.Select(v, view); } else { //框选 GISExtent extent = view.Rect2Extent(e.X, MouseStartX, e.Y, MouseStartY); sr = document.Select(extent); } if (sr == SelectResult.OK || Control.ModifierKeys != Keys.Control) { UpdateMap(); UpdateAttributeWindow(); } break; case MOUSECOMMAND.Zoomin: if (e.X == MouseStartX && e.Y == MouseStartY) { //单点放大 GISVertex MouseLocation = view.ToMapVertex(e.Location); GISExtent E1 = view.getRealExtent(); double newwidth = E1.getWidth() * GISConst.ZoominFactor; double newheight = E1.getHeight() * GISConst.ZoominFactor; double newminx = MouseLocation.x - (MouseLocation.x - E1.getMinX()) * GISConst.ZoominFactor; double newminy = MouseLocation.y - (MouseLocation.y - E1.getMinY()) * GISConst.ZoominFactor; view.UpdateExtent(new GISExtent(newminx, newminx + newwidth, newminy, newminy + newheight)); } else { //拉框放大 view.UpdateExtent(view.Rect2Extent(e.X, MouseStartX, e.Y, MouseStartY)); } UpdateMap(); break; case MOUSECOMMAND.Zoomout: if (e.X == MouseStartX && e.Y == MouseStartY) { //单点缩小 GISExtent e1 = view.getRealExtent(); GISVertex mouselocation = view.ToMapVertex(e.Location); double newwidth = e1.getWidth() / GISConst.ZoomoutFactor; double newheight = e1.getHeight() / GISConst.ZoomoutFactor; double newminx = mouselocation.x - (mouselocation.x - e1.getMinX()) / GISConst.ZoomoutFactor; double newminy = mouselocation.y - (mouselocation.y - e1.getMinY()) / GISConst.ZoomoutFactor; view.UpdateExtent(new GISExtent(newminx, newminx + newwidth, newminy, newminy + newheight)); } else { //拉框缩小 GISExtent e3 = view.Rect2Extent(e.X, MouseStartX, e.Y, MouseStartY); GISExtent e1 = view.getRealExtent(); double newwidth = e1.getWidth() * e1.getWidth() / e3.getWidth(); double newheight = e1.getHeight() * e1.getHeight() / e3.getHeight(); double newminx = e3.getMinX() - (e3.getMinX() - e1.getMinX()) * newwidth / e1.getWidth(); double newminy = e3.getMinY() - (e3.getMinY() - e1.getMinY()) * newheight / e1.getHeight(); view.UpdateExtent(new GISExtent(newminx, newminx + newwidth, newminy, newminy + newheight)); } UpdateMap(); break; case MOUSECOMMAND.Pan: if (e.X != MouseStartX && e.Y != MouseStartY) { GISExtent e1 = view.getRealExtent(); GISVertex m1 = view.ToMapVertex(new Point(MouseStartX, MouseStartY)); GISVertex m2 = view.ToMapVertex(e.Location); double newwidth = e1.getWidth(); double newheight = e1.getHeight(); double newminx = e1.getMinX() - (m2.x - m1.x); double newminy = e1.getMinY() - (m2.y - m1.y); view.UpdateExtent(new GISExtent(newminx, newminx + newwidth, newminy, newminy + newheight)); UpdateMap(); } break; } }