This code will work on the lastest dev build of QGIS.
from qgis.utils import iface
from qgis.core import *
from PyQt4.QtCore import QVariant
import random
def createRandomPoints(count):
# Create a new memory layer to store the points.
vl = QgsVectorLayer("Point", "distance nodes", "memory")
pr = vl.dataProvider()
pr.addAttributes( [ QgsField("distance", QVariant.Int) ] )
layer = iface.mapCanvas().currentLayer()
# For each selected object
for feature in layer.selectedFeatures():
geom = feature.geometry()
length = geom.length()
feats = []
# Loop until we reach the needed count of points.
for i in xrange(0,count):
# Get the random distance along the line.
distance = random.uniform(0, length)
# Work out the location of the point at that distance.
point = geom.interpolate(distance)
# Create the new feature.
fet = QgsFeature()
fet.setAttributeMap( { 0 : distance } )
fet.setGeometry(point)
feats.append(fet)
pr.addFeatures(feats)
vl.updateExtents()
QgsMapLayerRegistry.instance().addMapLayer(vl)
I know you said you are not very familiar with Python code but you should be able to run this pretty easy. Copy the above code into a file (mine is called locate.py
) and place it in your ~/.qgis/python
if you are on Windows 7 that will be in C:\Users\{your user name}\.qgis\python\
or on Windows XP C:\Documents and Settings\{your user name}\.qgis\python\
Once the file is in the python folder open QGIS and select some line objects.
Then open the Python console and run the following code:
import locate.py
locate.createRandomPoints(10)
The result should look something like this
If you want to run it again just select some more lines and run locate.createRandomPoints(10)
in the Python console again.
Note: locate.createRandomPoints(10) the 10 here is the number of points to generate per line
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
As comments could be deleted at a point, here the answer that helped solve the problem:
Sounds like your polygon data and the points have different CRS.