0

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

  • Did you get this same warning when using sf::st_read()? – elmuertefurioso Dec 4 '18 at 20:56
  • No, I don't get the same warning, but I am now struggling with how to convert to a SpatialPointsDataFrame after reading in that way, to have all the same info as I would have using shapefile or readOGR. An answer addressing that would be awesome, and I could edit my question – Emily Dec 4 '18 at 21:04
  • 1
    What do you mean by same information? Reading in with st_write() preserves all of the info of your file including projection, number of objects, and attribute data. Maybe edit the question for clarification. – elmuertefurioso Dec 4 '18 at 21:05
  • Why do you need a spatial polygon dataframe, specifically? What are you trying to do? Your simple features object is the same thing but wrapped in one dataframe-like object. – elmuertefurioso Dec 4 '18 at 21:26
  • Intersect with another spdf and calculate area, perimeter etc within features of that spdf. When I try to intersect I get "no method for coercing this S4 class to a vector" – Emily Dec 4 '18 at 21:28
1

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")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.