GEE基本概念(二)——1. Geometry&Feature

本文介绍了GEE中的几何要素(Point, Line, Polygon)及其子类型,如MultiPoint、MultiLineString、LinearRing和Polygon的绘制与操作。还讲解了Feature的构造方法,包括Geometry添加属性和FeatureCollection的使用。此外,涉及了矢量数据的处理,如FeatureCollection的加载、过滤和几何变换。

2.GEE重要概念——Geometry

总结:写在开头,Feature是由Geometry加上一些特殊属性构成,FeatureCollection则是由一系列的Feature在加上一些特殊属性构成。

介绍点线面特征:

图形Geometry(Point Line Polygon)

矢量Feature FeatureCollection

影像Image ImageCollection

2.1 Point and MultiPoint

点和多个点的绘制显示ee.Geometry.MultiPoint

var Forbidden_City = ee.Geometry.Point(116.3968, 39.9186);
var Forbidden_MultiCity = ee.Geometry.MultiPoint([
  [116.396659, 39.918576],
  [116.397303, 39.918586],
  [116.396678, 39.918272],
  [116.397316, 39.918294]
  ]);
Map.centerObject(Forbidden_MultiCity);   // Map映射中心
print(Forbidden_City);    
print(Forbidden_MultiCity);    
Map.addLayer(Forbidden_MultiCity);  //Map映射到地图上添加图层

2.2 Line

2.2.1 LineString and MultiLineString(it’s not close)

线和多段线的绘制,多段线没有闭合ee.Geometry.MultiLineString

var Forbidden_MultiCity = ee.Geometry.LineString([
  [116.396659, 39.918576],
  [116.397303, 39.918586],
  [116.397316, 39.918294],
  [116.396678, 39.918272]
  ]);
Map.centerObject(Forbidden_MultiCity);   
print(Forbidden_MultiCity);    
Map.addLayer(Forbidden_MultiCity);

var Forbidden_MultiCity = ee.Geometry.MultiLineString([
  [[116.396659, 39.918576],
  [116.397303, 39.918586]],
  
  [[116.397316, 39.918294],
  [116.396678, 39.918272]],
  ]);
Map.centerObject(Forbidden_MultiCity);   
print(Forbidden_MultiCity);    
Map.addLayer(Forbidden_MultiCity);

2.2.2 LinearRing and MultiLinearString(compared with LineString, it’s close)

相比于之前的多段线,此处是闭合的ee.Geometry.LinearRing

var Forbidden_MultiCity = ee.Geometry.LinearRing([
  [116.396659, 39.918576],
  [116.397303, 39.918586],
  [116.397316, 39.918294],
  [116.396678, 39.918272]
  ]);
Map.centerObject(Forbidden_MultiCity);   
print(Forbidden_MultiCity);    
Map.addLayer(Forbidden_MultiCity);

2.3 Polygon

polygon is defined similary to Line

与多段线命令相似,命令为:ee.Geometry.Polygon

var polygon = ee.Geometry.Polygon(        
      [[[114.62959747314449, 33.357067677774594],  
       [114.63097076416011, 33.32896028884253],  
       [114.68315582275386, 33.33125510961763],  
       [114.68178253173824, 33.359361757948754]]]);  
Map.centerObject(polygon, 9); 

2.3.1Geometry.transform()

将Geometry转换为带有WGS84坐标点的范围Geometry.transform(),此为Geometry转Feature。

ee.Geometry.Rectangle为通过左上角和右下角的坐标构建矩形。但是这个函数是根据左下角和右上角构建的Polygon。

var China_Geo =ee.Geometry.Rectangle(65.9,19.8,134.5,50.9)  //没定义坐标系,默认为WGS84
var China_Planr = ee.Geometry(China_Geo,null,false) //取消了WGS坐标系,拉平
var China_Planr_2 = China_Geo.transform('EPSG:4326', ee.ErrorMargin(100))        //  重新定义了坐标系

Map.addLayer(China_Geo,{color:'FF0000'},'geodesic polygon')
Map.addLayer(China_Planr,{color:'000000'},'planar polygon')
Map.addLayer(China_Planr_2,{color:'0000CD'},'planar_2 polygon')

2.4 Feature

1)Featur构建 的两种方法:1.由Geometry构建;2.在FeatureCollection中提取。

第一种是将Geometry加上或不加属性构建为Feature;第二种是直接提取,我们一般都是直接提取的。

//1.将点转为feature
var point = ee.Geometry.Point(109.202, 34.787);
var f1 = ee.Feature(point,{count:100});//添加了属性:由key-value构成
print('feature 1', f1);
Map.centerObject(f1,7);
Map.addLayer(f1,{color:'green'}, 'point');
//2.在FeatureCollection中提取Feature
var fCol = ee.FeatureCollection('users/qq1216079662/China');//导入中国的shp,是FeatureCollection
print('china', fCol);
Map.addLayer(fCol,{color:'red'}, 'fCol');

var f2 = ee.Feature(fCol.first());
print('first feature', f2);
Map.addLayer(f2,{color:'blue'}, 'f2');//排序第一个shp,是Feature

