问题:
I'm trying to use a classifier to classify the land use of Landsat images, but when I use the function which is "classifier. conflusionMatrix", I get an error as follows: Classifier confusionMatrix: Property 'type' of feature '00000000000000000014_ 0' is missing. (Error code: 3). I don't know how to correct it. I really need your help to solve this problem. By the way, I finally remembered to share the assets.
我正在尝试使用分类器对 Landsat 图像的土地利用进行分类,但是当我使用“classifier.conflusionMatrix”函数时,出现如下错误:Classifier confusionMatrix: Property 'type' of feature '000000000000000000014_ 0 ' 不见了。 (错误代码:3)。我不知道如何纠正它。我真的需要你的帮助来解决这个问题。对了,我终于记得分享资产了。
This is the link:
https://code.earthengine.google.com/d789c5e1a86b1406dbb93d5d9420d4a5?noload=1
代码:
var table = ee.FeatureCollection("users/yongxin/henan_bese"),
table3 = ee.FeatureCollection("users/yongxin/zhengzhou_sample");
//确定区域
var table = ee.FeatureCollection("users/yongxin/henan_bese");
var roi = ee.Geometry.Point([113.663221,34.7568711]);
var sCol = table.filterBounds(roi);
Map.centerObject(sCol,6);
Map.addLayer(sCol,{color:"red"},"zhengzhou");
/**
forest 0
urban 1
paddyrice 2
water 3
crop 4
*/
//iazai加载yangbendianshujv加载样本点数据
var sampleData = ee.FeatureCollection("users/yongxin/zhengzhou_sample");
Map.addLayer(sampleData, {}, "sampleData");
//Landsat8 SR数据去云
function rmCloud(image) {
var cloudShadowBitMask = (1 << 3);
var cloudsBitMask = (1 << 5);
var qa = image.select("pixel_qa");
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
.and(qa.bitwiseAnd(cloudsBitMask).eq(0));
return image.updateMask(mask);
}
//缩放
function scaleImage(image) {
var time_start = image.get("system:time_start");
image = image.multiply(0.0001);
image = image.set("system:time_start", time_start);
return image;
}
//NDVI
function NDVI(image) {
return image.addBands(
image.normalizedDifference(["B5", "B4"])
.rename("NDVI"));
}
//NDWI
function NDWI(image) {
return image.addBands(
image.normalizedDifference(["B3", "B5"])
.rename("NDWI"));
}
//NDBI
function NDBI(image) {
return image.addBands(
image.normalizedDifference(["B6", "B5"])
.rename("NDBI"));
}
//加载影像数据(未分类的Landsat影像数据)
var l8Col = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
.filterBounds(sCol)
.filterDate("2016-1-1", "2017-1-1")
.map(rmCloud)
.map(scaleImage)
.map(NDVI)
.map(NDWI)
.map(NDBI);
//DEM
var srtm = ee.Image("USGS/SRTMGL1_003");
var dem = ee.Algorithms.Terrain(srtm);
var elevation = dem.select("elevation");
var slope = dem.select("slope");
//定义波段
var bands = [
"B1", "B2", "B3", "B4", "B5", "B6", "B7",
"NDBI", "NDWI", "NDVI","SLOPE", "ELEVATION"
];
//影像处理
var l8Image = l8Col.median()
.addBands(elevation.rename("ELEVATION"))
.addBands(slope.rename("SLOPE"))
.clip(sCol)
.select(bands);
//rgb参数设置
var rgbVisParam = {
min: 0,
max: 0.3,
bands: ["B4", "B3", "B2"]
};
Map.addLayer(l8Image, rgbVisParam, "l8Image");
//切分生成训练数据和验证数据
sampleData = sampleData.randomColumn('random');
var sample_training = sampleData.filter(ee.Filter.lte("random", 0.7));
var sample_validate = sampleData.filter(ee.Filter.gt("random", 0.7));
print("sample_training", sample_training);
print("sample_validate", sample_validate);
// //生成监督分类训练使用的样本数据
// var training = l8Image.sampleRegions({
// collection: sample_training,
// properties: ["type"],
// scale: 30
// });
// //生成监督分类验证使用的样本数据
// var validation = l8Image.sampleRegions({
// collection: sample_validate,
// properties: ["type"],
// scale: 30
// });
// //初始化分类器
// var classifier = ee.Classifier.smileCart().train({
// features: training,
// classProperty: "type",
// inputProperties: bands
// });
//生成监督分类训练使用的样本数据
var training = l8Image.sampleRegions({
collection: sample_training,
properties: ["class"],
scale: 30
});
//生成监督分类验证使用的样本数据
var validation = l8Image.sampleRegions({
collection: sample_validate,
properties: ["class"],
scale: 30
});
//初始化分类器
var classifier = ee.Classifier.smileCart().train({
features: training,
classProperty: "class",
inputProperties: bands
});
//影像数据调用classify利用训练数据训练得到分类结果
var classified = l8Image.classify(classifier);
//训练结果的混淆矩阵
var trainAccuracy = classifier.confusionMatrix();
//导出训练精度结果CSV
Export.table.toDrive({
collection: ee.FeatureCollection([
ee.Feature(null, {
matrix: trainAccuracy.array(),
kappa: trainAccuracy.kappa(),
accuracy: trainAccuracy.accuracy()
}
)]),
description: "l8TrainConf_zhengzhou",
folder:"training_zhengzhou",
fileFormat: "CSV"
});
//导出影像
var resultImg = classified.clip(sCol).toByte();
resultImg = resultImg.remap([0,1,2,3,4], [1,2,3,4,5]);
Export.image.toAsset({
image: resultImg,
description: 'Asset-l8Classifiedmap_zhengzhou',
assetId: "training_zhengzhou/l8Classifiedmap_zhengzhou",
region: sCol,
scale:30,
crs: "EPSG:4326",
maxPixels: 1e13
});
//影像导出
Export.image.toDrive({
image:resultImg,
description:'Drive-l8Classifiedmap_zhengzhou',
fileNamePrefix: "l8Classifiedmap_zhengzhou",
folder:"training_zhengzhou",
region: sCol,
scale:30,
crs: "EPSG:4326",
maxPixels:1e13
});
我们根据提示可以看出,文件中不存在属性,所以我们需要查看我们打印或者输出的结果中到底有没有这个属性,可以看到这个属性中并没有“type”属性。我们要注意的是这里我们所需的不是矢量文件中每个属性,这是值得注意的,这里选择的就是分类后产生的新的分类属性。
矢量中的类型“type”代表的并不是属性列表,而是表明矢量类型就是矢量。
需要修改的代码:
修改后成功的下载结果
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
评论(0)