ArcMap to R – Addressing Z-Dimension Discarded When Reading Shapefiles

arcmapr

I'm reading in polygons from ArcMap 10.6.1 into R 3.5.1 using raster::shapefile. I sometimes get the following warning:

Warning message: In rgdal::readOGR(dirname(x), fn, stringsAsFactors =
stringsAsFactors, : Z-dimension discarded

I'm confused because I'm not even employing rgdal to read in. Area calculations using the resulting SpatialPolygonsDataFrame are incorrect, and I think this error reading in is why. It's projection is

CRS arguments: +proj=utm +zone=12 +datum=WGS84 +units=m +no_defs
+ellps=WGS84 +towgs84=0,0,0

I've tried readOGR and get the same warning. I am able to read in some other shape files in the same way with the same projection, and they work just fine. Why do I get this warning and how can I remedy it?

Edit: reading in using sp::st_read does not return an issue re: Z-dimensions until I try to convert. However, I need a SpatialPolygonsDataFrame to do what I need to do but this is how the file reads in:

Simple feature collection with 1 feature and 5 fields 
geometry type: MULTIPOLYGON 
dimension:  XYZ 
bbox:    xmin: 365305.5 ymin:5809943 xmax: 476043.9 ymax: 5891941 
epsg (SRID):    32612
proj4string:    +proj=utm +zone=12 +datum=WGS84 +units=m +no_defs

How do I convert to a spdf? df::as_Spatial return this error:

Error in StopZ(zm) : sp supports Z dimension only for POINT and
MULTIPOINT. use drop_zm(...) to coerce to XY dimensions

Best Answer

My understanding is that you are seeking to get the intersection of two layers and write that out to a shapefile. Using simple features in R it would look something like this:

# this is assuming your projections are matched
library(sf)

poly_file1 <- st_read("path_to_first_file.shp")
poly_file2 <- st_read("path_to_second_file.shp")

#get the intersection

poly_intersect <- st_intersection(poly_file1, poly_file2)

#write to file
st_write(poly_intersect, "path_to_new_file.shp")