/// <summary> /// 查询数据,将查询到的结果放到新建的 temp 类中 /// </summary> /// <param name="className">要查询的类的名字</param> /// <param name="search">查询语句</param> /// <param name="tempClass">要创建的临时类</param> /// <returns></returns> public static bool SearchData(string className, string search, string tempClass) { //定义变量 IVectorCls VectorCls = new SFeatureCls(); //打开简单要素类 bool rtn = VectorCls.Open("GDBP://MapGisLocal/Templates/sfcls/" + className); if (!rtn) { PUMessageBox.ShowDialog("简单要素类 " + className + " 打开失败", "失败"); return(false); } QueryDef def = new QueryDef(); //设置属性查询条件 def.Filter = search; //查询要素 RecordSet recordSet = VectorCls.Select(def); if (recordSet != null) { int num = recordSet.Count; } else { return(false); } Server svr = new Server(); //连接数据源 svr.Connect("MapGISLocal", "", ""); DataBase GDB = svr.OpenGDB("templates"); SFeatureCls tmpSFCls = new SFeatureCls(GDB); int id = tmpSFCls.Create(tempClass, GeomType.Pnt, 0, 0, null); if (id == 0) { bool temp = SFeatureCls.Remove(GDB, tempClass); id = tmpSFCls.Create(tempClass, GeomType.Pnt, 0, 0, null); if (id == 0) { PUMessageBox.ShowDialog("无法操作简单要素类,请检查是否有其他进程正在使用 " + tempClass, "失败"); return(false); } } rtn = tmpSFCls.CopySet(recordSet); if (rtn == false) { tmpSFCls.Close(); SFeatureCls.Remove(GDB, id); } //关闭类 VectorCls.Close(); return(true); }
/// <summary> /// 获取一天的骑行量 /// </summary> /// <returns></returns> public static double[] GetOneDayData(string className) { //0:00 1:00 ... 23:00 double[] results = new double[24] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //定义变量 IVectorCls VectorCls = new SFeatureCls(); //打开简单要素类 bool rtn = VectorCls.Open("GDBP://MapGisLocal/Templates/sfcls/" + className); if (!rtn) { PUMessageBox.ShowDialog("简单要素类 " + className + " 打开失败", "失败"); return(null); } QueryDef def = new QueryDef(); RecordSet recordSet = null; for (int i = 0; i < 23; i++) { //设置属性查询条件 def.Filter = "ParkTime > '" + i + ":00:00' AND ParkTime < '" + (i + 1).ToString() + ":00:00'"; //查询要素 recordSet = VectorCls.Select(def); if (recordSet != null) { results[i] = recordSet.Count; } } def.Filter = "ParkTime > '23:00:00'"; //查询要素 recordSet = VectorCls.Select(def); if (recordSet != null) { results[23] = recordSet.Count; } //关闭类 VectorCls.Close(); return(results); }
void test() { //设置转换源数据url,67数据为区简单要素类 string SrcUrl = @"D:\武汉地质调查中心---矿产资源潜力评价项目\广西壮族自治区\铝土矿种潜力评价图库\DZ_成矿地质背景\CJGZ_预测工作区沉积建造构造图\JWMAP_经纬坐标图件\MDZCJGZDFSPX_扶绥-凭祥预测工作区沉积建造构造图\LDLYAAB002.WT"; //目的数据的名称 string DesSFname = "convertsfcls"; SFeatureCls decsfc = null; Server Svr = null; DataBase GDB = null; Svr = new Server(); Svr.Connect("MapGISLocal", "", ""); GDB = Svr.OpenGDB("test"); decsfc = new SFeatureCls(GDB); //创建区简单要素类目的数据 decsfc.Create(DesSFname, GeomType.Pnt, 0, 0, null); //设置转换类型 DataConvert dataConvert = new DataConvert(); dataConvert.SetOption(ConvertOptionType.OPT_6TO7, 0); //打开源数据和目的数据 if (dataConvert.OpenSource(SrcUrl) > 0 && dataConvert.OpenDestination(decsfc) > 0) { //转换数据 bool rtn = dataConvert.Convert() > 0; if (rtn) { MessageBox.Show("数据迁移成功"); } else { MessageBox.Show("数据转换失败"); } dataConvert.Close(); decsfc.Close(); } }
/// <summary> /// 获取类的所有字段显示到列表中 /// </summary> /// <param name="className"></param> private void ShowFlds(string className) { AttrListView.Items.Clear(); FldsNames = new List <string>(); //打开选中的简单要素类 Server svr = new Server(); svr.Connect("MapGISLocal", "", ""); DataBase GDB = svr.OpenGDB("Templates"); SFeatureCls SFCls = new SFeatureCls(GDB); SFCls.Open(className, 0); Fields Flds = null; //获取属性结构 Field Fld = null; //获取属性字段信息 Fld = new Field(); Flds = new Fields(); //直接取它的属性结构 Flds = SFCls.Fields; if (Flds == null) { return; } //可以查看属性结构对象中的字段数目 int cou = Flds.Count; //获取属性字段 int i = 0; while (i < cou) { Fld = Flds.GetItem(i); AttrListView.Items.Add(Fld.FieldName + " (" + GetFieldTypeText(Fld.FieldType) + ")"); FldsNames.Add(Fld.FieldName); i++; } SFCls.Close(); }
/// <summary> /// 获取热点数据 /// </summary> /// <param name="className"></param> /// <returns></returns> public static double[] GetGeoHeatMapData(string className) { double[] results = new double[81]; for (int i = 0; i < 81; i++) { results[i] = 0; } //定义变量 IVectorCls VectorCls = new SFeatureCls(); //打开简单要素类 bool rtn = VectorCls.Open("GDBP://MapGisLocal/Templates/sfcls/" + className); if (!rtn) { PUMessageBox.ShowDialog("简单要素类 " + className + " 打开失败", "失败"); return(null); } QueryDef def = new QueryDef(); RecordSet recordSet = null; int index = 0; double xStart = 113.750; double yStart = 31.000; for (int y = 0; y < 9; y++) { for (int x = 0; x < 9; x++) { def.Filter = "yAsis > " + (xStart + 0.125 * x).ToString() + " AND yAsis < " + (xStart + 0.125 * x + 0.125).ToString() + " AND xAsis < " + (yStart - 0.125 * y).ToString() + " AND xAsis > " + (yStart - 0.125 * y - 0.125) + ""; recordSet = VectorCls.Select(def); if (recordSet != null) { results[index++] = recordSet.Count; } } } //关闭类 VectorCls.Close(); return(results); }
/// <summary> /// 获取简单要素类列表 /// </summary> /// <param name="url">要打开的地理数据库的url</param> /// <returns></returns> public static List <string> GetXClasses(string url) { List <string> names = new List <string>(); Server Svr = new Server(); Svr.Connect("MapGISLocal", "", ""); DataBase GDB = Svr.OpenGDB("Templates"); int count; List <int> dsIDs = null; //要素数据集ID列表 List <int> sfIDs = null; //要素数据集中矢量类的ID列表 List <int> dbSFIDs = null; //数据库中矢量类的ID列表 List <string> allnames = new List <string>(); //先查找要素数据集中的矢量类信息 if (true) { dsIDs = GDB.GetXclses(XClsType.Fds, 0); if (dsIDs != null) { count = dsIDs.Count; while (count > 0) { sfIDs = GDB.GetXclses(XClsType.SFCls, dsIDs[count - 1]); if (sfIDs == null) { break; } int n = sfIDs.Count; while (n > 0) { string ClsName = GDB.GetXclsName(XClsType.SFCls, sfIDs[n - 1]); string FdsName = GDB.GetXclsName(XClsType.Fds, dsIDs[count - 1]); allnames.Add(sfIDs[n - 1].ToString() + ". " + ClsName); allnames.Add(dsIDs[count - 1].ToString() + ". " + FdsName); n--; } count--; } } } //再查找数据库中的矢量类,如果查询的是要素数据集,则只需要执行下面语句就可以了 dbSFIDs = GDB.GetXclses(XClsType.SFCls, 0); if (dbSFIDs != null) { count = dbSFIDs.Count; while (count > 0) { string SFName = GDB.GetXclsName(XClsType.SFCls, dbSFIDs[count - 1]); allnames.Add(dbSFIDs[count - 1].ToString() + " " + SFName); //如果是简单要素类,则添加其几何类型 if (true) { SFeatureCls sfcls = new SFeatureCls(GDB); sfcls.Open(SFName, 0); sfcls.Close(); } count--; } } return(allnames); }
/// <summary> /// 创建简单要素类 /// </summary> /// <param name="layerName">要素类名</param> /// <param name="geomType">几何类型,枚举</param> /// <param name="gdbName">打开的地理数据库名</param> public static bool CreateXClass(string layerName, GeomType geomType, string gdbName, string[] fields) { //方法:指定类型、数据集ID,空间参考系,创建类 Server svr = new Server(); SFeatureCls sfcls = null; //连接数据源,打开数据库 bool rtn = svr.Connect("MapGISLocal", "", ""); if (rtn == true) { DataBase gdb = svr.OpenGDB(gdbName); //打开简单要素类 sfcls = gdb.GetXClass(XClsType.SFCls) as SFeatureCls; //创建区类型的简单要素类 int id = sfcls.Create(layerName, geomType, 0, 0, null); if (id <= 0) { //关闭类、数据库、断开数据源 sfcls.Close(); gdb.Close(); svr.DisConnect(); return(false); } sfcls.Open(layerName, 0); Fields temp = sfcls.Fields; if (temp == null) { temp = new Fields(); } //Field adding = null; #region 添加属性 //1: successful 0: failed int result = temp.AppendField(new Field { FieldName = "DataID", FieldLength = 255, FieldType = FieldType.FldString, Editable = 1, MskLength = 15 }); result = temp.AppendField(new Field { FieldName = "BikeID", FieldLength = 255, FieldType = FieldType.FldString, Editable = 1, MskLength = 15 }); result = temp.AppendField(new Field { FieldName = "ParkTime", FieldType = FieldType.FldTime, Editable = 1, FieldLength = 10, MskLength = 10 }); result = temp.AppendField(new Field { FieldName = "Date", FieldType = FieldType.FldTime, Editable = 1, FieldLength = 10, MskLength = 10 }); result = temp.AppendField(new Field { FieldName = "xAsis", FieldLength = 255, FieldType = FieldType.FldDouble, Editable = 1, MskLength = 15 }); result = temp.AppendField(new Field { FieldName = "yAsis", FieldLength = 255, FieldType = FieldType.FldDouble, Editable = 1, MskLength = 15 }); #endregion //foreach (var item in fields) //{ // adding = new Field // { // FieldName = item, // FieldLength = 255, // FieldType = FieldType.FldString, // Editable = 1, // MskLength = 15 // }; // int result = temp.AppendField(adding); //1: successful 0: failed //} sfcls.Fields = temp; //关闭类、数据库、断开数据源 sfcls.Close(); gdb.Close(); svr.DisConnect(); if (id > 0) { return(true); } else { return(false); } } else { return(false); } }
/// <summary> /// 矩形查询 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PUButton_Click_19(object sender, RoutedEventArgs e) { if (!(Regex.IsMatch(XMaxTextBox.Text, "^[\\d.]+$") && Regex.IsMatch(XMinTextBox.Text, "^[\\d.]+$") && Regex.IsMatch(YMaxTextBox.Text, "^[\\d.]+$") && Regex.IsMatch(YMinTextBox.Text, "^[\\d.]+$"))) { PUMessageBox.ShowDialog("输入的内容不符合规范"); return; } string sql = "xAsis < " + XMaxTextBox.Text + " AND xAsis > " + XMinTextBox.Text + " AND yAsis < " + YMaxTextBox.Text + " AND yAsis > " + YMinTextBox.Text; bool searchResult = DataHelper.SearchData("BikePnts", sql, "RectangleSearchResult"); if (searchResult == true) { Fields Flds = null; Field Fld = null; long ID = 0; Record Rcd = null; //变量初始化 Rcd = new Record(); Flds = new Fields(); Server svr = new Server(); //连接数据源 svr.Connect("MapGISLocal", "", ""); DataBase GDB = svr.OpenGDB("Templates"); SFeatureCls SFCls = new SFeatureCls(GDB); SFCls.Open("RectangleSearchResult", 0); //获取属性结构 Flds = SFCls.Fields; if (Flds == null) { SFCls.Close(); SearchTabControl.Visibility = Visibility.Collapsed; SearchResultGrid.Visibility = Visibility.Collapsed; NoResultGrid.Visibility = Visibility.Visible; return; } int num = Flds.Count; //目的类对象的个数 int objnum = SFCls.Count; //获取所有对象的ID,思想是根据对象的个数进行循环,若OID不存在,则OID自加继续循环直到循环objnum次 int n = 0; ID = 1; string[] listItem = new string[8]; SearchListView.Items.Clear(); while (n < objnum) { //取得ID=ID.Int的简单要素的属性 Rcd = SFCls.GetAtt(ID); //取得属性结构对象中的字段数目 if (Rcd != null) { Flds = Rcd.Fields; listItem[0] = ID.ToString(); //获取对应属性字段的值 for (int i = 0; i < num; i++) { object val = null; Fld = Flds.GetItem(i); string name = Fld.FieldName; val = Rcd.get_FldVal(name); listItem[i + 1] = ((val != null) ? val.ToString() : ""); } n++; SearchListView.Items.Add(new { BikeID = listItem[3], Time = listItem[4].Substring(9), XAsis = listItem[6].Substring(0, 8), YAsis = listItem[7].Substring(0, 8) }); } ID++; } SFCls.Close(); SearchTabControl.Visibility = Visibility.Collapsed; SearchResultGrid.Visibility = Visibility.Visible; NoResultGrid.Visibility = Visibility.Collapsed; } else { SearchTabControl.Visibility = Visibility.Collapsed; SearchResultGrid.Visibility = Visibility.Collapsed; NoResultGrid.Visibility = Visibility.Visible; } }
/// <summary> /// 获取单车的距离和时间统计 /// </summary> /// <param name="bikes"></param> /// <returns></returns> public static List <BikeSearchViewModel> GetLengthNTimeData(string className, string bikeid) { List <BikeSearchViewModel> searchResult = new List <BikeSearchViewModel>(); bool haveResult = DataHelper.SearchData("BikePnts", "BikeID = '" + bikeid + "'", bikeid + "Temp"); if (haveResult == false) { return(null); } Fields Flds = null; Field Fld = null; long ID = 0; Record Rcd = null; //变量初始化 Rcd = new Record(); Flds = new Fields(); Server svr = new Server(); //连接数据源 svr.Connect("MapGISLocal", "", ""); DataBase GDB = svr.OpenGDB("Templates"); SFeatureCls tmpSFCls = new SFeatureCls(GDB); tmpSFCls.Open(bikeid + "Temp", 0); //获取属性结构 Flds = tmpSFCls.Fields; if (Flds == null) { tmpSFCls.Close(); return(null); } int num = Flds.Count; //目的类对象的个数 int objnum = tmpSFCls.Count; //获取所有对象的ID,思想是根据对象的个数进行循环,若OID不存在,则OID自加继续循环直到循环objnum次 int n = 0; ID = 1; string[] listItem = new string[8]; while (n < objnum) { //取得ID=ID.Int的简单要素的属性 Rcd = tmpSFCls.GetAtt(ID); //取得属性结构对象中的字段数目 if (Rcd != null) { Flds = Rcd.Fields; listItem[0] = ID.ToString(); //获取对应属性字段的值 for (int j = 0; j < num; j++) { object val = null; Fld = Flds.GetItem(j); string name = Fld.FieldName; val = Rcd.get_FldVal(name); listItem[j + 1] = ((val != null) ? val.ToString() : ""); } n++; BikeSearchViewModel bike = new BikeSearchViewModel { BikeID = listItem[3], Time = Convert.ToDateTime(listItem[4].Substring(9)), XAsis = Convert.ToDouble(listItem[6].Substring(0, 8)), YAsis = Convert.ToDouble(listItem[7].Substring(0, 8)) }; searchResult.Add(bike); } ID++; } tmpSFCls.Close(); return(searchResult); }
/// <summary> /// 搜索 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click_1(object sender, RoutedEventArgs e) { SearchListView.Visibility = Visibility.Collapsed; RideChart.Visibility = Visibility.Collapsed; OnedayChart.Visibility = Visibility.Collapsed; BackgroundStackPanel.Visibility = Visibility.Collapsed; NoResultStackPanel.Visibility = Visibility.Collapsed; SearchListView.Items.Clear(); bool searchResult = DataHelper.SearchData("BikePnts", "BikeID = '" + SearchTextBox.Text + "'", "SearchTemp"); if (searchResult == true) { Fields Flds = null; Field Fld = null; long ID = 0; Record Rcd = null; //变量初始化 Rcd = new Record(); Flds = new Fields(); Server svr = new Server(); //连接数据源 svr.Connect("MapGISLocal", "", ""); DataBase GDB = svr.OpenGDB("Templates"); SFeatureCls SFCls = new SFeatureCls(GDB); SFCls.Open("SearchTemp", 0); //获取属性结构 Flds = SFCls.Fields; if (Flds == null) { SFCls.Close(); OnedayChart.Visibility = Visibility.Collapsed; RideChart.Visibility = Visibility.Collapsed; SearchListView.Visibility = Visibility.Collapsed; BackgroundStackPanel.Visibility = Visibility.Collapsed; NoResultStackPanel.Visibility = Visibility.Visible; return; } int num = Flds.Count; //目的类对象的个数 int objnum = SFCls.Count; //获取所有对象的ID,思想是根据对象的个数进行循环,若OID不存在,则OID自加继续循环直到循环objnum次 int n = 0; ID = 1; string[] listItem = new string[8]; while (n < objnum) { //取得ID=ID.Int的简单要素的属性 Rcd = SFCls.GetAtt(ID); //取得属性结构对象中的字段数目 if (Rcd != null) { Flds = Rcd.Fields; listItem[0] = ID.ToString(); //获取对应属性字段的值 for (int i = 0; i < num; i++) { object val = null; Fld = Flds.GetItem(i); string name = Fld.FieldName; val = Rcd.get_FldVal(name); listItem[i + 1] = ((val != null) ? val.ToString() : ""); } n++; SearchListView.Items.Add(new { OID = listItem[0], BikeID = listItem[3], Time = listItem[4].Substring(9), XAsis = listItem[6].Substring(0, 8), YAsis = listItem[7].Substring(0, 8) }); } ID++; } SFCls.Close(); SearchListView.Visibility = Visibility.Visible; } else { OnedayChart.Visibility = Visibility.Collapsed; RideChart.Visibility = Visibility.Collapsed; SearchListView.Visibility = Visibility.Collapsed; BackgroundStackPanel.Visibility = Visibility.Collapsed; NoResultStackPanel.Visibility = Visibility.Visible; } }
/// <summary> /// 导入数据后显示地图 /// </summary> private void ShowMaps() { MapGrid.Visibility = Visibility.Visible; BackgroundGrid.Visibility = Visibility.Collapsed; MapGIS.UI.Controls.MapWorkSpaceTree _Tree = new MapGIS.UI.Controls.MapWorkSpaceTree(); //地图文档 Document doc = _Tree.Document; if (doc.Close(false)) { OpenFileDialog mapxDialog = new OpenFileDialog(); mapxDialog.Filter = ".mapx(地图文档)|*.mapx|.map(地图文档)|*.map|.mbag(地图包)|*.mbag"; if (mapxDialog.ShowDialog() != DialogResult.OK) { return; } string mapUrl = mapxDialog.FileName; //打开地图文档 doc.Open(mapUrl); } Maps maps = doc.GetMaps(); if (maps.Count > 0) { //获取当前第一个地图 Map map = maps.GetMap(0); //设置地图的第一个图层为激活状态 map.get_Layer(0).State = LayerState.Active; this.WuhanMapControl.ActiveMap = map; this.WuhanMapControl.Restore(); } else { return; } disp = WuhanMapControl.Display; userDrawGeoInfo = disp.GetUserDrawGeoInfo(); pntInfo = userDrawGeoInfo.GetPntInfo(); EditGeomInfoForm pntInfoForm = new EditGeomInfoForm((VectorLayer)null, pntInfo); if (pntInfoForm.ShowDialog() == DialogResult.OK) { } pntInfoForm.Dispose(); //画点 Fields Flds = null; Field Fld = null; long ID = 0; Record Rcd = null; //变量初始化 Rcd = new Record(); Flds = new Fields(); Server svr = new Server(); //连接数据源 svr.Connect("MapGISLocal", "", ""); DataBase GDB = svr.OpenGDB("Templates"); SFeatureCls SFCls = new SFeatureCls(GDB); SFCls.Open("BikePnts", 0); //获取属性结构 Flds = SFCls.Fields; if (Flds == null) { SFCls.Close(); return; } int num = Flds.Count; //目的类对象的个数 int objnum = SFCls.Count; //获取所有对象的ID,思想是根据对象的个数进行循环,若OID不存在,则OID自加继续循环直到循环objnum次 int n = 0; ID = 1; string[] listItem = new string[8]; while (n < objnum) { //取得ID=ID.Int的简单要素的属性 Rcd = SFCls.GetAtt(ID); //取得属性结构对象中的字段数目 if (Rcd != null) { Flds = Rcd.Fields; listItem[0] = ID.ToString(); //获取对应属性字段的值 for (int i = 0; i < num; i++) { object val = null; Fld = Flds.GetItem(i); string name = Fld.FieldName; val = Rcd.get_FldVal(name); listItem[i + 1] = ((val != null) ? val.ToString() : ""); } n++; Dot dot = new Dot(); dot.X = Convert.ToDouble(listItem[6]); dot.Y = Convert.ToDouble(listItem[7]); DrawPoint(dot); } ID++; } SFCls.Close(); }