32

I have a non-georeferenced vector layer that I need to be georeferenced. With raster layers the task is easy and straightforward, but I have no idea what should I do with my vector layer. I have a few control points with known coordinates which should provide some basis to transformation. So, let's say I know points with id-s of 1, 2 and 3 should have the coordinates of x1,y1 ; x2,y2 ; x3,y3. There might be some rotation and scale transformation in addition to simple shifting.

Any ideas?

  • Have you tried the qgsAffine plugin? Similar question here: gis.stackexchange.com/questions/22691/how-to-georeference-a-dxf – Rayner Sep 11 '12 at 11:25
  • The Affine plug-in will probably do the transformation, but you will need to know the transformation parameters to use it. Can you post the vector coordinates and the corresponding real-world coordinates for your three points? N. – nhopton Sep 11 '12 at 11:38
  • Can you convert the shapefile to a tiff using gdal_rasterize, georeference the tiff, then extract the parameters from the world file? – klewis Sep 11 '12 at 20:55
11

To georeference a vector layer, try the qgsAffine plugin.

There is more info at Where to find qgsaffine in the menu?

| improve this answer | |
  • 3
    Thank you for answers. I guess I have to use the qgsAffine then and figure out the transofrmation parameters manually. I was just hoping that there's some way to automate that process. Perhaps I'll try writing some Python code to do that – AHaav Sep 11 '12 at 11:46
  • 1
    Thinking about it, you might find that GRASS v.transform (it's in the Sextante toolkit) is friendlier to use than the qgsAffine plug-in. N. – nhopton Sep 11 '12 at 12:03
  • 1
    By the look of it v.transform will do the sums for you as well, see grass.osgeo.org/gdp/html_grass63/v.transform.html – nhopton Sep 11 '12 at 12:28
9

I recommend the Vector Bender plugin for QGIS. I tried it and it works fine and is user friendly. Depending on amount of pair of points that you define, you can either do:

  • translations: translation from one starting to ending point (1 pair)
  • uniform: translation, scaling and rotation (2 pairs)
  • bending: additional deformation (3 pairs or more)

You find a short video here and I advice you to read the Vector Bender help once you installed the plugin.

| improve this answer | |
  • I agree, Vector bender plugin is intuitive and fast to work with. However, I could not manage to mirror/flip vectors. For this operation I use Affine transformation plugin (QGIS v2.18). – jurajb Jan 19 '18 at 22:19
8

Given the fact that you have some points of control, you should be able to use an Affine transformation to shift your vector data. Have a look at this recipe. The process is a two part process:

  1. Use your control points to define the coefficients of your affine function required
  2. take the coefficients and apply them to the ST_Affine() in postgis.

If you put your control points into a CSV file (old_x,old_y,new_x,new_y), you can just about cut'n paste the R commands from the link to solve the coefficients part.

| improve this answer | |
6

With the release of GDAL 1.10 this is now possible.

It is done by performing a basic transformation in ogr2ogr and supplying the control points in the -gcp tag. http://www.gdal.org/ogr2ogr.html

For a guide please see: http://gisforthought.com/georeferencing-vector-data-using-qgis-and-ogr2ogr/

| improve this answer | |
3

I just had to do that, and ended up doing this :

  1. Rasterize the shapefile
  2. Georeference the raster using the Georeferencer plugin
  3. Save the GCP as a gcps.points file
  4. Compute the affine transformation using this file
  5. Apply the affine transformation to the shapefile using qgsAffine

The following script computes the affine transformation matrix using the saved GCPs :

# Computes an affine transform based on QGis GCPs
# Usage: gcp_affine.py gcps.points

import csv
import sys
import numpy as np
from skimage.transform import AffineTransform

u = list(csv.DictReader(open(sys.argv[1], "rb")))
source = [(d["pixelX"], d["pixelY"]) for d in u]
dest = [(d["mapX"], d["mapY"]) for d in u]
source = [map(float, s) for s in source]
dest = [map(float, s) for s in dest]
source = np.array(source)
dest = np.array(dest)
aft = AffineTransform()
aft.estimate(source, dest)
np.set_printoptions(suppress=True)
print aft._matrix
| improve this answer | |
3

Several of the answers provided here are no longer options for QGIS 3, and/or only allow for linear transformations of the vector file being georeferenced. This may answer the OP's question, but others who look at this post may want other transformation options for georeferencing vector files.

Issues with some of the solutions for georeferencing vector files:

  • The qgsAffine plugin only allows for linear transformations. This means it can scale, rotate, and move the vector file, but it cannot bend or warp the vector file. The Georeferencer for raster files provides more options, and allows for a variety of transformations, including second order polynomial and thin plate spline. Also, it does not appear that the qgsAffine plugin is still available in QGIS 3.

  • The GRASS module v.transform is similar to the qgsAffine plugin, and only allows for linear transformations.

  • The Vector Bender plugin looks like it was a good option, but it has not been fully ported to QGIS 3, so isn't currently an option. It does not look like it is being maintained anymore.

Using ogr2ogr, as is suggested in HeikkiVesanto's post, is a good option. It allows one to use GCPs (Ground Control Points), and specify what type of transformation you would like. I could not find instructions anywhere on how to do this, so I have included some instructions below:

  1. Generate a set of GCP pairs, with one point being the original location in the ungeoreferenced vector file, and the other point being the desired location in your georeferenced workspace.

  2. There are a variety of ways to generate GCPs. I used the Georeferencer GDAL plugin in QGIS. This is available under the Raster dropdown menu, after it is installed using the Plugin manager. It only works on raster files, so you first must convert the vector file to a raster file. This is possible with "Processing Toolbox: GDAL: Vector conversion: Rasterize (vector to raster)". Set the resolution to number that still allows you to see the features of the vector file.

  3. The Georeferencer has a nice interface, that allows you to add many GCP points. You can also test out different transformations, and see which works best. And you can see if any of your GCP points are likely to be in error, by looking at their residuals. Once you have added all your GCP points, and checked that you like the output, you can export the GCP file from the Georeferencer to use for georeferencing the original vector file.

  4. The GCP file needs to be changed to be in the following format. You can add as many GCP points as you like, each is proceeded by a -gcp and separated by a space. I ran this with 800+ GCP points, and it worked great.

    -gcp <ungeoref_x1> <ungeoref_y1> <georef_x1> <georef_y1> -gcp <ungeoref_x2> <ungeoref_y2> <georef_x2> <georef_y2>
  1. There are a variety of ways that one could change the GCP points into the right format. I imported them into Excel as a csv file, and created a column to output the right format for each point. Then I pasted that column into TextEdit and replaced the line ends with spaces.

  2. The vector file that you want to georeference should be imported into your workspace using the same SRS that you want for the final georeferenced file, which should be the same SRS as your workspace. It might be a long ways from where it should be, if it originally used a different SRS, but the transformation will move it to the correct location.

  3. Access ogr2ogr through "Processing Toolbox: GDAL: Vector conversion: Convert format". This allows you to select the vector file you want to georeference, and your output file. In the "Additional creation options" field, add a command specifying what transformation type you want, and then the line with all your GCP points.

  4. Commands for different transformation types include:

    • "-order 1" for linear
    • "-order 2" for second order polynomial
    • "-order 3" for third order polynomial
    • "-tps" for Thin Plate Spline.
  5. For example, the code added into the "Additional creation options" field might look like:

-order 2 -gcp <ungeoref_x1> <ungeoref_y1> <georef_x1> <georef_y1> -gcp <ungeoref_x2> <ungeoref_y2> <georef_x2> <georef_y2>
| improve this answer | |
2

See my answer to Snapping two polygons together and give the free openjump a try.

I think that its affine transformation is much more user friendly than the qgis affine plugin.

| improve this answer | |
2

Following-on from my comments on Rayner's answer, GRASS v.transform, which can be run from the Sextante toolbox, can be used to calculate transformation parameters and apply them to a vector layer to perform an affine transformation. A text file containing control points is required, in the format shown here.

It's very easy to use and works well.

| improve this answer | |
  • Maybe I am getting old, but just wondering whether folks remember the ShapeWarp user addon in Arcview 3.0. It was so easy to use. I have always found these other tools (v.transform/ affine) much more cumbersome. Hasn't someone tried to recreate the ease of ShapeWarp on QGIS? – Sharad Mar 19 '19 at 6:25

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.