I have a shapefile with track lines and I need to generate random points from each 1 km of every line. There is a function in QGIS Random points but it offers to generate random points only inside a polygon. Any ideas for a specific plug-in or another solution?
[GIS] How to generate random points that lie on a line in QGIS
qgisrandom
Related Solutions
In ArcGIS, this can be done with Create random point if you have spatial analyst or 3D analyst or Advanced(ArcInfo) licence
select each shapefile as "constraining_feature_class" and set the "minimum_allowed_distance"to 250 m.
In QGIS, from what I know, you can use "Vector ‣ Research Tools ‣ Random points" with a constraining feature class, but not for distance. Again, you can use this tool in a loop to add points one by one until you reach the required number.
On the other hand, a solution for simple random sampling with distance contraint is to recursively add your random point and erase the buffer around your random point from the feature class until all your features are completely erased. As mentioned by @Whuber, this will not optimize the number of sample points, but you can control the number of points per strata.
Finally, the optimal number of points with a distance constraint can be achieved by systematic sampling : create a grid (with fishnet) with evenly spaced points (in your case 250 m) and randomly select a rotation angle and shift value betwen 0 and 250 in X and Y direction. This will optimize the number of point samples (and you could randomly subselect some points to reach a given number inside each startum). However, note that systematic sample could be biased because of the spatial autocorrelation.
I had initially posted the question on geonet. The matter being rather urgent, I posted the question here as well when I didn't recieve an answer there. In the meantime, Xander Bakker posted an excellent script that solves the problem neatly. The full answer can be view here, this is the section that solves the problem (with some slight modifications from me).
import arcpy
import random
import math
def main(path,fc_pol,fc_pnt,fc_mp,min_dist,number_of_points):
# path = path for the input and output data
# fc_pol = name of the input polygon shapefile
# fc_pnt = name of the output point shapefile
# fc_mp = name of the output point shapefile, used for visualization it and some manual checking
arcpy.env.overwriteOutput = True
arcpy.CreateFeatureclass_management(path, fc_pnt, "POINT")
arcpy.CreateFeatureclass_management(path, fc_mp, "POINT")
lst_all_pnts = []
lst_mp = []
cnt_pol = 0
with arcpy.da.SearchCursor(path+fc_pol, "SHAPE@") as curs:
for row in curs:
cnt_pol += 1
# print "polygon: {0}".format(cnt_pol)
polygon = row[0]
lst_pnts = []
sr = polygon.spatialReference
# create points inside polygons
while len(lst_pnts) < number_of_points:
print " - len(lst_pnts)={0}".format(len(lst_pnts))
lst_pnts, bln_ok = add_random_point(lst_pnts, lst_all_pnts, polygon, min_dist)
if bln_ok == False:
# saturation reached
print "saturation reached"
break
# write points
if len(lst_pnts) > 0:
mp = create_multipoint(lst_pnts, sr)
lst_mp.append(mp)
lst_all_pnts.extend(lst_pnts)
# create outputs
sr = polygon.spatialReference
lst_pntgeom = create_pointgeometry_list(lst_all_pnts, sr)
arcpy.CopyFeatures_management(lst_pntgeom, path + fc_pnt)
arcpy.CopyFeatures_management(lst_mp, path + fc_mp)
Best Answer
What about from the processing toolbox where there's a tool called Random points along line.
You can also set the minimum distance.