Day 3-feature collection

本文仅仅是将GEE官网上的代码整理一下,一方面自学,另一方面自我监督。文中有些问题是我想不通的,还需经历时间来理解。 

3 feature collection

3.1 from polygons

官网上给的代码截图:

逐行分析代码:

// Create and render a feature collection from polygons.##从多边形,创建和反馈特征集

// Construct a FeatureCollection from a list of features.##构建属性集

var fc = ee.FeatureCollection([

  // Create each feature with a geometry and properties.##创建每一个图形的几何特征和属性值

  ee.Feature(

      ee.Geometry.Polygon({

        coords: [[-109.05, 41], [-109.05, 37], [-102.05, 37], [-102.05, 41]],

        geodesic: false, // The state boundaries are not geodesic.##边界不是测地线。测地线是曲面上面两点最短的距离。

        maxError: 1000 // Make the error margin large; we don't need accuracy.

      }), {name: 'Colorado', fill: 1}), // Pass properties as a dictionary.

  ee.Feature(

      ee.Geometry.Polygon({

        coords: [

          [-114.05, 37.0], [-109.05, 37.0], [-109.05, 41.0],

          [-111.05, 41.0], [-111.05, 42.0], [-114.05, 42.0]

        ],

        geodesic: false,

        maxError: 1000

      }), {name: 'Utah', fill: 2})

]);

// Fill, then outline the polygons into a blank image.##填充,然后把外边框放到一个image中。

var image = ee.Image()

    .paint(fc, 'fill') // Get color from property named 'fill'

    .paint(fc, 3, 5) // Outline using color 3, width 5.##外边框的颜色和宽度。颜色3是蓝色,1是红,2是绿。

    .toByte();

Map.addLayer(image, {

    palette: ['000000', 'FF0000', '00FF00', '0000FF'],

    max: 3,##调节颜色。

    opacity: 0.5

});

Map.setCenter(-107, 41, 6);

3.2 buffer

官网上给的代码截图:


逐行分析代码:

// Feature buffer example.

// Display the area within 2 kilometers of San Francisco BART stations.

// Instantiate a FeatureCollection of BART locations in Downtown San Francisco ##用例子说明地铁站的特征集

// (points).##找到三个点。

var stations = [

  ee.Feature(

      ee.Geometry.Point(-122.42, 37.77), {'name': '16th St. Mission (16TH)'}),

  ee.Feature(

      ee.Geometry.Point(-122.42, 37.75), {'name': '24th St. Mission (24TH)'}),

  ee.Feature(

      ee.Geometry.Point(-122.41, 37.78),

      {'name': 'Civic Center/UN Plaza (CIVC)'})

];

var bartStations = ee.FeatureCollection(stations);

// Map a function over the collection to buffer each feature.##构建一个函数,让

var buffered = bartStations.map(function(f) {##map里面有多个function,butter是其中的一个。

  return f.buffer(2000, 100); // Note that the errorMargin is set to 100.

});

Map.addLayer(buffered, {color: '800080'});

Map.setCenter(-122.4, 37.7, 11);


3.3 computed area filter

官网上给的代码截图:


逐行分析代码:

// Computed area filter.

// Find US counties smaller than 3k square kilometers in area.##选择县域面积小于3000平方千米的县城。

// Load counties from TIGER boundaries table

var counties = ee.FeatureCollection('TIGER/2016/Counties');

// Map a function over the counties to set the area of each.##设置一个函数,获得每一个面积值。

var countiesWithArea = counties.map(function(f) {##点后面是函数

  // Compute area in square meters.  Convert to hectares.##平方米转到公顷。

  var areaHa = f.area().divide(100 * 100);

  // A new property called 'area' will be set on each feature.

  return f.set({area: areaHa});

});

// Filter to get only smaller counties.

var smallCounties = countiesWithArea.filter(ee.Filter.lt('area', 3e5));##现在单位是公顷,面积小于300 000公顷。也就是面积小于300 000 0000平方米。3000 000 000平方米就是3千平方千米。

Map.addLayer(smallCounties, {color: '900000'});##颜色是红色

Map.setCenter(-119.7, 38.26, 7);


3.4 distance 

官网上给的代码截图:


逐行分析代码:

// Collection.distance example.

// Computes the distance to the nearest feature in a collection.

