[VC++代码片断] 从一个Layer取数据,然后在另一个Layer根据数据生成范围专题图。
1void CMapXSampleMDIView::DisplayTheme()
2{
3 COptionalVariant optVt; //无用变量,用作填充函数输入
4 try
5 {
6 //1 设置专题绑定数据集的来源层
7 CMapXLayer lyCap = m_ctrlMapX.GetLayers().Item("US Capitals"); //州首府层
8 COleVariant lyCapVt; //lyCap的COM智能指针
9 lyCapVt.vt = VT_DISPATCH;
10 lyCapVt.pdispVal = lyCap.m_lpDispatch;
11 lyCapVt.pdispVal->AddRef();
12 //m_ctrlMapX.GetDatasets().RemoveAll();
13 CMapXDataset dataset = m_ctrlMapX.GetDatasets().Add(miDataSetLayer, lyCapVt); //从"US Capitals"层中生成DataSet,挂到MapX上
14
15 //2 从US Capitals层中读取州人口数据,存入SafeArray中
16 //2.1 创建一个 N x 2 的SafeArray, N是州的数目
17 COleSafeArray theData;
18 CMapXFeatures ftsCap = lyCap.Search("1=1", optVt); //州首府的全部图元的集合
19 long iFtsCount = ftsCap.GetCount();
20 unsigned long dims[2] = {iFtsCount, 2}; //创建2列,iFtsCount行的SafeArray.
21 theData.Create(VT_VARIANT, 2, dims); //因为要创建的SafeArray是二维数组,所以Create的第二个参数填2
22
23 //2.2 向SafeArray填充数据
24 long indx[2] = {0, 0};
25 for (int i = 0; i < iFtsCount; i++)
26 {
27 //2.2.1 遍历dataSet的图元,取出每一个图元的行数据集合
28 CMapXFeature ftCap = ftsCap.Item(i+1); //MapX下标从1开始遍历
29 COleVariant ftCapVt; //图元的COM引用
30 ftCapVt.vt = VT_DISPATCH;
31 ftCapVt.pdispVal = ftCap.m_lpDispatch;
32 ftCapVt.pdispVal->AddRef();
33 CMapXRowValues oneRow = dataset.GetRowValues(ftCapVt);
34 /// 如果要修改值,可以这样做:
35 /// CMapXRowValue tempRow = rowsCap.Item(COleVariant("Pop_1990"));
36 /// long newValue=10000;
37 /// tempRow.SetValue(COleVariant(newValue));
38
39 //2.2.2 将一行数据中的State属性加入SafeArray中
40 indx[0] = i;
41 indx[1] = 0;
42 theData.PutElement(indx, COleVariant(oneRow.Item(COleVariant("State")).GetValue()));
43
44 //2.2.3 将一行数据中的Pop_1990属性加入SafeArray中
45 indx[0] = i;
46 indx[1] = 1;
47 theData.PutElement(indx, COleVariant(oneRow.Item(COleVariant("Pop_1990")).GetValue()));
48 }
49
50 //3 将SafeArray绑定到DataSet
51 //3.1 创建新的fields对象,并描述 SafeArray的结构,以便列名可被 Datasets.Add函数识别
52 CMapXFields flds; //属性列的集合
53 flds.CreateDispatch(flds.GetClsid());
54 flds.Add(COleVariant(1l), COleVariant("State"), COleVariant((long)miAggregationIndividual), COleVariant((long)miTypeString));
55 flds.Add(COleVariant(2l), COleVariant("Pop_1990"), COleVariant((long)miAggregationSum), COleVariant((long)miTypeNumeric));
56
57 COleVariant fldsVt;//flds的COM智能指针
58 fldsVt.vt = VT_DISPATCH;
59 fldsVt.pdispVal = flds.m_lpDispatch;
60 fldsVt.pdispVal->AddRef();
61
62 //3.2 设置专题绑定的层
63 CMapXLayer lyState = m_ctrlMapX.GetLayers().Item("USA");
64 COleVariant lyStateVt; //lyCap的COM智能指针
65 lyStateVt.vt = VT_DISPATCH;
66 lyStateVt.pdispVal = lyState.m_lpDispatch;
67 lyStateVt.pdispVal->AddRef();
68
69 //3.3 从SafeArray生成新的DataSet
70 CMapXDataset ds = m_ctrlMapX.GetDatasets().Add(miDataSetSafeArray, theData, COleVariant("My Dataset"), COleVariant(1l), optVt, lyStateVt, fldsVt, optVt);
71 //COleVariant(1l) 是SafeArray的列下标,说明第1列包含了地理信息
72 //在本例中将lyStateVt改为optVt也能正常执行,因为MapX会自动寻找匹配的层
73
74 //4 在新的DataSet上面根据Pop_1990字段自动生成范围专题图
75 CMapXTheme theme = ds.GetThemes().Add(miThemeRanged, "Pop_1990", "My Theme");
76 }
77 catch (COleDispatchException *e)
78 {
79 e->ReportError();
80 e->Delete();
81 }
82 catch (COleException *e)
83 {
84 e->ReportError();
85 e->Delete();
86 }
87}
2{
3 COptionalVariant optVt; //无用变量,用作填充函数输入
4 try
5 {
6 //1 设置专题绑定数据集的来源层
7 CMapXLayer lyCap = m_ctrlMapX.GetLayers().Item("US Capitals"); //州首府层
8 COleVariant lyCapVt; //lyCap的COM智能指针
9 lyCapVt.vt = VT_DISPATCH;
10 lyCapVt.pdispVal = lyCap.m_lpDispatch;
11 lyCapVt.pdispVal->AddRef();
12 //m_ctrlMapX.GetDatasets().RemoveAll();
13 CMapXDataset dataset = m_ctrlMapX.GetDatasets().Add(miDataSetLayer, lyCapVt); //从"US Capitals"层中生成DataSet,挂到MapX上
14
15 //2 从US Capitals层中读取州人口数据,存入SafeArray中
16 //2.1 创建一个 N x 2 的SafeArray, N是州的数目
17 COleSafeArray theData;
18 CMapXFeatures ftsCap = lyCap.Search("1=1", optVt); //州首府的全部图元的集合
19 long iFtsCount = ftsCap.GetCount();
20 unsigned long dims[2] = {iFtsCount, 2}; //创建2列,iFtsCount行的SafeArray.
21 theData.Create(VT_VARIANT, 2, dims); //因为要创建的SafeArray是二维数组,所以Create的第二个参数填2
22
23 //2.2 向SafeArray填充数据
24 long indx[2] = {0, 0};
25 for (int i = 0; i < iFtsCount; i++)
26 {
27 //2.2.1 遍历dataSet的图元,取出每一个图元的行数据集合
28 CMapXFeature ftCap = ftsCap.Item(i+1); //MapX下标从1开始遍历
29 COleVariant ftCapVt; //图元的COM引用
30 ftCapVt.vt = VT_DISPATCH;
31 ftCapVt.pdispVal = ftCap.m_lpDispatch;
32 ftCapVt.pdispVal->AddRef();
33 CMapXRowValues oneRow = dataset.GetRowValues(ftCapVt);
34 /// 如果要修改值,可以这样做:
35 /// CMapXRowValue tempRow = rowsCap.Item(COleVariant("Pop_1990"));
36 /// long newValue=10000;
37 /// tempRow.SetValue(COleVariant(newValue));
38
39 //2.2.2 将一行数据中的State属性加入SafeArray中
40 indx[0] = i;
41 indx[1] = 0;
42 theData.PutElement(indx, COleVariant(oneRow.Item(COleVariant("State")).GetValue()));
43
44 //2.2.3 将一行数据中的Pop_1990属性加入SafeArray中
45 indx[0] = i;
46 indx[1] = 1;
47 theData.PutElement(indx, COleVariant(oneRow.Item(COleVariant("Pop_1990")).GetValue()));
48 }
49
50 //3 将SafeArray绑定到DataSet
51 //3.1 创建新的fields对象,并描述 SafeArray的结构,以便列名可被 Datasets.Add函数识别
52 CMapXFields flds; //属性列的集合
53 flds.CreateDispatch(flds.GetClsid());
54 flds.Add(COleVariant(1l), COleVariant("State"), COleVariant((long)miAggregationIndividual), COleVariant((long)miTypeString));
55 flds.Add(COleVariant(2l), COleVariant("Pop_1990"), COleVariant((long)miAggregationSum), COleVariant((long)miTypeNumeric));
56
57 COleVariant fldsVt;//flds的COM智能指针
58 fldsVt.vt = VT_DISPATCH;
59 fldsVt.pdispVal = flds.m_lpDispatch;
60 fldsVt.pdispVal->AddRef();
61
62 //3.2 设置专题绑定的层
63 CMapXLayer lyState = m_ctrlMapX.GetLayers().Item("USA");
64 COleVariant lyStateVt; //lyCap的COM智能指针
65 lyStateVt.vt = VT_DISPATCH;
66 lyStateVt.pdispVal = lyState.m_lpDispatch;
67 lyStateVt.pdispVal->AddRef();
68
69 //3.3 从SafeArray生成新的DataSet
70 CMapXDataset ds = m_ctrlMapX.GetDatasets().Add(miDataSetSafeArray, theData, COleVariant("My Dataset"), COleVariant(1l), optVt, lyStateVt, fldsVt, optVt);
71 //COleVariant(1l) 是SafeArray的列下标,说明第1列包含了地理信息
72 //在本例中将lyStateVt改为optVt也能正常执行,因为MapX会自动寻找匹配的层
73
74 //4 在新的DataSet上面根据Pop_1990字段自动生成范围专题图
75 CMapXTheme theme = ds.GetThemes().Add(miThemeRanged, "Pop_1990", "My Theme");
76 }
77 catch (COleDispatchException *e)
78 {
79 e->ReportError();
80 e->Delete();
81 }
82 catch (COleException *e)
83 {
84 e->ReportError();
85 e->Delete();
86 }
87}