Create SpatialPointsDataframe

后端 未结 2 477
南方客
南方客 2021-01-04 02:17

I have a dataframe df1 with 10 columns. Two of these columns are lng and lat. I want to create a SpatialPointsDataframe f

2条回答
  •  -上瘾入骨i
    2021-01-04 02:58

    To make a SpatialPointsDataFrame you need 3 components:

    1. coordinates
    2. data
    3. proj4string of the coordinates (AKA, coordinate reference system (CRS))

    # load some example data
    library(sp)                       # spatial library
    data(meuse)                       # load built in dataset
    
    # prepare coordinates, data, and proj4string
    coords <- meuse[ , c("x", "y")]   # coordinates
    data   <- meuse[ , 3:14]          # data
    crs    <- CRS("+init=epsg:28992") # proj4string of coords
    
    # make the SpatialPointsDataFrame object
    spdf <- SpatialPointsDataFrame(coords      = coords,
                                   data        = data, 
                                   proj4string = crs)
    

    # check the object class
    class(spdf)
    
    [1] "SpatialPointsDataFrame"
    attr(,"package")
    [1] "sp"
    
    # plot the copper column 
    spplot(spdf, "copper")
    

提交回复
热议问题