add a field to sf object and point shape in kml

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

add a field to sf object and point shape in kml

Martocas
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.
# Is this the proper way to add fields or there s another one?
a$b = 2
a
# you can see that the second field is after the geometry, which shows it
is nested.

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')


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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: add a field to sf object and point shape in kml

Bede-Fazekas Ákos
Dear Marta,

I have answer only to your first question.
The way you created the new column (a$b = 2) is correct.
The order of the columns and the place of the geometry column are
usually not so important, but if you want to rearrange the columns, just
use one of the following lines:
a <- a[, c(colnames(a)[!(colnames(a) %in% attr(a, "sf_column"))],
attr(a, "sf_column"))]
a <- st_sf(cbind(st_set_geometry(a, NULL), st_geometry(a)))

HTH,
Ákos Bede-Fazekas
Hungarian Academy of Sciences


2019.05.07. 13:54 keltezéssel, Marta Rufino írta:

> 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.
> # Is this the proper way to add fields or there s another one?
> a$b = 2
> a
> # you can see that the second field is after the geometry, which shows it
> is nested.
>
> 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')
>
>
> 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
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: add a field to sf object and point shape in kml

Barry Rowlingson-3
In reply to this post by Martocas
On Tue, May 7, 2019 at 12:54 PM Marta Rufino <[hidden email]>
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
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: add a field to sf object and point shape in kml

Barry Rowlingson-3
In reply to this post by Martocas
On Tue, May 7, 2019 at 12:54 PM Marta Rufino <[hidden email]>
wrote:

> Hi,
>
> Two very simple question:
>
> 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')
>
>
Setting styles for writing KML using GDAL/OGR is described here:

https://gis.stackexchange.com/questions/297494/how-to-style-kml-through-libkml-layer-creation-options

For example I have an sf data frame of Osprey tracking data (as points),
and if I create a new field:


osp$OGR_STYLE="PEN(c:#128020,w:5px)"

and then:

st_write(osp, "/tmp/osp.kml",driver="libkml")

then the features in the KML get written with:

         <LineStyle>
            <color>ff208012</color>
            <width>5</width>
          </LineStyle>

I realise now that "PEN" is probably wrong for point features, but it
should all be explained in the google OGR style docs:

https://www.gdal.org/ogr_feature_style.html

and in the links in that GIS stack exchange question...

Barry

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: add a field to sf object and point shape in kml

Vijay Lulla
Good one Barry!  As far as I'm aware sf uses rgdal to write various file
formats and writeOGR has options called dataset_options and layer_options
which are considered experimental.  Do you know if either of these options
can be used instead of creating a new field/attribute for the sf
dataframe?  More importantly, I would like to know your opinion of using
these options vs creating an attribute.
Thanks in advance,
Vijay.

On Tue, May 7, 2019 at 11:07 AM Barry Rowlingson <[hidden email]>
wrote:

> On Tue, May 7, 2019 at 12:54 PM Marta Rufino <[hidden email]>
> wrote:
>
> > Hi,
> >
> > Two very simple question:
> >
> > 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')
> >
> >
> Setting styles for writing KML using GDAL/OGR is described here:
>
>
> https://gis.stackexchange.com/questions/297494/how-to-style-kml-through-libkml-layer-creation-options
>
> For example I have an sf data frame of Osprey tracking data (as points),
> and if I create a new field:
>
>
> osp$OGR_STYLE="PEN(c:#128020,w:5px)"
>
> and then:
>
> st_write(osp, "/tmp/osp.kml",driver="libkml")
>
> then the features in the KML get written with:
>
>          <LineStyle>
>             <color>ff208012</color>
>             <width>5</width>
>           </LineStyle>
>
> I realise now that "PEN" is probably wrong for point features, but it
> should all be explained in the google OGR style docs:
>
> https://www.gdal.org/ogr_feature_style.html
>
> and in the links in that GIS stack exchange question...
>
> Barry
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: add a field to sf object and point shape in kml

Martocas
Hi,

Cool! Thank you for the very quick and efficient reply.

