多边形的非唯一属性的中位栅格值

huangapple go评论104阅读模式
英文:

median raster value for polygons by attribute that is not unique

问题

给定这个栅格和多边形数据集:

  1. f <- system.file("ex/elev.tif", package="terra")
  2. r <- rast(f)
  3. fs <- system.file("ex/lux.shp", package="terra")
  4. v <- vect(fs)

如何提取多边形 v 中具有相同 "NAME_1" 值的栅格单元的中位数值,并将其添加到 v 的属性中?

请注意,"NAME_1" 在 v 中不是唯一的:

  1. v
  2. # class : SpatVector
  3. # geometry : polygons
  4. # dimensions : 12, 6 (geometries, attributes)
  5. # extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax)
  6. # source : lux.shp
  7. # coord. ref. : lon/lat WGS 84 (EPSG:4326)
  8. # names : ID_1 NAME_1 ID_2 NAME_2 AREA POP
  9. # type : <num> <chr> <num> <chr> <num> <int>
  10. # values : 1 Diekirch 1 Clervaux 312 18081
  11. # 1 Diekirch 2 Diekirch 218 32543
  12. # 1 Diekirch 3 Redange 259 18664
英文:

Given this this raster and polygon dataset

  1. f &lt;- system.file(&quot;ex/elev.tif&quot;, package=&quot;terra&quot;)
  2. r &lt;- rast(f)
  3. fs &lt;- system.file(&quot;ex/lux.shp&quot;, package=&quot;terra&quot;)
  4. v &lt;- vect(fs)

How can I extract the median value of the raster cells for polygons with the same value of "NAME_1" in v, and add this to the attributes of v?

Please note that "NAME_1" is not unique in v:

  1. v
  2. # class : SpatVector
  3. # geometry : polygons
  4. # dimensions : 12, 6 (geometries, attributes)
  5. # extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax)
  6. # source : lux.shp
  7. # coord. ref. : lon/lat WGS 84 (EPSG:4326)
  8. # names : ID_1 NAME_1 ID_2 NAME_2 AREA POP
  9. # type : &lt;num&gt; &lt;chr&gt; &lt;num&gt; &lt;chr&gt; &lt;num&gt; &lt;int&gt;
  10. # values : 1 Diekirch 1 Clervaux 312 18081
  11. # 1 Diekirch 2 Diekirch 218 32543
  12. # 1 Diekirch 3 Redange 259 18664

答案1

得分: 1

为了计算所有几何体的中位数,您可以执行以下操作:

  1. library(terra)
  2. r <- rast(system.file("ex/elev.tif", package="terra"))
  3. v <- vect(system.file("ex/lux.shp", package="terra"))
  4. v$median = extract(r, v, fun=median, na.rm=TRUE, ID=FALSE)
  5. v
  6. # class : SpatVector
  7. # geometry : polygons
  8. # dimensions : 12, 7 (几何体, 属性)
  9. # extent : 5.74414, 6.528252, 49.44781, 50.18162 (最小x, 最大x, 最小y, 最大y)
  10. # source : lux.shp
  11. # coord. ref. : lon/lat WGS 84 (EPSG:4326)
  12. # names : ID_1 NAME_1 ID_2 NAME_2 AREA POP median
  13. # type : <num> <chr> <num> <chr> <num> <int> <num>
  14. # values : 1 Diekirch 1 Clervaux 312 18081 467.1
  15. # 1 Diekirch 2 Diekirch 218 32543 333.9
  16. # 1 Diekirch 3 Redange 259 18664 377.4

由于"NAME_1"对于多个几何体具有相同的值,我们需要多做一些工作。您可以选择执行以下操作之一:

  1. a <- aggregate(v, "NAME_1")
  2. e <- extract(r, a, fun=median, na.rm=TRUE)
  3. e$NAME_1 <- a$NAME_1[e$ID]
  4. e$ID <- NULL
  5. ve <- merge(v, e, by="NAME_1")

