R - 识别两个栅格具有不同值的位置并绘制这些差异
R - Identifying where two rasters have different values and plotting these differences
我是 R 中 GIS 处理的新手。我在同一区域内有两个栅格,我需要计算它们的值在哪里不同并绘制这些差异。 我该怎么做? 这个问题中提到的所有数据 can be found and downloaded here。
这是我到目前为止所做的:
#Read packages
library(sf)
library(raster)
#Read the data
cdl_2016 <- raster('CDL_2016_clip_20220329135051_1368678123.tif')
cdl_2019 <- raster('CDL_2019_clip_20220329135051_1368678123.tif')
wcw <- st_read("Wildcat_Creek_Watershed.shp")
#Change projection of wcw to match the rasters
wcw <- st_transform(wcw, crs(cdl_2016))
#Plot data to examine what we are working with
plot(cdl_2016)
plot(cdl_2019)
#Clip data to shapefile
wcw_2016 <- crop(cdl_2016, wcw)
wcw_2016 <- mask(wcw_2016, mask = wcw)
plot(wcw_2016)
wcw_2019 <- crop(cdl_2019, wcw)
wcw_2019 <- mask(wcw_2019, mask = wcw)
plot(wcw_2019)
#Examine the unique raster values. A legend is available at: https://nassgeodata.gmu.edu/CropScape/
unique(wcw_2016)
unique(wcw_2019)
我认为找到栅格之间的差异会起作用,但输出是一个带有负数的栅格。 我只需要找到并绘制它们彼此不同的地方。
dif_rast = wcw_2019 - wcw_2016
plot(dif_rast)
freq(dif_rast)
freq(dif_raster) 的输出:
value count
[1,] -212 1
[2,] -194 2
[3,] -191 1
[4,] -189 19
[5,] -185 16
[6,] -175 183
[7,] -172 27
[8,] -171 286
[9,] -166 2
[10,] -159 1
[11,] -158 5
[12,] -154 3
[13,] -153 2
[14,] -152 28
[15,] -150 3
[16,] -149 12
[17,] -148 1
[18,] -141 1
[19,] -140 178
[20,] -139 917
[21,] -137 3
[22,] -136 73
[23,] -130 1
[24,] -126 5
[25,] -121 13
[26,] -120 189
[27,] -118 7
[28,] -117 21
[29,] -116 163
[30,] -115 23
[31,] -106 2
[32,] -105 13
[33,] -104 19
[34,] -99 2
[35,] -98 8
[36,] -97 18
[37,] -95 2
[38,] -90 1
[39,] -86 1
[40,] -85 22
[41,] -84 112
[42,] -79 1
[43,] -74 12
[44,] -73 2
[45,] -70 1
[46,] -69 35
[47,] -68 15
[48,] -67 2
[49,] -65 54
[50,] -64 1
[51,] -61 2
[52,] -60 29
[53,] -59 1
[54,] -58 1
[55,] -57 10
[56,] -56 23
[57,] -55 1529
[58,] -54 747
[59,] -53 435
[60,] -52 59
[61,] -50 1
[62,] -49 781
[63,] -47 1
[64,] -45 54
[65,] -37 14
[66,] -36 13
[67,] -35 1666
[68,] -34 2
[69,] -33 17
[70,] -32 14
[71,] -31 140
[72,] -30 6
[73,] -27 10
[74,] -25 6
[75,] -24 4
[76,] -23 67
[77,] -21 1
[78,] -20 426
[79,] -19 265
[80,] -18 66
[81,] -17 6
[82,] -14 45
[83,] -13 1
[84,] -12 28
[85,] -11 4
[86,] -10 27
[87,] -9 14
[88,] -8 24
[89,] -7 2
[90,] -4 2606
[91,] -3 381
[92,] -2 42
[93,] -1 1031
[94,] 0 101236
[95,] 1 1381
[96,] 2 90
[97,] 3 51
[98,] 4 1849
[99,] 5 18
[100,] 7 1
[101,] 8 7
[102,] 10 3
[103,] 11 2
[104,] 12 45
[105,] 13 4
[106,] 14 6
[107,] 18 3
[108,] 19 273
[109,] 20 716
[110,] 21 70
[111,] 22 10
[112,] 23 12
[113,] 24 1
[114,] 25 4
[115,] 29 3
[116,] 30 88
[117,] 31 39
[118,] 32 46
[119,] 33 6
[120,] 34 1
[121,] 35 729
[122,] 36 4
[123,] 37 21
[124,] 45 59
[125,] 48 2
[126,] 49 115
[127,] 50 12
[128,] 52 1
[129,] 53 3
[130,] 54 126
[131,] 55 1170
[132,] 56 209
[133,] 57 51
[134,] 60 66
[135,] 61 2
[136,] 62 6
[137,] 63 1
[138,] 65 124
[139,] 69 2
[140,] 70 17
[141,] 79 1
[142,] 80 1
[143,] 84 4
[144,] 85 8
[145,] 86 2
[146,] 87 5
[147,] 88 1
[148,] 93 1
[149,] 94 1
[150,] 95 1
[151,] 96 6
[152,] 97 1
[153,] 98 2
[154,] 99 2
[155,] 104 10
[156,] 105 23
[157,] 106 1
[158,] 115 8
[159,] 116 57
[160,] 117 35
[161,] 118 21
[162,] 119 5
[163,] 120 14
[164,] 121 6
[165,] 122 26
[166,] 123 8
[167,] 136 20
[168,] 137 2
[169,] 139 204
[170,] 140 28
[171,] 152 1
[172,] 159 1
[173,] 171 33
[174,] 172 18
[175,] 175 17
[176,] 185 1
[177,] 189 1
[178,] 231 2
[179,] NA 238680
解决方案是减去栅格并将 non-zero 值设置为非零值。绘图时,对应于 1 的值表示该区域多年来发生了变化,而对应于 0 的区域表示没有变化。
dif <- wcw_2019 - wcw_2016
dif[dif != 0] <- 1
plot(dif)
我是 R 中 GIS 处理的新手。我在同一区域内有两个栅格,我需要计算它们的值在哪里不同并绘制这些差异。 我该怎么做? 这个问题中提到的所有数据 can be found and downloaded here。
这是我到目前为止所做的:
#Read packages
library(sf)
library(raster)
#Read the data
cdl_2016 <- raster('CDL_2016_clip_20220329135051_1368678123.tif')
cdl_2019 <- raster('CDL_2019_clip_20220329135051_1368678123.tif')
wcw <- st_read("Wildcat_Creek_Watershed.shp")
#Change projection of wcw to match the rasters
wcw <- st_transform(wcw, crs(cdl_2016))
#Plot data to examine what we are working with
plot(cdl_2016)
plot(cdl_2019)
#Clip data to shapefile
wcw_2016 <- crop(cdl_2016, wcw)
wcw_2016 <- mask(wcw_2016, mask = wcw)
plot(wcw_2016)
wcw_2019 <- crop(cdl_2019, wcw)
wcw_2019 <- mask(wcw_2019, mask = wcw)
plot(wcw_2019)
#Examine the unique raster values. A legend is available at: https://nassgeodata.gmu.edu/CropScape/
unique(wcw_2016)
unique(wcw_2019)
我认为找到栅格之间的差异会起作用,但输出是一个带有负数的栅格。 我只需要找到并绘制它们彼此不同的地方。
dif_rast = wcw_2019 - wcw_2016
plot(dif_rast)
freq(dif_rast)
freq(dif_raster) 的输出:
value count
[1,] -212 1
[2,] -194 2
[3,] -191 1
[4,] -189 19
[5,] -185 16
[6,] -175 183
[7,] -172 27
[8,] -171 286
[9,] -166 2
[10,] -159 1
[11,] -158 5
[12,] -154 3
[13,] -153 2
[14,] -152 28
[15,] -150 3
[16,] -149 12
[17,] -148 1
[18,] -141 1
[19,] -140 178
[20,] -139 917
[21,] -137 3
[22,] -136 73
[23,] -130 1
[24,] -126 5
[25,] -121 13
[26,] -120 189
[27,] -118 7
[28,] -117 21
[29,] -116 163
[30,] -115 23
[31,] -106 2
[32,] -105 13
[33,] -104 19
[34,] -99 2
[35,] -98 8
[36,] -97 18
[37,] -95 2
[38,] -90 1
[39,] -86 1
[40,] -85 22
[41,] -84 112
[42,] -79 1
[43,] -74 12
[44,] -73 2
[45,] -70 1
[46,] -69 35
[47,] -68 15
[48,] -67 2
[49,] -65 54
[50,] -64 1
[51,] -61 2
[52,] -60 29
[53,] -59 1
[54,] -58 1
[55,] -57 10
[56,] -56 23
[57,] -55 1529
[58,] -54 747
[59,] -53 435
[60,] -52 59
[61,] -50 1
[62,] -49 781
[63,] -47 1
[64,] -45 54
[65,] -37 14
[66,] -36 13
[67,] -35 1666
[68,] -34 2
[69,] -33 17
[70,] -32 14
[71,] -31 140
[72,] -30 6
[73,] -27 10
[74,] -25 6
[75,] -24 4
[76,] -23 67
[77,] -21 1
[78,] -20 426
[79,] -19 265
[80,] -18 66
[81,] -17 6
[82,] -14 45
[83,] -13 1
[84,] -12 28
[85,] -11 4
[86,] -10 27
[87,] -9 14
[88,] -8 24
[89,] -7 2
[90,] -4 2606
[91,] -3 381
[92,] -2 42
[93,] -1 1031
[94,] 0 101236
[95,] 1 1381
[96,] 2 90
[97,] 3 51
[98,] 4 1849
[99,] 5 18
[100,] 7 1
[101,] 8 7
[102,] 10 3
[103,] 11 2
[104,] 12 45
[105,] 13 4
[106,] 14 6
[107,] 18 3
[108,] 19 273
[109,] 20 716
[110,] 21 70
[111,] 22 10
[112,] 23 12
[113,] 24 1
[114,] 25 4
[115,] 29 3
[116,] 30 88
[117,] 31 39
[118,] 32 46
[119,] 33 6
[120,] 34 1
[121,] 35 729
[122,] 36 4
[123,] 37 21
[124,] 45 59
[125,] 48 2
[126,] 49 115
[127,] 50 12
[128,] 52 1
[129,] 53 3
[130,] 54 126
[131,] 55 1170
[132,] 56 209
[133,] 57 51
[134,] 60 66
[135,] 61 2
[136,] 62 6
[137,] 63 1
[138,] 65 124
[139,] 69 2
[140,] 70 17
[141,] 79 1
[142,] 80 1
[143,] 84 4
[144,] 85 8
[145,] 86 2
[146,] 87 5
[147,] 88 1
[148,] 93 1
[149,] 94 1
[150,] 95 1
[151,] 96 6
[152,] 97 1
[153,] 98 2
[154,] 99 2
[155,] 104 10
[156,] 105 23
[157,] 106 1
[158,] 115 8
[159,] 116 57
[160,] 117 35
[161,] 118 21
[162,] 119 5
[163,] 120 14
[164,] 121 6
[165,] 122 26
[166,] 123 8
[167,] 136 20
[168,] 137 2
[169,] 139 204
[170,] 140 28
[171,] 152 1
[172,] 159 1
[173,] 171 33
[174,] 172 18
[175,] 175 17
[176,] 185 1
[177,] 189 1
[178,] 231 2
[179,] NA 238680
解决方案是减去栅格并将 non-zero 值设置为非零值。绘图时,对应于 1 的值表示该区域多年来发生了变化,而对应于 0 的区域表示没有变化。
dif <- wcw_2019 - wcw_2016
dif[dif != 0] <- 1
plot(dif)