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 void Update(XExtent _Extent, Rectangle _Rectangle) { //CurrentMapExtent = _Extent; //MapWindowSize = _Rectangle; //MapMinX = CurrentMapExtent.GetMinX(); //MapMinY = CurrentMapExtent.GetMinY(); //WinW = MapWindowSize.Width; //WinH = MapWindowSize.Height; //MapW = CurrentMapExtent.GetWidth(); //MapH = CurrentMapExtent.GetHeight(); //ScaleX = MapW / WinW; //ScaleY = MapH / WinH; //ScaleX = Math.Max(ScaleX, ScaleY); //ScaleY = ScaleX; CurrentMapExtent = _Extent; MapWindowSize = _Rectangle; WinW = MapWindowSize.Width; WinH = MapWindowSize.Height; ScaleX = CurrentMapExtent.GetWidth() / WinW; ScaleY = CurrentMapExtent.GetHeight() / WinH; ScaleX = Math.Max(ScaleX, ScaleY); ScaleY = ScaleX; MapW = MapWindowSize.Width * ScaleX; MapH = MapWindowSize.Height * ScaleY; XVertex center = CurrentMapExtent.GetCenter(); MapMinX = center.X - MapW / 2; MapMinY = center.Y - MapH / 2; CurrentMapExtent = new XExtent(new XVertex(MapMinX, MapMinY), new XVertex(MapMinX + MapW, MapMinY + MapH)); }
internal void SelectByExtent(XExtent extent) { foreach (XFeature feature in Features) { feature.Selected = extent.Contains(feature.Spatial.Extent); } }
internal void SelectByExtent(XExtent extent, bool IntersestOrContain, bool PlusSelect) //,bool EmptyOrNot { if (PlusSelect) { foreach (XFeature f in Features) { if (f.Selected == true) { continue; } f.Selected = IntersestOrContain ? extent.IntersectWith(f.Spatial.Extent) : extent.Contains(f.Spatial.Extent); } } else { foreach (XFeature f in Features) { f.Selected = IntersestOrContain ? extent.IntersectWith(f.Spatial.Extent) : extent.Contains(f.Spatial.Extent); } } }
internal bool Contains(XExtent extent) { return(extent.GetMinX() >= GetMinX() && extent.GetMinY() >= GetMinY() && extent.GetMaxX() <= GetMaxX() && extent.GetMaxY() <= GetMaxY()); }
public void SelectByInterExtent(XExtent extent) { foreach (XFeature feature in Features) { feature.Selected = extent.IntersectWith(feature.Spatial.Extent); } }
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 XLayer(string _Name, SHAPETYPE _ShapeType, XExtent _Extent, List <XField> _Fields) { Name = _Name; ShapeType = _ShapeType; Extent = _Extent; Fields = _Fields; }
private void FormXGIS_MouseUp(object sender, MouseEventArgs e) { //没有打开任何图层 if (layer == null) { return; } //未发生变化 if (e.Location.X == MouseDownLocation.X && e.Location.Y == MouseDownLocation.Y) { if (MouseSelect) { layer.SelectByClick(e.Location, view); UpdateMap(); } } else { XVertex v1 = view.ToMapVertex(MouseDownLocation); XVertex v2 = view.ToMapVertex(e.Location); //如果是拉框放大 if (MouseSelect) { XExtent extent = new XExtent(v1, v2); //if (Control.ModifierKeys == Keys.Control) //{ // layer.PlusSelectByExtent(extent); //} //else //{ // layer.SelectByExtent(extent, Control.ModifierKeys == Keys.Shift, // Control.ModifierKeys == Keys.Control); //} if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift && (Control.ModifierKeys & Keys.Control) == Keys.Control) { layer.SelectByExtent(extent, true, true); } else { layer.SelectByExtent(extent, Control.ModifierKeys == Keys.Shift, Control.ModifierKeys == Keys.Control); } } else if (MouseZoomIn) { XExtent extent = new XExtent(v1, v2); view.UpdateExtent(extent); } else if (MousePan) { view.OffsetCenter(v1, v2); } UpdateMap(); } MouseSelect = MouseZoomIn = MousePan = false; }
public void Update(XExtent _Extent, Rectangle _Rectangle) { CurrentMapExtent = _Extent; MapWindowSize = _Rectangle; MapMinX = CurrentMapExtent.GetMinX(); MapMinY = CurrentMapExtent.GetMinY(); WinW = MapWindowSize.Width; WinH = MapWindowSize.Height; MapW = CurrentMapExtent.GetWidth(); MapH = CurrentMapExtent.GetHeight(); ScaleX = MapW / WinW; ScaleY = MapH / WinH; }
private void FormXGIS_MouseUp(object sender, MouseEventArgs e) { //没有打开任何图层 if (layer == null) { return; } //未发生变化 if (e.Location.X == MouseDownLocation.X && e.Location.Y == MouseDownLocation.Y) { if (MouseSelect) { layer.selectByClick(e.Location, view); UpdateMap(); } } else//框选 { XVertex v1 = view.ToMapVertex(MouseDownLocation); XVertex v2 = view.ToMapVertex(e.Location); if (MouseSelect) { XExtent extent = new XExtent(v1, v2); layer.SelectByExtent(extent); } else if (MouseZoomIn) { XExtent extent = new XExtent(v1, v2); view.UpdateExtent(extent); } else if (MousePan)//如果是平移地图 { view.OffsetCenter(v1, v2); } else if (MouseInterSelect) { XExtent extent = new XExtent(v1, v2); layer.SelectByInterExtent(extent); } UpdateMap(); } MouseSelect = MousePan = MouseZoomIn = MouseInterSelect = false; }
public XView(XExtent _Extent, Rectangle _Rectangle) { Update(_Extent, _Rectangle); }
public List <XLayer> ReadJSONFile(string filename) { List <XLayer> layers = new List <XLayer>(); StreamReader sr = new StreamReader(filename, Encoding.UTF8); Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(sr.ReadToEnd()); //读入一个json文件 Newtonsoft.Json.Linq.JToken features = o["features"]; //读取所有空间对象数组 List <Newtonsoft.Json.Linq.JToken> flst = features.ToList(); List <XField> fields = new List <XField>(); /*ReadFields(mfh.FieldCount, br);*/ XLayer pointlayer = new XLayer(filename, SHAPETYPE.point, null, fields); XLayer linelayer = new XLayer(filename, SHAPETYPE.line, null, fields); XLayer polygonlayer = new XLayer(filename, SHAPETYPE.polygon, null, fields); double maxx = Double.MinValue; double minx = Double.MaxValue; double maxy = Double.MinValue; double miny = Double.MaxValue; for (int index = 0; index < flst.Count; index++) { List <XVertex> _Vertexes = new List <XVertex>(); //记录feature的节点 string featuretype = (string)(flst[index]["geometry"]["type"]); //读取对象类型 包括Point LineString Polygon Newtonsoft.Json.Linq.JToken properties = flst[index]["properties"]; //读取对象的所有字段与属性 //if (featuretype == "Polygon") flst[index]["geometry"]["coordinates"][0] if (featuretype == "Point")//如果是点对象 则按点对象的数据结构进行处理 { var location = flst[index]["geometry"]["coordinates"]; double lon = (double)location.First; double lat = (double)location.Last; Console.WriteLine(lon); if (lon > maxx) { maxx = lon; } else if (lon < minx) { minx = lon; } if (lat > maxy) { maxy = lat; } else if (lat < miny) { miny = lat; } _Vertexes.Add(new XVertex(lon, lat)); ReadJSONField(properties, pointlayer);//右边返回一个Fields泛型赋给左边图层字段 } else if (featuretype == "LineString")//如果是线对象 则按线对象的数据结构进行处理 { foreach (var location in flst[index]["geometry"]["coordinates"]) { double lon = (double)location.First; double lat = (double)location.Last; //Console.WriteLine(lon); if (lon > maxx) { maxx = lon; } else if (lon < minx) { minx = lon; } if (lat > maxy) { maxy = lat; } else if (lat < miny) { miny = lat; } _Vertexes.Add(new XVertex(lon, lat)); ReadJSONField(properties, linelayer);//右边返回一个Fields泛型赋给左边图层字段 } } else if (featuretype == "Polygon")//如果是多边形对象 则按多边形对象的数据结构进行处理 { foreach (var singlepoly in flst[index]["geometry"]["coordinates"]) { foreach (var location in singlepoly) { double lon = (double)location.First; double lat = (double)location.Last; Console.WriteLine(lon); if (lon > maxx) { maxx = lon; } else if (lon < minx) { minx = lon; } if (lat > maxy) { maxy = lat; } else if (lat < miny) { miny = lat; } _Vertexes.Add(new XVertex(lon, lat)); ReadJSONField(properties, polygonlayer);//右边返回一个Fields泛型赋给左边图层字段 } } } if (featuretype == "Point") { XFeature onefeature = new XFeature(new XPointSpatial(_Vertexes[0]), null); //暂时默认无属性 pointlayer.AddFeature(onefeature); //给线图层添加对象 } else if (featuretype == "LineString") { XFeature onefeature = new XFeature(new XLineSpatial(_Vertexes), null); //暂时默认无属性 linelayer.AddFeature(onefeature); //给线图层添加对象 } else if (featuretype == "Polygon") { XFeature onefeature = new XFeature(new XPolygonSpatial(_Vertexes), null); //暂时默认无属性 polygonlayer.AddFeature(onefeature); //给线图层添加对象 } } //Console.WriteLine(minx.ToString(), miny.ToString(), maxx.ToString(), maxy.ToString()); XExtent layersextent = new XExtent(new XVertex(minx, miny), new XVertex(maxx, maxy)); layers.Add(polygonlayer); layers.Add(linelayer); layers.Add(pointlayer); foreach (XLayer ll in layers) { ll.Extent = layersextent;//给三种layer赋上共同的extent ll.DrawAttributeOrNot = false; } sr.Close(); return(layers); }
internal bool IntersectWith(XExtent _Extent) { return(!(GetMaxX() < _Extent.GetMinX() || GetMinX() > _Extent.GetMaxX() || GetMaxY() < _Extent.GetMinY() || GetMinY() > _Extent.GetMaxY())); }
internal void UpdateExtent(XExtent extent) { CurrentMapExtent.CopyFrom(extent); Update(CurrentMapExtent, MapWindowSize); }
internal void CopyFrom(XExtent extent) { UpRight.CopyFrom(extent.UpRight); BottomLeft.CopyFrom(extent.BottomLeft); }
public XPointSpatial(XVertex _Location) { Centroid = _Location; Extent = new XExtent(_Location, _Location); }