[R-sig-Geo] add a field to sf object and point shape in kml
b@row||ng@on @end|ng |rom gm@||@com
Tue May 7 16:53:16 CEST 2019
On Tue, May 7, 2019 at 12:54 PM Marta Rufino <marta.m.rufino using gmail.com>
> Two very simple question:
> What is the best way to add a variable (field) to an sf object?
> # For example, if I do:
> (a = st_sf(a=1, geom = st_sfc(st_point(0:1))))
> # I would expect this would work, and it does, but then it makes some kind
> of a nested structure which I cannot work with properly.
What you've got there is an "sf data frame":
> a = st_sf(a=1, geom = st_sfc(st_point(0: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
# Is this the proper way to add fields or there s another one?
> a$b = 2
that's how you add columns to a plain data frame, so that's how you add
columns to an sf data frame!
> # you can see that the second field is after the geometry, which shows it
> is nested.
> column order is irrelevant in plain data frames, so its irrelevant in sf
data frames! The geometry can come first if you want:
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)
Simple feature collection with 1 feature and 1 field
geometry type: POINT
bbox: xmin: 0 ymin: 1 xmax: 0 ymax: 1
epsg (SRID): NA
1 1 POINT (0 1)
now `a` is an sf data frame with a geometry column called `foo`.
> Can we change the polygon col/fill and point shape/col when exporting sf
> obejcts to kml, using the function:
> st_write(sf.object, " sf.object .kml", driver='kml')
Hmmm not sure. What do you get if you try and read a KML with those
> Thank you very much in advance,
> Best wishes,
> My first steps in sf: https://rpubs.com/MRufino/488297
> [[alternative HTML version deleted]]
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
[[alternative HTML version deleted]]
More information about the R-sig-Geo