convert character linestring to geometry in sf

user63230

Basic question, how do I convert type character LINESTRING variable to a geometry type in sf and then separately, separate the two coordinates into different variables. E.g.

library(tidyverse)
library(sf)
df <- structure(list(geometry = c("LINESTRING (-85.76 38.34, -85.72 38.38)", 
                            "LINESTRING (-85.46 38.76, -85.42 38.76)", 
                            "LINESTRING (-85.89 38.31, -85.85 38.32)"
), var1 = c(4, 5, 6
), var2 = c(1, 2, 3
)), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
))
df
# # A tibble: 3 x 3
#   geometry                                 var1  var2
#   <chr>                                   <dbl> <dbl>
# 1 LINESTRING (-85.76 38.34, -85.72 38.38)     4     1
# 2 LINESTRING (-85.46 38.76, -85.42 38.76)     5     2
# 3 LINESTRING (-85.89 38.31, -85.85 38.32)     6     3

Desired output:

final
#                     geometry   start_lon start_lat end_lon end_lat var1 var2
# -85.76, -85.72, 38.34, 38.38      -85.76     38.34  -85.72   38.38    4    1
# -85.46, -85.42, 38.76, 38.76      -85.46     38.76  -85.42   38.76    5    2
# -85.89, -85.85, 38.31, 38.32      -85.89     38.31  -85.85   38.32    6    3


#so I can quickly plot lines between points
plot(final[, "var1"])

I thought some combination like final <- st_as_sf(df) %>% st_geometry(geometry) would convert it but I cant figure it out. Perhaps sfheaders is useful here?

EDIT

I want an sf type solution and not to manually extract coordinates.

Any suggestions? thanks

SymbolixAU

library(sf) can read Well-known Text direclty, if you tell it to using the wkt = argument. This gives you your sf object.

sf <- sf::st_as_sf( df, wkt = "geometry" )
sf
# Simple feature collection with 3 features and 2 fields
# geometry type:  LINESTRING
# dimension:      XY
# bbox:           xmin: -85.89 ymin: 38.31 xmax: -85.42 ymax: 38.76
# CRS:            NA
#                         geometry var1 var2
# 1 LINESTRING (-85.76 38.34, -...    4    1
# 2 LINESTRING (-85.46 38.76, -...    5    2
# 3 LINESTRING (-85.89 38.31, -...    6    3

Then there are many ways you can get the coordinates out and reshape them. Here I'm using sfheaders to convert the sf object to a long data.frame, then using library(data.table) to reshape it.

library(sfheaders)
library(data.table)

dt <- sfheaders::sf_to_df( sf, fill = TRUE )
setDT( dt )

## In this example, every 2nd row is to be made into new columns

odds <- 1:nrow(dt) %% 2 == 1
evens <- !odds

dt <- dt[odds][
  dt[evens, .(end_lon = x, end_lat = y, linestring_id)]
  , on = "linestring_id"
]

setnames( dt, c("x","y"), c("start_lon", "start_lat"))

dt
#    var1 var2 sfg_id linestring_id start_lon start_lat end_lon end_lat
# 1:    4    1      1             1    -85.76     38.34  -85.72   38.38
# 2:    5    2      2             2    -85.46     38.76  -85.42   38.76
# 3:    6    3      3             3    -85.89     38.31  -85.85   38.32

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

변수 별 SF LINESTRING의 색상 부분

분류에서Dev

Como posso criar um objeto linnet a partir de um objeto sf com a coluna de geometria LINESTRING?

분류에서Dev

convert character set (German)

분류에서Dev

How to convert SFZ and GIG soudfonts to SF2 under Linux?

분류에서Dev

how to convert a character array to decibel

분류에서Dev

VIM - Bat to convert character encodings

분류에서Dev

Trying to Convert String to Character Pointer?

분류에서Dev

R에서 sf 라인 스트링을 플롯 할 수 없음 : CPL_geos_is_empty (st_geometry (x))의 오류

분류에서Dev

LINESTRING 도형 열이있는 SF 개체에서 시작하는 Linnet 개체를 어떻게 만들 수 있나요?

분류에서Dev

How to convert decimal value to character in c language

분류에서Dev

How to convert unicode to its original character in Python

분류에서Dev

Swift Convert Hex String or Character to Integer

분류에서Dev

boost :: geometry?

분류에서Dev

OpenGL Geometry Extrusion with geometry Shader

분류에서Dev

How to convert a json string into an individual character array in bash?

분류에서Dev

Convert an array of character to string from index i to j in c++

분류에서Dev

Convert keycode to character using javascript,like keyCode 27 to esc

분류에서Dev

Delete weird ANSI character and convert accented ones using Python

분류에서Dev

How do I convert a TTF into individual PNG character images?

분류에서Dev

LINESTRING에서 gpx로

분류에서Dev

In bash, how can I convert a Unicode Codepoint [0-9A-F] into a printable character?

분류에서Dev

Convert escape characters in strings (like "\\n" - two characters) into ASCII character (newline)

분류에서Dev

Handbrake "Storage Geometry"및 "Display Geometry"차이

분류에서Dev

Using sf::Shape with a default constructor

분류에서Dev

Compute the volume of a constructive solid geometry

분류에서Dev

Multi Coloured Geometry Control Background

분류에서Dev

ggplot2 및 sf : coord_sf에서 설정 한 제한 내 geom_sf_text

분류에서Dev

geom_sf mapping points/shapes

분류에서Dev

sf 용 ggplot의 facet_wrap

Related 관련 기사

  1. 1

    변수 별 SF LINESTRING의 색상 부분

  2. 2

    Como posso criar um objeto linnet a partir de um objeto sf com a coluna de geometria LINESTRING?

  3. 3

    convert character set (German)

  4. 4

    How to convert SFZ and GIG soudfonts to SF2 under Linux?

  5. 5

    how to convert a character array to decibel

  6. 6

    VIM - Bat to convert character encodings

  7. 7

    Trying to Convert String to Character Pointer?

  8. 8

    R에서 sf 라인 스트링을 플롯 할 수 없음 : CPL_geos_is_empty (st_geometry (x))의 오류

  9. 9

    LINESTRING 도형 열이있는 SF 개체에서 시작하는 Linnet 개체를 어떻게 만들 수 있나요?

  10. 10

    How to convert decimal value to character in c language

  11. 11

    How to convert unicode to its original character in Python

  12. 12

    Swift Convert Hex String or Character to Integer

  13. 13

    boost :: geometry?

  14. 14

    OpenGL Geometry Extrusion with geometry Shader

  15. 15

    How to convert a json string into an individual character array in bash?

  16. 16

    Convert an array of character to string from index i to j in c++

  17. 17

    Convert keycode to character using javascript,like keyCode 27 to esc

  18. 18

    Delete weird ANSI character and convert accented ones using Python

  19. 19

    How do I convert a TTF into individual PNG character images?

  20. 20

    LINESTRING에서 gpx로

  21. 21

    In bash, how can I convert a Unicode Codepoint [0-9A-F] into a printable character?

  22. 22

    Convert escape characters in strings (like "\\n" - two characters) into ASCII character (newline)

  23. 23

    Handbrake "Storage Geometry"및 "Display Geometry"차이

  24. 24

    Using sf::Shape with a default constructor

  25. 25

    Compute the volume of a constructive solid geometry

  26. 26

    Multi Coloured Geometry Control Background

  27. 27

    ggplot2 및 sf : coord_sf에서 설정 한 제한 내 geom_sf_text

  28. 28

    geom_sf mapping points/shapes

  29. 29

    sf 용 ggplot의 facet_wrap

뜨겁다태그

보관