Using QGIS 2.18.14

I have a delimited text layer imported, this contains coordinates that essentially plots a linear grid across the country. Each coordinate in the file denotes a corner of the grid and each coordinate has an value attributed to it.

I can visualise the coordinates with their attributed value but what I want to do is use the lower left-hand coordiates attributed value and display it in the middle of the corresponding square.

As follows:

enter image description here

How do I achieve this in QGIS?


You can do that easily enough by offsetting the location of the label.

Open the label properties and navigate to "placement". Setting the values to "offset from point" and then the quadrant to directly over the point will locate the labels directly over the points. You can then play with the "offset X,Y" settings to get your labels in the right place - obviously this depends on the spatial resolution of your grid.

Images below show the label settings and an example what it produces - the labels are associated with the purple points.

enter image description here

enter image description here

| improve this answer | |

One solution is to create a new shapefile layer with the result, through calculating the mean coordinate of the four points and then assigning the left-bottom most value (or the minimum) to the mean coordinate. This can be implemented with this Python code in the QGIS Python Console:

The code assumes input as the first layer in the Layer overview, so you need to make sure that is the point layer created from the CSV (it can be a temporary layer). It also assumes the field with your values (H22,I25) is named 'id', so if it's named differently make sure to change the code to reflect the appropriate field name. Lastly, the layer created will contain the required information (ID of left-bottom most feature), and you'll need to enable labels to view it.

If you need to run this code recursively, it can be also be easily adjusted, this is a rough but working solution.

import processing
import ogr
from PyQt4.QtCore import QVariant

#Read all active layers from Layers Panel
mapcanvas = iface.mapCanvas()
layers = mapcanvas.layers()
#Select where you want output layer saved (assuming Shapefile)
output = "C:\\temp\\meanCenterWithValue.shp"

#Get the point layer from Layer Overview (make sure it's the first one and activated), after being created from CSV
points = layers[0]
print "Points Layer name is: " + layers[0].name()

def addValueField(lyr, value):
    myField = QgsField( 'value', QVariant.String )
    idx = lyr.fieldNameIndex( 'value' )

    for f in lyr.getFeatures():
        f[idx] = value
        lyr.updateFeature( f)


meanCenterAlg=processing.runalg('qgis:meancoordinates', points , None, None, output)
meanCenterLayer = processing.getObject(meanCenterAlg['OUTPUT'])

coordList = []
for feat in points.getFeatures():
    #print feat['id'] #Verify output
    geom = feat.geometry()
    (x,y) = geom.asPoint()

for feat in points.getFeatures():
    geom = feat.geometry()
    (x,y) = geom.asPoint()
    if (x,y) == min (coordList):
        print "Lower-left hand coordinate ID is: " + str(feat['id'])

print 'DONE'

Assuming you only want the label of the mean coordinate displayed, you can set the resulting layer's Fill and Outline Opacity to 0%, so the output can look something like this:

enter image description here

| improve this answer | |

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.