Just remembered an alternative for the kml challenge... using plotKML after
converting it to spatial.

library(plotKML)
plotKML(as(my.sf.file, "Spatial"), colour_scale=SAGA_pal[[1]]) # this
function has a very detailed help with many easy options. This is just an
example...

It is a pity that it still does not work directly with sf objects (hehehe -
Edzer? Tom?) - but this way it is fine.

I am trying to retrieve the issues I found previously with adding fields
this way... if I find it I will report those.

Cheers,
M.


Vijay Lulla <[hidden email]> escreveu no dia terça, 7/05/2019 à(s)
18:11:

> Good one Barry!  As far as I'm aware sf uses rgdal to write various file
> formats and writeOGR has options called dataset_options and layer_options
> which are considered experimental.  Do you know if either of these options
> can be used instead of creating a new field/attribute for the sf
> dataframe?  More importantly, I would like to know your opinion of using
> these options vs creating an attribute.
> Thanks in advance,
> Vijay.
>
> On Tue, May 7, 2019 at 11:07 AM Barry Rowlingson <[hidden email]>
> wrote:
>
>> On Tue, May 7, 2019 at 12:54 PM Marta Rufino <[hidden email]>
>> wrote:
>>
>> > Hi,
>> >
>> > Two very simple question:
>> >
>> > 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')
>> >
>> >
>> Setting styles for writing KML using GDAL/OGR is described here:
>>
>>
>> https://gis.stackexchange.com/questions/297494/how-to-style-kml-through-libkml-layer-creation-options
>>
>> For example I have an sf data frame of Osprey tracking data (as points),
>> and if I create a new field:
>>
>>
>> osp$OGR_STYLE="PEN(c:#128020,w:5px)"
>>
>> and then:
>>
>> st_write(osp, "/tmp/osp.kml",driver="libkml")
>>
>> then the features in the KML get written with:
>>
>>          <LineStyle>
>>             <color>ff208012</color>
>>             <width>5</width>
>>           </LineStyle>
>>
>> I realise now that "PEN" is probably wrong for point features, but it
>> should all be explained in the google OGR style docs:
>>
>> https://www.gdal.org/ogr_feature_style.html
>>
>> and in the links in that GIS stack exchange question...
>>
>> Barry
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> [hidden email]
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
>

--
Marta M. Rufino (auxiliary researcher)

*____________________________________________________*MARE - Marine and
Environmental Sciences Centre
Faculty of Sciences, University of Lisbon
Campo Grande, 1749-016 Lisboa,
Portugal
Tel: + 351 21 750 00 00, extension: 22576

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: add a field to sf object and point shape in kml

Barry Rowlingson-3
In reply to this post by Vijay Lulla
On Tue, May 7, 2019 at 6:11 PM Vijay Lulla <[hidden email]> wrote:

> Good one Barry!  As far as I'm aware sf uses rgdal to write various file
> formats and writeOGR has options called dataset_options and layer_options
> which are considered experimental.
>

Not quite - `sf` uses the underlying C/C++ GDAL/OGR library directly. The
`rgdal` package does the same. One doesn't use the other.

To write KML the GDAL/OGR library uses either its "kml" or its "libkml"
driver which is documented here:

https://www.gdal.org/drv_libkml.html

dataset_options and layer_options tend to be single value options per
dataset and per layer - to set something per *feature* is going to need a
lot more information, and having an extra attribute in the feature table
(spatial data frame) seems reasonable.

I've not quite understood all the libkml creation options relating to
styles so there may be an easy way to do it but I don't think the overhead
of an extra column is too much trouble.

Barry



