[R-sig-Geo] Converting longitude and latitude to UK OS references
Micha Silver
t@v|b@r @end|ng |rom gm@||@com
Sun Oct 23 13:58:21 CEST 2022
Just to combine Rob's and Roger's responses:
library(sf)
# Create a data.frame from text
station_pts<-read.csv(text="Station_ID, Lat, Lon
+ 1, 55.8533, -2.38588
+ 2, 55.8551, -2.39620
+ 3, 55.8473, -2.11304")
# Make an sf object from the DF
# Careful about the order of Lon/Lat
stations<-st_as_sf(station_pts, coords=c("Lon", "Lat"), crs=4326)
# CRS transform
stations_OS <- st_transform(stations, 27700)
str(stations_OS)
Classes ‘sf’ and 'data.frame': 3 obs. of 2 variables:
$ Station_ID: int 1 2 3
$ geometry :sfc_POINT of length 3; first list element: 'XY' num
375940 662301
- attr(*, "sf_column")= chr "geometry"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA
..- attr(*, "names")= chr "Station_ID"
# Just to be sure, display CRS:
st_crs(stations_OS)
Coordinate Reference System:
User input: EPSG:27700
wkt:
PROJCRS["OSGB 1936 / British National Grid",
BASEGEOGCRS["OSGB 1936",
DATUM["OSGB 1936",
ELLIPSOID["Airy 1830",6377563.396,299.3249646,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4277]],
CONVERSION["British National Grid",
METHOD["Transverse Mercator",
ID["EPSG",9807]],
PARAMETER["Latitude of natural origin",49,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8801]],
PARAMETER["Longitude of natural origin",-2,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8802]],
PARAMETER["Scale factor at natural origin",0.9996012717,
SCALEUNIT["unity",1],
ID["EPSG",8805]],
PARAMETER["False easting",400000,
LENGTHUNIT["metre",1],
ID["EPSG",8806]],
PARAMETER["False northing",-100000,
LENGTHUNIT["metre",1],
ID["EPSG",8807]]],
CS[Cartesian,2],
AXIS["(E)",east,
ORDER[1],
LENGTHUNIT["metre",1]],
AXIS["(N)",north,
ORDER[2],
LENGTHUNIT["metre",1]],
USAGE[
SCOPE["Engineering survey, topographic mapping."],
AREA["United Kingdom (UK) - offshore to boundary of UKCS within
49°45'N to 61°N and 9°W to 2°E; onshore Great Britain (England, Wales
and Scotland). Isle of Man onshore."],
BBOX[49.75,-9,61.01,2.01]],
ID["EPSG",27700]]
If you have your station Long/Lat coordinates in a CSV file, then
`st_read()` will read directly into an sf object.
On 23/10/2022 12:08, Nick Wray wrote:
> I am trying to convert longitude and latitude values for UK weather
> stations to UK Ordnance Survey National Grid References. There are sites
> where one can do them one at a time but I have a large number. I have
> found some code which does the conversion and include the first three
> points which I want to convert as an example
>
> library(sp)
> xy<-as.data.frame(cbind(c(55.8533,55.8551,55.8473),c(-2.38588,-2.39620,-2.11304)))
> colnames(xy)<-c("lon","lat")
> xy
> coordinates(xy)<-~lon+lat
> ## see site in jounral convert llon lat
> proj4string(xy)<-CRS("+init=epsg:4326")
> ptsOS<-spTransform(xy,CRS("+init=epsg:27700"))
> ptsOS
>
> but it doesn't give the right answers - the output doesn't correspond to
> either what I get whn I put an individual point into a conversion site nor
> with other points in the same region which I have got from other sources
> and are mutually compatible
>
> Can anyone explain where I'm going wrong and how I can fix this? Thanks
> Nick Wray
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
--
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918
https://orcid.org/0000-0002-1128-1325
More information about the R-sig-Geo
mailing list