1

The code that I have below seems like it works in converting my vector roads which is in decimal degrees into a UTM CRS so that I can more easily work on the data in kilometers rather than decimal degrees.

However, upon inspecting roads.UTM I can see that the extent of the SpatialLinesDataFrame is still in decimal degrees, extent: 24.41195, 32.79441, -17.92888, -9.358345 (xmin, xmax, ymin, ymax), and yet the CRS says it's in +proj=UTM.

How can I convert roads into UTM so that the extent is in UTM and not decimal degrees?

If you want to download the roads shape file you may do so here: It is a shape file from the World Bank, which you can download by click on the link: "Download Zipped Shapefile Here: Zambia Roads".

Here is the code I have:

rm(list=ls())
library(sf)
roads<-readShapeSpatial("Zambia_Roads.shp")
crs(roads)
proj4string(roads) <- "+init=epsg:32735 +proj=utm +zone=35 +south +datum=WGS84 +units=m +no_defs
+ellps=WGS84 +towgs84=0,0,0"
roads.UTM<-spTransform(roads, CRS("+init=epsg:32735 +proj=utm +zone=35 +south +datum=WGS84 +units=m +no_defs
+ellps=WGS84 +towgs84=0,0,0"))
roads.UTM
7
  • It seems as if the wrong projection was assigned. You should be able to replace the defective .prj with a correct one, then project it to UTM. – Vince Nov 30 '20 at 4:06
  • @Vince I'm not sure what you mean.. is there something wrong with the code as I've written it or something that I need to add? – JAG2024 Nov 30 '20 at 4:08
  • 1
    Assigning the desired projection instead of the correct one is the cause one of the most common questions here in GIS SE. – Vince Nov 30 '20 at 4:11
  • Ok, I'm still not sure what you mean though. – JAG2024 Nov 30 '20 at 4:35
  • 1
    Note that UTM has length unit meters — not kilometers. – Mike T Dec 1 '20 at 3:59
1

After reading through the comments I figured out that I needed to set proj4string(roads)<-" +init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0" instead of the CRS I wanted to project to.

0

It looks like you are using maptools to read in the roads file and it seems that the particular function is not being maintained. If you are using library(sf) you can read the data directly into a simple features data.frame

Try:

library (sf)
roads.geo <- st_read("Zambia_Roads.shp")
roads.utm <- st_transform (roads.geo, crs = 32735)

on the other hand, if you need to re-assign a coordinate system try:

library (sf)
roads.geo <- st_read("Zambia_Roads.shp")
st_set_crs(roads.geo, value = 4326)       # setting crs to WGS84
roads.utm <- st_transform (roads.geo, crs = 32735)

you need to be aware though, that the produces a different class (sf data frame) to the SpatialLinesDataFrame you might be used to. If you can, get hold of the documentation for sf from CRAN to get a list of the host of functions available.

1
  • Thanks for this but I wanted to stick with a SpatialLinesDataFrame. – JAG2024 Dec 1 '20 at 3:05

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.