var sCol = fCol.filterBounds(point);
print('filter feature', sCol);
Map.addLayer(sCol,{color:'green'}, 'shaanxi');//用点提取的shp,还是FeatureCollection

注意第二种和第三章的不同之处,虽然都提取除了Feature,但是第二种是从集合种提取出某一个Feature,第三章则是提取出了FeatureCollection。

2)矢量数据一般为上传的,因此,介绍加载和过滤矢量数据的方法,过滤完仍为FeatureCollection。

//加载矢量数据
var counties = ee.FeatureCollection('users/qq1216079662/China');
Map.addLayer(counties, {}, 'counties');

//过滤矢量数据
// Load a feature collection.
var featureCollection = ee.FeatureCollection('users/qq1216079662/China');
// Filter the collection.
var filteredFC = featureCollection.filter(ee.Filter.eq('省', '陕西省'));
// Display the collection.
print(featureCollection);
print(filteredFC);
Map.addLayer(filteredFC, {color:'red'}, '陕西省');

遥感影像中心点的显示:

展示的是这个时期这个数据的图像的中心像素点的位置

// This function returns the image centroid as a new Feature.
var getGeom= function(image) {
return ee.Feature(image.geometry().centroid(), {foo: 1});
};
// Load a Landsat 8 collection.
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1')
.filterDate('2014-06-01', '2014-06-08');

// Map the function over the ImageCollection.
var featureCollection= ee.FeatureCollection(collection.map(getGeom));
// Print the collection.
print(featureCollection);
Map.addLayer(featureCollection)

矢量数据:缓冲区分析以及缓冲区的绘制,介绍矢量数据的应用,没有多大的作用。

var china = ee.FeatureCollection(table);        //加载中国区域的shp
var shaanxi = ee.Feature(china.filterBounds(ee.Geometry.Point([108.98,34.429])).first())        //通过点过滤该地区的feature
var SX_simple = shaanxi.simplify(50000);    //内接
var SX_centroid = shaanxi.centroid();   //几何中心
var SX_hull = shaanxi.convexHull();     //外接
var SX_bounds = shaanxi.bounds();       //四至
var SX_buffer = shaanxi.buffer(50000);  //缓冲区

print()
Map.centerObject(shaanxi);
Map.addLayer(shaanxi);
Map.addLayer(SX_simple);
Map.addLayer(SX_centroid);
Map.addLayer(SX_hull);
Map.addLayer(SX_bounds);
Map.addLayer(SX_buffer);

set and setMulti
set是替换,而setMulti是替换并增加新的属性(下面代码中table为自定义的roi)

var China_Geo =ee.Geometry.Rectangle(65.9,19.8,134.5,50.9);
var China_Geo_feature = ee.Feature(China_Geo,{name:'故宫', location:'北京'});
var China_Geo_set =China_Geo_feature.set({name:'gugong', location:'beijing'}) 
print(China_Geo_feature,China_Geo_set)
Map.centerObject(China_Geo);
Map.addLayer(China_Geo_set);

