Unknown CRS in QGIS when projecting to EPSG:25833 in R

Lutz

I want to project a spatial data frame to EPSG 25833 in R but QGIS does not seem to know it (for reproducibility, I use the code jazzurro created in his/her answer to this question with minor changes)

library(rgdal)

mydf <- structure(list(longitude = c(128.6979, 153.0046, 104.3261, 124.9019, 
                                     126.7328, 153.2439, 142.8673, 152.689), latitude = c(-7.4197, 
                                                                                          -4.7089, -6.7541, 4.7817, 2.1643, -5.65, 23.3882, -5.571)), .Names = c("longitude", 
                                                                                                                                                                 "latitude"), class = "data.frame", row.names = c(NA, -8L))


### Get long and lat from your data.frame. Make sure that the order is in lon/lat.

xy <- mydf[,c(1,2)]


# Here I use the projection EPSG:25833
spdf <- SpatialPointsDataFrame(coords = xy, data = mydf,
                               proj4string = CRS("+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"))


#Export as shapefile
writeOGR(spdf, "file location", "proj_test", driver="ESRI Shapefile",overwrite_layer = T) #now I write the subsetted network as a shapefile again

Now when I load the shapefile into QGIS it doesn´t know the projection.

Screenshot from QGIS

Any ideas?

Chris

In making your SpatialPointsDataFrame:

# Wrong!
spdf <- SpatialPointsDataFrame(coords = xy, data = mydf,
                               proj4string = CRS("+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"))`

You are telling the data frame what crs your points are in, so you should specify 4326 since your original data is lon/lat.

So it should be:

spdf <- SpatialPointsDataFrame(coords = xy, data = mydf,
                               proj4string = CRS("+proj=longlat +datum=WGS84"))

And then you can transform the data to another CRS using spTransform:

spTransform(spdf, CRS('+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs'))

For this particular data, we get an error because one of the points doesn't convert to your target CRS:

Error in spTransform(xSP, CRSobj, ...) : failure in points 3 In addition: Warning message: In spTransform(xSP, CRSobj, ...) : 1 projected point(s) not finite

I prefer working in sf so we could also do:

library(sf)
sfdf <- st_as_sf(mydf, coords = c('longitude', 'latitude'), crs=4326, remove=F)
sfdf_25833 <- sfdf %>% st_transform(25833)

sfdf_25833
#> Simple feature collection with 8 features and 2 fields (with 1 geometry empty)
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: 5589731 ymin: -19294970 xmax: 11478870 ymax: 19337710
#> epsg (SRID):    25833
#> proj4string:    +proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
#>   longitude latitude                   geometry
#> 1  128.6979  -7.4197 POINT (10198485 -17980649)
#> 2  153.0046  -4.7089  POINT (5636527 -19294974)
#> 3  104.3261  -6.7541                POINT EMPTY
#> 4  124.9019   4.7817  POINT (11478868 18432292)
#> 5  126.7328   2.1643  POINT (11046583 19337712)
#> 6  153.2439  -5.6500  POINT (5589731 -19158700)
#> 7  142.8673  23.3882   POINT (6353660 16093116)
#> 8  152.6890  -5.5710  POINT (5673080 -19163103)

and you can write and open with QGIS using:

write_sf(sfdf_25833, 'mysf.gpkg')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Projections with @Query don't work as expected when projecting to class

No module named sip when running QGIS from Python

Projecting my shapefile data on leaflet map using R

Projecting different points in an sf table into different UTM projections (using R)?

Projecting variables in python when applying the map function

How to change coordinate reference system (CRS) for a .shp file in R?

Alternative in R to cbind loop when final size unknown

Changing WGS84 to EPSG:5330 in R

Geoviews + Datashader is slow when projecting points

QGIS - cannot change CRS of shapefile

Unknown class R.string ... when extracting string resource

error reading shapefile in R that works fine in QGIS

Shapefile holes from ArcGIS not preserved in R or QGIS

Error when projecting partial class properties. The specified type member 'Password' is not supported in LINQ to Entities

Problems is projecting LON/LAT data in R

Projecting in DAX

error in projecting a shapefile from epsg:4326 to epsg:32056

Warning message couldBeLonLat(x) : CRS is NA using crop function in R

How to use specific CRS when creating geotools objects?

Projecting points with terra package R

adding polygon over map in R, CRS issues

Extract EPSG code from GeoDataFrame.crs result

projectRaster fails to change crs when applied to a list object in R

Nested ifelse() or case_when() for unknown number of queries in R

When replacing medians in an unknown category get NA in R

How to avoid connecting polygon vertices in the wrong direction when projecting a polygon?

Jittery xy coordinates when projecting a vector into NDC

Projecting a function between two vectors, into a data frame in R

Fundamental misunderstanding with crs and map projections in R