I wanted to make vector grid in QGIS basing on canvas extent and save it to SpatialLite db.

Therefore, I am creating new database:

Layer > Create Layer > New SpatiaLite Layer > ... > Save.

Then, I am starting grid generation: Processing > Toolbox > Vector Grid. I am choosing shapefile extent as Grid extent, setting X and Y spacing and Grid type.

At the bottom I am choosing recently created database as output. Process starts, and grid creates but it take long time in comprison with saving to shapefile.

Is there any way to speed up this process?

My QGIS version is 2.14 Essen

  • 2
    Welcome to GIS:SE @Tomek! You're using a fairly outdated version of QGIS, could you try repeating your steps for the latest version (as of now 2.18) and see if it still slow?
    – Joseph
    Jan 10 '18 at 10:39
  • Thank you for response @Joseph! I applied to your suggestions. It seems generation in qgis 2.18 is a bit faster. But this is still much slower than generation to shp. Do you maybe know how to speed this process up? Maybe change some pragmas in spatialite?
    – Tomek
    Jan 10 '18 at 15:23

Not sure why this is happening, but you have two options. First is to export to shapefile then import the shapefile to your spatialite database or you can create the grid in Spatialite itself.

First you need to know the extent you want, if you want to use a shapefile just go to the metadata section of its properties and you will find something like this:

xMin,yMin 140.999,-37.5049 : xMax,yMax 153.638,-28.1573

Alternatively if you want to just specify by using the map canvas, do it in the map composer where you can find the extent coordinates in the item properties.

Now that you have the two extent coordinates you can use the following set of queries in Spatialite:

Create the table


Add geometry column

SELECT AddGeometryColumn('grid','geom',4283,'MULTIPOLYGON','XY');

Create the grid

INSERT INTO grid (geom) SELECT ST_SquareGrid(SetSRID(BuildMBR(140.999,-37.5049,153.638,-28.1573),4283),0.1) as geom

The BuildMBR function turns those two coordinates into a rectangular polygon which is the extent you are looking for. It is important to ensure you use the same SRID in the SetSRID function as you do when you add the geometry, otherwise it will fail. Finally the just change the grid size, noting that the units will be that of the coordinate reference system you use.

  • There is only one problem with this solution. If resulting grid is too big for RAM insertion will crash. Is is somehow possible to overcome this?
    – Tomek
    Jan 10 '18 at 13:19

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.