2

Can anybody provide a simple code to be run in the Python Console of QGIS (I am using 1.8 Lisboa) to generate X number of random points (say e.g. 5000) over a certain global shape of polygons (all Vector data). I then need to count the number of points per polygon and calculate the %. All that can be done via GUI but the catch is that I would need to repeat the procedure 100 times, i.e. generate 5000 random points 100 different times and then calculate the mean and standard deviation. Does anybody have a simple code for this (repeating the random generation many times)?

I´m a total beginner with Python.

  • 2
    You could create a model in processing. And then run the model in batch, for all the files you need. – Alexandre Neto Jun 27 '15 at 10:04
2

You can activate Python plugin "ftools" and do "Random Points..." (from menu Vector > Research Tools).

Random Points from ftools

  • 1
    ... and Alexandre Neto is right: If this random function is what you look for, you can run it X times as a model in the "Processing" window. – Stefan Jun 29 '15 at 15:28
2

You should specify more your question. It's unclear what mean by doing a mean and stdev.

Here's a sample code you can run in QGIS Python console (still without further iteration):

"""
RPG - Random Point Generator
Version: 2015-06-30 Geometa Lab HSR, http://twitter.com/geometalab
License: MIT license, http://opensource.org/licenses/MIT

To run this: 
0. Start QGIS (must be version > 2.0)
1. Load a polygon layer and set it as the active layer.
2. Open Python Console and show editor.
3. Load this script and run it! (Don't forget to save memory layer).
"""
import random,sys
# This parameter sets the amount of random points to be generated
pointId = 10
# Test if active layer is vector layer and of type polygon
layer = iface.activeLayer()
sys.stdout.write('RPG: ')
if not (layer and layer.type() == 0 and layer.geometryType() == 2):
    print("No polygon layer selected.")
else:
    # Prepare new temporary editable memory layer
    pointLayer = iface.addVectorLayer("Point?crs="+layer.crs().toWkt(), "random_points", "memory")
    xmin=xmax=ymin=ymax = 0.0
    # Create global bounding box from polygons/features
    for polygon in layer.getFeatures():
        bounds = polygon.geometry().boundingBox()
        xmin = bounds.xMinimum() if bounds.xMinimum() < xmin else xmin
        xmax = bounds.xMaximum() if bounds.xMaximum() > xmax else xmax
        ymin = bounds.yMinimum() if bounds.yMinimum() < ymin else ymin
        ymax = bounds.yMaximum() if bounds.yMaximum() > ymax else ymax                            
    # Iterate until N random points found
    while pointId > 0:
        # Create random point
        xRandom = xmin + (random.random() * (xmax-xmin))
        yRandom = ymin + (random.random() * (ymax-ymin))
        randomPoint = QgsPoint(xRandom,yRandom)
        randomPointGeometry = QgsGeometry.fromPoint(randomPoint)
        # if random_point is inside polygon feature, create new point feature in temporary layer
        for polygon in layer.getFeatures():
            if polygon.geometry().contains(randomPointGeometry):
                pointFeature = QgsFeature()
                pointFeature.setGeometry(randomPointGeometry)
                pointLayer.dataProvider().addFeatures([pointFeature])
                pointId -= 1
                sys.stdout.write('.')
                break
    print(" Ok.")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.