[GRASS-user] SQL: generating numeric class numbers from class text labels?

Hernán De Angelis dhdeangelis at comhem.se
Wed Dec 4 09:27:53 PST 2019


Hi

I cannot think of a short and simple SQL "one liner" statement but can 
think of a short script that will do that using SQL statements.

In pseudo-code, translate to your favorite language:


# get the unique classes

SELECT distinct(label) FROM table;

-> fetch results in an array "txt_label"

# iterate over array and insert new integer labels in table

int_label = 0

foreach txt_label {

     INSERT INTO table.label_int VALUES int_label WHERE label = 'txt_label'

     n++

}


This may seem obvious to you, but since you asked :-)

Cheers and good luck


Hernán





On 2019-12-04 18:11, Markus Neteler wrote:
> Hi,
>
> I have a landuse map with text labels (forest, street, ...). For
> r.learn.ml I need to have them as numeric classes.
> It is not important for me which number is assigned but I search for
> an automated solution, i.e. SQL statement unless there is a different
> way.
>
> So:
>
> cat|label|label_int
> 1|forest|1
> 2|forest|1
> 3|street|2
> 4|forest|1
> 5|street|2
> 6|urban|3
> ...
>
> I guess I have done that already some years ago but I can't remember
> the trick :-)
>
> thanks for a hint,
> Markus
> _______________________________________________
> grass-user mailing list
> grass-user at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/grass-user


More information about the grass-user mailing list