[R-sig-Geo] add a field to sf object and point shape in kml
Barry Rowlingson
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>
wrote:
> Hi,
>
> Two very simple question:
>
> 1)
> 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)))
> 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?
> 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!
> a
> # 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:
> 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:
> a
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`.
> 2)
> 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
attributes set?
>
> Thank you very much in advance,
> Best wishes,
> M.
>
> ------
> 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
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list