I went through different websites and GEE Guides to find a way to label polygons. But, I could not find a solution for it. Is there anyone who knows how to label polygons based on their attribute data (as in ArcGIS or QGIS)?
2 Answers
There is a package made by Gennadii Donchyts that can do something like that.
var text = require('users/gena/packages:text')
var geometry = /* color: #d63000 */ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Polygon(
[[[-67.61062024461603, -24.643926337087994],
[-68.57741711961603, -27.71942562544417],
[-64.35866711961603, -28.34007192235468],
[-63.12819836961603, -24.96305728059903]]]),
{
"system:index": "0",
"name": "one"
}),
ee.Feature(
ee.Geometry.Polygon(
[[[-70.24733899461603, -39.39961661613476],
[-71.82937024461603, -42.97001338016304],
[-65.85280774461603, -43.35466563336871],
[-64.44655774461603, -39.94082924349792]]]),
{
"system:index": "1",
"name": "two"
}),
ee.Feature(
ee.Geometry.Polygon(
[[[-57.94265149461603, -30.33201882072655],
[-58.90944836961603, -34.195360416696275],
[-54.33913586961603, -34.99121789610241],
[-52.22976086961603, -30.937007567008546]]]),
{
"system:index": "2",
"name": "three"
})]);
Map.addLayer(geometry)
// scale text font relative to the current map scale
var scale = Map.getScale() * 1
var labels = geometry.map(function(feat) {
feat = ee.Feature(feat)
var name = ee.String(feat.get("name"))
var centroid = feat.geometry().centroid()
var t = text.draw(name, centroid, scale, {
fontSize:18,
textColor:'red',
outlineWidth: 1,
outlineColor: 'red'
})
return t
})
labels = ee.ImageCollection(labels)
Map.addLayer(labels)
Make an ee.Feature of the polygon, for example:
var geometry = ee.Geometry.Polygon(
[[[-89.91067474237019, 21.22910017997246],
[-89.90651195397908, 21.221619408800706],
[-89.88110607019001, 21.230180259976443],
[-89.88561218133503, 21.238860615702055]]]);
var labels = ee.Dictionary({label: 1, someOtherlabel: 'someText', area: geometry.area()})
var feature = ee.Feature(geometry, labels);
print(feature)
-
1I mean visualization of data below to polygon such as name or area. Please, look above edited question for clarification. Jan 25, 2019 at 17:09
-
1There is no option to add labels to geometries, only for features you can do that.– KuikJan 27, 2019 at 12:36
-