> Do you know if either of these options can be used instead of creating a
> new field/attribute for the sf dataframe?  More importantly, I would like
> to know your opinion of using these options vs creating an attribute.
> Thanks in advance,
> Vijay.
>
> On Tue, May 7, 2019 at 11:07 AM Barry Rowlingson <[hidden email]>
> wrote:
>
>> On Tue, May 7, 2019 at 12:54 PM Marta Rufino <[hidden email]>
>> wrote:
>>
>> > Hi,
>> >
>> > Two very simple question:
>> >
>> > 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')
>> >
>> >
>> Setting styles for writing KML using GDAL/OGR is described here:
>>
>>
>> https://gis.stackexchange.com/questions/297494/how-to-style-kml-through-libkml-layer-creation-options
>>
>> For example I have an sf data frame of Osprey tracking data (as points),
>> and if I create a new field:
>>
>>
>> osp$OGR_STYLE="PEN(c:#128020,w:5px)"
>>
>> and then:
>>
>> st_write(osp, "/tmp/osp.kml",driver="libkml")
>>
>> then the features in the KML get written with:
>>
>>          <LineStyle>
>>             <color>ff208012</color>
>>             <width>5</width>
>>           </LineStyle>
>>
>> I realise now that "PEN" is probably wrong for point features, but it
>> should all be explained in the google OGR style docs:
>>
>> https://www.gdal.org/ogr_feature_style.html
>>
>> and in the links in that GIS stack exchange question...
>>
>> Barry
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> [hidden email]
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: add a field to sf object and point shape in kml

Vijay Lulla
Thank you, Barry!  Your explanation of how all these various
packages/libraries work is very helpful.  And so is your justification
(explanation?) of styling using extra column vs writeOGR options.
Appreciatively,
Vijay.

On Tue, May 7, 2019 at 3:23 PM Barry Rowlingson <[hidden email]>
wrote:

>
>
> On Tue, May 7, 2019 at 6:11 PM Vijay Lulla <[hidden email]> wrote:
>
>> Good one Barry!  As far as I'm aware sf uses rgdal to write various file
>> formats and writeOGR has options called dataset_options and layer_options
>> which are considered experimental.
>>
>
> Not quite - `sf` uses the underlying C/C++ GDAL/OGR library directly. The
> `rgdal` package does the same. One doesn't use the other.
>
> To write KML the GDAL/OGR library uses either its "kml" or its "libkml"
> driver which is documented here:
>
> https://www.gdal.org/drv_libkml.html
>
> dataset_options and layer_options tend to be single value options per
> dataset and per layer - to set something per *feature* is going to need a
> lot more information, and having an extra attribute in the feature table
> (spatial data frame) seems reasonable.
>
> I've not quite understood all the libkml creation options relating to
> styles so there may be an easy way to do it but I don't think the overhead
> of an extra column is too much trouble.
>
> Barry
>
>
>
>> Do you know if either of these options can be used instead of creating a
>> new field/attribute for the sf dataframe?  More importantly, I would like
>> to know your opinion of using these options vs creating an attribute.
>> Thanks in advance,
>> Vijay.
>>
>> On Tue, May 7, 2019 at 11:07 AM Barry Rowlingson <[hidden email]>
>> wrote:
>>
>>> On Tue, May 7, 2019 at 12:54 PM Marta Rufino <[hidden email]>
>>> wrote:
>>>
>>> > Hi,
>>> >
>>> > Two very simple question:
>>> >
>>> > 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')
>>> >
>>> >
>>> Setting styles for writing KML using GDAL/OGR is described here:
>>>
>>>
>>> https://gis.stackexchange.com/questions/297494/how-to-style-kml-through-libkml-layer-creation-options
>>>
>>> For example I have an sf data frame of Osprey tracking data (as points),
>>> and if I create a new field:
>>>
>>>
>>> osp$OGR_STYLE="PEN(c:#128020,w:5px)"
>>>
>>> and then:
>>>
>>> st_write(osp, "/tmp/osp.kml",driver="libkml")
>>>
>>> then the features in the KML get written with:
>>>
>>>          <LineStyle>
>>>             <color>ff208012</color>
>>>             <width>5</width>
>>>           </LineStyle>
>>>
>>> I realise now that "PEN" is probably wrong for point features, but it
>>> should all be explained in the google OGR style docs:
>>>
>>> https://www.gdal.org/ogr_feature_style.html
>>>
>>> and in the links in that GIS stack exchange question...
>>>
>>> Barry
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> [hidden email]
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>>
>>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo