Definition of a coordinate system in R
It is common that no coordinate system is associated with the loaded raster or vector data. The lack of a defined system can lead to errors when using spatial analysis tools. Therefore, it is shown here how to assign a layout to a layer in a simple way.
Let’s load a previously used railroad layer without a defined coordinate system (just delete the PRJ file):
library(rgdal) kolej <- readOGR("D:/","railroad",stringsAsFactors = F)
Let’s display the 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. : NA variables : 4 names : osm_id, code, fclass, name min values : 120785643, 6101, narrow_gauge, NA max values : 460572267, 6106, rail, NA
You can see that the coord. ref. attribute has the value NA, which means that the reference coordinate system is unknown. To assign it, we will use the projection tool from the raster library:
library(raster) > projection(railroad) [1] NA
We know that the geometry data in this layer is defined in the PUWG 1992 layout. First we need to define this coordinate system using the crs function from the raster library. Argument of this function can be coordinate system parameters written in PROJ4 format:
puwg1992 = crs("+proj=tmerc +lat_0=0 +lon_0=19 +k=0.9993 +x_0=500000 +y_0=-5300000 +ellps=GRS80 +units=m +no_defs ")
or we can use EPSG codes:
puwg1992 = crs("+init=EPSG:2180")
We assign the coordinate system to the layer with an expression:
projection(railroad) = puwg1992
The presented way of assigning a coordinate system also works for rasters.