使用dismo::randompoints在循环中

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

Using dismo::randompoints in a loop

问题

I have an SF object consisting of 81 polygons.
What I would like to do is generate 460 random points within each polygon.

compared to st_sample, dismo::randomPoints has a few features I like to make use of; such as not having any created points be in the same raster cell as pre-existing points.

I am trying to loop the randompoints function to write random points for each row in my object "hrstest" (see reproducible example at the end of the question, "hrstest" has only 3 polygons instead of 81).

I also have pre-existing points ("samplexy"), and defining these within the randomPoints function ensures the resulting generated points are in cells of the masking raster ("mask") that do not contain any pre-existing points.

So I try the following:

papoints <- list()
for(i in 1:nrow(hrstest)){
  
  papoints[i] <- randomPoints(mask=roads, n= 460, p=samplexy ext= hrstest[i,])
}

where "mask=roads" is the masking raster, and "p=samplexy" are the points that take prevalence in the raster (a raster cell containing point(s) from "samplexy", will not have points generated within it). As I see it this should then loop over each row (polygon) from "hrstest", resulting in 460 random points per polygon.
But it returns

Error in .doCellFromXY(object@ncols, object@nrows, object@extent@xmin,  : 
  Not compatible with requested type: [type=list; target=double].

I could not figure out what this means, so tried taking a step back, and remove the requirement to not generate points within raster cells containing a point from 'samplexy'.
This runs, but does not give the correct result. It returns a warning for each list that is output:

Warning messages:
1: In papoints[i] <- randomPoints(mask = roads, n = 460, ext = hrstest[i,  :
  number of items to replace is not a multiple of replacement length

The real output is a list for each polygon (good), but instead of containing 460 points, each contains just one value, which looks like a longitude value in the crs I'm using (16N UTM).

Creating "hrstest":

structure(list(id = c("10_17", "10_18", "103_19"), area = c(9330.16971002, 
11839.809165, 19727.1596434), Shape_Leng = c(45088.8473474, 54527.1219449, 
203178.756164), Shape_Area = c(92923453.7616, 117622362.856, 
168371931.317), size = c(92.9234537616, 117.622362856, 168.371931317
), geometry = structure(list(structure(list(list(structure(c(385555.4573, 
385555.4573, 385758.714, 385855.4573, 385855.4573, 385991.2446, 
386194.3538, 386224.7214, 386971.075, 387715.4883, 387717.4286, 
387955.4573, 387955.4573, 387955.4573, 388049.9122, 388255.4573, 
388255.4573, 388463.7821, 389035.5557, 388798.3266, 388463.7821, 
387848.1566, 387717.4286, 387364.8016, 387070.6686, 386971.075, 
386402.3183, 386224.7214, 385478.3678, 384944.6887, 384732.0143, 
383985.6607, 383837.6516, 383239.3071, 382492.9535, 382361.38, 
381746.6, 381468.6452, 381000.2464, 380278.6783, 380253.8928, 
380081.1312, 379507.5392, 378761.1857, 378697.631, 378014.8321, 
377268.4785, 376527.7521, 376522.1249, 376519.1168, 375775.7714, 
375310.1579, 375029.4178, 374738.2147, 374465.3875, 374488.8299, 
374812.1018, 375029.4178, 375549.9777, 375775.7714, 376522.124

<details>
<summary>英文:</summary>

I have an SF object consisting of 81 polygons.
What I would like to do, is generate 460 random points within each polygon.

compared to st_sample, dismo::randomPoints has a few features I like to make use of; 
such as not having any created points be in the same raster cell as pre-existing points.

I am trying to loop the randompoints function to write random points for each row in my object &quot;hrstest&quot; (see reproducible example at the end of the question, &quot;hrstest&quot; has only 3 polygons instead of 81).
[![enter image description here][1]][1]


I also have pre-existing points (&quot;samplexy&quot;), and defining these within the randomPoints function ensures the resulting generated points are in cells of the masking raster (&quot;mask&quot;) that do not contain any pre-existing points.

So I try the following:

papoints <- list()
for(i in 1:nrow(hrstest)){

papoints[i] <- randomPoints(mask=roads,n= 460, p=samplexy ext= hrstest[i,])
}

whereby &quot;mask=roads&quot; is the masking raster, and &quot;p=samplexy&quot; are the points that take prevalence in the raster (a raster cell containing point(s) from &quot;samplexy&quot;, will not have points generated within it). As I see it this should then loop over each row (polygon) from &quot;hrstest&quot;, resulting in 460 random points per polygon.
But it returns

Error in .doCellFromXY(object@ncols, object@nrows, object@extent@xmin, :
Not compatible with requested type: [type=list; target=double].


I could not figure out what this means, so tried taking a step back, and remove the requirement to not generate points within raster cells containing a point from &#39;samplexy&#39;.
This runs, but does not give the correct result. It returns a warning for each list that is output: 

Warning messages:
1: In papoints[i] <- randomPoints(mask = roads, n = 460, ext = hrstest[i, :
number of items to replace is not a multiple of replacement length

The real output is a list for each polygon (good), but instead of containing 460 points, each contains just one value, which looks like a longitude value in the crs I&#39;m using (16N UTM).


 





Creating &quot;hrstest&quot;

structure(list(id = c("10_17", "10_18", "103_19"), area = c(9330.16971002,
11839.809165, 19727.1596434), Shape_Leng = c(45088.8473474, 54527.1219449,
203178.756164), Shape_Area = c(92923453.7616, 117622362.856,
168371931.317), size = c(92.9234537616, 117.622362856, 168.371931317
), geometry = structure(list(structure(list(list(structure(c(385555.4573,
385555.4573, 385758.714, 385855.4573, 385855.4573, 385991.2446,
386194.3538, 386224.7214, 386971.075, 387715.4883, 387717.4286,
387955.4573, 387955.4573, 387955.4573, 388049.9122, 388255.4573,
388255.4573, 388463.7821, 389035.5557, 388798.3266, 388463.7821,
387848.1566, 387717.4286, 387364.8016, 387070.6686, 386971.075,
386402.3183, 386224.7214, 385478.3678, 384944.6887, 384732.0143,
383985.6607, 383837.6516, 383239.3071, 382492.9535, 382361.38,
381746.6, 381468.6452, 381000.2464, 380278.6783, 380253.8928,
380081.1312, 379507.5392, 378761.1857, 378697.631, 378014.8321,
377268.4785, 376527.7521, 376522.1249, 376519.1168, 375775.7714,
375310.1579, 375029.4178, 374738.2147, 374465.3875, 374488.8299,
374812.1018, 375029.4178, 375549.9777, 375775.7714, 376522.1249,
376988.419, 376933.8419, 376735.4819, 376687.5998, 376775.8782,
377268.4785, 377305.192, 377648.6809, 378014.8321, 378457.775,
378761.1857, 379030.9914, 379507.5392, 379989.2528, 380253.8928,
380886.3294, 381000.2464, 381341.2598, 381567.7587, 381746.6,
382492.9535, 382558.9363, 383239.3071, 383985.6607, 384413.5521,
384605.2871, 384732.0143, 385207.7997, 385395.4148, 385468.3629,
385478.3678, 385560.0056, 385555.4573, 5179361.7933, 5179061.7933,
5179061.7933, 5178936.9796, 5178761.7933, 5178761.7933, 5178499.7514,
5178453.178, 5178109.6814, 5177900.5453, 5177900.0002, 5177832.2868,
5177639.669, 5177261.7933, 5177261.7933, 5177261.7933, 5177742.4158,
5177641.5691, 5177007.0442, 5176260.6907, 5175993.9917, 5175514.3371,
5175369.7666, 5174767.9835, 5174021.6299, 5173868.6523, 5173275.2764,
5173159.5539, 5173057.5855, 5173275.2764, 5173397.8173, 5173970.0677,
5174021.6299, 5174115.973, 5174326.2719, 5174380.1449, 5174631.8678,
5174767.9835, 5175004.2082, 5175514.3371, 5175528.4884, 5175514.3371,
5175338.8984, 5174795.5846, 5174767.9835, 5174405.6552, 5174109.817,
5174021.6299, 5174020.0274, 5174021.6299, 5174261.5425, 5174767.9835,
5175075.3551, 5175514.3371, 5176260.6907, 5177007.0442, 5177753.3978,
5178030.7593, 5178499.7514, 5178671.2707, 5179043.8197, 5179246.105,
5179992.4585, 5180738.8121, 5181485.1657, 5182231.5193, 5182929.4197,
5182977.8728, 5183281.4913, 5183605.1416, 5183724.2264, 5183877.6992,
5183884.9422, 5183897.7352, 5183724.2264, 5183679.5435, 5183336.0766,
5183274.21, 5182977.8728, 5182773.5188, 5182612.1628, 5182245.657,
5182231.5193, 5182178.2832, 5181993.6597, 5181642.5123, 5181485.1657,
5181345.35, 5180738.8121, 5179992.4585, 5179586.5723, 5179530.9047,
5179361.7933, 5179361.7933), dim = c(94L, 2L)), structure(c(384355.4573,
384355.4573, 384655.4573, 384655.4573, 384355.4573, 5179361.7933,
5178761.7933, 5178761.7933, 5179361.7933, 5179361.7933), dim = c(5L,
2L)))), class = c("XY", "MULTIPOLYGON", "sfg")), structure(list(
list(structure(c(385855.4573, 385855.4573, 385959.4383, 385855.4573,
5179061.7933, 5179117.0398, 5179061.7933, 5179061.7933), dim = c(4L,
2L))), list(structure(c(385855.4573, 385855.4573, 385855.4573,
385991.2446, 386155.4573, 386155.4573, 386524.0766, 386455.4573,
386455.4573, 386755.4573, 386755.4573, 387116.4588, 387180.3257,
387576.9181, 387715.4883, 387955.4573, 387955.4573, 388049.9122,
388139.1863, 387947.7161, 387576.9181, 387132.6196, 386792.6041,
386008.2902, 385223.9763, 385009.3406, 384439.6623, 383655.3484,
382871.0344, 382361.38, 382086.7205, 381302.4065, 380518.0926,
379733.7786, 378949.4647, 378165.1507, 377380.8368, 376596.5228,
375812.2089, 375180.608, 375027.8949, 374586.5939, 374339.1955,
374338.8903, 374514.1207, 374989.701, 375027.8949, 375623.0661,
375812.2089, 376346.0161, 376596.5228, 376606.7716, 376623.959,
376652.7501, 376596.5228, 376500.7639, 376319.777, 375947.4693,
375812.2089, 375278.5195, 375027.8949, 374880.4479, 374843.8941,
374795.3957, 374907.8486, 374958.3542, 375027.8949, 375083.1332,
375812.2089, 376036.5158, 376596.5228, 376809.2076, 377380.8368,
377910.8019, 378165.1507, 378613.7816, 378949.4647, 379733.7786,
380256.1042, 380518.0926, 381302.4065, 382086.7205, 382274.0792,
382871.0344, 383035.7792, 383398.7978, 383655.3484, 384024.9363,
384037.0755, 384283.8356, 384413.5521, 384433.2484, 384439.6623,
384733.319, 385095.3502, 385223.9763, 385468.3629, 385708.9576,
385560.0056, 385555.4573, 385555.4573, 385758.714, 385855.4573,
5179061.7933, 5178936.9796, 5178761.7933, 5178761.7933, 5178761.7933,
5178957.6457, 5178761.7933, 5178761.7933, 5178461.7933, 5178461.7933,
5178638.8576, 5178447.0526, 5178401.8646, 5178051.1884, 5177900.5453,
5177639.669, 5177261.7933, 5177261.7933, 5176833.2367, 5176048.9227,
5175648.0172, 5175264.6088, 5175021.0027, 5174802.3614, 5174544.6308,
5174480.2948, 5174251.0143, 5174197.4447, 5174319.7744, 5174380.1449,
5174412.6794, 5174397.3025, 5174454.1085, 5174473.8842, 5174279.5017,
5174146.3891, 5173988.4062, 5173891.9155, 5173950.4157, 5174480.2948,
5174616.1845, 5175264.6088, 5176048.9227, 5176833.2367, 5177617.5506,
5178401.8646, 5178457.3636, 5179186.1785, 5179378.1207, 5179970.4925,
5180672.7173, 5180754.8064, 5181539.1204, 5182323.4343, 5182813.0528,
5183107.7483, 5183364.3035, 5183892.0622, 5184019.0524, 5184676.3761,
5185183.8974, 5185460.6901, 5185797.7732, 5186245.004, 5186560.2336,
5186701.8117, 5186896.7494, 5187029.318, 5187772.3159, 5187813.6319,
5187921.0511, 5187813.6319, 5187542.5725, 5187029.318, 5186742.0253,
5186245.004, 5185952.466, 5185726.4063, 5185460.6901, 5185397.0371,
5185137.0641, 5184793.003, 5184676.3761, 5184344.6481, 5184203.3704,
5183892.0622, 5183587.8129, 5183107.7483, 5183070.9736, 5182323.4343,
5181642.5123, 5181539.1204, 5181515.2186, 5180754.8064, 5179970.4925,
5179814.894, 5179586.5723, 5179361.7933, 5179361.7933, 5179361.7933,
5179061.7933, 5179061.7933, 5179061.7933), dim = c(103L,
2L)), structure(c(384655.4573, 384655.4573, 384355.4573,
384355.4573, 384655.4573, 5178761.7933, 5179361.7933, 5179361.7933,
5178761.7933, 5178761.7933), dim = c(5L, 2L)), structure(c(375655.4573,
375655.4573, 375398.826, 375355.4573, 375355.4573, 375355.4573,
375355.4573, 375503.2266, 375505.9722, 375655.4573, 375655.4573,
5185088.8491, 5185361.7933, 5185361.7933, 5185361.7933, 5185331.9535,
5185242.2479, 5185061.7933, 5185061.7933, 5185061.7933, 5185061.7933,
5185088.8491), dim = c(11L, 2L)), structure(c(376255.4573,
376255.4573, 375955.4573, 375955.4573, 376255.4573, 5185961.7933,
5186261.7933, 5186261.7933, 5185961.7933, 5185961.7933), dim = c(5L,
2L)), structure(c(377455.4573, 377455.4573, 377155.4573,
377155.4573, 377455.4573, 5186861.7933, 5187161.7933, 5187161.7933,
5186861.7933, 5186861.7933), dim = c(5L, 2L)))), class = c("XY",
"MULTIPOLYGON", "sfg")), structure(list(list(structure(c(294055.4573,
292871.3803, 292878.7851, 293431.1617, 293807.3674, 294355.4573,
294355.4573, 294055.4573, 294055.4573, 5111561.7933, 5111561.7933,
5111565.2434, 5111666.5591, 5111869.1482, 5112101.0424, 5111861.7933,
5111861.7933, 5111561.7933), dim = c(9L, 2L))), list(structure(c(300955.4573,
300655.4573, 300655.4573, 300955.4573, 300955.4573, 301255.4573,
301255.4573, 301257.6623, 301555.4573, 301555.4573, 301589.8148,
301855.4573, 301855.4573, 301555.4573, 301555.4573, 302523.3409,
302706.5714, 302742.6684, 303093.19, 303735.4698, 303655.4573,
303655.4573, 304020.0907, 304021.7722, 304568.9386, 304858.3603,
304950.3545, 304994.1833, 304950.3545, 304931.4019, 304665.0746,
304562.7652, 304950.3545, 305041.1855, 305424.5251, 305625.3124,
305654.7787, 305455.4573, 305455.4573, 305704.9745, 305780.6823,
305813.5565, 305755.4573, 305755.4573, 305835.5938, 305848.8938,
305727.597, 305161.693, 304950.3545, 304021.7722, 303425.2564,
303355.4573, 303355.4573, 303055.4573, 303055.4573, 303345.7875,
303093.19, 302164.6077, 301271.6697, 301236.0254, 300307.4432,
299755.4573, 299755.4573, 299455.4573, 299455.4573, 299378.8609,
298855.4573, 298855.4573, 298275.5273, 298255.4573, 298255.4573,
297955.4573, 297955.4573, 297655.4573, 297655.4573, 297497.5827,
297388.0502, 296593.1141, 296155.4573, 296155.4573, 296008.3466,
295664.5319, 295263.5263, 294735.9496, 294655.4573, 294655.4573,
294955.4573, 294955.4573, 294355.4573, 294355.4573, 294055.4573,
294055.4573, 293155.4573, 293155.4573, 293755.4573, 293755.4573,
294355.4573, 294355.4573, 294655.4573, 294655.4573, 294955.4573,
294955.4573, 295255.4573, 295255.4573, 294955.4573, 294955.4573,
294655.4573, 294655.4573, 294055.4573, 294055.4573, 293755.4573,
293755.4573, 293455.4573, 293455.4573, 293155.4573, 293155.4573,
292855.4573, 292855.4573, 293455.4573, 293455.4573, 292855.4573,
292855.4573, 293155.4573, 293155.4573, 292555.4573, 292555.4573,
292255.4573, 292255.4573, 291950.2028, 291425.3219, 291021.6206,
290093.0383, 289864.3852, 289164.456, 288859.5268, 288588.2921,
288955.4573, 288955.4573, 289255.4573, 289255.4573, 288955.4573,
288955.4573, 288089.596, 288055.4573, 288055.4573, 287944.1494,
287831.054, 287709.0977, 288003.6653, 288235.8738, 288673.4505,
289164.456, 290093.0383, 290145.1913, 291021.6206, 291424.1196,
291722.5007, 291955.4573, 291955.4573, 292855.4573, 292855.4573,
293755.4573, 293755.4573, 294355.4573, 294355.4573, 294655.4573,
294655.4573, 294955.4573, 294955.4573, 295555.4573, 295555.4573,
295855.4573, 295855.4573, 295555.4573, 295555.4573, 295255.4573,
295255.4573, 294955.4573, 294955.4573, 294499.0442, 294735.9496,
295530.4953, 295664.5319, 296251.0113, 296593.1141, 296783.157,
297521.6964, 297733.1901, 298039.4201, 298450.2787, 299378.8609,
299720.685, 300307.4432, 301104.079, 300955.4573, 300955.4573,
5114261.7933, 5114261.7933, 5113961.7933, 5113961.7933, 5114261.7933,
5114261.7933, 5114453.9353, 5114452.3059, 5114281.4997, 5114261.7933,
5114261.7933, 5114109.4288, 5113961.7933, 5113961.7933, 5113661.7933,
5113661.7933, 5113523.7236, 5113503.7047, 5113309.3101, 5112761.7933,
5112761.7933, 5112461.7933, 5112461.7933, 5112459.2775, 5111666.5591,
5110737.9768, 5109975.7734, 5109809.3945, 5109273.2444, 5108880.8123,
5107952.23, 5107023.6478, 5106245.86, 5106095.0655, 5105166.4832,
5104237.901, 5104061.7933, 5104061.7933, 5103761.7933, 5103761.7933,
5103309.3187, 5102861.7933, 5102861.7933, 5102561.7933, 5102561.7933,
5102380.7365, 5101452.1542, 5100523.5719, 5100329.961, 5099776.1589,
5099594.9897, 5099565.8326, 5099861.7933, 5099861.7933, 5099561.7933,
5099561.7933, 5099456.2762, 5099416.5347, 5099594.9897, 5099601.4896,
5099801.3524, 5099883.9439, 5100161.7933, 5100161.7933, 5099928.8318,
5099940.2926, 5100027.7207, 5100161.7933, 5100161.7933, 5100169.4184,
5100461.7933, 5100461.7933, 5100283.396, 5100397.3736, 5100461.7933,
5100461.7933, 5100523.5719, 5100765.9725, 5100987.3733, 5101061.7933,
5101061.7933, 5101235.7215, 5101452.1542, 5101714.9121, 5101728.9843,
5101961.7933, 5101961.7933, 5102561.7933, 5102561.7933, 5102861.7933,
5102861.7933, 5102561.7933, 5102561.7933, 5102861.7933, 5102861.7933,
5103161.7933, 5103161.7933, 5103461.7933, 5103461.7933, 5103761.7933,
5103761.7933, 5104061.7933, 5104061.7933, 5104961.7933, 5104961.7933,
5105861.7933, 5105861.7933, 5106161.7933, 5106161.7933, 5106461.7933,
5106461.7933, 5106161.7933, 5106161.7933, 5105261.7933, 5105261.7933,
5104961.7933, 5104961.7933, 5104661.7933, 5104661.7933, 5104061.7933,
5104061.7933, 5103161.7933, 5103161.7933, 5102861.7933, 5102861.7933,
5102261.7933, 5102261.7933, 5101986.7176, 5102047.1621, 5102380.7365,
5102525.8381, 5103123.2076, 5103309.3187, 5103898.3098, 5104237.901,
5104661.7933, 5104661.7933, 5105261.7933, 5105261.7933, 5105861.7933,
5105861.7933, 5105561.7933, 5105561.7933, 5105632.2082, 5105861.7933,
5105861.7933, 5106095.0655, 5107023.6478, 5107952.23, 5108297.9388,
5108880.8123, 5109274.7497, 5109779.2704, 5109809.3945, 5110501.7185,
5110737.9768, 5110961.7933, 5110961.7933, 5111135.0415, 5111554.3744,
5111261.7933, 5111261.7933, 5110961.7933, 5110961.7933, 5110361.7933,
5110361.7933, 5110961.7933, 5110961.7933, 5110661.7933, 5110661.7933,
5110961.7933, 5110961.7933, 5111261.7933, 5111261.7933, 5111561.7933,
5111561.7933, 5111861.7933, 5111861.7933, 5112161.7933, 5112161.7933,
5112262.0268, 5112595.1413, 5112717.6672, 5113523.7236, 5114080.3961,
5114452.3059, 5115291.7351, 5115380.8881, 5115504.8637, 5115671.1977,
5115569.6196, 5115380.8881, 5115126.2997, 5114561.7933, 5114561.7933,
5114261.7933), dim = c(196L, 2L)), structure(c(297955.4573, 297955.4573,
298555.4573, 298555.4573, 298555.4573, 298255.8337, 297955.4573,
5114261.7933, 5113361.7933, 5113361.7933, 5113662.9351, 5114261.7933,
5114261.7933, 5114261.7933), dim = c(7L, 2L)), structure(c(299755.4573,
299755.4573, 299755.4573, 299455.4573, 299455.4573, 299455.4573,
299755.4573, 299755.4573, 300055.4573, 300055.4573, 300955.4573,
300955.4573, 301255.4573, 301255.4573, 300955.4573, 300955.4573,
300655.4573, 300655.4573, 300955.4573, 300955.4573, 300955.4573,
301282.3034, 301555.4573, 301555.4573, 301555.4573, 300955.4573,
300955.4573, 300055.4573, 300055.4573, 300355.4573, 300355.4573,
300355.4573, 299755.4573, 5112761.7933, 5112956.0212, 5113061.7933,
5113061.7933, 5113052.3448, 5112761.7933, 5112761.7933, 5112461.7933,
5112461.7933, 5112161.7933, 5112161.7933, 5111861.7933, 5111861.7933,
5112161.7933, 5112161.7933, 5112461.7933, 5112461.7933, 5112761.7933,
5112761.7933, 5112999.6538, 5113061.7933, 5113061.7933, 5113061.7933,
5113125.8941, 5113361.7933, 5113361.7933, 5113661.7933, 5113661.7933,
5113361.7933, 5113361.7933, 5112934.0395, 5112761.7933, 5112761.7933
), dim = c(33L, 2L)), structure(c(302155.4573, 302155.4573, 303055.4573,
303055.4573, 303355.4573, 303355.4573, 302155.4573, 5112761.7933,
5112461.7933, 5112461.7933, 5112161.7933, 5112161.7933, 5112761.7933,
5112761.7933), dim = c(7L, 2L)), structure(c(301555.4573, 301555.4573,
301855.4573, 301855.4573, 301555.4573, 301555.4573, 302155.4573,
302155.4573, 301555.4573, 5111861.7933, 5111561.7933, 5111561.7933,
5111261.7933, 5111261.7933, 5110961.7933, 5110961.7933, 5111861.7933,
5111861.7933), dim = c(9L, 2L)), structure(c(304255.4573, 304255.4573,
304555.4573, 304555.4573, 304255.4573, 5111261.7933, 5110961.7933,
5110961.7933, 5111261.7933, 5111261.7933), dim = c(5L, 2L)),
structure(c(302755.4573, 302755.4573, 303055.4573, 303055.4573,
302755.4573, 5110961.7933, 5110661.7933, 5110661.7933, 5110961.7933,
5110961.7933), dim = c(5L, 2L)), structure(c(301255.4573,
301255.4573, 300655.4573, 300655.4573, 301255.4573, 5110661.7933,
5110961.7933, 5110961.7933, 5110661.7933, 5110661.7933), dim = c(5L,
2L)), structure(c(299755.4573, 299755.4573, 299455.4573,
299455.4573, 298855.4573, 298855.4573, 300355.4573, 300355.4573,
299755.4573, 5110661.7933, 5110961.7933, 5110961.7933, 5110661.7933,
5110661.7933, 5110361.7933, 5110361.7933, 5110661.7933, 5110661.7933
), dim = c(9L, 2L)), structure(c(300955.4573, 300955.4573,
301255.4573, 301255.4573, 300955.4573, 300955.4573, 300655.4573,
300655.4573, 300955.4573, 5110061.7933, 5109761.7933, 5109761.7933,
5110061.7933, 5110061.7933, 5110361.7933, 5110361.7933, 5110061.7933,
5110061.7933), dim = c(9L, 2L)), structure(c(296755.4573,
296755.4573, 296155.4573, 296155.4573, 296455.4573, 296455.4573,
296755.4573, 5110061.7933, 5110361.7933, 5110361.7933, 5109761.7933,
5109761.7933, 5110061.7933, 5110061.7933), dim = c(7L, 2L
)), structure(c(292255.4573, 292255.4573, 291955.4573, 291955.4573,
292255.4573, 5110361.7933, 5110661.7933, 5110661.7933, 5110361.7933,
5110361.7933), dim = c(5L, 2L)), structure(c(297655.4573,
297655.4573, 297355.4573, 297355.4573, 297655.4573, 5111261.7933,
5111561.7933, 5111561.7933, 5111261.7933, 5111261.7933), dim = c(5L,
2L)), structure(c(299155.4573, 299155.4573, 298255.4573,
298255.4573, 299155.4573, 5112461.7933, 5112761.7933, 5112761.7933,
5112461.7933, 5112461.7933), dim = c(5L, 2L)), structure(c(299755.4573,
299755.4573, 300055.4573, 300055.4573, 299755.4573, 5109461.7933,
5109161.7933, 5109161.7933, 5109461.7933, 5109461.7933), dim = c(5L,
2L)), structure(c(291955.4573, 291955.4573, 292555.4573,
292555.4573, 291955.4573, 5109161.7933, 5108861.7933, 5108861.7933,
5109161.7933, 5109161.7933), dim = c(5L, 2L)), structure(c(302455.4573,
302455.4573, 302755.4573, 302755.4573, 303355.4573, 303355.4573,
302755.4573, 302755.4573, 302455.4573, 5108561.7933, 5107661.7933,
5107661.7933, 5107961.7933, 5107961.7933, 5108261.7933, 5108261.7933,
5108561.7933, 5108561.7933), dim = c(9L, 2L)), structure(c(300355.4573,
300355.4573, 299755.4573, 299755.4573, 298855.4573, 298855.4573,
298555.4573, 298555.4573, 298255.4573, 298255.4573, 297955.4573,
297955.4573, 299455.4573, 299455.4573, 300355.4573, 300355.4573,
300655.4573, 300655.4573, 300055.4573, 300055.4573, 300355.4573,
5107961.7933, 5108261.7933, 5108261.7933, 5107961.7933, 5107961.7933,
5107661.7933, 5107661.7933, 5107061.7933, 5107061.7933, 5107661.7933,
5107661.7933, 5106761.7933, 5106761.7933, 5106461.7933, 5106461.7933,
5106761.7933, 5106761.7933, 5107661.7933, 5107661.7933, 5107961.7933,
5107961.7933), dim = c(21L, 2L)), structure(c(290155.4573,
290155.4573, 289555.4573, 289555.4573, 288955.4573, 288955.4573,
289255.4573, 289255.4573, 289555.4573, 289555.4573, 289855.4573,
289855.4573, 290155.4573, 5106761.7933, 5107061.7933, 5107061.7933,
5107361.7933, 5107361.7933, 5107061.7933, 5107061.7933, 5106761.7933,
5106761.7933, 5106461.7933, 5106461.7933, 5106761.7933, 5106761.7933
), dim = c(13L, 2L)), structure(c(300355.4573, 300355.4573,
300055.4573, 300055.4573, 299755.4573, 299755.4573, 298855.4573,
298855.4573, 298255.4573, 298255.4573, 297955.4573, 297955.4573,
298255.4573, 298255.4573, 298555.4573, 298555.4573, 298855.4573,
298855.4573, 299455.4573, 299455.4573, 299755.4573, 299755.4573,
298855.4573, 298855.4573, 299455.4573, 299455.4573, 300055.4573,
300055.4573, 299755.4573, 299755.4573, 300055.4573, 300055.4573,
300655.4573, 300655.4573, 300355.4573, 300355.4573, 300655.4573,
300655.4573, 300355.4573, 300355.4573, 299455.4573, 299455.4573,
298855.4573, 298855.4573, 298555.4573, 298555.4573, 298255.4573,
298255.4573, 299155.4573, 299155.4573, 300355.4573, 5105261.7933,
5104961.7933, 5104961.7933, 5104661.7933, 5104661.7933, 5104961.7933,
5104961.7933, 5105261.7933, 5105261.7933, 5104961.7933, 5104961.7933,
5104661.7933, 5104661.7933, 5104361.7933, 5104361.7933, 5104061.7933,
5104061.7933, 5104361.7933, 5104361.7933, 5104061.7933, 5104061.7933,
5103461.7933, 5103461.7933, 5103161.7933, 5103161.7933, 5102861.7933,
5102861.7933, 5103161.7933, 5103161.7933, 5103461.7933, 5103461.7933,
5104061.7933, 5104061.7933, 5105261.7933, 5105261.7933, 5105861.7933,
5105861.7933, 5106161.7933, 5106161.7933, 5105861.7933, 5105861.7933,
5106161.7933, 5106161.7933, 5106461.7933, 5106461.7933, 5105861.7933,
5105861.7933, 5105561.7933, 5105561.7933, 5105261.7933, 5105261.7933
), dim = c(51L, 2L)), structure(c(300955.4573, 300955.4573,
301255.4573, 301255.4573, 300955.4573, 5105261.7933, 5104961.7933,
5104961.7933, 5105261.7933, 5105261.7933), dim = c(5L, 2L
)), structure(c(304555.4573, 304555.4573, 305155.4573, 305155.4573,
304855.4573, 304855.4573, 304555.4573, 5104661.7933, 5103761.7933,
5103761.7933, 5104361.7933, 5104361.7933, 5104661.7933, 5104661.7933
), dim = c(7L, 2L)), structure(c(301255.4573, 301255.4573,
301555.4573, 301555.4573, 301255.4573, 5103761.7933, 5103161.7933,
5103161.7933, 5103761.7933, 5103761.7933), dim = c(5L, 2L
)), structure(c(297655.4573, 297655.4573, 297355.4573, 297355.4573,
297655.4573, 5104061.7933, 5104361.7933, 5104361.7933, 5104061.7933,
5104061.7933), dim = c(5L, 2L)), structure(c(290755.4573,
290755.4573, 291055.4573, 291055.4573, 290755.4573, 5107661.7933,
5107061.7933, 5107061.7933, 5107661.7933, 5107661.7933), dim = c(5L,
2L)), structure(c(290155.4573, 290155.4573, 289855.4573,
289855.4573, 290155.4573, 5107661.7933, 5107961.7933, 5107961.7933,
5107661.7933, 5107661.7933), dim = c(5L, 2L)), structure(c(291055.4573,
291355.4573, 291355.4573, 291055.4573, 291055.4573, 290755.4573,
290755.4573, 291055.4573, 291055.4573, 5108261.7933, 5108261.7933,
5108561.7933, 5108561.7933, 5108261.7933, 5108261.7933, 5107961.7933,
5107961.7933, 5108261.7933), dim = c(9L, 2L)), structure(c(290455.4573,
290155.4573, 290155.4573, 290455.4573, 290455.4573, 290755.4573,
290755.4573, 290455.4573, 290455.4573, 5104961.7933, 5104961.7933,
5104361.7933, 5104361.7933, 5104961.7933, 5104961.7933, 5105261.7933,
5105261.7933, 5104961.7933), dim = c(9L, 2L)), structure(c(290755.4573,
290755.4573, 291355.4573, 291355.4573, 291055.4573, 291055.4573,
290755.4573, 5104661.7933, 5104361.7933, 5104361.7933, 5104961.7933,
5104961.7933, 5104661.7933, 5104661.7933), dim = c(7L, 2L
)), structure(c(296455.4573, 296455.4573, 296155.4573, 296155.4573,
296455.4573, 296455.4573, 297655.4573, 297655.4573, 297055.4573,
297055.4573, 296455.4573, 5102561.7933, 5102261.7933, 5102261.7933,
5101961.7933, 5101961.7933, 5101361.7933, 5101361.7933, 5101961.7933,
5101961.7933, 5102561.7933, 5102561.7933), dim = c(11L, 2L
)), structure(c(301855.4573, 301855.4573, 302155.4573, 302155.4573,
302455.4573, 302455.4573, 302755.4573, 302755.4573, 301855.4573,
5101361.7933, 5100761.7933, 5100761.7933, 5100461.7933, 5100461.7933,
5100761.7933, 5100761.7933, 5101361.7933, 5101361.7933), dim = c(9L,
2L)), structure(c(304555.4573, 304555.4573, 304855.4573,
304855.4573, 304555.4573, 5100761.7933, 5100461.7933, 5100461.7933,
5100761.7933, 5100761.7933), dim = c(5L, 2L)), structure(c(300955.4573,
300955.4573, 300655.4573, 300655.4573, 300955.4573, 5099861.7933,
5100761.7933, 5100761.7933, 5099861.7933, 5099861.7933), dim = c(5L,
2L)), structure(c(300055.4573, 300055.4573, 299755.4573,
299755.4573, 300055.4573, 5100761.7933, 5101061.7933, 5101061.7933,
5100761.7933, 5100761.7933), dim = c(5L, 2L)))), class = c("XY",
"MULTIPOLYGON", "sfg"))), class = c("sfc_MULTIPOLYGON", "sfc"
), precision = 0, bbox = structure(c(xmin = 287709.0977, ymin = 5099416.5347,
xmax = 389035.5557, ymax = 5187921.0511), class = "bbox"), crs = structure(list(
input = "unknown", wkt = "PROJCRS[&quot;unknown&quot;,\n BASEGEOGCRS[&quot;GCS_unknown&quot;,\n DATUM[&quot;World Geodetic System 1984&quot;,\n ELLIPSOID[&quot;WGS 84&quot;,6378137,298.257223563,\n LENGTHUNIT[&quot;metre&quot;,1]],\n ID[&quot;EPSG&quot;,6326]],\n PRIMEM[&quot;Greenwich&quot;,0,\n ANGLEUNIT[&quot;Degree&quot;,0.0174532925199433]]],\n CONVERSION[&quot;UTM zone 16N&quot;,\n METHOD[&quot;Transverse Mercator&quot;,\n ID[&quot;EPSG&quot;,9807]],\n PARAMETER[&quot;Latitude of natural origin&quot;,0,\n ANGLEUNIT[&quot;Degree&quot;,0.0174532925199433],\n ID[&quot;EPSG&quot;,8801]],\n PARAMETER[&quot;Longitude of natural origin&quot;,-87,\n ANGLEUNIT[&quot;Degree&quot;,0.0174532925199433],\n ID[&quot;EPSG&quot;,8802]],\n PARAMETER[&quot;Scale factor at natural origin&quot;,0.9996,\n SCALEUNIT[&quot;unity&quot;,1],\n ID[&quot;EPSG&quot;,8805]],\n PARAMETER[&quot;False easting&quot;,500000,\n LENGTHUNIT[&quot;metre&quot;,1],\n ID[&quot;EPSG&quot;,8806]],\n PARAMETER[&quot;False northing&quot;,0,\n LENGTHUNIT[&quot;metre&quot;,1],\n ID[&quot;EPSG&quot;,8807]],\n ID[&quot;EPSG&quot;,16016]],\n CS[Cartesian,2],\n AXIS[&quot;(E)&quot;,east,\n ORDER[1],\n LENGTHUNIT[&quot;metre&quot;,1,\n ID[&quot;EPSG&quot;,9001]]],\n AXIS[&quot;(N)&quot;,north,\n ORDER[2],\n LENGTHUNIT[&quot;metre&quot;,1,\n ID[&quot;EPSG&quot;,9001]]]]"), class = "crs"), n_empty = 0L)), row.names = c(NA,
-3L), sf_column = "geometry", agr = structure(c(id = NA_integer_,
area = NA_integer_, Shape_Leng = NA_integer_, Shape_Area = NA_integer_,
size = NA_integer_), levels = c("constant", "aggregate", "identity"
), class = "factor"), class = c("sf", "tbl_df", "tbl", "data.frame"
))

Creating &quot;samplexy&quot;

structure(list(X = c(293859.5241, 297077.2033, 293824.4975, 302682.9115,
298934.3725, 302925.7667, 305010.6482, 292583.6686, 300802.0569,
293888.2412, 293998.3872, 297002.6123, 297002.5627, 300887.6253,
304739.1686), Y = c(5107678.897, 5103268.472, 5107719.002, 5100904.022,
5103984.938, 5112739.93, 5102591.506, 5105784.204, 5105996.207,
5109226.247, 5107712.057, 5109400.563, 5104080.651, 5105794.349,
5106965.357), geometry = structure(list(structure(c(293859.5241,
5107678.897), class = c("XY", "POINT", "sfg")), structure(c(297077.2033,
5103268.472), class = c("XY", "POINT", "sfg")), structure(c(293824.4975,
5107719.002), class = c("XY", "POINT", "sfg")), structure(c(302682.9115,
5100904.022), class = c("XY", "POINT", "sfg")), structure(c(298934.3725,
5103984.938), class = c("XY", "POINT", "sfg")), structure(c(302925.7667,
5112739.93), class = c("XY", "POINT", "sfg")), structure(c(305010.6482,
5102591.506), class = c("XY", "POINT", "sfg")), structure(c(292583.6686,
5105784.204), class = c("XY", "POINT", "sfg")), structure(c(300802.0569,
5105996.207), class = c("XY", "POINT", "sfg")), structure(c(293888.2412,
5109226.247), class = c("XY", "POINT", "sfg")), structure(c(293998.3872,
5107712.057), class = c("XY", "POINT", "sfg")), structure(c(297002.6123,
5109400.563), class = c("XY", "POINT", "sfg")), structure(c(297002.5627,
5104080.651), class = c("XY", "POINT", "sfg")), structure(c(300887.6253,
5105794.349), class = c("XY", "POINT", "sfg")), structure(c(304739.1686,
5106965.357), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT",
"sfc"), precision = 0, bbox = structure(c(xmin = 292583.6686,
ymin = 5100904.022, xmax = 305010.6482, ymax = 5112739.93), class = "bbox"), crs = structure(list(
input = NA_character_, wkt = NA_character_), class = "crs"), n_empty = 0L)), sf_column = "geometry", agr = structure(c(X = NA_integer_,
Y = NA_integer_), levels = c("constant", "aggregate", "identity"
), class = "factor"), row.names = c("1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15"), class = c("sf",
"data.frame"))



  [1]: https://i.stack.imgur.com/s2L40.png

</details>


# 答案1
**得分**: 1

1. 使用双括号分配一个列表项([为什么][1]?)
2. 将您的类 'sf' 对象 "samplexy" 转换为 `randomPoints` 所需的类 'Spatial*':
3. 使用类 'extent' 的对象设置范围:

```R
library(sf)
library(dismo)
library(raster)

for(i in 1:nrow(hrstest)){
  papoints[[i]] <-   ## (1)
    dismo::randomPoints(mask = roads, n = 460,
                        p = as_Spatial(samplexy), ## (2)
                        ext = extent(st_bbox(hrstest[i,])) ## (3)
                        )
}
## > papoints[[1]] |> head()
##             x       y
## [1,] 313040.7 5183496
## [2,] 373836.6 5165795
## [3,] 292775.4 5130393
## [4,] 313040.7 5165795
## [5,] 353571.3 5103842
## [6,] 333306.0 5174645
英文:

It should work if you

  1. use the double bracket to assign a list item (why?)
  2. convert your class 'sf' object "samplexy" to class 'Spatial*' required by randomPoints
  3. set the extent with an object of class 'extent':
library(sf)
library(dismo)
library(raster)

for(i in 1:nrow(hrstest)){
  papoints[[i]] &lt;-   ## (1)
    dismo::randomPoints(mask = roads, n = 460,
                        p = as_Spatial(samplexy), ## (2)
                        ext = extent(st_bbox(hrstest[i,])) ## (3)
                        )
}
## &gt; papoints[[1]] |&gt; head()
##             x       y
## [1,] 313040.7 5183496
## [2,] 373836.6 5165795
## [3,] 292775.4 5130393
## [4,] 313040.7 5165795
## [5,] 353571.3 5103842
## [6,] 333306.0 5174645

huangapple
  • 本文由 发表于 2023年6月6日 05:13:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410052.html
匿名

发表评论

匿名网友

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

确定