简体   繁体   中英

Converting a data frame to a spatial object

I'm trying to publish a map on Shiny, but keep running into this issue.

Let's call the data.frame region . Here are the columns:

  library(mapedit)
  library(mapview)
  library(shiny)
  library(leaflet)
  library(leaflet.extras)   



  region$address
  region$city
  region$state
  region$zip
  region$county
  region$xcol (these are the longitude coordinates)
  region$ycol (these are the latitude coordinates)

But when I run the mapview(region)@map it produces the following error:

Error: oops! Arguments xcol and/or ycol are missing! You probably expected turf_clean to be a spatial object. However it is of class data.frame. Either convert turf_clean to a spatial object or provide xcol and ycol.

I provided the x and y cols, but it's still not producing what I need.

The library sf and its st_as_sf() is a way to convert your data.frame to a spatial object. crs refers to the datum and projection, which I just guessed assuming your lat/long are relative to the WGS84 datum. I believe the projection is what maps.google.com uses - web mercator auxiliary sphere.

library(sf)
turf_clean <- st_as_sf(region, coords = c("xcol", "ycol"), crs = 4326) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM