12

I used the research tool "Regular Points" to produce a certain number of points within a polygon, but it forms a rectangular grid of points rather than only within the shape of the polygon. Since I need a certain number of points within the polygon, is there any way to do so without trial and error?

E.g. right now only 8 of the 25 points I want are within the polygon, but I want 25 points, so I could increase to 50 points and see how many then appear within the polygon

enter image description here

3
  • I want also to Create regularly spaced, defined # of points within polygon in QGIS, does exist any plugin for this? I could do it with an Arcgis's extension called ET GEOWIZARD (Uniform points in polygons) , but I need to do in QGIS!! Can you help me?
    – Ana María
    Commented Feb 14, 2019 at 15:25
  • Has there been any solution to this? Commented Sep 7, 2021 at 15:52
  • Like below/above I would like to specify an exakt number of points regularly spaced within a polygon if possible i prefer a random start point Its for random measurepoints Commented Sep 7, 2021 at 17:57

4 Answers 4

3

What QGIS is referring to in terms of the layer boundary is actually the envelope or bounding box of the geometry.

I've documented a solution which is probably the closest you will get to automating this is (short of filing a bug / feature request).

The process works based on the ratio between area of the two objects: the feature geometry and it's bounding box).

Check the following steps below:

  1. Select the layer you want to work on.

  2. Select the feature you wish to work on. Make sure nothing else is selected.

  3. Run the following snippet within the Python Console.

    layer = qgis.utils.iface.activeLayer()
    features = layer.selectedFeatures()
    feature = features[0]
    geom = feature.geometry()
    env_rect = geom.boundingBox()
    env_geom = QgsGeometry.fromRect(env_rect)
    env_geom.area() / geom.area()
    

    Be sure to push enter as the last line will not automatically return.

  4. Run the Regular Points tool again, but enter the number of points that you want inside the box multiplied by the output given by your script.

In my sample below, the result I received from the output was a value of ~2. I requested 20 dots to be output by the process where I actually only wanted 10. I was given 8 in return, which is fairly close. Depending on the actual shape you're using the results will vary.

If you were so inclined, you could automate (recursively) the Regular Points process modifying the variables until you arrived at your desired number.

Sample

4
  • +1 Your suggestion is close, but can fail to converge. What you want to do is (a) estimate a spacing that will cause the desired number of points to fall within the polygon and (b) iterate with a random offset (shown in the screenshot).
    – whuber
    Commented Jul 20, 2012 at 13:05
  • If I do a random offset, then it's no longer a grid, right? A grid within the polygon with a certain number of points is what I want, to sample throughout the area. Or maybe it's not what I want, because choosing one start point and going from there isn't random enough?
    – coelacanth
    Commented Aug 6, 2012 at 15:00
  • @coelacanth Perhaps you aren't revealing enough information about your issue then? What would be the next step in your analysis? There may be other ways to achieve your goal, aside from using a point grid.
    – nagytech
    Commented Aug 6, 2012 at 23:34
  • Hmmm, I'm new to the whole field of GPS/GIS and survey design, so please do let me know if there's a better way to do things. I want to set up a certain number of sampling areas within a polygon (i.e. a section of a forest) to sample vegetation. I want the areas to be spread throughout the site so that I'm getting a representation of the whole site. What I want to use this feature for is to set the locations of those sampling areas within the boundary. Then I'd navigate to that point using a GPS unit and collect my data.
    – coelacanth
    Commented Aug 7, 2012 at 3:08
2

You can easily do that just clipping: generate the regular grid of points just as you did and then use Vector>Geoprocessing tools>Clip, input the generated point grid and use the feature as clip mask.

-1

A simplistic solution is suggested by the screenshot. You could extract the polygons into new layers with Vector -> Data Managment Tools -> Split vector layer, import them and then choose the layer with the feature (ID) you want as the input layer for the "Regular Points" sampler.

If you need to do this for only a handful of polygons, finding them and exporting them separately could be more efficient. Once you have one selected, right click on the layer name and choose Save Selection As ..., choose shapefile, fill out the rest and then repeat as before.

EDIT: Since you already have a single feature, I can't think of anything simpler but using a denser grid, intersecting it with the polygon again and finally removing all extraneus points (easiest through the attribute table, since you get both easy centering and total count).

2
  • There is just one polygon in the shapefile already.
    – coelacanth
    Commented Jul 19, 2012 at 21:31
  • Oh, how annoying then (like the fact it created 20 instead of 25 points). I suggest you open a feature request on hub.qgis.org/projects/quantum-gis/issues — I suppose it should have an additional checkbox to use the true layer boundary. Commented Jul 19, 2012 at 23:01
-2

Apply a definition query to your polygons (in 1.8 right click on the layer and choose Query). Then run the tool. Remove the query.

2
  • How does this help. It is the same as doing a selection.
    – Nathan W
    Commented Jul 20, 2012 at 15:02
  • Pretty much. But this works in QGIS right now whereas the selection doesn't, at least on my machines.
    – John
    Commented Jul 20, 2012 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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