2

I have following vector layer that represents countour lines. With QGIS 3, labels placed near lines (Layer Properties -> Labels) were taken from TEMP_ENE field in attributes table.

enter image description here

I would like a similar behavior in R. In this case, I used rgdal library to read vector layer as follows:

library(rgdal)
isoterma.ene<- readOGR('isotermas_1grado_ene_4326.shp')
isoterma.ene
class       : SpatialLinesDataFrame 
features    : 24 
extent      : -71.84042, -68.01765, -41.09749, -36.15523  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
variables   : 2
names       : ID, TEMP_ENE 
min values  :  0,       12 
max values  :  8,       23 

I found out this question where is used locator() function.

coords <- locator()
coords
$`x`
[1] -68.97818
$y
[1] -38.4115
text(x=-68.97818, y=-38.4115,label="23")

I tried it out in only one case (value 23) and it worked.

enter image description here

Is there one method where automatically values in isoterma.ene$TEMP_ENE of SpatialLinesDataFrame can be placed in these lines?

Editing Note:

Based in mdsummer answer, following approach works manually:

library(rgdal)
library(raster)
isoterma.ene<- readOGR('isotermas_1grado_ene_4326.shp')
plot(isoterma.ene)

By using click from raster library:

c1<- click(isoterma.ene, id = TRUE, xy = TRUE)
c2<- click(isoterma.ene, id = TRUE, xy = TRUE)
c3<- click(isoterma.ene, id = TRUE, xy = TRUE)
values<- c(c1$TEMP_ENE, rev(c2$TEMP_ENE), rev(c3$TEMP_ENE))
values
[1] 23 22 21 20 19 18 17 16 15 14 13 12 14 13

It looks as follows:

enter image description here

By using locator():

coor1<- locator()
coor2<- locator()
coor3<- locator()

x<- c(coor1$x, coor2$x, coor3$x)
y<- c(coor1$y, coor2$y, coor3$y)
x
 [1] -69.04728 -69.47572 -69.62774 -69.71067 -69.86269 -70.00090 -70.15292
 [8] -70.24967 -70.38787 -70.66428 -70.89923 -71.12036 -70.12528 -71.17564
y
 [1] -38.50867 -39.16729 -39.53439 -39.73954 -39.86910 -39.96628 -40.13903
 [8] -40.33338 -40.50613 -40.61410 -40.75446 -41.09997 -36.26288 -37.01868
plot(isoterma.ene)
text(x=x, y=y,label=values, cex=0.6)

Result is similar as expected; but in manual way:

enter image description here

2

A very rough start can be made by obtaining the coordinates along the lines and taking a small sample:

library(raster)
#> Loading required package: sp
r <- raster(volcano)
l <- rasterToContour(r)
plot(l)
coords <- as(l, "SpatialPointsDataFrame")
head(coords)
#>       level Lines.NR Lines.ID Line.NR
#> C_1     100        1      C_1       1
#> C_1.1   100        1      C_1       1
#> C_1.2   100        1      C_1       1
#> C_1.3   100        1      C_1       1
#> C_1.4   100        1      C_1       1
#> C_1.5   100        1      C_1       1
coords <- as.data.frame(as(l, "SpatialPointsDataFrame"))
names(coords) ## use this to check the actual column names (may not be coords.x1,coords.x2)
#> [1] "level"     "Lines.NR"  "Lines.ID"  "Line.NR"   "coords.x1" "coords.x2"
text(coords.x2~coords.x1, data = coords[sample(1:nrow(coords), 25),], lab = level, pos = 2, cex = 0.8)

Created on 2019-10-18 by the reprex package (v0.3.0)

Making them look good as in contour() is definitely not easy, but there might be a way to use the logic inside that function.

If you want to use locator() itself, try the function raster::click() to identify which line, and use that as an index to get the "level" value (TEMP_ENE in your case).

Interactively, I thought this would work but it only returns the values:

click(l, id = TRUE, xy = TRUE)

so,

click(as(l, "SpatialLines"), id = TRUE)

to get the value, and locator to get the xy, and manually combine. (If I find a better manual way I'll edit).

  • Thanks for your answer but, it doesn't work automatically. Labels were placed wrongly. However, I used your manual proposal (see my Editing Note) and it worked but, I'm looking for an automatic method. – xunilk Oct 18 at 15:18

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.