1

Is it possible to define point grid spacing by attribute in QGIS?

EDIT: I really dunno is this even possible. Added a picture where I try to demonstrate the situation.

So the problem is that I want to generate a grid where I can create a grid that takes the spacing value from an attribute table like the one below. And also that the grid is only placed inside the polygons. Normally I do this so that i create a grid and the I select the points that are located inside a specific polygon and delete all the points that are not inside the wanted polygons.

(I'm not very familiar to using the python console, but every answer is welcome.)

Example:

attributes

enter image description here

(Simple question, but I didn't find any answers to it.)

  • Are your features continuous or are there gaps? Do you want one single grid, or several small ones? What did googling yield? What have you tried so far? – Erik Feb 6 at 15:49
  • My features are different shaped and sized polygons. I have a shp file of city green areas, and it consists of parks and other green areas, so there's also gaps. I would like to have a more dense grid to the parks. Single grid and several small ones are both ok as long as i don't have to divide my shapefile to several shapefiles just to get the grid done. (Edit: I haven't tried much a thing, because googling didnt give me any answers.. maybe i used wrong entries or I don't know what I'm asking for first place :D) – H. L. Feb 6 at 16:02
  • So, you have one shapefile with several classes of land use with no gaps in between? Also, I don't see the hassle about dividing your shapefile? – Erik Feb 6 at 16:08
  • I'm not sure I understand, do you want to symbolize the polygons differently on a map or actually break them up? – Gabriel C. Feb 6 at 16:19
  • I edited the original post. I'm a beginner in gis things. If you don't understand then im not sure if i can explain better :D thank you – H. L. Feb 6 at 18:21
1

I got a processing model to almost do what you want. It creates a grid inside each polygon. Each grid should have its own spacing, as defined by the fields "Horizontal_spacing" and "Vertical_spacing". But the grid spacing is identical for each polygon. Hopefully this will be useful to get you started in the right direction.


Create a processing model.

Add one input: Vector feature

enter image description here

Add two algorithms:

  1. Create Grid

    grid extent: extent of input feature

    horizontal spacing: pre-calculated value with the expression "Horizontal_Spacing" (the field name of the horizontal spacing field, in double quotes)

    vertical spacing: pre-calculated value with the expression "Vertical_Spacing" (the field name of the horizontal spacing field, in double quotes)

    enter image description here

  2. Clip

    Input layer: 'Grid' from algorithm 'create grid'

    Overlay layer: Input Feature

    enter image description here

The model looks like this:

enter image description here

Run the model. Click the round green arrow to "iterate over this layer, creating a separate output for every feature in the layer"

enter image description here

Here's the model output:

enter image description here

As you can see, the grid spacing is identical for each polygon. It should have a different spacing for each feature, as defined by the fields "Horizontal_spacing" and "Vertical_spacing."


I couldn't figure out how to make this work as intended, but here's a workaround I figured out:

Add 2 Number inputs called Horizontal and Vertical spacing. Use these instead of the field values in the Create Grid algorithm.

enter image description here

Make a list of all unique combinations of vertical and horizontal spacing you have. In my example, that list is {1x1,2x2,3x3}. You'll have to run the model as many times as you have unique combinations.

Before you run the model, use the "select by expression" tool to select features where the horizontal and vertical spacing match the first combination on the list.

enter image description here

Run the model, checking the box for "selected features only." Type in the horizontal and vertical spacing values you used in the "select by expression" tool. The output will be a point grid inside the selected polygons, with horizontal and vertical spacing defined by the values you entered.

Repeat for every unique combination of vertical and horizontal spacing.

  • I had tried to add an expression input as a child to the input layer with attribute($currentfeature,'spacing_field') and feed this in the spacing for the grid tool. When running the model, testing the expressions return the correct field values so if only it was possible to assign an expression input to the relevant parameters, it would work perfectly. – Gabriel C. Feb 6 at 21:17
  • I tried using that expression with my spacing field, attribute($currentfeature,'Horizontal_spacing'), in the Create Grid algorithm, but it still created a grid with the same spacing for all the features. It seems to be taking the attributes from the first feature in the layer instead of from the current feature. – csk Feb 6 at 21:29
  • My guess is that an expression directly in the tool can't reference the input as it always seems to return NULL. The resulting grid spacing is always 1 meter, regardless of the attributes (I tried 20m, 10m, 5m like in the OP's example). – Gabriel C. Feb 6 at 21:47
  • Thank you. It takes time to orientate to this, but I will try this as soon as possible :) – H. L. Feb 8 at 10:37

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.