2.4.4 矢量编辑
矢量编辑
矢量编辑是指对矢量图层中的要素进行编辑。包括要素添加、要素获取、要素更新和要素删除4类功能。并且编辑完之后可以进行图层保存和另存功能。
为了方便操作,CooRun SDK为矢量编辑操作封装了要素图层对象。在创建图层之后,可以调用场景对象的GetFeatureModelLayer()接口获取要素图层对象。通过要素图层对象可以实现要素的添加、获取、更新、删除以及图层保存等功能。
要素作为矢量数据的基本单元,单个要素包含顶点几何数据和属性数据。因此在对要素进行编辑时,可同时编辑几何和属性数据。可以调用场景对象的CreateFeature()接口创建要素对象。通过要素对象可以设置要素的几何类型、关键点信息和属性信息等。
一般情况下,矢量编辑包含以下几个操作:
1. 创建图层。 可以创建已有数据的图层进行编辑,也可以创建空的图层,然后批量添加要素进行编辑。在创建空的图层时,需要设置Fields
参数添加属性表头,设置GeometryType
参数确定图层加载数据的几何类型。
2. 获取要素图层对象。 调用GetFeatureModelLayer
接口获取要素图层对象,后续的操作都在该对象上进行操作。
3. 创建并添加要素。 创建新的要素对象,设置要素对象配置信息,然后将要素对象添加到矢量图层当中进行显示。
4. 更新要素。 可以对已有的要素进行更新。
5. 删除要素。 可以删除指定的要素。
6. 获取要素信息。 可通过矢量拾取响应器或者相应的获取要素信息接口获取指定的要素对象信息。
7. 保存图层。 编辑完之后,可以保存矢量图层。
添加要素
图层创建之后,可以进行要素的添加,添加要素的过程如下:
- 创建要素对象。 调用CreateFeature()接口创建新要素对象。
- 设置要素ID。要素图层对象中的每个要素的Id是从0开始递增的唯一整数值,调用SetFeatureId()设置要素的唯一Id值。为了保证每个要素的Id是唯一的,可以调用要素图层对象的GetMaxFeatureID()接口获取当前最大的Id值maxId,然后设置新要素的Id为maxId+1。
- 添加几何类型。 调用SetGeometryType()设置要素的几何类型。主要的几何类型有:1-点; 2-线; 3-环; 4-面。
- 添加关键点坐标。 调用AddPoint()接口添加关键点的经纬度高程坐标。一般点几何需要添加一个关键点,线几何至少需要添加两个关键点,面几何至少要添加三个关键点。
- 添加属性信息。 调用AddAttribute()接口添加要素的属性信息。属性字段是在创建图层时
Fields
参数设置的属性表头中的字段。字段值类型有:1-int; 2-long; 3-float; 4-double; 5-string; 6-bool。 - 添加要素。 调用AddFeature()接口将新要素添加到矢量图层中。
在添加要素时,需要注意:
- 要素ID必须是唯一的,如果出现重复Id的要素,场景在渲染时有可能会将重复的要素裁剪掉,并且保存矢量图层时会自动剔除重复的要素。
- 要素的几何类型也需要和矢量图层数据类型保持一致,否则将会无法保存。
- 步骤2-5没有明确的顺序,只需要保证在6之前设定好就可以。
更新要素
更新要素就是对矢量图层中原有的要素进行更新替换操作,它是通过调用矢量图层的UpdateFeatureById()接口实现的。该接口第一参数需要传入原要素的Id值,第二参数需要传入新要素对象。
新要素对象的创建方法可参考添加要素中的步骤1-5。只是在更新要素时,可以将新要素对象的Id和原要素ID设为一致。
如果UpdateFeatureById()接口的第二个参数传入一个空的要素对象,可以实现删除要素的操作。
删除要素
删除要素就是将矢量图层中的某个要素删除的操作。删除要素有三种方式:
- 通过Id删除要素。 这种方式在更新要素中已经提到,就是调用矢量图层的UpdateFeatureById()接口时,第一个参数传入需要删除的Id,第二个参数传空的要素即可实现删除的效果。
- 通过坐标删除要素。 调用矢量图层的DeleteFeatureByGeoPos()接口传入经纬度高程坐标实现删除操作。
- 通过拾取删除要素。 这种方式需要先创建矢量拾取响应器,并且拾取到要素之后,再调用矢量图层的DeleteFeature接口实现要素删除操作。
获取要素
获取要素就是获取矢量图层中某个要素的几何信息和属性信息。获取要素的方式有:
- 通过拾取获取要素。 这种方式需要先拾取要素,再调用矢量图层的GetFeatureByPick()接口获取要素操作。
- 通过Id获取要素。 如果知道要素的Id,可以通过矢量图层的GetFeatureById()接口获取要素信息。
- 通过游标遍历要素。 这种方式可以获取图层中的所有要素信息。首先通过矢量图层的CreateFeatureCursor()接口创建游标,然后循环调用GetNextFeature()接口获取每个要素信息,最后调用ClearFeatureCursor()接口清空游标。
上面几种方法都可以获取到要素对象,获取要素对象之后,可以通过要素对象的接口获取相应的几何关键点信息和属性信息。
要素对象提供了GetFeatureId()接口获取要素ID,GetGeometryType()接口获取要素几何类型,GetPoints()接口获取几何关键点信息和GetAttributeValueByName()接口获取属性信息。
在获取属性信息之前,可以通过矢量图层的GetFeatureSchema()接口获取属性表头,然后根据表头中的每个属性字段去获取要素的属性值。
保存图层
在矢量图层编辑完之后,可以将修改的内容保存到矢量文件当中,也可以将整个图层另存为一个新的矢量文件。
保存时需要调用矢量图层的SaveLayer()接口,也可以调用SaveAsLayer()接口进行另存。
代码调用示例
下面是进行矢量编辑所有功能涉及到的代码调用,其中创建的矢量图层可使用代码中涉及到的矢量点、线、面图层,也可以使用任意一种符号化的矢量图层。
Javascript调用
var shpLayer = null;//矢量图层
var pickResp = null;//矢量拾取对象
var polygoneditLayer = null;
var addState = false;//要素创建开关
var featureId = null;//要素ID
var event;
var FeatureType = 4; //要素几何类型(1:点; 2:线; 3:环; 4:面; 5:多结构)
var addFeature;
var responseStr;//点位坐标
addEvent(obj, "FireOnResponserNotify", callBackFun);//响应器对应事件均为FireOnResponserNotify
//鼠标左键弹起回调
function callBackLButtonFun(posX, posY){
if(addState){
var transformate = map.CreateTransformation();
var pos = transformate.ScreenPosToWorldPos(posX, posY);//将屏幕坐标点转换成经纬度坐标
addFeature.AddPoint(pos.GetX(), pos.GetY(), pos.GetZ());////向编辑图层添加坐标点信息
responseStr = pos.GetX() + "," + pos.GetY() + "," + pos.GetZ() + ";";
}
}
function callBackFun(respType, state){
var str2 = respType.split("|");
if(str2[1] == "PickVectorResponser" || respType == "PickVectorResponser")
{
responseStr = pickResp.GetResponserResult().GetConfigValueByKey("PickPointList"); //获取经纬度列表
var PickLayerList = pickResp.GetResponserResult().GetConfigValueByKey("PickLayerList"); //获取图层ID
alert("Point:" + responseStr + "; PickLayerList:" + PickLayerList);
var featureSchema = pickResp.GetResponserResult().GetConfigValueByKey("FeatureSchema"); //获取要素属性表头
var attTable = featureSchema.split(";");
var resultValue = "";
for(var i = 0; i < attTable.length-1; i++){
var value = pickResp.GetResponserResult().GetConfigValueByKey(attTable[i]);//获取要素属性
resultValue += attTable[i] + "=" + value+";";
}
alert(resultValue);
}
if(respType == "VectorEditResponser" && state == 0)
{
var Points = vectorEditResp.GetResponserResult().GetConfigValueByKey("Points"); //获取经纬度列表
alert("Points:" + Points);
}
}
//************************
//1.图层操作
//************************
//创建矢量图层
function createLayer(){
if(event == null){
event = addEvent(obj, "FireOnLButtonUp", callBackLButtonFun);//响应器对应事件均为FireOnResponserNotify
}
var selOption = document.getElementById("selOption");
switch(selOption.selectedIndex){
case 0://"点矢量":
createPointShpLayer();FeatureType =1;break;
case 1://"线矢量":
createLineShpLayer();FeatureType =2;break;
case 2://"面矢量":
createPolygonShpLayer();FeatureType =4;break;
default:
alert("no item selected!");
}
}
//创建点矢量图层
function createPointShpLayer(){
if(shpLayer == null){
//1.创建点符号对象
var pSymbol = map.CreateSymbol("PointSymbol");//创建符号对象,PointSymbol代表点符号
pSymbol.AddConfig("Size", "5");//点大小,(0-10)
pSymbol.AddConfig("Color", "1.0,1.0,0.0,1.0");//颜色值(RGBA,各值取值范围0-1之间)
//2.创建样式对象
var pStyle = map.CreateStyle("style");//创建样式
pStyle.SetName("point");//设置别名point
pStyle.AddSymbol("PointSymbol", pSymbol.GetConfig()); //将符号对象添加到样式,第一参数为符号类型
pStyle.AddFilterName("BuildGeometryFilter"); //添加构建器名称.几何构建器为:BuildGeometryFilter
//3.创建样式表对象
var styleSheet = map.CreateStyleSheet(); // 创建样式表
styleSheet.AddStyle(pStyle.GetConfig()); //将样式对象配置添加至样式表
//4.创建并添加图层对象
var tlo = map.CreateLayerOptions("shp");//创建图层配置对象,名称任意
tlo.AddConfig("LayerOptionsName", "FeatureModelLayerOptions");//图层配置对象名称, FeatureModelLayerOptions代表矢量数据配置
tlo.AddConfig("DataSourceTypeName", "fmgeom");//数据源类型,代表fmgeom插件
tlo.AddConfig("Driver", "ESRI Shapefile"); //ogr数据的解析驱动
tlo.AddConfig("FeatureSourceType", "ogr"); //要素数据源类型,本地数据为ogr
tlo.AddConfig("Url", "E://testPoint.shp");//数据路径
tlo.AddConfig("Fields", "ID:Int:100:0,Name:String:100:0,Height:Double:100:3,Width:Float:100:3");//创建矢量的属性字段,属性名:属性类型:类型长度:小数点后几位
tlo.AddConfig("GeometryType", "Point"); ////几何类型。Point-点;Polyline-线;Polygon-面.此项配置不能少或字符串一定不能错误,否则保存文件不成功
tlo.AddConfig("TileSizeFactor", "1.0"); //瓦片大小的影响因子,建议是1.0
tlo.AddConfig("TileSize", "1000"); //瓦片大小,根据最大显示范围设置,一般为MaxRange的1/10左右
tlo.AddConfig("LiftUp", "0"); //图层抬升高度,正值为抬高,负值为降低
tlo.AddConfig("MaxRange", "10000.0"); // 最大显示范围,大于最小显示范围-无穷大
tlo.AddConfig("MinRange", "0.0"); // 最小显示范围,0-无穷大
tlo.AddConfig("StyleSheet", styleSheet.GetConfig()); //将样式表配置添加至图层配置对象
shpLayer = map.CreateLayer("FeatureModelLayer", tlo); //创建矢量图层,第一项参数为FeatureModelLayer
map.AddLayer(shpLayer); //添加图层
var id = shpLayer.GetLayerID(); ////获取图层id
polygoneditLayer = map.GetFeatureModelLayer(id); ////获取要素图层对象
//pointShpLayer.Locate(); //矢量图层定位
}else{
alert("请勿重复创建矢量图层!");
}
}
//创建线矢量图层
function createLineShpLayer(){
if(shpLayer == null){
//1.创建线符号对象
var lSymbol = map.CreateSymbol("LineSymbol"); // 创建符号对象,LineSymbol代表线符号
lSymbol.AddConfig("Stipple", "-1"); // 线条类型,-1 实线 1~65535 虚线
lSymbol.AddConfig("Width", "10"); // 线宽(0-10)
lSymbol.AddConfig("Color", "0.82, 0.48, 0, 1.0"); // 颜色值(RGBA)0-1
//2.创建样式对象
var lStyle = map.CreateStyle("style"); // 创建样式
lStyle.SetName("line"); // 设置别名line
lStyle.AddSymbol("LineSymbol", lSymbol.GetConfig()); // 将符号对象添加到样式,第一参数为符号类型
lStyle.AddFilterName("BuildGeometryFilter"); // 添加构建器名称,几何构建器为:BuildGeometryFilter
//3.创建样式表对象
var styleSheet = map.CreateStyleSheet(); // 创建样式表
styleSheet.AddStyle(lStyle.GetConfig()); // 将样式配置添加至样式表
//4.创建并添加图层对象
var tlo = map.CreateLayerOptions("shp");//创建图层配置对象,名称任意
tlo.AddConfig("LayerOptionsName", "FeatureModelLayerOptions");//图层配置对象名称, FeatureModelLayerOptions代表矢量数据配置
tlo.AddConfig("DataSourceTypeName", "fmgeom");//数据源类型,代表fmgeom插件
tlo.AddConfig("Driver", "ESRI Shapefile"); //ogr数据的解析驱动
tlo.AddConfig("FeatureSourceType", "ogr"); //要素数据源类型,本地数据为ogr
tlo.AddConfig("Url", "E://testLine.shp");//数据路径
tlo.AddConfig("Fields", "ID:Int:100:0,Name:String:100:0,Height:Double:100:3,Width:Float:100:3");//创建矢量的属性字段,属性名:属性类型:类型长度:小数点后几位
tlo.AddConfig("GeometryType", "Polyline"); ////几何类型。Point-点;Polyline-线;Polygon-面.此项配置不能少或字符串一定不能错误,否则保存文件不成功
tlo.AddConfig("TileSizeFactor", "1.0"); //瓦片大小的影响因子,建议是1.0
tlo.AddConfig("TileSize", "1000"); //瓦片大小,根据最大显示范围设置,一般为MaxRange的1/10左右
tlo.AddConfig("LiftUp", "0"); //图层抬升高度,正值为抬高,负值为降低
tlo.AddConfig("MaxRange", "10000.0"); // 最大显示范围,大于最小显示范围-无穷大
tlo.AddConfig("MinRange", "0.0"); // 最小显示范围,0-无穷大
tlo.AddConfig("StyleSheet", styleSheet.GetConfig()); //将样式表配置添加至图层配置对象
shpLayer = map.CreateLayer("FeatureModelLayer", tlo); //创建矢量图层,第一项参数为FeatureModelLayer
map.AddLayer(shpLayer); //添加图层
var id = shpLayer.GetLayerID(); ////获取图层id
polygoneditLayer = map.GetFeatureModelLayer(id); ////获取要素图层对象
shpLayer.Locate(); //矢量图层定位
}else{
alert("请勿重复创建矢量图层!");
}
}
//创建面矢量图层
function createPolygonShpLayer(){
if(shpLayer == null){
//1.1创建面符号对象
var pSymbol = map.CreateSymbol("PolygonSymbol"); // 创建符号对象,PolygonSymbol代表面符号
pSymbol.AddConfig("Color", "0.64, 0.01, 0.97, 0.6");// 颜色值(RGBA)0-1
pSymbol.AddConfig("SurfaceStyleName", "Surface"); //
//1.2创建线符号对象
var lSymbol = map.CreateSymbol("LineSymbol"); // 创建符号对象,LineSymbol代表线符号
lSymbol.AddConfig("Stipple", "-1"); // 线条类型,-1 实线 1~65535 虚线
lSymbol.AddConfig("Width", "2"); // 线宽(0-10)
lSymbol.AddConfig("Color", "0.42, 0.65, 0.96, 1.0"); // 颜色值(RGBA)0-1
//2.创建样式对象
var pStyle = map.CreateStyle("style"); // 创建样式
pStyle.SetName("polygon"); // 设置别名polygon
pStyle.AddSymbol("PolygonSymbol", pSymbol.GetConfig()); // 将符号对象添加到样式,第一参数为符号类型
pStyle.AddSymbol("LineSymbol", lSymbol.GetConfig()); // 将符号对象添加到样式,第一参数为符号类型
pStyle.AddFilterName("BuildGeometryFilter"); // 添加构建器名称,几何构建器为:BuildGeometryFilter
//3.创建样式表对象
var styleSheet = map.CreateStyleSheet(); // 创建样式表
styleSheet.AddStyle(pStyle.GetConfig()); // 将样式配置添加至样式表
//4.创建并添加图层对象
var tlo = map.CreateLayerOptions("shp");//创建图层配置对象,名称任意
tlo.AddConfig("LayerOptionsName", "FeatureModelLayerOptions");//图层配置对象名称, FeatureModelLayerOptions代表矢量数据配置
tlo.AddConfig("DataSourceTypeName", "fmgeom");//数据源类型,代表fmgeom插件
tlo.AddConfig("Driver", "ESRI Shapefile"); //ogr数据的解析驱动
tlo.AddConfig("FeatureSourceType", "ogr"); //要素数据源类型,本地数据为ogr
tlo.AddConfig("Url", "E://testPolygon.shp");//数据路径
tlo.AddConfig("Fields", "ID:Int:100:0,Name:String:100:0,Height:Double:100:3,Width:Float:100:3");//创建矢量的属性字段,属性名:属性类型:类型长度:小数点后几位
tlo.AddConfig("GeometryType", "Polygon"); ////几何类型。Point-点;Polyline-线;Polygon-面.此项配置不能少或字符串一定不能错误,否则保存文件不成功
tlo.AddConfig("TileSizeFactor", "1.0"); //瓦片大小的影响因子,建议是1.0
tlo.AddConfig("TileSize", "1000"); //瓦片大小,根据最大显示范围设置,一般为MaxRange的1/10左右
tlo.AddConfig("LiftUp", "0"); //图层抬升高度,正值为抬高,负值为降低
tlo.AddConfig("MaxRange", "10000.0"); // 最大显示范围,大于最小显示范围-无穷大
tlo.AddConfig("MinRange", "0.0"); // 最小显示范围,0-无穷大
tlo.AddConfig("IsHasTexture", "false");//是否有纹理,默认为false,该参数在进行分析时起效
tlo.AddConfig("StyleSheet", styleSheet.GetConfig()); //将样式表配置添加至图层配置对象
shpLayer = map.CreateLayer("FeatureModelLayer", tlo); //创建矢量图层,第一项参数为FeatureModelLayer
map.AddLayer(shpLayer); //添加图层
var id = shpLayer.GetLayerID(); ////获取图层id
polygoneditLayer = map.GetFeatureModelLayer(id); ////获取要素图层对象
shpLayer.Locate(); //矢量图层定位
}else{
alert("请勿重复创建矢量图层!");
}
}
//移除矢量图层
function removeLayer(){
if(shpLayer){
map.RemoveLayer(shpLayer);//删除矢量图层
polygoneditLayer=null;
shpLayer = null;
editShpLayer = null;
}else{
alert("图层未创建或者已移除");
}
}
//保存图层
function polygonLayerSave() {
polygoneditLayer.SaveLayer(); //编辑图层保存,一般用于首次创建保存
}
//另存图层
function polygonLayerAsSave() {
polygoneditLayer.SaveAsLayer("F:\\test" + FeatureType +".shp"); //编辑图层另保存。用于多次打开后保存
}
//************************
//2.要素操作
//************************
//创建要素
function CreateFeature()
{
addFeature = map.CreateFeature();//创建要素对象
addFeature.SetGeometryType(FeatureType);//设置要素几何类型(1:点; 2:线; 3:环; 4:面; 5:多结构)
addFeature.SetComponentType(1);//创建子几何类型(当GeometryType为5时生效)
addFeature.AddAttribute("Height", "43.5", 4);//添加属性值(1:int; 2:long; 3:float; 4:double; 5:string; 6:bool)
addFeature.AddAttribute("Name", "测试", 5);//添加属性值
addFeature.AddAttribute("Width", "54", 3);//添加属性值
addFeature.AddAttribute("ID", "1", 1);//添加属性值
addState = true;//通过设置该状态,可以在鼠标左键事件中将顶点信息加入要素中
}
//添加要素
function AddFeature()
{
featureId = polygoneditLayer.GetMaxFeatureID() + 1;//获取矢量图层要素最大ID
addFeature.SetFeatureId(featureId); //设置FeatureID
polygoneditLayer.AddFeature(addFeature);//添加到矢量图层
addState = false;
}
//************************
//3.要素获取操作
//************************
//创建要素拾取
function CreatePickPolygon() {
if(!pickResp)
{
var pOption = map.CreateResponserOptions("123"); //创建响应器配置,参数任意名称
pOption.AddConfig("PickLayerIdList", polygoneditLayer.GetLayerID()); //拾取图层id
pOption.AddConfig("PickColor", "1.0,0,0,0.5"); //拾取颜色
pOption.AddConfig("IsChangeColor", "true"); //是否变色
pickResp = map.CreateResponser("PickVectorResponser", pOption); //创建矢量拾取响应器,第一参必须为PickVectorResponser字符串
pickResp.AddObserver();
map.AddResponser(pickResp); //添加响应器
}
else
{
alert("图层存在,无需创建");
}
}
//移除要素拾取
function RemovePickPolygon() {
if(pickResp)
{
map.RemoveResponser("PickVectorResponser"); //移除响应器
pickResp = null;
}
else
{
alert("图层不存在");
}
}
//删除拾取的要素
function RemoveFeature() {
polygoneditLayer.DeleteFeature(); //删除要素
}
//通过拾取获取要素
function GetFeatureByPick() {
if(pickResp)
{
if (null == addFeature) {
addFeature = map.CreateFeature(); //创建要素对象
}
addFeature.ClearFeature(); //创建清空要素信息
polygoneditLayer.GetFeatureByPick(addFeature); //通过ID获取要素对象值
var type = addFeature.GetGeometryType();//获取要素的几何类型
var Ctype = addFeature.GetComponentType();//获取要素的子几何类型
var points = addFeature.GetPoints();//获取要素的几何点集
var featureSchema = polygoneditLayer.GetFeatureSchema();//获取要素属性表头
var id = addFeature.GetFeatureId();//获取要素ID
alert("id=" + id + " | type=" + type + " | Ctype=" + Ctype + "\npoints=" + points +"\nfeatureSchema=" + featureSchema);
var attTable = featureSchema.split(";");
var resultValue = "";
for(var i = 0; i < attTable.length-1; i++){
var tmp = attTable[i].split(":");
var value = addFeature.GetAttributeValueByName(tmp[0]); //获取要素属性字段对应属性值
resultValue += tmp[0] + "=" + value+";";
}
alert(resultValue);
}
}
//通过ID获取要素
function GetFeatureById() {
if (null == addFeature) {
addFeature = map.CreateFeature(); //创建要素对象
}
addFeature.ClearFeature(); //创建清空要素信息
polygoneditLayer.GetFeatureById(0, addFeature);//通过ID获取要素对象值
var type = addFeature.GetGeometryType();//获取要素的几何类型
var Ctype = addFeature.GetComponentType();//获取要素的子几何类型
var points = addFeature.GetPoints();//获取要素的几何点集
var featureSchema = polygoneditLayer.GetFeatureSchema();//获取要素属性表头
var id = addFeature.GetFeatureId();//获取要素ID
alert("id=" + id + " | type=" + type + " | Ctype=" + Ctype + "\npoints=" + points +"\nfeatureSchema=" + featureSchema);
var attTable = featureSchema.split(";");
var resultValue = "";
for(var i = 0; i < attTable.length-1; i++){
var tmp = attTable[i].split(":");
var value = addFeature.GetAttributeValueByName(tmp[0]); //获取要素属性字段对应属性值
resultValue += tmp[0] + "=" + value+";";
}
alert(resultValue);
}
//根据Id更新要素
function UpdateFeatureById() {
if (null == addFeature) {
addFeature = map.CreateFeature(); //创建要素对象
}
var type = addFeature.GetGeometryType(); //获取要素的几何类型
var points = addFeature.GetPoints(); //获取要素的几何点集
var id = addFeature.GetFeatureId(); //获取要素ID
var Name = addFeature.GetAttributeValueByName("Name"); //获取要素属性值
var NewFeature = map.CreateFeature(); //创建要素对象
NewFeature.SetGeometryType(type); //设置要素几何类型(1:点; 2:线; 3:环; 4:面; 5:多结构)
NewFeature.SetComponentType(1); //设置子几何类型(当GeometryType为5时生效)
NewFeature.AddPoints(points); //添加坐标点信息
NewFeature.AddAttribute("Height", "100", 4);//添加属性值(1:int; 2:long; 3:float; 4:double; 5:string; 6:bool)
NewFeature.AddAttribute("Name", "更新了", 5);//添加属性值
NewFeature.AddAttribute("Width", "54", 3);//添加属性值
NewFeature.AddAttribute("ID", "10", 1);//添加属性值
var featureId = polygoneditLayer.GetMaxFeatureID(); //获取矢量图层要素最大ID
NewFeature.SetFeatureId(featureId + 1); //设置FeatureID
var state = polygoneditLayer.UpdateFeatureById(id, NewFeature); //NewFeature若为NULL则为删除id为某值的要素
}
//根据Id删除要素
function DeleteFeatureById() {
var NewFeature = map.CreateFeature(); //创建要素对象
var state = polygoneditLayer.UpdateFeatureById(addFeature.GetFeatureId(), NewFeature); //NewFeature若为NULL则为删除id为某值的要素
}
//根据经纬度删除要素
function RemoveFetureByGeo() {
if (responseStr == null) {
alert("pick point is null!");
}
var blh = responseStr.split(",;");
blh = blh[blh.length - 1];
blh = blh.replace(";", "");
var LonLatH = blh.split(",");
alert(LonLatH);//获取经纬度坐标
var state = polygoneditLayer.DeleteFeatureByGeoPos(LonLatH[0], LonLatH[1], LonLatH[2]);//根据坐标删除矢量要素
}
//************************
//要素游标查询操作
//************************
//创建要素获取游标
function CreateFeatureCursor() {
var state = polygoneditLayer.CreateFeatureCursor(0, 0, 0, 0,"");//创建要素获取游标,参数含义:MinXPos,MaxXPos,MinYPos,MaxYPos,Srs
alert("CreateFeatureCursor:" + state);
}
//获取下一个要素
function GetNextFeature() {
var feature = map.CreateFeature();//创建要素对象
var bRet = polygoneditLayer.GetNextFeature(feature);//获取游标下一个要素
if(bRet) {
var type = feature.GetGeometryType();//获取要素的几何类型
var points = feature.GetPoints();//获取要素的几何点集
var featureSchema = polygoneditLayer.GetFeatureSchema();//获取要素属性表头
var id = feature.GetFeatureId();//获取要素ID
alert("id=" + id + " | type=" + type + "\npoints=" + points +"\nfeatureSchema=" + featureSchema);
var attTable = featureSchema.split(";");
var resultValue = "";
for(var i = 0; i < attTable.length-1; i++){
var tmp = attTable[i].split(":");
var value = addFeature.GetAttributeValueByName(tmp[0]); //获取要素属性字段对应属性值
resultValue += tmp[0] + "=" + value+";";
}
alert(resultValue);
}
}
//清空游标
function ClearFeatureCursor() {
var state = polygoneditLayer.ClearFeatureCursor();//清空游标
alert("ClearFeatureCursor:" + state);
}
//************************
//要素编辑操作
//************************
//创建矢量编辑
var vectorEditResp;
function CreateVectorEdit() {
var pOption = map.CreateResponserOptions("123");//创建响应器配置,参数任意名称
pOption.AddConfig("PickLayerId", polygoneditLayer.GetLayerID());//拾取图层id
pOption.AddConfig("IsOpenZChanged", "true");//是否开启Z值变化(高度)
vectorEditResp = map.CreateResponser("VectorEditResponser", pOption);//创建矢量编辑响应器,第一参必须为VectorEditResponser字符串
vectorEditResp.AddObserver();
map.AddResponser(vectorEditResp);//添加响应器
vectorEditResp.SetEnabled(true);//开始矢量编辑
map.RemoveResponser("PickVectorResponser");//开启编辑后,需要移除拾取响应器,否则在编辑完成之后会爆机
pickResp = null;
}
//移除矢量编辑
function RemoveVectorEdit() {
map.RemoveResponser("VectorEditResponser");//移除响应器
}
//更新要素点位置
function UpdateEditResponse() {
var pOption = map.CreateResponserOptions("123");//创建响应器配置,参数任意名称
pOption.AddConfig("Point", responseStr);//点坐标
pOption.AddConfig("XMove", "0.000001");//X方向移动量(经度)
pOption.AddConfig("YMove", "0.000001");//Y方向移动量(纬度)
pOption.AddConfig("ZMove", "10.0");//Z方向移动量(高程值m)
vectorEditResp.UpdateResponserOptions(pOption);//更新响应器配置项
}
//设置矢量编辑类型
function SetEditType(type) {
var iTmp = polygoneditLayer.GetEditType();//获取当前编辑模式
polygoneditLayer.SetEditType(type);//设置编辑模式(1:要素整体移动, 2:顶点添加, 3:顶点拾取, 4:顶点移动, 5:顶点删除)
}
//保存矢量编辑
function SaveVectorEdit() {
var ret = polygoneditLayer.SaveVectorEdit();//保存矢量编辑,如果需要保存最终结果,需要再次调用矢量图层保存功能
RemoveVectorEdit();//移除矢量编辑
}
//取消矢量编辑
function CancelVectorEdit() {
var ret = polygoneditLayer.CancelVectorEdit();//取消矢量编辑
RemoveVectorEdit();//移除矢量编辑
}
C++调用
BaseObjectCOMLib::ILayerObjectPtr shpLayer;//矢量图层对象
BaseObjectCOMLib::IFeatureModelLayerPtr polygoneditLayer;//要素图层对象
BaseObjectCOMLib::IResponserObjectPtr pickResp;//拾取响应器对象
BaseObjectCOMLib::IResponserObjectPtr vectorEditResp;//矢量编辑响应器对象
ConfigOptionsCOMLib::IFeaturePtr addFeature;//要素对象
//加载点矢量图层
void Cf3c3VectorEditDlg::createPointShpLayer()
{
//1.创建点符号
ConfigOptionsCOMLib::ISymbolObjectPtr pSymbol=map->CreateSymbol("PointSymbol");//创建符号对象,PointSymbol代表点符号
pSymbol->AddConfig("Size", "5");//点大小,(0-10)
pSymbol->AddConfig("Color", "1.0,1.0,0.0,1.0");//颜色值(RGBA,各值取值范围0-1之间)
//2.创建样式对象
ConfigOptionsCOMLib::IStylePtr pStyle = map->CreateStyle("style");//创建样式
pStyle->SetName("point");//设置样式名称为point
pStyle->AddSymbol("PointSymbol", pSymbol->GetConfig()); //将符号对象添加到样式,第一参数为符号类型
pStyle->AddFilterName("BuildGeometryFilter"); //添加构建器名称.几何构建器为:BuildGeometryFilter
//3创建样式表
ConfigOptionsCOMLib::IStyleSheetPtr styleSheet = map->CreateStyleSheet();//创建样式表
styleSheet->AddStyle(pStyle->GetConfig());//将样式配置添加至样式表
//4创建图层
ConfigOptionsCOMLib::ILayerOptionsPtr tlo = (ConfigOptionsCOMLib::ILayerOptionsPtr)map->CreateLayerOptions("shp"); //创建图层配置对象,名称任意
tlo->AddConfig("LayerOptionsName", "FeatureModelLayerOptions");//图层配置对象名称, FeatureModelLayerOptions代表矢量数据配置
tlo->AddConfig("DataSourceTypeName", "fmgeom");//数据源类型,代表fmgeom插件
tlo->AddConfig("Driver", "ESRI Shapefile"); //ogr数据的解析驱动
tlo->AddConfig("FeatureSourceType", "ogr"); //要素数据源类型,本地数据为ogr
tlo->AddConfig("Url", "E://testPoint.shp");//数据路径
tlo->AddConfig("Fields", "ID:Int:100:0,Name:String:100:0,Height:Double:100:3,Width:Float:100:3");//创建矢量的属性字段,属性名:属性类型:类型长度:小数点后几位
tlo->AddConfig("GeometryType", "Polygon"); ////几何类型。Point-点;Polyline-线;Polygon-面.此项配置不能少或字符串一定不能错误,否则保存文件不成功
tlo->AddConfig("TileSizeFactor", "1.0"); //瓦片大小的影响因子,建议是1.0
tlo->AddConfig("TileSize", "1000"); //瓦片大小,根据最大显示范围设置,一般为MaxRange的1/10左右
tlo->AddConfig("LiftUp", "0"); //图层抬升高度,正值为抬高,负值为降低
tlo->AddConfig("MaxRange", "10000.0"); // 最大显示范围,大于最小显示范围-无穷大
tlo->AddConfig("MinRange", "0.0"); // 最小显示范围,0-无穷大
tlo->AddConfig("StyleSheet", styleSheet->GetConfig());//将样式表配置添加至图层配置对象
shpLayer = map->CreateLayer("FeatureModelLayer", tlo);//创建矢量图层,第一项参数必须为FeatureModelLayer
map->AddLayer(shpLayer);//添加矢量图层
shpLayer->Locate();//图层定位
}
//加载线矢量图层
void Cf3c3VectorEditDlg::createLineShpLayer()
{
//1.创建线符号
ConfigOptionsCOMLib::ISymbolObjectPtr lSymbol=map->CreateSymbol("LineSymbol");//创建符号对象,LineSymbol代表线符号
lSymbol->AddConfig("Stipple", "-1"); // 线条类型,-1 实线 1~65535 虚线
lSymbol->AddConfig("Width", "10"); // 线宽(0-10)
lSymbol->AddConfig("Color", "0.82, 0.48, 0, 1.0"); // 颜色值(RGBA)0-1
//2.创建样式对象
ConfigOptionsCOMLib::IStylePtr lStyle = map->CreateStyle("style");//创建样式
lStyle->SetName("line"); // 设置别名line
lStyle->AddSymbol("LineSymbol", lSymbol->GetConfig()); // 将符号对象添加到样式,第一参数为符号类型
lStyle->AddFilterName("BuildGeometryFilter"); // 添加构建器名称,几何构建器为:BuildGeometryFilter
//3创建样式表
ConfigOptionsCOMLib::IStyleSheetPtr styleSheet = map->CreateStyleSheet();//创建样式表
styleSheet->AddStyle(lStyle->GetConfig());//将样式配置添加至样式表
//4创建图层
ConfigOptionsCOMLib::ILayerOptionsPtr tlo = (ConfigOptionsCOMLib::ILayerOptionsPtr)map->CreateLayerOptions("shp"); //创建图层配置对象,名称任意
tlo->AddConfig("LayerOptionsName", "FeatureModelLayerOptions");//图层配置对象名称, FeatureModelLayerOptions代表矢量数据配置
tlo->AddConfig("DataSourceTypeName", "fmgeom");//数据源类型,代表fmgeom插件
tlo->AddConfig("Driver", "ESRI Shapefile"); //ogr数据的解析驱动
tlo->AddConfig("FeatureSourceType", "ogr"); //要素数据源类型,本地数据为ogr
tlo->AddConfig("Url", "E://testLine.shp");//数据路径
tlo->AddConfig("Fields", "ID:Int:100:0,Name:String:100:0,Height:Double:100:3,Width:Float:100:3");//创建矢量的属性字段,属性名:属性类型:类型长度:小数点后几位
tlo->AddConfig("GeometryType", "Polygon"); //几何类型。Point-点;Polyline-线;Polygon-面.此项配置不能少或字符串一定不能错误,否则保存文件不成功
tlo->AddConfig("TileSizeFactor", "1.0"); //瓦片大小的影响因子,建议是1.0
tlo->AddConfig("TileSize", "1000"); //瓦片大小,根据最大显示范围设置,一般为MaxRange的1/10左右
tlo->AddConfig("LiftUp", "0"); //图层抬升高度,正值为抬高,负值为降低
tlo->AddConfig("MaxRange", "10000.0"); // 最大显示范围,大于最小显示范围-无穷大
tlo->AddConfig("MinRange", "0.0"); // 最小显示范围,0-无穷大
tlo->AddConfig("StyleSheet", styleSheet->GetConfig());//将样式表配置添加至图层配置对象
shpLayer = map->CreateLayer("FeatureModelLayer", tlo);//创建矢量图层,第一项参数必须为FeatureModelLayer
map->AddLayer(shpLayer);//添加矢量图层
shpLayer->Locate();//图层定位
}
//加载面矢量图层
void Cf3c3VectorEditDlg::createPolygonShpLayer()
{
//1.1创建面符号
ConfigOptionsCOMLib::ISymbolObjectPtr pSymbol=map->CreateSymbol("PolygonSymbol");//创建符号对象,PolygonSymbol代表面符号
pSymbol->AddConfig("Color", "0.64, 0.01, 0.97, 0.6");// 颜色值(RGBA)0-1
pSymbol->AddConfig("SurfaceStyleName", "Surface");
//1.2创建线符号对象
ConfigOptionsCOMLib::ISymbolObjectPtr lSymbol=map->CreateSymbol("LineSymbol");//创建符号对象,LineSymbol代表线符号
lSymbol->AddConfig("Stipple", "-1"); // 线条类型,-1 实线 1~65535 虚线
lSymbol->AddConfig("Width", "2"); // 线宽(0-10)
lSymbol->AddConfig("Color", "0.42, 0.65, 0.96, 1.0"); // 颜色值(RGBA)0-1
//2.创建样式对象
ConfigOptionsCOMLib::IStylePtr pStyle = map->CreateStyle("style");//创建样式
pStyle->SetName("polygon"); // 设置别名polygon
pStyle->AddSymbol("PolygonSymbol", pSymbol->GetConfig()); // 将符号对象添加到样式,第一参数为符号类型
pStyle->AddSymbol("LineSymbol", lSymbol->GetConfig()); // 将符号对象添加到样式,第一参数为符号类型
pStyle->AddFilterName("BuildGeometryFilter"); // 添加构建器名称,几何构建器为:BuildGeometryFilter
//3创建样式表
ConfigOptionsCOMLib::IStyleSheetPtr styleSheet = map->CreateStyleSheet();//创建样式表
styleSheet->AddStyle(pStyle->GetConfig());//将样式配置添加至样式表
//4创建图层
ConfigOptionsCOMLib::ILayerOptionsPtr tlo = (ConfigOptionsCOMLib::ILayerOptionsPtr)map->CreateLayerOptions("shp"); //创建图层配置对象,名称任意
tlo->AddConfig("LayerOptionsName", "FeatureModelLayerOptions");//图层配置对象名称, FeatureModelLayerOptions代表矢量数据配置
tlo->AddConfig("DataSourceTypeName", "fmgeom");//数据源类型,代表fmgeom插件
tlo->AddConfig("Driver", "ESRI Shapefile"); //ogr数据的解析驱动
tlo->AddConfig("FeatureSourceType", "ogr"); //要素数据源类型,本地数据为ogr
tlo->AddConfig("Url", "E://testPolygon.shp");//数据路径
tlo->AddConfig("Fields", "ID:Int:100:0,Name:String:100:0,Height:Double:100:3,Width:Float:100:3");//创建矢量的属性字段,属性名:属性类型:类型长度:小数点后几位
tlo->AddConfig("GeometryType", "Polygon"); //几何类型。Point-点;Polyline-线;Polygon-面.此项配置不能少或字符串一定不能错误,否则保存文件不成功
tlo->AddConfig("TileSizeFactor", "1.0"); //瓦片大小的影响因子,建议是1.0
tlo->AddConfig("TileSize", "1000"); //瓦片大小,根据最大显示范围设置,一般为MaxRange的1/10左右
tlo->AddConfig("LiftUp", "0"); //图层抬升高度,正值为抬高,负值为降低
tlo->AddConfig("MaxRange", "10000.0"); // 最大显示范围,大于最小显示范围-无穷大
tlo->AddConfig("MinRange", "0.0"); // 最小显示范围,0-无穷大
tlo->AddConfig("StyleSheet", styleSheet->GetConfig());//将样式表配置添加至图层配置对象
shpLayer = map->CreateLayer("FeatureModelLayer", tlo);//创建矢量图层,第一项参数必须为FeatureModelLayer
map->AddLayer(shpLayer);//添加矢量图层
shpLayer->Locate();//图层定位
}
//鼠标左键弹起事件
void Cf3c3VectorEditDlg::FireOnLButtonUpVpsdkctrl1(short xPos, short yPos)
{
if(addState)
{
CoordinateSystemCOMLib::ITransformatePtr transformate = map->CreateTransformation();
BaseObjectCOMLib::IPositionPtr pos = transformate->ScreenPosToWorldPos(xPos, yPos);//将屏幕坐标点转换成经纬度坐标
addFeature->AddPoint(pos->GetX(), pos->GetY(), pos->GetZ());////向编辑图层添加坐标点信息
}
}
void Cf3c3VectorEditDlg::FireOnResponserNotifyVpsdkctrl1(LPCTSTR respType, long notifyType)
{
ConfigOptionsCOMLib::IResponserOptionPtr opt = pickResp->GetResponserResult();
responseStr = opt->GetConfigValueByKey("PickPointList"); //获取经纬度列表
std::string PickLayerList = opt->GetConfigValueByKey("PickLayerList"); //获取图层ID
std::cout << "Point:" << responseStr << "\n"<< "PickLayerList: " << PickLayerList<< std::endl;
}
//获取矢量类型
void Cf3c3VectorEditDlg::OnCbnSelchangeCombo1()
{
CString m_paramname;
m_combo.GetLBText(m_combo.GetCurSel(),m_paramname);
value = CT2A(m_paramname.GetBuffer());
}
//下拉框内容
void Cf3c3VectorEditDlg::OnCbnSetfocusCombo1()
{
m_combo.ResetContent();//清空下拉框
m_combo.AddString(L"点矢量");
m_combo.AddString(L"线矢量");
m_combo.AddString(L"面矢量");
m_combo.SetCurSel(0);
}
//创建矢量图层
void Cf3c3VectorEditDlg::OnBnClickedcreatelayer()
{
if(shpLayer)
{
return;
}
if(value == "点矢量")
{
createPointShpLayer();
FeatureType =1;
}
else if(value == "线矢量")
{
createLineShpLayer();
FeatureType =2;
}
else if(value == "面矢量")
{
createPolygonShpLayer();
FeatureType =4;
}
polygoneditLayer = map->GetFeatureModelLayer(shpLayer->GetLayerID()); ////获取要素图层对象
}
//矢量图层移除
void Cf3c3VectorEditDlg::OnBnClickedremovelayer()
{
if(!shpLayer)
{
return;
}
map->RemoveLayer(shpLayer);//删除图层
shpLayer = NULL;
polygoneditLayer = NULL;
}
//保存图层
void Cf3c3VectorEditDlg::OnBnClickedpolygonlayersave()
{
polygoneditLayer->SaveLayer();
}
//另存图层
void Cf3c3VectorEditDlg::OnBnClickedpolygonlayerassave()
{
polygoneditLayer->SaveAsLayer("F:\\test.shp"); //编辑图层另保存。用于多次打开后保存
}
//************************
//要素添加
//************************
//创建要素
void Cf3c3VectorEditDlg::OnBnClickedCreatefeature()
{
addFeature = map->CreateFeature();//创建要素对象
addFeature->SetGeometryType(FeatureType);//设置要素几何类型(1:点; 2:线; 3:环; 4:面; 5:多结构)
addFeature->SetComponentType(1);//创建子几何类型(当GeometryType为5时生效)
addFeature->AddAttribute("Height", "43.5", 4);//添加属性值(1:int; 2:long; 3:float; 4:double; 5:string; 6:bool)
addFeature->AddAttribute("Name", "测试", 5);//添加属性值
addFeature->AddAttribute("Width", "54", 3);//添加属性值
addFeature->AddAttribute("ID", "1", 1);//添加属性值
addState = TRUE;//通过设置该状态,可以在鼠标左键事件中将顶点信息加入要素中
}
//添加要素
void Cf3c3VectorEditDlg::OnBnClickedAddfeature()
{
featureId = polygoneditLayer->GetMaxFeatureID() + 1;//获取矢量图层要素最大ID
addFeature->SetFeatureId(featureId); //设置FeatureID
polygoneditLayer->AddFeature(addFeature);//添加到矢量图层
addState = FALSE;
}
//************************
//要素拾取操作
//************************
//创建要素拾取
void Cf3c3VectorEditDlg::OnBnClickedCreatepickpolygon()
{
if(pickResp)
{
return;
}
std::stringstream stream;
stream << polygoneditLayer->GetLayerID();
ConfigOptionsCOMLib::IResponserOptionPtr pOption = map->CreateResponserOptions("");//创建响应器配置,参数任意名称
pOption->AddConfig("PickLayerIdList", stream.str().c_str()); //拾取图层id
pOption->AddConfig("PickColor", "1.0,0,0,0.3");//拾取颜色
pOption->AddConfig("IsChangeColor", "true");//是否变色
pickResp = map->CreateResponser("PickVectorResponser", pOption);//创建矢量拾取响应器,第一参必须为PickVectorResponser字符串
pickResp->AddObserver();//添加监听
map->AddResponser(pickResp);//添加响应器
}
//移除要素拾取
void Cf3c3VectorEditDlg::OnBnClickedRemovepickpolygon()
{
map->RemoveResponser("PickVectorResponser");//移除响应器
pickResp = NULL;
}
//删除拾取的要素
void Cf3c3VectorEditDlg::OnBnClickedRemovefeature()
{
polygoneditLayer->DeleteFeature();//删除要素
}
//通过拾取获取要素
void Cf3c3VectorEditDlg::OnBnClickedGetfeaturebypick()
{
if(!pickResp)
{
return;
}
if(!addFeature)
{
addFeature = map->CreateFeature();//创建要素对象
}
addFeature->ClearFeature();//创建清空要素信息
polygoneditLayer->GetFeatureByPick(addFeature); //通过ID获取要素对象值
short type = addFeature->GetGeometryType();//获取要素的几何类型
short Ctype = addFeature->GetComponentType();//获取要素的子几何类型
std::string points = addFeature->GetPoints();//获取要素的几何点集
std::string featureSchema = polygoneditLayer->GetFeatureSchema();//获取要素属性表头
long id = addFeature->GetFeatureId();//获取要素ID
std::cout << "id:" << id << "\n"<< "type: " << type<< std::endl;
std::cout << "Ctype:" << Ctype << "\n"<< "points: " << points<< std::endl;
std::cout << "featureSchema:" << featureSchema << std::endl;
std::vector<std::string> attTable,tmp;
SplitString(featureSchema,attTable,";");
for(int i = 0; i < attTable.size(); i++){
SplitString(attTable[i],tmp,":");
std::string value = addFeature->GetAttributeValueByName(tmp[0].c_str());//获取要素属性字段对应属性值
std::cout << tmp[0] <<"=" << value << std::endl;
tmp.clear();
}
}
//根据oldId更新要素
void Cf3c3VectorEditDlg::OnBnClickedUpdatefeaturebyid()
{
if(!addFeature)
{
addFeature = map->CreateFeature();//创建要素对象
}
short type = addFeature->GetGeometryType();//获取要素的几何类型
std::string points = addFeature->GetPoints();//获取要素的几何点集
long id = addFeature->GetFeatureId();//获取要素ID
std::string Name = addFeature->GetAttributeValueByName("Name"); //获取要素属性值
ConfigOptionsCOMLib::IFeaturePtr NewFeature = map->CreateFeature(); //创建要素对象
NewFeature->SetGeometryType(type); //设置要素几何类型(1:点; 2:线; 3:环; 4:面; 5:多结构)
NewFeature->SetComponentType(1); //设置子几何类型(当GeometryType为5时生效)
NewFeature->AddPoints(points.c_str()); //添加坐标点信息
NewFeature->AddAttribute("Height", "100", 4);//添加属性值(1:int; 2:long; 3:float; 4:double; 5:string; 6:bool)
NewFeature->AddAttribute("Name", "更新了", 5);//添加属性值
NewFeature->AddAttribute("Width", "54", 3);//添加属性值
NewFeature->AddAttribute("ID", "10", 1);//添加属性值
long featureId = polygoneditLayer->GetMaxFeatureID();//获取矢量图层要素最大ID
NewFeature->SetFeatureId(featureId + 1); //设置FeatureID
polygoneditLayer->UpdateFeatureById(id, NewFeature); //NewFeature若为NULL则为删除id为某值的要素
}
//根据oldId删除要素
void Cf3c3VectorEditDlg::OnBnClickedDeletefeaturebyid()
{
ConfigOptionsCOMLib::IFeaturePtr NewFeature = map->CreateFeature();//创建要素对象
polygoneditLayer->UpdateFeatureById(addFeature->GetFeatureId(), NewFeature); //NewFeature若为NULL则为删除id为某值的要素
}
//经纬度删除要素
void Cf3c3VectorEditDlg::OnBnClickedRemovefeturebygeo()
{
if (responseStr.empty()) {
std::cout << "pick point is null!:" << std::endl;
}
//对responseStr进行处理分割
std::vector<std::string> blh1,blh2,LonLatH,LonLatH1;
std::string tmp;
SplitString(responseStr,blh1,",;");
tmp =blh1[blh1.size() - 1];
SplitString(tmp,LonLatH,",");
SplitString(LonLatH[2],LonLatH1,",");
//将LonLatH转为double类型
std::vector<double> responsePoint;
std::istringstream is(LonLatH[0]);
is >> responsePoint[0];
std::istringstream is1(LonLatH[1]);
is1 >> responsePoint[1];
std::istringstream is2(LonLatH1[0]);
is2 >> responsePoint[2];
polygoneditLayer->DeleteFeatureByGeoPos(responsePoint[0], responsePoint[1], responsePoint[2]);//根据坐标删除矢量要素
}
//通过Id获取要素
void Cf3c3VectorEditDlg::OnBnClickedGetfeaturebyid()
{
if(!addFeature)
{
addFeature = map->CreateFeature();//创建要素对象
}
addFeature->ClearFeature();//创建清空要素信息
polygoneditLayer->GetFeatureById(0, addFeature);//通过ID获取要素对象值
short type = addFeature->GetGeometryType();//获取要素的几何类型
short Ctype = addFeature->GetComponentType();//获取要素的子几何类型
std::string points = addFeature->GetPoints();//获取要素的几何点集
std::string featureSchema = polygoneditLayer->GetFeatureSchema();//获取要素属性表头
long id = addFeature->GetFeatureId();//获取要素ID
std::cout << "id:" << id << "\n"<< "type: " << type<< std::endl;
std::cout << "Ctype:" << Ctype << "\n"<< "points: " << points<< std::endl;
std::cout << "featureSchema:" << featureSchema << std::endl;
std::vector<std::string> attTable,tmp;
SplitString(featureSchema,attTable,";");
for(int i = 0; i < attTable.size(); i++){
SplitString(attTable[i],tmp,":");
std::string value = addFeature->GetAttributeValueByName(tmp[0].c_str());//获取要素属性字段对应属性值
std::cout << tmp[0] <<"=" << value << std::endl;
tmp.clear();
}
}
//************************
//要素游标查询操作
//************************
//创建要素获取游标
void Cf3c3VectorEditDlg::OnBnClickedCreatefeaturecursor()
{
polygoneditLayer->CreateFeatureCursor(0, 0, 0, 0,"");//创建要素获取游标,参数含义:MinXPos,MaxXPos,MinYPos,MaxYPos,Srs
}
//获取下一个要素
void Cf3c3VectorEditDlg::OnBnClickedGetnextfeature()
{
ConfigOptionsCOMLib::IFeaturePtr feature = map->CreateFeature(); //创建要素对象
bool bRet = polygoneditLayer->GetNextFeature(feature);//获取游标下一个要素
if(bRet)
{
short type = feature->GetGeometryType();//获取要素的几何类型
std::string points = feature->GetPoints();//获取要素的几何点集
std::string featureSchema = polygoneditLayer->GetFeatureSchema();//获取要素属性表头
long id = feature->GetFeatureId();//获取要素ID
std::cout << "id:" << id << "\n"<< "type: " << type<< std::endl;
std::cout <<"points: " << points<< std::endl;
std::cout << "featureSchema:" << featureSchema << std::endl;
}
}
//清空游标
void Cf3c3VectorEditDlg::OnBnClickedClearfeaturecursor()
{
polygoneditLayer->ClearFeatureCursor();//清空游标
}
//************************
//要素编辑操作
//************************
//创建矢量编辑
void Cf3c3VectorEditDlg::OnBnClickedCreatevectoredit()
{
ConfigOptionsCOMLib::IResponserOptionPtr pOption = map->CreateResponserOptions("123");//创建响应器配置,参数任意名称
std::stringstream stream;
stream << polygoneditLayer->GetLayerID();
pOption->AddConfig("PickLayerId",stream.str().c_str());//拾取图层id
pOption->AddConfig("IsOpenZChanged", "true");//是否开启Z值变化(高度)
vectorEditResp = map->CreateResponser("VectorEditResponser", pOption);//创建矢量编辑响应器,第一参必须为VectorEditResponser字符串
map->AddResponser(vectorEditResp);//添加响应器
vectorEditResp->SetEnabled(true);//开始矢量编辑
map->RemoveResponser("PickVectorResponser");//开启编辑后,需要移除拾取响应器,否则在编辑完成之后会爆机
pickResp = NULL;
}
//关闭矢量编辑
void Cf3c3VectorEditDlg::OnBnClickedRemovevectoredit()
{
map->RemoveResponser("VectorEditResponser");//移除响应器
}
//添加顶点
void Cf3c3VectorEditDlg::OnBnClickedSetedittype2()
{
SetEditType(2);
}
//拾取
void Cf3c3VectorEditDlg::OnBnClickedSetedittype3()
{
SetEditType(3);
}
//移动
void Cf3c3VectorEditDlg::OnBnClickedSetedittype4()
{
SetEditType(4);
}
//删除
void Cf3c3VectorEditDlg::OnBnClickedSetedittype5()
{
SetEditType(5);
}
//整体移动
void Cf3c3VectorEditDlg::OnBnClickedSetedittype1()
{
SetEditType(1);
}
//顶点更新
void Cf3c3VectorEditDlg::OnBnClickedUpdateeditresponse()
{
ConfigOptionsCOMLib::IResponserOptionPtr pOption = map->CreateResponserOptions("123");//创建响应器配置,参数任意名称
pOption->AddConfig("Point", responseStr.c_str());//点坐标
pOption->AddConfig("XMove", "0.000001");//X方向移动量(经度)
pOption->AddConfig("YMove", "0.000001");//Y方向移动量(纬度)
pOption->AddConfig("ZMove", "10.0");//Z方向移动量(高程值m)
vectorEditResp->UpdateResponserOptions(pOption);//更新响应器配置项
}
//保存矢量编辑
void Cf3c3VectorEditDlg::OnBnClickedSavevectoredit()
{
polygoneditLayer->SaveVectorEdit();//保存矢量编辑,如果需要保存最终结果,需要再次调用矢量图层保存功能
map->RemoveResponser("VectorEditResponser");//移除响应器
}
//取消矢量编辑
void Cf3c3VectorEditDlg::OnBnClickedCancelvectoredit()
{
polygoneditLayer->CancelVectorEdit();//取消矢量编辑
map->RemoveResponser("VectorEditResponser");//移除响应器
}
C#调用
ILayerObject shpLayer = null;//矢量图层
IResponserObject pickResp = null;//矢量拾取对象
IFeatureModelLayer polygoneditLayer;//要素模型图层对象
bool addState = false;//要素创建开关
int featureId = null;//要素ID
short FeatureType = 4; //要素几何类型(1:点; 2:线; 3:环; 4:面; 5:多结构)
IFeature addFeature;
string responseStr;//点位坐标
//加载点矢量
private void createPointShpLayer()
{
if (shpLayer == null)
{
//1.创建点符号对象
ISymbolObject pSymbol = map.CreateSymbol("PointSymbol");//创建符号对象,PointSymbol代表点符号
pSymbol.AddConfig("Size", "5");//点大小,(0-10)
pSymbol.AddConfig("Color", "1.0,1.0,0.0,1.0");//颜色值(RGBA,各值取值范围0-1之间)
//2.创建样式对象
IStyle pStyle = map.CreateStyle("style");//创建样式
pStyle.SetName("point");//设置别名point
pStyle.AddSymbol("PointSymbol", pSymbol.GetConfig()); //将符号对象添加到样式,第一参数为符号类型
pStyle.AddFilterName("BuildGeometryFilter"); //添加构建器名称.几何构建器为:BuildGeometryFilter
//3.创建样式表对象
IStyleSheet styleSheet = map.CreateStyleSheet(); // 创建样式表
styleSheet.AddStyle(pStyle.GetConfig()); //将样式对象配置添加至样式表
//4.创建并添加图层对象
ILayerOptions tlo = map.CreateLayerOptions("shp");//创建图层配置对象,名称任意
tlo.AddConfig("LayerOptionsName", "FeatureModelLayerOptions");//图层配置对象名称, FeatureModelLayerOptions代表矢量数据配置
tlo.AddConfig("DataSourceTypeName", "fmgeom");//数据源类型,代表fmgeom插件
tlo.AddConfig("Driver", "ESRI Shapefile"); //ogr数据的解析驱动
tlo.AddConfig("FeatureSourceType", "ogr"); //要素数据源类型,本地数据为ogr
tlo.AddConfig("Url", "E://testPoint.shp");//数据路径
tlo.AddConfig("Fields", "ID:Int:100:0,Name:String:100:0,Height:Double:100:3,Width:Float:100:3");//创建矢量的属性字段,属性名:属性类型:类型长度:小数点后几位
tlo.AddConfig("GeometryType", "Point"); ////几何类型。Point-点;Polyline-线;Polygon-面.此项配置不能少或字符串一定不能错误,否则保存文件不成功
tlo.AddConfig("TileSizeFactor", "1.0"); //瓦片大小的影响因子,建议是1.0
tlo.AddConfig("TileSize", "1000"); //瓦片大小,根据最大显示范围设置,一般为MaxRange的1/10左右
tlo.AddConfig("LiftUp", "0"); //图层抬升高度,正值为抬高,负值为降低
tlo.AddConfig("MaxRange", "10000.0"); // 最大显示范围,大于最小显示范围-无穷大
tlo.AddConfig("MinRange", "0.0"); // 最小显示范围,0-无穷大
tlo.AddConfig("StyleSheet", styleSheet.GetConfig()); //将样式表配置添加至图层配置对象
shpLayer = map.CreateLayer("FeatureModelLayer", tlo); //创建矢量图层,第一项参数为FeatureModelLayer
map.AddLayer(shpLayer); //添加图层
int id = shpLayer.GetLayerID(); ////获取图层id
polygoneditLayer = map.GetFeatureModelLayer(id); ////获取要素图层对象
//pointShpLayer.Locate(); //矢量图层定位
}
else
{
MessageBox.Show("请勿重复创建矢量图层!");
}
}
//加载线矢量
private void createLineShpLayer()
{
if (shpLayer == null)
{
//1.创建线符号对象
ISymbolObject lSymbol = map.CreateSymbol("LineSymbol"); // 创建符号对象,LineSymbol代表线符号
lSymbol.AddConfig("Stipple", "-1"); // 线条类型,-1 实线 1~65535 虚线
lSymbol.AddConfig("Width", "10"); // 线宽(0-10)
lSymbol.AddConfig("Color", "0.82, 0.48, 0, 1.0"); // 颜色值(RGBA)0-1
//2.创建样式对象
IStyle lStyle = map.CreateStyle("style"); // 创建样式
lStyle.SetName("line"); // 设置别名line
lStyle.AddSymbol("LineSymbol", lSymbol.GetConfig()); // 将符号对象添加到样式,第一参数为符号类型
lStyle.AddFilterName("BuildGeometryFilter"); // 添加构建器名称,几何构建器为:BuildGeometryFilter
//3.创建样式表对象
IStyleSheet styleSheet = map.CreateStyleSheet(); // 创建样式表
styleSheet.AddStyle(lStyle.GetConfig()); // 将样式配置添加至样式表
//4.创建并添加图层对象
ILayerOptions tlo = map.CreateLayerOptions("shp");//创建图层配置对象,名称任意
tlo.AddConfig("LayerOptionsName", "FeatureModelLayerOptions");//图层配置对象名称, FeatureModelLayerOptions代表矢量数据配置
tlo.AddConfig("DataSourceTypeName", "fmgeom");//数据源类型,代表fmgeom插件
tlo.AddConfig("Driver", "ESRI Shapefile"); //ogr数据的解析驱动
tlo.AddConfig("FeatureSourceType", "ogr"); //要素数据源类型,本地数据为ogr
tlo.AddConfig("Url", "E://testLine.shp");//数据路径
tlo.AddConfig("Fields", "ID:Int:100:0,Name:String:100:0,Height:Double:100:3,Width:Float:100:3");//创建矢量的属性字段,属性名:属性类型:类型长度:小数点后几位
tlo.AddConfig("GeometryType", "Polygon"); ////几何类型。Point-点;Polyline-线;Polygon-面.此项配置不能少或字符串一定不能错误,否则保存文件不成功
tlo.AddConfig("TileSizeFactor", "1.0"); //瓦片大小的影响因子,建议是1.0
tlo.AddConfig("TileSize", "1000"); //瓦片大小,根据最大显示范围设置,一般为MaxRange的1/10左右
tlo.AddConfig("LiftUp", "0"); //图层抬升高度,正值为抬高,负值为降低
tlo.AddConfig("MaxRange", "10000.0"); // 最大显示范围,大于最小显示范围-无穷大
tlo.AddConfig("MinRange", "0.0"); // 最小显示范围,0-无穷大
tlo.AddConfig("StyleSheet", styleSheet.GetConfig()); //将样式表配置添加至图层配置对象
shpLayer = map.CreateLayer("FeatureModelLayer", tlo); //创建矢量图层,第一项参数为FeatureModelLayer
map.AddLayer(shpLayer); //添加图层
int id = shpLayer.GetLayerID(); ////获取图层id
polygoneditLayer = map.GetFeatureModelLayer(id); ////获取要素图层对象
shpLayer.Locate(); //矢量图层定位
}
else
{
MessageBox.Show("请勿重复创建矢量图层!");
}
}
//创建面矢量图层
private void createPolygonShpLayer()
{
if (shpLayer == null)
{
//1.1创建面符号对象
ISymbolObject pSymbol = map.CreateSymbol("PolygonSymbol"); // 创建符号对象,PolygonSymbol代表面符号
pSymbol.AddConfig("Color", "0.64, 0.01, 0.97, 0.6");// 颜色值(RGBA)0-1
pSymbol.AddConfig("SurfaceStyleName", "Surface"); //
//1.2创建线符号对象
ISymbolObject lSymbol = map.CreateSymbol("LineSymbol"); // 创建符号对象,LineSymbol代表线符号
lSymbol.AddConfig("Stipple", "-1"); // 线条类型,-1 实线 1~65535 虚线
lSymbol.AddConfig("Width", "2"); // 线宽(0-10)
lSymbol.AddConfig("Color", "0.42, 0.65, 0.96, 1.0"); // 颜色值(RGBA)0-1
//2.创建样式对象
IStyle pStyle = map.CreateStyle("style"); // 创建样式
pStyle.SetName("polygon"); // 设置别名polygon
pStyle.AddSymbol("PolygonSymbol", pSymbol.GetConfig()); // 将符号对象添加到样式,第一参数为符号类型
pStyle.AddSymbol("LineSymbol", lSymbol.GetConfig()); // 将符号对象添加到样式,第一参数为符号类型
pStyle.AddFilterName("BuildGeometryFilter"); // 添加构建器名称,几何构建器为:BuildGeometryFilter
//3.创建样式表对象
IStyleSheet styleSheet = map.CreateStyleSheet(); // 创建样式表
styleSheet.AddStyle(pStyle.GetConfig()); // 将样式配置添加至样式表
//4.创建并添加图层对象
ILayerOptions tlo = map.CreateLayerOptions("shp");//创建图层配置对象,名称任意
tlo.AddConfig("LayerOptionsName", "FeatureModelLayerOptions");//图层配置对象名称, FeatureModelLayerOptions代表矢量数据配置
tlo.AddConfig("DataSourceTypeName", "fmgeom");//数据源类型,代表fmgeom插件
tlo.AddConfig("Driver", "ESRI Shapefile"); //ogr数据的解析驱动
tlo.AddConfig("FeatureSourceType", "ogr"); //要素数据源类型,本地数据为ogr
tlo.AddConfig("Url", "E://testPolygon.shp");//数据路径
tlo.AddConfig("Fields", "ID:Int:100:0,Name:String:100:0,Height:Double:100:3,Width:Float:100:3");//创建矢量的属性字段,属性名:属性类型:类型长度:小数点后几位
tlo.AddConfig("GeometryType", "Polygon"); ////几何类型。Point-点;Polyline-线;Polygon-面.此项配置不能少或字符串一定不能错误,否则保存文件不成功
tlo.AddConfig("TileSizeFactor", "1.0"); //瓦片大小的影响因子,建议是1.0
tlo.AddConfig("TileSize", "1000"); //瓦片大小,根据最大显示范围设置,一般为MaxRange的1/10左右
tlo.AddConfig("LiftUp", "0"); //图层抬升高度,正值为抬高,负值为降低
tlo.AddConfig("MaxRange", "10000.0"); // 最大显示范围,大于最小显示范围-无穷大
tlo.AddConfig("MinRange", "0.0"); // 最小显示范围,0-无穷大
tlo.AddConfig("IsHasTexture", "false");//是否有纹理,默认为false,该参数在进行分析时起效
tlo.AddConfig("StyleSheet", styleSheet.GetConfig()); //将样式表配置添加至图层配置对象
shpLayer = map.CreateLayer("FeatureModelLayer", tlo); //创建矢量图层,第一项参数为FeatureModelLayer
map.AddLayer(shpLayer); //添加图层
int id = shpLayer.GetLayerID(); ////获取图层id
polygoneditLayer = map.GetFeatureModelLayer(id); ////获取要素图层对象
shpLayer.Locate(); //矢量图层定位
}
else
{
MessageBox.Show("请勿重复创建矢量图层!");
}
}
//矢量图层创建
private void createLayer_Click(object sender, EventArgs e)
{
Console.WriteLine("Text:" + comboBox1.SelectedItem.ToString());
switch (comboBox1.SelectedItem.ToString())
{
case "点矢量":
createPointShpLayer();
FeatureType = 1;
break;
case "线矢量":
createLineShpLayer();
FeatureType = 2;
break;
case "面矢量":
createPolygonShpLayer();
FeatureType = 4;
break;
}
}
//矢量图层移除
private void removeLayer_Click(object sender, EventArgs e)
{
if (shpLayer != null)
{
map.RemoveLayer(shpLayer);//删除矢量图层
polygoneditLayer = null;
shpLayer = null;
}
else
{
MessageBox.Show("请勿重复移除图层!");
}
}
//************************
//要素添加
//************************
//创建要素
private void CreateFeature_Click(object sender, EventArgs e)
{
addFeature = map.CreateFeature();//创建要素对象
addFeature.SetGeometryType(FeatureType);//设置要素几何类型(1:点; 2:线; 3:环; 4:面; 5:多结构)
addFeature.SetComponentType(1);//创建子几何类型(当GeometryType为5时生效)
addFeature.AddAttribute("Height", "43.5", 4);//添加属性值(1:int; 2:long; 3:float; 4:double; 5:string; 6:bool)
addFeature.AddAttribute("Name", "测试", 5);//添加属性值
addFeature.AddAttribute("Width", "54", 3);//添加属性值
addFeature.AddAttribute("ID", "1", 1);//添加属性值
addState = true;//通过设置该状态,可以在鼠标左键事件中将顶点信息加入要素中
}
//添加要素
private void AddFeature_Click(object sender, EventArgs e)
{
featureId = polygoneditLayer.GetMaxFeatureID() + 1;//获取矢量图层要素最大ID
addFeature.SetFeatureId(featureId); //设置FeatureID
polygoneditLayer.AddFeature(addFeature);//添加到矢量图层
addState = false;
}
//保存图层
private void polygonLayerSave_Click(object sender, EventArgs e)
{
polygoneditLayer.SaveLayer(); //编辑图层保存,一般用于首次创建保存
}
//另存图层
private void polygonLayerAsSave_Click(object sender, EventArgs e)
{
polygoneditLayer.SaveAsLayer("F:\\test" + FeatureType + ".shp"); //编辑图层另保存。用于多次打开后保存
}
//************************
//要素拾取操作
//************************
//创建要素拾取
private void CreatePickPolygon_Click(object sender, EventArgs e)
{
if (pickResp == null)
{
IResponserOption pOption = map.CreateResponserOptions("123"); //创建响应器配置,参数任意名称
pOption.AddConfig("PickLayerIdList", polygoneditLayer.GetLayerID().ToString()); //拾取图层id
pOption.AddConfig("PickColor", "1.0,0,0,0.3"); //拾取颜色
pOption.AddConfig("IsChangeColor", "true"); //是否变色
pickResp = map.CreateResponser("PickVectorResponser", pOption); //创建矢量拾取响应器,第一参必须为PickVectorResponser字符串
pickResp.AddObserver();
map.AddResponser(pickResp); //添加响应器
}
else
{
MessageBox.Show("图层存在,无需创建");
}
}
//移除要素拾取
private void RemovePickPolygon_Click(object sender, EventArgs e)
{
if (pickResp != null)
{
map.RemoveResponser("PickVectorResponser"); //移除响应器
pickResp = null;
}
else
{
MessageBox.Show("图层不存在");
}
}
//删除拾取的要素
private void RemoveFeature_Click(object sender, EventArgs e)
{
polygoneditLayer.DeleteFeature(); //删除要素
}
//通过拾取获取要素
private void GetFeatureByPick_Click(object sender, EventArgs e)
{
if (pickResp!=null)
{
if (null == addFeature)
{
addFeature = map.CreateFeature(); //创建要素对象
}
addFeature.ClearFeature(); //创建清空要素信息
polygoneditLayer.GetFeatureByPick(addFeature); //通过ID获取要素对象值
short type = addFeature.GetGeometryType();//获取要素的几何类型
short Ctype = addFeature.GetComponentType();//获取要素的子几何类型
string points = addFeature.GetPoints();//获取要素的几何点集
string featureSchema = polygoneditLayer.GetFeatureSchema();//获取要素属性表头
int id = addFeature.GetFeatureId();//获取要素ID
MessageBox.Show("id=" + id + " | type=" + type + " | Ctype=" + Ctype + "\npoints=" + points + "\nfeatureSchema=" + featureSchema);
string[] attTable = featureSchema.Split(';');
var resultValue = "";
for (int i = 0; i < attTable.Length; i++)
{
var tmp = attTable[i].Split(':');
var value = addFeature.GetAttributeValueByName(tmp[0]); //获取要素属性字段对应属性值
resultValue += attTable[i] + "=" + value + ";";
}
MessageBox.Show(resultValue);
}
}
//根据Id更新要素
private void UpdateFeatureById_Click(object sender, EventArgs e)
{
if (null == addFeature)
{
addFeature = map.CreateFeature(); //创建要素对象
}
var type = addFeature.GetGeometryType(); //获取要素的几何类型
var points = addFeature.GetPoints(); //获取要素的几何点集
var id = addFeature.GetFeatureId(); //获取要素ID
var Name = addFeature.GetAttributeValueByName("Name"); //获取要素属性值
var NewFeature = map.CreateFeature(); //创建要素对象
NewFeature.SetGeometryType(type); //设置要素几何类型(1:点; 2:线; 3:环; 4:面; 5:多结构)
NewFeature.SetComponentType(1); //设置子几何类型(当GeometryType为5时生效)
NewFeature.AddPoints(points); //添加坐标点信息
NewFeature.AddAttribute("Height", "100", 4);//添加属性值(1:int; 2:long; 3:float; 4:double; 5:string; 6:bool)
NewFeature.AddAttribute("Name", "更新了", 5);//添加属性值
NewFeature.AddAttribute("Width", "54", 3);//添加属性值
NewFeature.AddAttribute("ID", "10", 1);//添加属性值
var featureId = polygoneditLayer.GetMaxFeatureID(); //获取矢量图层要素最大ID
NewFeature.SetFeatureId(featureId + 1); //设置FeatureID
var state = polygoneditLayer.UpdateFeatureById(id, NewFeature); //NewFeature若为NULL则为删除id为某值的要素
}
//根据Id删除要素
private void DeleteFeatureById_Click(object sender, EventArgs e)
{
var NewFeature = map.CreateFeature(); //创建要素对象
var state = polygoneditLayer.UpdateFeatureById(addFeature.GetFeatureId(), NewFeature); //NewFeature若为NULL则为删除id为某值的要素
}
//通过ID获取要素
private void GetFeatureById_Click(object sender, EventArgs e)
{
if (null == addFeature)
{
addFeature = map.CreateFeature(); //创建要素对象
}
addFeature.ClearFeature();//创建清空要素信
polygoneditLayer.GetFeatureById(0, addFeature);//通过ID获取要素对象值
var type = addFeature.GetGeometryType();//获取要素的几何类型
var Ctype = addFeature.GetComponentType();//获取要素的子几何类型
var points = addFeature.GetPoints();//获取要素的几何点集
var featureSchema = polygoneditLayer.GetFeatureSchema();//获取要素属性表头
var id = addFeature.GetFeatureId();//获取要素ID
MessageBox.Show("id=" + id + " | type=" + type + " | Ctype=" + Ctype + "\npoints=" + points + "\nfeatureSchema=" + featureSchema);
var attTable = featureSchema.Split(';');
var resultValue = "";
for (var i = 0; i < attTable.Length; i++)
{
var tmp = attTable[i].Split(':');
var value = addFeature.GetAttributeValueByName(tmp[0]); //获取要素属性字段对应属性值
resultValue += attTable[i] + "=" + value + ";";
}
MessageBox.Show(resultValue);
}
//************************
//要素游标查询操作
//************************
//创建要素获取游标
private void CreateFeatureCursor_Click(object sender, EventArgs e)
{
var state = polygoneditLayer.CreateFeatureCursor(0, 0, 0, 0,"");//创建要素获取游标
MessageBox.Show("CreateFeatureCursor:" + state);
}
//获取下一个要素
private void GetNextFeature_Click(object sender, EventArgs e)
{
var feature = map.CreateFeature();//创建要素对象
var bRet = polygoneditLayer.GetNextFeature(feature);//获取游标下一个要素
if (bRet)
{
var type = feature.GetGeometryType();//获取要素的几何类型
var points = feature.GetPoints();//获取要素的几何点集
var featureSchema = polygoneditLayer.GetFeatureSchema();//获取要素属性表头
var id = feature.GetFeatureId();//获取要素ID
MessageBox.Show("id=" + id + " | type=" + type + "\npoints=" + points + "\nfeatureSchema=" + featureSchema);
var attTable = featureSchema.Split(';');
var resultValue = "";
for (var i = 0; i < attTable.Length; i++)
{
var tmp = attTable[i].Split(':');
var value = addFeature.GetAttributeValueByName(tmp[0]); //获取要素属性字段对应属性值
resultValue += attTable[i] + "=" + value + ";";
}
MessageBox.Show(resultValue);
}
}
//清空游标
private void ClearFeatureCursor_Click(object sender, EventArgs e)
{
var state = polygoneditLayer.ClearFeatureCursor();//清空游标
MessageBox.Show("ClearFeatureCursor:" + state);
}
//************************
//要素拾取操作
//************************
//创建矢量编辑
private void CreateVectorEdit_Click(object sender, EventArgs e)
{
var pOption = map.CreateResponserOptions("123");//创建响应器配置,参数任意名称
pOption.AddConfig("PickLayerId", polygoneditLayer.GetLayerID());//拾取图层id
pOption.AddConfig("IsOpenZChanged", "true");//是否开启Z值变化(高度)
vectorEditResp = map.CreateResponser("VectorEditResponser", pOption);//创建矢量编辑响应器,第一参必须为VectorEditResponser字符串
vectorEditResp.AddObserver();
map.AddResponser(vectorEditResp);//添加响应器
vectorEditResp.SetEnabled(true);//开始矢量编辑
map.RemoveResponser("PickVectorResponser");//开启编辑后,需要移除拾取响应器,否则在编辑完成之后会爆机
pickResp = null;
}
//移除矢量编辑
private void RemoveVectorEdit_Click(object sender, EventArgs e)
{
map.RemoveResponser("VectorEditResponser");//移除响应器
}
//添加顶点
private void SetEditType2_Click(object sender, EventArgs e)
{
var iTmp = polygoneditLayer.GetEditType();//获取当前编辑模式
polygoneditLayer.SetEditType(2);//设置编辑模式(1:要素整体移动, 2:顶点添加, 3:顶点拾取, 4:顶点移动, 5:顶点删除)
}
//顶点拾取
private void SetEditType3_Click(object sender, EventArgs e)
{
var iTmp = polygoneditLayer.GetEditType();//获取当前编辑模式
polygoneditLayer.SetEditType(3);//设置编辑模式(1:要素整体移动, 2:顶点添加, 3:顶点拾取, 4:顶点移动, 5:顶点删除)
}
//顶点移动
private void SetEditType4_Click(object sender, EventArgs e)
{
var iTmp = polygoneditLayer.GetEditType();//获取当前编辑模式
polygoneditLayer.SetEditType(4);//设置编辑模式(1:要素整体移动, 2:顶点添加, 3:顶点拾取, 4:顶点移动, 5:顶点删除)
}
//顶点删除
private void SetEditType5_Click(object sender, EventArgs e)
{
var iTmp = polygoneditLayer.GetEditType();//获取当前编辑模式
polygoneditLayer.SetEditType(5);//设置编辑模式(1:要素整体移动, 2:顶点添加, 3:顶点拾取, 4:顶点移动, 5:顶点删除)
}
//要素整体移动
private void SetEditType1_Click(object sender, EventArgs e)
{
var iTmp = polygoneditLayer.GetEditType();//获取当前编辑模式
polygoneditLayer.SetEditType(1);//设置编辑模式(1:要素整体移动, 2:顶点添加, 3:顶点拾取, 4:顶点移动, 5:顶点删除)
}
//更新要素点位置
private void UpdateEditResponse_Click(object sender, EventArgs e)
{
var pOption = map.CreateResponserOptions("123");//创建响应器配置,参数任意名称
pOption.AddConfig("Point", responseStr);//点坐标
pOption.AddConfig("XMove", "0.000001");//X方向移动量(经度)
pOption.AddConfig("YMove", "0.000001");//Y方向移动量(纬度)
pOption.AddConfig("ZMove", "10.0");//Z方向移动量(高程值m)
vectorEditResp.UpdateResponserOptions(pOption);//更新响应器配置项
}
//保存矢量编辑
private void SaveVectorEdit_Click(object sender, EventArgs e)
{
polygoneditLayer.SaveVectorEdit();//保存矢量编辑,如果需要保存最终结果,需要再次调用矢量图层保存功能
map.RemoveResponser("VectorEditResponser");//移除响应器
}
//取消矢量编辑
private void CancelVectorEdit_Click(object sender, EventArgs e)
{
var ret = polygoneditLayer.CancelVectorEdit();//取消矢量编辑
map.RemoveResponser("VectorEditResponser");//移除响应器
}
//拾取响应器回调
private void axVPSDKCtrl1_FireOnResponserNotify(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnResponserNotifyEvent e)
{
responseStr = pickResp.GetResponserResult().GetConfigValueByKey("PickPointList"); //获取经纬度列表
var PickLayerList = pickResp.GetResponserResult().GetConfigValueByKey("PickLayerList"); //获取图层ID
MessageBox.Show("Point:" + responseStr + "; PickLayerList:" + PickLayerList);
var featureSchema = pickResp.GetResponserResult().GetConfigValueByKey("FeatureSchema"); //获取要素属性表头
var attTable = featureSchema.split(";");
var resultValue = "";
for (var i = 0; i < attTable.length-1; i++)
{
var value = pickResp.GetResponserResult().GetConfigValueByKey(attTable[i]);//获取要素属性
resultValue += attTable[i] + "=" + value + ";";
}
MessageBox.Show(resultValue);
}
//鼠标左键事件回调
private void axVPSDKCtrl1_FireOnLButtonDown(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnLButtonDownEvent e)
{
if (addState)
{
var transformate = map.CreateTransformation();
var pos = transformate.ScreenPosToWorldPos(e.xPos, e.yPos);//将屏幕坐标点转换成经纬度坐标
addFeature.AddPoint(pos.GetX(), pos.GetY(), pos.GetZ());////向编辑图层添加坐标点信息
responseStr = pos.GetX() + "," + pos.GetY() + "," + pos.GetZ() + ";";
}
}