在写论文中,对研究区的分析我们通常会处理研究区外接矩形。GEE是对数据处理的一个很好的平台,通常研究者偏向把研究区的矢量数据传到GEE中,再在GEE中,使用GEE的数据对研究区进行处理。于是,这就存在一个问题了:如何在GEE中为自己上传的矢量文件添加外接矩形?
废话不多说,方法1:
//table是上传的数据,可以直接import。本研究的table是中国行政区的矢量图
var cd = table.filter(ee.Filter.eq('city','chengdu')).first();
var cd = table.filter(ee.Filter.eq('city','chengdu')).first();
//将cd强制转换成ee.Feature()
cd = ee.Feature(cd);
//外接矩形
var bounds = cd.bounds()
Map.addLayer(bounds,{color:'yellow'},'bounds',false);
缺陷:当然这个用到了筛选,还有其他的一些功能,但是我上传的直接就是研究区了,没有筛选。这使得我运行不出来.
下面是的实际代码:
var voi = ee.FeatureCollection("users/GISer_djq/RWEQ/Tibet_bounds");
voi = ee.Feature(voi);
var table1 = voi.bounds();
Map.addLayer(table1,{},"table1");
Map.addLayer(voi,{},"voi")
好家伙,直接报错了。下面是报错的结果:table1: Layer error: Feature.bounds: Unable to use a collection in an algorithm that requires a feature or image. This may happen when trying to use a collection of collections where a collection of features is expected; use flatten, or map a function to convert inner collections to features. Use clipToCollection (instead of clip) to clip an image to a collection.
简单总结一下,意思就是说我要弄成feature(然而,我不是弄成feature了嘛,至少我是这样认为的)。因而,我放弃了。但又想想本着本着叔本华的为世人留著作,为后人铺路的精神,我于是又坚持了。经过不懈努力,我找到了一个另一个方法。(具体方法论:不断试错,我也不知道原理,后续如果知道原理会进行补充的)具体代码实现如下:(方法2)
var voi = ee.FeatureCollection("users/GISer_djq/RWEQ/Tibet_bounds");
var voi1 = voi.geometry().buffer(5000);
var table1 = voi1.bounds();
Map.addLayer(table1,{},"table1");
Map.addLayer(voi,{},"voi")
这样就具体实现了,我暂且认为geometry()这个函数把featurecollection转成了feature,后续再补充。

809

被折叠的 条评论
为什么被折叠?



