Monday 12 February 2018

random - Randomly sampling points in R with minimum distance constraint?


I'm trying to randomly select a number of points within my data frame (example data below) with a constraint that the minimum distance between the selected point must be greater than a certain distance. I managed to do the randomly selection bit using the sample function in R, but I can't figure out how to add the constraint bit into my code. I suppose this must involve using some spatial analysis package in R but I haven't got a clue where to start.


I know ArcGIS has a tool called Create Random Points which can specify the minimum distance between points. But my situation requires a larger number of repeated sampling, thus making me feel doing this in R would be much easier because it can be incorporated with a loop.


Example data:


grid_index x        y

grid_168 323012.5 674187.5
grid_169 323012.5 674212.5
grid_292 323037.5 672287.5
grid_293 323037.5 672312.5
grid_368 323037.5 674187.5
grid_369 323037.5 674212.5

Answer



You can do this either with R or ArcGIS independently.


With ArcGIS, first create a feature class (e.g. shape file) from your grid coordinates. Then use this grid feature class as constraining_extent parameter of "Create Random Points" tool.


The only coding you have to do is to put that tool in a loop which can be achieved via Model Builder or Arcpy.



here is a sample (100 iteration):


import arcpy

for i in range(100):
print i
arcpy.CreateRandomPoints_management("c:/data/project", "samplepoints", "c:/data/studyarea.shp", "", 500, "", "POINT", "")

For R, use genrandompnts from spatialecology website. This tool is similiar to ArcGis "Create Random Points" tool.


In addition, there is another thread similiar to your quesiton.


How to create random points outside polygons?



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...