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

前端 未结 4 1611
误落风尘
误落风尘 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:58

    The equivalent as @Yo B. answer but with sf

    library(sf)
    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))
    
    # You need first to close your polygon 
    # (first and last points must be identical)
    df <- rbind(df, df[1,])
    
    poly <- st_sf(st_sfc(st_polygon(list(as.matrix(df)))), crs = 4326)
    poly
    
    ## Simple feature collection with 1 feature and 0 fields
    ## geometry type:  POLYGON
    ## dimension:      XY
    ## bbox:           xmin: 119.4 ymin: -5.192 xmax: 119.5 ymax: -5.187
    ## epsg (SRID):    4326
    ## proj4string:    +proj=longlat +datum=WGS84 +no_defs
    ##   st_sfc.st_polygon.list.as.matrix.df....
    ## 1          POLYGON ((119.4 -5.192, 119...
    

    edit to answer a question in the comments

    See the main sf vignette for a clear and detailed explanation of sf, sfc and sfg objects summarized as :

    The three classes used to represent simple features are:

    • sf, the table (data.frame) with feature attributes and feature geometries, which contains
    • sfc, the list-column with the geometries for each feature (record), which is composed of
    • sfg, the feature geometry of an individual simple feature.

    The st_sfc function builds only the geometry column (which is a list of polygons - here with only one polygon). The "c" in sfc stands for "column". The function st_sf builds a full sf object (which has also a data.frame class) which is a data frame with a geometry column. In the given example there is no data attached to the polygon (no attributes). You can attach data by building a data.frame :

    poly <- st_sf(data.frame(landuse = "Forest", 
                             size = 23 , 
                             st_sfc(st_polygon(list(as.matrix(df))))), 
                  crs = 4326)
    poly
    ## ## Simple feature collection with 1 feature and 2 fields
    ## geometry type:  POLYGON
    ## dimension:      XYZ
    ## bbox:           xmin: 1 ymin: 119.4 xmax: 5 ymax: 119.5
    ## epsg (SRID):    4326
    ## proj4string:    +proj=longlat +datum=WGS84 +no_defs
    ## landuse size                       geometry
    ## 1  Forest   23 POLYGON Z ((1 119.4 -5.192,...
    

    You can then extract each of these elemnts form the spatial object and check their class :

    Full sf object : a data.frame with a sfc geometry column

    class(poly)
    ## "sf"         "data.frame"
    

    Third column extracted as a list : sfc object

    class(poly[[3]])
    ## "sfc_POLYGON" "sfc"    
    

    First element of the geometry column : an sfg polygon object

    class(poly[[3]][[1]])
    ## "XY"      "POLYGON" "sfg"  
    

提交回复
热议问题