Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I transform a SF object into a Spatial Points Data Frame?

I'm currently trying to map all the zip codes in North Carolina onto a leaflet map. Ultimately, I'd like to color-code these with values from another data-set.

While I've managed to put markers onto the map with these values, doing so in a colorized fashion with zip-codes would really add value to my project.

I downloaded the USAboundaries library in R, and so I think I have enough data to create the polygons in leaflet, but the data is not in the SpatialPointsDataFrame format.

The sample output from the USAboundaries dataset looks like this:

USAboundaries::us_zipcodes()

Simple feature collection with 33144 features and 6 fields
geometry type:  POINT
dimension: XY
bbox:  
 xmin: -176.6316 
 ymin: -14.22075 
 xmax: 145.7536 
 ymax: 71.2738

epsg (SRID):    4326

proj4string:    +proj=longlat +datum=WGS84 +no_defs

First 10 features:
  zipcode zcta5ce10     affgeoid10 geoid10   aland10 awater10                   geometry
    21914     21914 8600000US21914   21914   1924479   477175 POINT (-75.98187 39.57303)
    01001     01001 8600000US01001   01001  29731610  2118827 POINT (-72.62585 42.06258)
    34736     34736 8600000US34736   34736 322808220 78588518 POINT (-81.89624 28.55458)
    46151     46151 8600000US46151   46151 530632048  9804480  POINT (-86.4305 39.44806)
    48039     48039 8600000US48039   48039  59592687  4845242 POINT (-82.55054 42.69666)

In my mind, this should be enough to transform into a SpatialPointsDataFrame, but I don't know how to do it in R! Any tips would be greatly appreciated!

like image 789
seabass20 Avatar asked Sep 09 '19 20:09

seabass20


People also ask

How do you convert data frames to spatial coordinates?

First, you take the columns of lon and lat and create an object for coord . Then, you subtract them from the original data frame and create a new object. You finally use SpatialPointsDataFrame() to create a SpatialPointsDataFrame . When you create a SpatialPointsDataFrame , you need to assign proj4string .

What is a spatial points data frame?

names (i.e. is a data. frame), then the SpatialPointsDataFrame object is formed by matching the row names of both components, leaving the order of the coordinates in tact. Checks are done to see whether both row names are sufficiently unique, and all data are matched.

What is St_as_sf?

st_as_sf: Convert foreign object to an sf object logical; the value for fill that was used in the call to map. group. logical; if TRUE , group id labels from map by their prefix before : crs. coordinate reference system to be assigned; object of class crs.


1 Answers

Leaflet supports sf objects, therefore you should not experiment any problem. Nevertheless, you can transform a sf object to a sp (such as Spatial Points Data Frame) with as(YourObject, "Spatial"). For change from sp to sf you use as_as_sf

library(sf)
library(sp)
data(meuse)
coordinates(meuse) = ~x+y
m.sf = st_as_sf(meuse) 
x = st_sfc(st_point(c(5,5)), st_point(c(6,9)), crs = 4326)
as(x, "Spatial")

See this

like image 59
Orlando Sabogal Avatar answered Nov 15 '22 09:11

Orlando Sabogal