Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

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.

share|improve this answer
    
How does this help. It is the same as doing a selection. –  Nathan W Jul 20 '12 at 15:02
    
Pretty much. But this works in QGIS right now whereas the selection doesn't, at least on my machines. –  John Jul 20 '12 at 18:23
add comment

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

share|improve this answer
    
+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 Jul 20 '12 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 Aug 6 '12 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. –  Geoist Aug 6 '12 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 Aug 7 '12 at 3:08
add comment

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).

share|improve this answer
    
There is just one polygon in the shapefile already. –  coelacanth Jul 19 '12 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. –  lynxlynxlynx Jul 19 '12 at 23:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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