6

im using a special system to do geomagnetic surveys. Its software let me output the information as image files, ASCII 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 arround 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 visualisastion comparable to a DEM tiff file in my qGIS project, without having the problem to export this through the original software everytime.

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? Thanks for any help!

Update:

So i finally imported a fraction of my Data, sorting everything to Y. Saving everything as tiff wasnt a problem as well. Now my next step was to get this spacialy correct data (interms of lenght) into my project. the coordinates in the file is just a local project oriented coordinate system.

Georeferencing the created tiff wasnt a big problem but it results in a little annoying problem. after georeferencing my perfect square gets rotatet a bit. resulting in big nodata areas. My data also contains positive as well as negative data and even zero is important.

I couldnt 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.

7

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 '14 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 '14 at 21:27
3

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 '14 at 18:03
1

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':
    continue
  grid.append(map(float, line.split(' ')))
asc.close()
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')
out.close()

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.

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.