或者

  1. ee <- extract(r, v, na.rm=TRUE)
  2. ee$NAME_1 <- v$NAME_1[ee$ID]
  3. ee <- aggregate(ee[,2,drop=FALSE], ee[,"NAME_1",drop=FALSE], median, na.rm=TRUE)
  4. vee <- merge(v, ee, by="NAME_1")
  5. values(vee)
  6. # NAME_1 ID_1 ID_2 NAME_2 AREA POP elevation
  7. #1 Diekirch 1 1 Clervaux 312 18081 422.0
  8. #2 Diekirch 1 2 Diekirch 218 32543 422.0
  9. #3 Diekirch 1 3 Redange 259 18664 422.0
  10. #4 Diekirch 1 4 Vianden 76 5163 422.0
  11. #5 Diekirch 1 5 Wiltz 263 16735 422.0
  12. #6 Grevenmacher 2 6 Echternach 188 18899 288.5
  13. #7 Grevenmacher 2 7 Remich 129 22366 288.5
  14. #8 Grevenmacher 2 12 Grevenmacher 210 29828 288.5
  15. #9 Luxembourg 3 8 Capellen 185 48187 314.0
  16. #10 Luxembourg 3 9 Esch-sur-Alzette 251 176820 314.0
  17. #11 Luxembourg 3 10 Luxembourg 237 182607 314.0
  18. #12 Luxembourg 3 11 Mersch 233 32112 314.0
英文:

To compute the median for all geometries, you can do

  1. library(terra)
  2. r &lt;- rast(system.file(&quot;ex/elev.tif&quot;, package=&quot;terra&quot;) )
  3. v &lt;- vect(system.file(&quot;ex/lux.shp&quot;, package=&quot;terra&quot;))
  4. v$median = extract(r, v, fun=median, na.rm=TRUE, ID=FALSE)
  5. v
  6. # class : SpatVector
  7. # geometry : polygons
  8. # dimensions : 12, 7 (geometries, attributes)
  9. # extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax)
  10. # source : lux.shp
  11. # coord. ref. : lon/lat WGS 84 (EPSG:4326)
  12. # names : ID_1 NAME_1 ID_2 NAME_2 AREA POP median
  13. # type : &lt;num&gt; &lt;chr&gt; &lt;num&gt; &lt;chr&gt; &lt;num&gt; &lt;int&gt; &lt;num&gt;
  14. # values : 1 Diekirch 1 Clervaux 312 18081 467.1
  15. # 1 Diekirch 2 Diekirch 218 32543 333.9
  16. # 1 Diekirch 3 Redange 259 18664 377.4

Since "NAME_1" has the same value for multiple geometries, we have to do a bit more work. You can do either

  1. a &lt;- aggregate(v, &quot;NAME_1&quot;)
  2. e &lt;- extract(r, a, fun=median, na.rm=TRUE)
  3. e$NAME_1 &lt;- a$NAME_1[e$ID]
  4. e$ID &lt;- NULL
  5. ve &lt;- merge(v, e, by=&quot;NAME_1&quot;)

or

  1. ee &lt;- extract(r, v, na.rm=TRUE)
  2. ee$NAME_1 &lt;- v$NAME_1[ee$ID]
  3. ee &lt;- aggregate(ee[,2,drop=FALSE], ee[,&quot;NAME_1&quot;,drop=FALSE], median, na.rm=TRUE)
  4. vee &lt;- merge(v, ee, by=&quot;NAME_1&quot;)
  5. values(vee)
  6. # NAME_1 ID_1 ID_2 NAME_2 AREA POP elevation
  7. #1 Diekirch 1 1 Clervaux 312 18081 422.0
  8. #2 Diekirch 1 2 Diekirch 218 32543 422.0
  9. #3 Diekirch 1 3 Redange 259 18664 422.0
  10. #4 Diekirch 1 4 Vianden 76 5163 422.0
  11. #5 Diekirch 1 5 Wiltz 263 16735 422.0
  12. #6 Grevenmacher 2 6 Echternach 188 18899 288.5
  13. #7 Grevenmacher 2 7 Remich 129 22366 288.5
  14. #8 Grevenmacher 2 12 Grevenmacher 210 29828 288.5
  15. #9 Luxembourg 3 8 Capellen 185 48187 314.0
  16. #10 Luxembourg 3 9 Esch-sur-Alzette 251 176820 314.0
  17. #11 Luxembourg 3 10 Luxembourg 237 182607 314.0
  18. #12 Luxembourg 3 11 Mersch 233 32112 314.0

huangapple
  • 本文由 发表于 2023年4月11日 01:52:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定