2

I have a dataset of rasters (driver kea) whose has, let's say, 20-30 attributes in their RAT. I'm realizing how to have these attributes in shapefiles (without using proprietary software). I've read a lot of stuff in GDAL and Python, but I was not able to solve it. Since I can't convert my rasters to shapefiles via gdal_polygonize.py (It doesn't deal with RATs), I'm trying to find and alternative to convert the RATs to CSV or any format that allows me to do a join, after all.

I'm failing to identify a proper solution. Could anyone point me some clue? How can I convert rasters (kea format, in this case) to shape so that I can have the entire attribute table? Or can I export the RAT so that I can join it to a shapefile?

EDIT

Recently I made a progress. I'm able to store the column names (and eventually column values in python. But how to export/save them in a manageable format (cvs,txt,ascii) is still lacking solution.

>#!/usr/bin/env python 
># Import python modules 
>from rsgislib import rastergis 
>from osgeo import gdal 
># The output segments (clumps) image 
>seg = "cur_segs_50_1000_UTM.kea" 
>outfile = "test.txt" 
>img= gdal.Open(seg) 
>band=img.GetRasterBand(1) 
>rat=band.GetDefaultRAT() 
>icolcount=rat.GetColumnCount() 
>cols=[] 
>for icol in range(icolcount): 
>cols.append(rat.GetNameOfCol(icol))
2
  • Recently I made a progress. I'm able to store the column names (and eventually column values in python. But how to export/save them in a manageable format (cvs,txt,ascii) is still lacking solution. >#!/usr/bin/env python ># Import python modules >from rsgislib import rastergis >from osgeo import gdal ># The output segments (clumps) image >seg = "cur_segs_50_1000_UTM.kea" >outfile = "test.txt" >img= gdal.Open(seg) >band=img.GetRasterBand(1) >rat=band.GetDefaultRAT() >icolcount=rat.GetColumnCount() >cols=[] >for icol in range(icolcount): >cols.append(rat.GetNameOfCol(icol))`
    – Jecogeo
    Commented Jan 22, 2016 at 13:11
  • If you have RSGISLib installed why don't you use the rastergis.export2Ascii function? (rsgislib.org/…)
    – danclewley
    Commented Jun 1, 2016 at 18:40

1 Answer 1

3

You can use the rios (raster input/output simplification) module to read the columns of the RAT, giving you a numpy array which can be written to a csv

col_names = rios.rat.getColumnNames(img)
rat_values = []
for col in col_names:
    rat_values.append(rios.rat.readCoumn(img, col)

Alternatively it will possibly be easier to create a gdal image directly from the RAT:

rsgislib.rastergis.exportCols2GDALImage

Allowing you to use gdal_polygonize.py, see here for the docs: http://www.rsgislib.org/rsgislib_rastergis.html

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.