Create sf object from two-column matrix

后端 未结 2 872
北荒
北荒 2021-01-04 13:24

I have a simple two-column matrix that I want to convert to an sf object where each row specifies a point:

> set.seed(123);m=matrix(runif(10)         


        
相关标签:
2条回答
  • 2021-01-04 13:32

    As per the sf docs

    m %>% 
      as.data.frame %>% 
      sf::st_as_sf(coords = c(1,2))
    
    0 讨论(0)
  • 2021-01-04 13:42

    You can use the sfheaders library on matrices directly

    sfheaders::sf_point(m)
    
    # Simple feature collection with 5 features and 0 fields
    # geometry type:  POINT
    # dimension:      XY
    # bbox:           xmin: 0.2875775 ymin: 0.0455565 xmax: 0.9404673 ymax: 0.892419
    # epsg (SRID):    NA
    # proj4string:    NA
    # geometry
    # 1 POINT (0.2875775 0.0455565)
    # 2 POINT (0.7883051 0.5281055)
    # 3  POINT (0.4089769 0.892419)
    # 4  POINT (0.8830174 0.551435)
    # 5 POINT (0.9404673 0.4566147)
    
    0 讨论(0)
提交回复
热议问题