Skip to content
Prev 27271 / 29559 Next

add a field to sf object and point shape in kml

On Tue, May 7, 2019 at 12:54 PM Marta Rufino <marta.m.rufino at gmail.com>
wrote:
What you've got there is an "sf data frame":

 > a = st_sf(a=1, geom = st_sfc(st_point(0:1)))
 > class(a)
 [1] "sf"         "data.frame"

 I'm not sure why you think its a "nested structure" - it mostly works like
a data frame but with a special column (called `geom`) that stores geometry
objects.


# Is this the proper way to add fields or there s another one?
that's how you add columns to a plain data frame, so that's how you add
columns to an sf data frame!
data frames!  The geometry can come first if you want:

 > a[,c("geom","a")]

is an equally valid sf data frame.

Strictly you should use the `st_geometry` function to get the geometry
column of an sf data frame, since it could be called `geom`, `geometry`,
`the_geom`, or anything really. For example:

copy the geom column to a new column (standard data frame ops)

a$foo = a$geom

tell `a` its geometry is in the `foo` column:

st_geometry(a) = "foo"

delete the `geom` column (standard data frame op)

a$geom=NULL

and now:
Simple feature collection with 1 feature and 1 field
geometry type:  POINT
dimension:      XY
bbox:           xmin: 0 ymin: 1 xmax: 0 ymax: 1
epsg (SRID):    NA
proj4string:    NA
  a         foo
1 1 POINT (0 1)

now `a` is an sf data frame with a geometry column called `foo`.
Hmmm not sure. What do you get if you try and read a KML with those
attributes set?