I'm using a special system to do geomagnetic surveys. The software let's me output the information as image files, ASCII grid or in its own proprietary file format.

Now I want to get its information in my GIS (using QGIS). I can of course import the exported image files but I would love to import the raw data to play around with the visualisation a bit more, not depending on the original software.

The files look like this:

X   Y   Messwert
995.00  1000.00 0.00
995.00  1000.05 0.14
995.00  1000.10 0.28
995.00  1000.15 -0.07
995.00  1000.20 -0.42
995.00  1000.25 -1.26
995.00  1000.30 -2.17

and so on..... (looooong file ;) )

Every 0.05 cm movement on X and Y contains a data value, positive and negative.

I want to convert this data into a raster file, a pixel for every data value. My goal is to tune the visualisation comparable to a DEM TIFF file in my QGIS project, without having the problem to export this through the original software every time.

What would be the best way to do this?

I think GDAL is the program to use I need some help. Perhaps there is even a way to do this in QGIS?


So I finally imported a fraction of my data, sorting everything to Y. Saving everything as TIFF wasn't a problem as well. Now my next step is to get this spatially correct data (in terms of length) into my project. The coordinates in the file are just a local project oriented coordinate system.

Georeferencing the created TIFF wasn't a big problem but it results in a little annoying problem. after georeferencing my perfect square gets rotated a bit, resulting in big nodata areas.

My data also contains positive as well as negative data and even zero is important.

I couldn’t find a way to get this nodata area to disappear, QGIS georeferencing gives it a value that is contained in the data areas as well. if I set this to transparency my raster files gets some annoying holes.


3 Answers 3


You can easily open ASCII xyz triplicate data in QGIS under "Add Raster Data" with a "ASCII Gridded XYZ (.xyz)" file type. You can also covert it to a different format under the "Raster > Conversion > Translate (Convert format)" menu. Alternately, you can do this under the "Raster > Conversion > Rasterize" menu with a "Comma Separated Value (.csv)" file type.

  • 1
    Thanks for the quick reply but this doesnt seem to work for some reason. i always get the following error when i try to convert or import the data "At line 2, X spacing was 0.000000. Expected >0 value GDALOpen failed - 1 " even tho the file is properly formated regarding to this page gdal.org/frmt_xyz.html
    – Sonic
    Sep 24, 2014 at 21:23
  • i allready tried changing the seperator to space, commas and also changed the header... i always get the same error.
    – Sonic
    Sep 24, 2014 at 21:27

According to http://www.gdal.org/frmt_xyz.html:

Cells with same Y coordinates must be placed on consecutive lines

which is not fulfilled by your dataset.

So you can

  • resort the tabular data with an external program
  • exchange X and Y in the header (you have to mirror your raster later)
  • load the data as point data using Delimited text, then rasterize it.
  • Oh man... yes you are right. fixed it and now the file loaded fine... well you should stop trying if your to tired to see the difference between x and y :)
    – Sonic
    Sep 25, 2014 at 18:03

I would hit it with a python script.

import numpy as np
asc = open('yourfilename', 'r')
grid = []
for line in asc:
  line = line.strip()
  if line == 'X   Y   Messwert':
  grid.append(map(float, line.split(' ')))
grid = np.array(grid)

#naively get number of rows
rows = np.sum(grid[:,0] == grid[0,0])

#naively get number of colums
cols = np.sum(grid[:,1] == grid[0,1])

#get values
vals = grid[:, 2]

#pad some nodata values if the total number of elements does not form
#a nice rectangle with exactly rows*cols number of cells.
if len(vals) != rows * cols:
   vals = np.hstack((vals, -9999 * np.ones(rows * cols - len(vals))))

#turn data into 2-d matrix(column-wise using order='F')
value_grid = np.reshape(vals, (rows, cols), order='F')

#write out dummy ascii raster file:
out = open('raster.asc', 'wb+')
out.write('ncols         %i\n' % cols)
out.write('nrows         %i\n' % rows)
out.write('xllcorner     %i\n' % 1111)#dummy or put your own
out.write('yllcorner     %i\n' % 2222)#dummy or put your own
out.write('cellsize      %i\n' % 0.05)
out.write('NODATA_value  %i\n' % -9999)

np.savetxt(out, value_grid, fmt='%%.%2f')

granted, you can load the file with a numpy command which would save a few lines and you can combine some operations but my intent is to give something readable...and hopefully useable. Please excuse any syntax errors, I didn't run this myself.

  • It give me ERROR: TypeError: 'float' object is not subscriptable Jul 24, 2019 at 10:14

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.