1、数据介绍
信息提取和解译的过程中,经常会生成一部分中间临时矢量数据,这些数据在执行完对应操作后就失去了存在的价值,针对这种情况,PIE增加了内存矢量数据集,来协助用户完成对自定义矢量数据的读取和显示,下面以Micaps1类数据为例介绍自定义矢量数据的构建和显示。
2、实现思路及原理说明
第一步 |
构建数据的字段、空间参考等信息; |
第二步 |
根据字段和空间参考信息创建内存矢量数据集; |
第三步 |
在内存矢量数据集中逐条添加数据记录(包括几何图形和属性); |
第四步 |
通过内存数据集创建矢量图层; |
第五步 |
添加第四步中的图层到地图,并刷新; |
3、核心接口与方法
接口/类 |
方法 |
说明 |
SysDataSource::FieldPtr |
构造字段 |
|
SysDataSource::FieldsPtr |
AddField () |
添加字段 |
SysDataSource::DatasetFactory |
CreateFeatureDataset |
创建数据集 |
SysDataSource::FeatureDatasetPtr |
CreateNewFeature() |
创建要素 |
AddFeature() |
添加要素 |
|
SysDataSource::FeaturePtr |
SetValue() |
属性赋值 |
4、示例代码
项目名称 |
百度云盘地址下/PIE示例程序/03.数据加载/10.打开自定义矢量数据 |
数据位置 |
百度云盘地址下/ PIE示例数据/矢量数据/Micaps/micaps1/ |
视频位置 |
百度云盘地址下/PIE视频教程/03.数据加载/10.打开自定义矢量数据.avi |
示例代码 |
|
方法(一)打开自定义矢量数据 //以解析Micaps1数据为例 void PIEMainWindow::On_ActionGetMemFeature_Triggered(bool checked) { QString filter = "Micaps数据 (*.000)"; QString micapsFile = QFileDialog::getOpenFileName(nullptr, "添加数据", "", filter); if (micapsFile.isEmpty()) return;
//1、读取Micaps内容 QFile file(micapsFile); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; int count = 0; //2、创建内存矢量数据集 SysDataSource::FieldsPtr fields = new SysDataSource::Fields(); SysDataSource::FieldPtr field_id = new SysDataSource::Field("PointID", SysDataSource::FieldType::OFTInteger, 20, 4); field_id->SetAliasName("站号");
SysDataSource::FieldPtr field_x = new SysDataSource::Field("lon", SysDataSource::FieldType::OFTInteger, 20, 4); field_id->SetAliasName("经度");
SysDataSource::FieldPtr field_y = new SysDataSource::Field("lat", SysDataSource::FieldType::OFTInteger, 20, 4); field_id->SetAliasName("纬度");
SysDataSource::FieldPtr field_h = new SysDataSource::Field("height", SysDataSource::FieldType::OFTInteger, 20, 4); field_id->SetAliasName("海拔高度"); fields->AddField(field_id); fields->AddField(field_x); fields->AddField(field_y); fields->AddField(field_h); SysDataSource::FeatureDatasetPtr memFeatureDataSetPtr = SysDataSource::DatasetFactory::Instance()->CreateFeatureDataset("", fields, SysGeometry::GeometryType::GeometryPoint, nullptr, "MEM"); //3、遍历进行数据写入 while (!file.atEnd()) { QByteArray line = file.readLine(); QString str = line; if (count == 0 || count == 1) { count = count + 1; continue; } QStringList valueList = str.split(' ',QString::SplitBehavior::SkipEmptyParts); SysDataSource::FeaturePtr newFeature = memFeatureDataSetPtr->CreateNewFeature(); //4、写入属性值 newFeature->SetValue(0, valueList[0]); newFeature->SetValue(1, valueList[1]); newFeature->SetValue(0, valueList[2]); newFeature->SetValue(0, valueList[3]); //5、写入图形 SysGeometry::PointPtr geometry = new SysGeometry::Point(); geometry->SetX(QString(valueList[1]).toDouble()); geometry->SetY(QString(valueList[2]).toDouble()); newFeature->SetGeometry(geometry);
//6、保存至Dataset中 memFeatureDataSetPtr->AddFeature(newFeature); count = count + 1; } if (memFeatureDataSetPtr->Save()) { SysCarto::LayerPtr layer = SysCarto::LayerFactory::Instance()->CreateDefaultFeatureLayer(memFeatureDataSetPtr); if (layer!=nullptr) { layer->SetName("内存矢量图层"); m_pCurrentControl->GetMap()->AddLayer(layer); m_pCurrentControl->GetActiveView()->Refresh(); } } } |