How to Convert data frame to spatial coordinates

前端 未结 3 1964
醉话见心
醉话见心 2020-11-30 00:34

I have been working on earthquake data that has lat long values, and I want to convert those lat long values to spatial coordinates.

Suppose I have the following dat

3条回答
  •  一向
    一向 (楼主)
    2020-11-30 00:55

    With

    structure(list(longitude = c(128.6979, 153.0046, 104.3261, 124.9019, 
    126.7328, 153.2439, 142.8673, 152.689), latitude = c(-7.4197, 
    -4.7089, -6.7541, 4.7817, 2.1643, -5.65, 23.3882, -5.571)), .Names = c("longitude", "latitude"), class = "data.frame", row.names = c(NA, -8L))
    

    To convert to SpatialPointsDataFrame

    coordinates(df) <- cbind(df$longitude , df$latitude)
    

    As pointed out by @jazzurro you will probably need to assign a CRS to your spatial object.

    proj4string(df) = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
    

    the reverse process SpatialPointsDataFrame to original df

    df <- data.frame(longitude = coordinates(df)[,1], latitude = coordinates(df)[,2])
    

提交回复
热议问题