Convert sequence of longitude and latitude to polygon via sf in R

前端 未结 4 1610
误落风尘
误落风尘 2020-12-09 12:17

I have five longitude and latitude that form a shape like this.

df <- c(order=1:5,
        lon=c(119.4,119.4,119.4,119.5,119.5), 
        lat=c(-5.192,-5.         


        
4条回答
  •  攒了一身酷
    2020-12-09 12:55

    library(sfheaders) on CRAN from 20191004 can take a data.frame and convert it to sf objects

    library(sf)
    library(sfheaders)
    
    df <- data.frame(
      lon = c(119.4, 119.4, 119.4, 119.5, 119.5),
      lat = c(-5.192, -5.192, -5.187, -5.187, -5.191)
    )
    
    sfheaders::sf_polygon(
      obj = df
    )
    ## given only two columns of data are in df there's no need to specify lon & lat arguments
    
    # Simple feature collection with 1 feature and 1 field
    # geometry type:  POLYGON
    # dimension:      XY
    # bbox:           xmin: 119.4 ymin: -5.192 xmax: 119.5 ymax: -5.187
    # epsg (SRID):    NA
    # proj4string:    
    #   id                       geometry
    # 1  1 POLYGON ((119.4 -5.192, 119...
    

提交回复
热议问题