[GRASS-user] SQL: generating numeric class numbers from class text labels?
Micha Silver
tsvibar at gmail.com
Wed Dec 4 09:58:44 PST 2019
How about doing this in R? The labels will be read into R as factors,
and the factor levels can easily be extracted as numbers.
Something like this:
micha at tp480:~$ v.info -c stations
Displaying column types/names for database connection of layer <1>:
INTEGER|cat
INTEGER|station_num
TEXT|station_he
TEXT|station_en
TEXT|type
INTEGER|x_coord
INTEGER|y_coord
DOUBLE PRECISION|long
DOUBLE PRECISION|lat
INTEGER|elev
TEXT|date_open
DOUBLE PRECISION|dist
DOUBLE PRECISION|azim
micha at tp480:~$ R
R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
.....
> library(rgrass7)
Loading required package: XML
GRASS GIS interface loaded with GRASS version: GRASS 7.6.0 (2019)
and location: ITM
> use_sf()
> stations = readVECT("stations")
WARNING: Vector map <stations> is 3D. Use format specific layer creation
options (parameter 'lco') to export <in 3D rather than 2D
(default).
Exporting 94 features...
100%
.....
> stations['new_station_num'] = as.numeric(stations$station_en)
> stations$new_station_num
[1] 71 26 6 55 54 63 7 8 31 30 46 84 92 38 32 88 27 12 67 62 47 33
53 76 89
[26] 2 86 11 40 65 64 45 13 85 60 59 1 74 73 22 19 15 39 50 56 14 44
23 36 83
[51] 41 42 43 18 17 75 16 82 81 37 48 28 87 3 66 10 34 91 61 93 94 72
5 4 68
[76] 78 77 9 29 51 58 57 49 52 24 25 80 79 35 70 69 90 21 20
> writeVECT(SDF=stations, vname="new_stations")
Best regards, Micha
On 04/12/2019 19: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
--
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918
https://orcid.org/0000-0002-1128-1325
More information about the grass-user
mailing list