2.1.5 矢量拾取
矢量拾取
矢量拾取是指通过使用矢量拾取响应器,在场景中点击矢量图形,使矢量图形变色并获取矢量要素相关信息的操作。
主要包括添加(创建)、更新、删除和获取结果4种操作
注意:矢量拾取响应器可以更新,更新时可对所有参数都进行更新。
下图是矢量拾取效果:
参数说明
矢量拾取响应器可配置的参数如下表所示:
配置项 | 配置说明 | 值类型 | 备注 |
---|---|---|---|
PickLayerIdList | 图层ID列表 | int列表 | 传-1则默认遍历所有矢量图层,若为多个图层,可使用xx,xx,xx 此种格式。图层Id可以通过图层对象的GetLayerId()方法获取 |
PickColor | 颜色透明度 | R,G,B,A | 颜色取值范围在0-1.0之间,如 "0.9,0.8,0.8,1.0",A的取值必须大于0.2 |
IsChangeColor | 是否变色 | bool | 默认值false,设为true,则PickColor 配置的颜色起效 |
IsMultiPick | 是否多要素拾取 | bool | 是否开启多要素拾取。 默认值false |
注意: IsMultiPick
控制是否需要进行多要素拾取,在拾取时,默认会将相交的第一个要素变色并返回,如果开启了多要素拾取,则会将相交到的所有要素变色并批量返回结果信息。
获取结果说明
矢量拾取可以通过响应器对象的GetRespnserResult() 方法获取结果。获取的结果对象参数如下表所示:
配置项 | 配置说明 | 值类型 | 备注 |
---|---|---|---|
PickPointList | 拾取坐标点位 | x,y,z | 鼠标点击位置与模型相交的交点经纬度高程坐标 |
PickSize | 拾取要素数量 | int | 拾取到的要素数量 |
FeaturePos | 要素坐标点集 | string | 拾取到的要素几何坐标点集 |
FeatureSchema | 属性表头 | string | 拾取到的要素所在图层包含的属性表头 |
FeatureAttribute | 要素属性值 | string | 拾取到的要素包含的属性值 |
需要注意的是:
- 单要素和多要素返回值格式不同,多要素需要用|分割每个要素,包括要素属性表头、坐标点集和要素属性字段。
- 如果是多要素拾取,可以通过
FeatureAttribute
获取返回的属性值列表,其格式为:name:value;…|name:value;…|
, - 如果是单要素拾取,可以先获取属性表头,然后根据表头中的每个属性字段去获取要素的属性值。
代码调用示例
Javascript调用
var pickVectorResponser;
var bIsMultiPick = false;//是否开启多要素拾取
var event = addEvent(obj, "FireOnResponserNotify", callBackFun);//响应器对应事件均为FireOnResponserNotify
//响应器回调执行函数
function callBackFun(respType, state){
var str = respType.split("|");
if(str[1] == "PickVectorResponser" || respType == "PickVectorResponser"){//判断回调事件发起者为PickVectorResponser
pickPoint = getFeatureResult();
}
}
//创建矢量拾取响应器
function createVectorPickResponser(){
if(pickVectorResponser == null){
var pOption = map.CreateResponserOptions("shp");//创建响应器配置,参数任意名称
pOption.AddConfig("PickLayerIdList", -1);//传-1则默认遍历所有矢量图层,若为多个图层,可使用xx,xx,xx此种格式
pOption.AddConfig("PickColor", "1.0,0,0,0.5");//拾取颜色
pOption.AddConfig("IsChangeColor", "true");//是否变色
if(bIsMultiPick){
pOption.AddConfig("IsMultiPick", "true");//是否多要素拾取
} else {
pOption.AddConfig("IsMultiPick", "false");//是否多要素拾取
}
pickVectorResponser = map.CreateResponser("PickVectorResponser", pOption);//创建矢量拾取响应器,第一参必须为PickVectorResponser字符串
pickVectorResponser.AddObserver();
map.AddResponser(pickVectorResponser);//添加响应器
}else{
alert("矢量拾取响应器已创建,无需重复创建!");
}
}
//更新矢量拾取响应器
function updateVectorPickResponser(){
if(pickVectorResponser){
var pOption = map.CreateResponserOptions("shp");//创建响应器配置,参数任意名称
pOption.AddConfig("PickColor", "0.0,1,0,0.5");//拾取颜色
pickVectorResponser.UpdateResponserOptions(pOption);
}else{
alert("矢量拾取响应器不存在,无法更新!");
}
}
//删除矢量拾取响应器
function removeVectorPickResponser(){
if(pickVectorResponser){
map.RemoveResponser("PickVectorResponser");
pickVectorResponser = null;
}else{
alert("矢量拾取响应器已移除,无需重复移除!");
}
}
//获取拾取矢量要素属性
function getFeatureResult(){
if(pickVectorResponser){
var responseStr = pickVectorResponser.GetResponserResult().GetConfigValueByKey("PickPointList");//获取经纬度列表
var pickLayerList = pickVectorResponser.GetResponserResult().GetConfigValueByKey("PickLayerList");//获取图层ID
alert("Point:" + responseStr + "; PickLayerList:" + pickLayerList);
var FeatureSchema = pickVectorResponser.GetResponserResult().GetConfigValueByKey("FeatureSchema"); //获取要素属性表头
var FeaturePos = pickVectorResponser.GetResponserResult().GetConfigValueByKey("FeaturePos"); //获取要素坐标点集
var PickSize = pickVectorResponser.GetResponserResult().GetConfigValueByKey("PickSize"); //获取拾取要素数量
alert("PickSize: " + PickSize + "\nFeatureSchema: " + FeatureSchema + "\nFeaturePos: " + FeaturePos);
//是否拾取多要素--单要素和多要素返回值格式不同,多要素需要用|分割每个要素,包括要素属性表头、坐标点集和要素属性字段
if(bIsMultiPick){
//要素属性字段格式为:"name:value;…|name:value;…|"
var FeatureAttribute = pickVectorResponser.GetResponserResult().GetConfigValueByKey("FeatureAttribute"); //获取要素属性
alert("FeatureAttribute: " + FeatureAttribute);
} else {
var attTable = FeatureSchema.split(";");
var resultValue = "";
for(var i = 0; i < attTable.length-1; i++){
var value = pickVectorResponser.GetResponserResult().GetConfigValueByKey(attTable[i]); //获取要素属性字段对应属性值
resultValue += attTable[i] + "=" + value+";";
}
alert(resultValue);
}
return responseStr;
}else{
alert("矢量拾取响应器不存在,无法获取属性信息!");
}
}
C++调用
BaseObjectCOMLib::IResponserObjectPtr pickResp;//拾取响应器对象
bool bIsMultiPick = true;//是否开启多要素拾取
BEGIN_EVENTSINK_MAP(Cf0c3VectorLoadDlg, CDialogEx)
ON_EVENT(Cf0c3VectorLoadDlg, IDC_VPSDKCTRL1, 15, Cf0c3VectorLoadDlg::FireOnResponserNotifyVpsdkctrl1, VTS_BSTR VTS_I4)
END_EVENTSINK_MAP()
//创建矢量拾取响应器
void Cf0c3VectorLoadDlg::OnBnClickedcreatevectorpickresponser()
{
if(pickResp)
{
return;
}
ConfigOptionsCOMLib::IResponserOptionPtr pOption = map->CreateResponserOptions("");//创建响应器配置,参数任意名称
pOption->AddConfig("PickLayerIdList", "-1"); //拾取图层id
pOption->AddConfig("PickColor", "1.0,0,0,0.3");//拾取颜色
pOption->AddConfig("IsChangeColor", "true");//是否变色
if(bIsMultiPick){
pOption->AddConfig("IsMultiPick", "true");//是否多要素拾取
} else {
pOption->AddConfig("IsMultiPick", "false");//是否多要素拾取
}
pickResp = map->CreateResponser("PickVectorResponser", pOption);//创建矢量拾取响应器,第一参必须为PickVectorResponser字符串
pickResp->AddObserver();//添加监听
map->AddResponser(pickResp);//添加响应器
}
//更新矢量拾取响应器
void Cf0c3VectorLoadDlg::OnBnClickedupdatevectorpickresponser()
{
if(!pickResp)
{
return;
}
ConfigOptionsCOMLib::IResponserOptionPtr pOption = map->CreateResponserOptions("shp");//创建响应器配置,参数任意名称
pOption->AddConfig("PickColor", "0.0,1,0,0.5");//拾取颜色
pickResp->UpdateResponserOptions(pOption);//更新
}
//删除矢量拾取响应器
void Cf0c3VectorLoadDlg::OnBnClickedremovevectorpickresponser()
{
if(!pickResp)
{
return;
}
map->RemoveResponser("PickVectorResponser");//移除响应器
pickResp = NULL;
}
//获取拾取矢量要素属性
void Cf0c3VectorLoadDlg::OnBnClickedgetfeatureresult()
{
if(!pickResp)
{
return;
}
ConfigOptionsCOMLib::IResponserOptionPtr respOpt = pickResp->GetResponserResult();
CString responseStr = respOpt->GetConfigValueByKey("PickPointList");//获取经纬度列表
CString pickLayerList = respOpt->GetConfigValueByKey("PickLayerList");//获取图层ID
CString FeatureSchema = respOpt->GetConfigValueByKey("FeatureSchema"); //获取要素属性表头
CString FeaturePos = respOpt->GetConfigValueByKey("FeaturePos"); //获取要素坐标点集
CString PickSize = respOpt->GetConfigValueByKey("PickSize"); //获取拾取要素数量
MessageBox(L"PickSize: " + PickSize + "\nFeatureSchema: " + FeatureSchema + "\nFeaturePos: " + FeaturePos);
//是否拾取多要素--单要素和多要素返回值格式不同,多要素需要用|分割每个要素,包括要素属性表头、坐标点集和要素属性字段
if(bIsMultiPick){
//要素属性字段格式为:"name:value;…|name:value;…|"
CString FeatureAttribute = respOpt->GetConfigValueByKey("FeatureAttribute"); //获取要素属性
MessageBox(L"FeatureAttribute: " + FeatureAttribute);
} else {
CString attTable;
CString resultValue;
int pos = FeatureSchema.Find(L";");
attTable = FeatureSchema.Left(pos);
while(attTable.GetLength() > 0)
{
CString value = respOpt->GetConfigValueByKey(attTable.GetString()); //获取要素属性字段对应属性值
resultValue += attTable + L"=" + value+ L";";
FeatureSchema = FeatureSchema.Mid(pos+1);
pos = FeatureSchema.Find(L";");
attTable = FeatureSchema.Left(pos);
}
MessageBox(L"FeatureAttribute: " + resultValue);
}
}
//矢量拾取回调
void Cf0c3VectorLoadDlg::FireOnResponserNotifyVpsdkctrl1(LPCTSTR respType, long notifyType)
{
OnBnClickedgetfeatureresult();
}
C#调用
IResponserObject pickVectorResponser;//拾取响应器
bool bIsMultiPick = false;//是否开启多要素拾取
//创建矢量拾取响应器
private void createVectorPickResponser_Click(object sender, EventArgs e)
{
if (pickVectorResponser == null)
{
IResponserOption pOption = map.CreateResponserOptions("shp");//创建响应器配置,参数任意名称
pOption.AddConfig("PickLayerIdList", "-1");//传-1则默认遍历所有矢量图层,若为多个图层,可使用xx,xx,xx此种格式
pOption.AddConfig("PickColor", "1.0,0,0,0.5");//拾取颜色
pOption.AddConfig("IsChangeColor", "true");//是否变色
if (bIsMultiPick)
{
pOption.AddConfig("IsMultiPick", "true");//是否多要素拾取
}
else
{
pOption.AddConfig("IsMultiPick", "false");//是否多要素拾取
}
pickVectorResponser = map.CreateResponser("PickVectorResponser", pOption);//创建矢量拾取响应器,第一参必须为PickVectorResponser字符串
pickVectorResponser.AddObserver();
map.AddResponser(pickVectorResponser);//添加响应器
}
}
//更新矢量拾取响应器
private void updateVectorPickResponser_Click(object sender, EventArgs e)
{
if (pickVectorResponser != null)
{
IResponserOption pOption = map.CreateResponserOptions("shp");//创建响应器配置,参数任意名称
pOption.AddConfig("PickColor", "0.0,1,0,0.5");//拾取颜色
pickVectorResponser.UpdateResponserOptions(pOption);
}
}
//删除矢量拾取响应器
private void removeVectorPickResponser_Click(object sender, EventArgs e)
{
if (pickVectorResponser != null)
{
map.RemoveResponser("PickVectorResponser");
pickVectorResponser = null;
}
}
//获取拾取矢量要素属性
private void getFeatureResult_Click(object sender, EventArgs e)
{
string responseStr = pickVectorResponser.GetResponserResult().GetConfigValueByKey("PickPointList");//获取经纬度列表
string pickLayerList = pickVectorResponser.GetResponserResult().GetConfigValueByKey("PickLayerList");//获取图层ID
MessageBox.Show("Point:" + responseStr + "; PickLayerList:" + pickLayerList);
string FeatureSchema = pickVectorResponser.GetResponserResult().GetConfigValueByKey("FeatureSchema");//获取要素属性所有属性字段
string FeaturePos = pickVectorResponser.GetResponserResult().GetConfigValueByKey("FeaturePos"); //获取要素坐标点集
string PickSize = pickVectorResponser.GetResponserResult().GetConfigValueByKey("PickSize"); //获取拾取要素数量
MessageBox.Show("PickSize: " + PickSize + "\nFeatureSchema: " + FeatureSchema + "\nFeaturePos: " + FeaturePos);
//是否拾取多要素--单要素和多要素返回值格式不同,多要素需要用|分割每个要素,包括要素属性表头、坐标点集和要素属性字段
if (bIsMultiPick)
{
//要素属性字段格式为:"name:value;…|name:value;…|"
string FeatureAttribute = pickVectorResponser.GetResponserResult().GetConfigValueByKey("FeatureAttribute"); //获取要素属性
MessageBox.Show("FeatureAttribute: " + FeatureAttribute);
}
else
{
string[] attTable = FeatureSchema.Split(';');
string resultValue = "";
for (var i = 0; i < attTable.Length - 1; i++)
{
var value = pickVectorResponser.GetResponserResult().GetConfigValueByKey(attTable[i]); //获取要素属性字段对应属性值
resultValue += attTable[i] + "=" + value + ";";
}
MessageBox.Show(resultValue);
}
}
//拾取回调函数
private void axVPSDKCtrl1_FireOnResponserNotify_1(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnResponserNotifyEvent e)
{
string responseStr = pickVectorResponser.GetResponserResult().GetConfigValueByKey("PickPointList");//获取经纬度列表
string pickLayerList = pickVectorResponser.GetResponserResult().GetConfigValueByKey("PickLayerList");//获取图层ID
MessageBox.Show("Point:" + responseStr + "; PickLayerList:" + pickLayerList);
string FeatureSchema = pickVectorResponser.GetResponserResult().GetConfigValueByKey("FeatureSchema");//获取要素属性所有属性字段
string FeaturePos = pickVectorResponser.GetResponserResult().GetConfigValueByKey("FeaturePos"); //获取要素坐标点集
string PickSize = pickVectorResponser.GetResponserResult().GetConfigValueByKey("PickSize"); //获取拾取要素数量
MessageBox.Show("PickSize: " + PickSize + "\nFeatureSchema: " + FeatureSchema + "\nFeaturePos: " + FeaturePos);
//是否拾取多要素--单要素和多要素返回值格式不同,多要素需要用|分割每个要素,包括要素属性表头、坐标点集和要素属性字段
if (bIsMultiPick)
{
//要素属性字段格式为:"name:value;…|name:value;…|"
string FeatureAttribute = pickVectorResponser.GetResponserResult().GetConfigValueByKey("FeatureAttribute"); //获取要素属性
MessageBox.Show("FeatureAttribute: " + FeatureAttribute);
}
else
{
string[] attTable = FeatureSchema.Split(';');
string resultValue = "";
for (var i = 0; i < attTable.Length - 1; i++)
{
var value = pickVectorResponser.GetResponserResult().GetConfigValueByKey(attTable[i]); //获取要素属性字段对应属性值
resultValue += attTable[i] + "=" + value + ";";
}
MessageBox.Show(resultValue);
}
}