var table_set = table.setMulti({'system:asset_size':'15934'})
print(table,table_set)
Map.centerObject(table);
Map.addLayer(table);
<think> 我们有一个具体的需求:在GEE中将CSV文件中的坐标(通常是以两列形式存在的经度和纬度)转换为geometry列,以便正确导入为要素集合(FeatureCollection)。 根据之前的讨论,我们知道GEE上传CSV时要求有一个geometry列,且该列应为WKT格式(如POINT(lon lat))或者我们可以通过指定经纬度列来自动创建点几何。 解决方案有两种: 1. 在CSV文件中预处理,添加一个geometry列,格式为WKT字符串。 2. 在上传CSV到GEE时,通过指定经纬度列(xColumn和yColumn)让GEE自动创建点几何。 根据引用[2]和之前的回答,我们注意到如果在导出CSV时缺少geometry信息,可能是因为在reduceRegions操作中没有正确指定几何信息。但是,现在的问题是如何在导入CSV时创建geometry。 实际上,在GEE中上传CSV文件时,如果CSV中没有geometry列,我们可以通过指定经纬度列(例如lon和lat)来创建点要素。具体步骤如下: 步骤1:准备CSV文件 - 确保CSV文件包含经度列和纬度列(例如,列名分别为'lon'和'lat'),并且坐标是WGS84(EPSG:4326)下的经纬度。 步骤2:上传CSV到GEE Assets - 在GEE代码编辑器的Assets标签页,点击New -> Table Upload。 - 选择CSV文件。 - 在配置界面中,设置: CRS: EPSG:4326 (默认就是这个,但最好确认) xColumn: 输入经度列的名称(如'lon') yColumn: 输入纬度列的名称(如'lat') - 这样,GEE会自动根据这两列创建点几何,生成一个FeatureCollection。 步骤3:在代码中调用 - 上传成功后,可以在代码中使用该资源: ```javascript var table = ee.FeatureCollection('users/your_username/your_asset_name'); Map.addLayer(table); ``` 如果已经上传了CSV但没有设置经纬度列,导致没有geometry,那么需要重新上传并正确设置。 另外,如果希望在GEE内部将已有的两个属性列(经度和纬度)转换为几何列,可以这样操作: - 首先,将现有的CSV作为没有几何的要素集合上传(比如作为属性表),然后在GEE中使用代码转换。 例如,假设我们有一个没有geometryFeatureCollection,但它有'lon'和'lat'属性。我们可以使用map操作来创建点几何: ```javascript // 假设原始的上传没有设置几何,我们通过属性创建几何 var tableWithoutGeometry = ee.FeatureCollection('path/to/your/asset'); // 将属性转换为几何 var tableWithGeometry = tableWithoutGeometry.map(function(feature) { // 获取经度和纬度属性 var lon = feature.getNumber('lon'); var lat = feature.getNumber('lat'); // 创建点几何 var geom = ee.Geometry.Point([lon, lat]); // 返回新的要素,包含原属性和新几何 return feature.setGeometry(geom); }); // 现在tableWithGeometry就有了几何信息 ``` 但是,这种方法需要在GEE中运行代码,并且原始上传的资产虽然不包含几何,但也是FeatureCollection类型(只是每个要素的几何为null)。因此,更推荐在上传时就设置经纬度列,直接生成几何。 如果用户已经在GEE中有一个没有几何的FeatureCollection(例如通过其他方式导入的表格),那么上述map方法是一个有效的解决方案。 总结: - 上传时设置经纬度列是最直接的方法。 - 如果已经上传但没有几何,可以通过GEE代码转换(前提是表格中有经纬度属性)。 相关问题: 1. 如何将已有的点要素集合导出为带有经纬度列的CSV? 2.GEE中,如何将线或面要素的坐标提取为属性? 3. 上传CSV时出现“Invalid column name”错误怎么办? 注意:在回答中,我们引用了之前的讨论和规则,特别是引用[2]提到了几何信息缺失的问题。 </think> 在 Google Earth Engine (GEE) 中将 CSV 文件中的坐标列转换为 `geometry` 列有两种主要方法,以下是详细操作指南: --- ### 一、上传时自动转换(推荐方法) 当 CSV 文件包含**分列的经度(longitude)和纬度(latitude)** 时,可在上传时自动创建几何: 1. **准备 CSV 格式**: ```csv id,lon,lat,value 1,116.4074,39.9042,25 2,121.4737,31.2304,30 ``` 2. **上传参数设置**: - 在 Assets 上传界面: ```js xColumn: 'lon' // 经度列名 yColumn: 'lat' // 纬度列名 crs: 'EPSG:4326' // 坐标系 ``` - GEE 会自动生成 `geometry` 列:`POINT(经度 纬度)` 3. **验证结果**: ```javascript var table = ee.FeatureCollection('users/your_account/your_table'); print(table.first().geometry()); // 输出:Point(116.4074, 39.9042) ``` > ⚠️ **关键点**: > - 列名必须为英文且无特殊字符 > - 经纬度值必须是 WGS84 (EPSG:4326) > - 若坐标顺序错误,使用 `.map()` 交换坐标[^2]: > ```javascript > var corrected = table.map(function(feat){ > var coords = feat.geometry().coordinates(); > return feat.setGeometry(ee.Geometry.Point([coords.get(1), coords.get(0)])); > }); > ``` --- ### 、代码中动态转换 若 CSV 已上传为无几何的表格: 1. **加载 CSV 资产**: ```javascript var rawTable = ee.FeatureCollection('users/your_account/raw_csv'); ``` 2. **添加几何列**: ```javascript var withGeometry = rawTable.map(function(feature) { var lon = feature.getNumber('lon'); // 获取经度值 var lat = feature.getNumber('lat'); // 获取纬度值 var point = ee.Geometry.Point([lon, lat]); return feature.setGeometry(point); }); ``` 3. **导出为新资产**: ```javascript Export.table.toAsset({ collection: withGeometry, description: 'converted_table', assetId: 'users/your_account/converted_geom' }); ``` --- ### 三、常见问题解决 | **问题** | **解决方案** | |-------------------------|-------------| | 坐标值非数值类型 | 上传前用 `parseFloat()` 转换数据类型 | | 出现 `Error: Invalid column` | 检查列名拼写,避免空格用下划线替代 | | 几何点显示在非洲(0,0) | 确认经纬度列未颠倒,参考坐标交换代码 | | 要素集合显示 `0 elements` | 在 `map()` 函数中添加错误处理逻辑 | > 💡 **最佳实践**: > 优先使用上传参数转换(方法一),避免代码处理的开销。对于大型数据集,建议先将 CSV 在本地 GIS 软件(如 QGIS)中转换为 WKT 格式再上传[^1]。 --- ### 相关问题 1. 如何从 GEE 导出包含几何信息的 CSV 文件? 2. 处理 CSV 时如何避免 `Error: Column 'geometry' already exists`? 3.GEE 中如何将点要素集合转换为线要素? 4. 如何批量处理多个 CSV 文件的几何转换? [^1]: GEE系列:第4单元 Google 地球引擎中的数据导入和导出 [^2]: GEE土地分类——分类后样本点值提取至点过程中,导出的csv数据表中不存在geometry的位置信息
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值