Vector layer reprojection
Last time we showed how to give a coordinate system to a loaded vector layer. What if we want to convert the coordinates of objects from one system to another? We can use the spTransform tool from the sp library (the sp library is loaded along with rgdal).
library(rgdal) kolej <- readOGR("D:/","railroad",stringsAsFactors = F)
Display information about the railroad layer:
> railroad class : SpatialLinesDataFrame features : 213 extent : 638667.5, 678777.7, 490053.6, 519583.1 (xmin, xmax, ymin, ymax) coord. ref. : +proj=tmerc +lat_0=0 +lon_0=19 +k=0.9993 +x_0=500000 +y_0=-5300000 +ellps=GRS80 +units=m +no_defs variables : 4 names : osm_id, code, fclass, name min values : 120785643, 6101, narrow_gauge, NA max values : 460572267, 6106, rail, NA
The layer is in the system PUWG 1992. Let’s transform the coordinates of the objects into the geographic system WGS84 (EPSG:4326).
First, let’s write the layout parameters to the cs_wgs84 variable:
cs_wgs84 = crs("+init=EPSG:4326")
> cs_wgs84 CRS arguments: +init=EPSG:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
Korzystając z funkcji spTransform przeliczmy wszystkie współrzędne warstwy kolej do nowego układu:
With the function spTransform we transform all coordinates of the railroad layer into the new coordinate system:
railroad_wgs84 = spTransform(railroad,cs_wgs84)
Display the layer information:
> railroad_wgs84 class : SpatialLinesDataFrame features : 213 extent : 21.04023, 21.6354, 52.25615, 52.51271 (xmin, xmax, ymin, ymax) coord. ref. : +init=EPSG:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 variables : 4 names : osm_id, code, fclass, name min values : 120785643, 6101, narrow_gauge, NA max values : 460572267, 6106, rail, NA
The objects have been transformed to the new coordinate system.