// Construct a FeatureCollection from a list of geometries.

var fc = ee.FeatureCollection([##设置三个点

  ee.Geometry.Point(-72.94411, 41.32902),

  ee.Geometry.Point(-72.94411, 41.33402),

  ee.Geometry.Point(-72.94411, 41.33902),

  // The geometries do not need to be the same type.

  ee.Geometry.LineString(##设置一条线段,线段有三个点。

      -72.93411, 41.30902, -72.93411, 41.31902, -72.94411, 41.31902)

]);

// Compute distance from the dfeatures, to a max of 1000 meters.

var distance = fc.distance(1000, 100);##1000是缓冲距离,100不知道什么意思。

Map.setCenter(-72.94, 41.32, 13);

Map.addLayer(distance, {min: 0, max: 1000, palette: ['yellow', 'red']});

Map.addLayer(fc);


3.5 join

官网上给的代码截图:


逐行分析代码:

// Simple Join example.

// Show parks in San Francisco within 2 kilometers of a BART station.##显示在地铁站2千米范围内的公园。

// Load fusion tables for BART station locations and local parks.

var bart = ee.FeatureCollection('GOOGLE/EE/DEMOS/bart-locations');

var parks = ee.FeatureCollection('GOOGLE/EE/DEMOS/sf-parks');

// Create a filter to pass the left features within 2km of the right features.##创建一个滤波通过左的数据(在右边数据的2km内)

var joinFilter = ee.Filter.withinDistance({

  distance: 2000,

  leftField: '.geo',

  rightField: '.geo'

});

// Apply the join.  The leftField corresponds to the primary collection##应用join功能,左边数据是主要的,右边数据是次要的。匹配条件是设定的过滤器。

// and the rightField corresponds to the secondary collection.  The

// matching condition is specified by the filter.

var closeParks = ee.Join.simple().apply({

  primary: parks,

  secondary: bart,

  condition: joinFilter

});

// Buffer the bart stations by 2km for display purposes.

var bufferedBart = bart.map(function(f) { return f.buffer(2000, 100); });

Map.setCenter(-122.45, 37.75, 13);

Map.addLayer(bufferedBart, {color: 'b0b0b0'});

Map.addLayer(closeParks, {color: '008000'});


3.6 reduce to image

官网上给的代码截图:


逐行分析代码:

// Example of FeatureCollection.reduceToImage()

// Define a feature collection with a value we want to average.

var fc = new ee.FeatureCollection([

  ee.Feature(

    ee.Geometry.Rectangle(

      -122.4550, 37.8035,

      -122.4781, 37.7935),

    {'value': 0}),

  ee.Feature(

    ee.Geometry.Polygon([

      [-122.4427, 37.8027],

      [-122.4587, 37.7987],

      [-122.4440, 37.7934]]),

    {'value': 1})

  ]);

// Reduce the collection to an image, where each pixel##每一个像元是所有的feature(相交在某像元上)的中间值。

// is the mean of the 'value' property in all features

// intersecting that pixel.

var image_reduced = fc.reduceToImage(['value'], 'mean');

Map.setCenter(-122.4561, 37.7983, 14);

Map.addLayer(image_reduced, {

  min: 0,

  max: 1,

  palette: ['008800', '00FF00']});

问题:不知道这个有什么用场。


3.7 from eargh engine asset

官网上给的代码截图:


逐行分析代码:

// Create a FeatureCollection from an Earth Engine Table.##创建一个特征集。

// Load census roads.##提取道路,先把州际的挑出来,再把地表的挑出来。

var roads = ee.FeatureCollection('TIGER/2016/Roads');

// Get only interstates.

var interstates = roads.filter(ee.Filter.eq('rttyp', 'I'));

// Get only surface roads.

var surfaceRoads = roads.filter(ee.Filter.eq('rttyp', 'M'));

// Display the roads in different colors.

Map.addLayer(surfaceRoads, {color: 'gray'}, 'surface roads');

Map.addLayer(interstates, {color: 'red'}, 'interstates');

这次的学习就到这里了,希望帮助自己学到知识, 也可以帮助到需要的朋友。不足之处,还望见谅。

我是小白,生产不了代码。copyright from Google earth engine。

你可能感兴趣的:(Day 3-feature collection)