Friday, 13 November 2015

qgis - How to create a raster from scratch


I realize this isn't a geographic question as I'm not trying to make this into a geographically placed image. But I hope you all will know how to do this.


I want to make a raster from scratch. I have a database that I would like to be able to query and display the results as a sort of color map. I can figure out how to do the math and raster calculating, but I can't figure out how to make a raster. I expect that I need to add a couple of fields for the x and y coordinates, then give each record a value. Just 1s and 0s to begin with.


I work with ArcGIS, Oasis Montaj, and Microsoft Access at work all day, but I'd like to do this in QGIS and whatever other tools are necessary. I have spent almost no time with QGIS but would like to learn and work with it on my own projects. But I need to point in the right direction for this one.


Thanks



Answer



The simplest way I can think of for your situation would be to output the data as an Ascii Raster. Write an SQL query to dump the relevant value for each row of your database into a space delimited stream to a simple text file. Then add some header lines, give it the extension '.asc' and your task is done.


The advantage of this approach is that you don't need to get bogged down in coordinate systems or adding xy fields etc. You header takes the form:



NCOLS xxx
NROWS xxx
XLLCENTER xxx | XLLCORNER xxx
YLLCENTER xxx | YLLCORNER xxx
CELLSIZE xxx
NODATA_VALUE xxx

The first two lines just define the shape of your raster based on the data values. The rows here won't necessarily equate to your rows in your database - it depends on how your database is set up and you'll understand that best. When you create your SQL output, just export the value you will use for representation. You can ascribe a colour later so this doesn't matter too much at this stage. Set the next two lines to zero for either XLLCenter or XLLCorner (it's either or not both and defines where the data point is in relation to the raster cell - for you it doesn't matter but I'd go will Center). Your CELLSIZE again doesn't matter and I'd set it to '1'. A common value for NoData is -9999. Now you have an ascii raster you can use QGIS to convert it to a TIFF or add a colour pallet etc.


You could achieve a similar result without GIS and there are hundreds of ways to do this but the above solution should meet you need in a very simple way.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...