I have a delimited text layer created from table with a few hundred coordinates (lat/long) in columns A and B and several hundred values associated with these coordinates in columns C to XXX. Now, I would like to create mean coordinates for each column from C to XXX - weighted by the values in the given column.

Columns in table (section)

Manually, I can create these mean coordinates in QGIS 3.16.14 (LTR) via "Processing Toolbox"/"Mean coordinate(s"): I choose the delimited text layer as input layer and select a column via "Weight field". This works fine. However, I would have to do this individually for every column. And this would a few days.

Is there an easy way to automatize this process? One "mean coordinate" per column C to XXX? The expected result would be a "line" of points on the map, one "mean coordinate" for every column, for example like this:

enter image description here

I tried "Run as Batch Process" in the toolbox but I still would have to select every column manually...

How can this workflow be automated? Any ideas?

SAMPLE DATA (for a semicolon CSV):

  • Thanks for your reply! And sorry if I wasn't clear here! I calculate the mean coordinate for every column (C to XXX) from the coordinates given in the columns A and B.
    – Alrik
    Dec 22, 2021 at 21:50
  • Thus, every mean coordinate is calculated by dozens of coordinates, weighted by the values of the given columns (C to XXX).
    – Alrik
    Dec 22, 2021 at 21:51
  • 1
    Run Mean Coordinate(s) in Batch mode: click at the lower left of the dialog window
    – Babel
    Dec 22, 2021 at 21:52
  • 1
    OK, thanks. I appreciate your help very much! An example for such an expression would be great (see my sample data above). This would help me very much. I'll check the thread tomorrow.
    – Alrik
    Dec 22, 2021 at 22:27
  • 1
    Btw, what's the difference between "Calculate by expression" and "Add values by expression"? Both direct to the "Expression string builder".
    – Alrik
    Dec 22, 2021 at 22:30

2 Answers 2


You can use the following script. It adds all results to one layer.

  • Add CSV to QGIS.
  • Select the layer and run the script.
layer = iface.activeLayer()

crs = layer.crs().authid()
uri = "Point?crs=" + crs + "&field=field:string&field=MEAN_X:double&field=MEAN_Y:double&index=yes"
mean_layer = QgsVectorLayer(uri, 'TEST', 'memory')

fields = layer.fields().names()[2:]
features = list(layer.getFeatures())

for field in fields:
    weighted_xs = []
    weighted_ys = []
    weights = []
    for f in features:
        weight = f[field] if f[field] != NULL else 1.0
        p = f.geometry().asPoint()
        weighted_xs.append(p.x() * weight)
        weighted_ys.append(p.y() * weight)
    feat = QgsFeature(mean_layer.fields())
    feat["field"] = field
    mx = sum(weighted_xs) / sum(weights)
    my = sum(weighted_ys) / sum(weights)
    feat["MEAN_X"] = mx
    feat["MEAN_Y"] = my
    geom = QgsGeometry.fromPointXY(QgsPointXY(mx, my))



enter image description here

Attribute Table: (Field names are added as field value)

enter image description here

  • Addin all to one layer - maybe even better!
    – Babel
    Dec 23, 2021 at 22:26
  • 3
    @Babel I guess, there are thousands of columns. In Excel, C to XXX means that. A to ZZZ equals 26+26*26+26*26*26). Dec 23, 2021 at 22:36
  • 1
    As stated above: XXX was just a (poorly chosen) placeholder. Sorry for this misunderstanding. There are just a few hundred columns.
    – Alrik
    Dec 23, 2021 at 23:31
  • The script works fine! Thanks again! Just one question: The script does only work with INTEGER values, but not with FLOAT values. Is there an alternative to the operator "*" that works with floating-point numbers?
    – Alrik
    Jan 3, 2022 at 11:49
  • 1
    Not tested, but try weight = float(f[field]) if f[field] != NULL else 1.0 Jan 3, 2022 at 12:21

Solution: Use batch mode

Run Mean Coordinate(s) in Batch mode: click at the lower left of the dialog window.

To automatically fill the Weight field in the dialog for each attribute, insert an array of the corresponding fieldnames clicking Autofill... / Add values by Expression... like this:


Then for every element of the array (fieldname), a new row will be generated and the tool runs separately for each row - see screenshots below.

Automatically create an array of all fieldnames

Edit: You can create the array of field names automatically - handy if you have hundreds of fieldnames to include. Here the expression (with the help of @MrXsquared, see comments) to use. This gets you all fieldnames of the layer points (end of line 2), except the fieldnames excluded in line 3:

array_filter (
    @element not in ('fid','lat','lon', 'fields')

enter image description here

Output of the tool in batch mode for the sample dataset you provided: the colored points inside the red line are the weighted mean coordinates for each of the attribute values (97 to 111): enter image description here

  • 2
    Reading your answer i slowly (but still not entirely) get the question. May I suggest to use something like map_akeys(attributes(get_feature_by_id('layername',1))) to auto get an array of all fieldnames, that avoids typing all hundreds of names by hand.
    – MrXsquared
    Dec 23, 2021 at 22:02
  • 1
    @MrXsquared - that's what I was trying for some time in different variants, however, it does not work. Somehow the layer is not identified and no attributes are returned. Would be interesting to see if there is a way to achieve this. Workaround: create a list of attribute names on the layer itself, copy the result and paste it to the batch mode expression editor. Will update the answer accordingly.
    – Babel
    Dec 23, 2021 at 22:07
  • You're right - I overlooked the attributes(feature) variant - just hat attributes() in view (I knew that it should work somehow). Maybe worth adding as another answer?
    – Babel
    Dec 23, 2021 at 22:25
  • 1
    Thank you very much for your ideas and suggestions! Very instructive and much to think about. I did not know about this possibility to automatically create an array of all fieldnames. That's very helpful, not only in this context.
    – Alrik
    Dec 23, 2021 at 23:39
  • The script works fine! Thanks again! Just one question: The script does only work with INTEGER values, but not with FLOAT values. Is there an alternative to the operator "*" that works with floating-point number?
    – Alrik
    Jan 3, 2022 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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