UK BNG (Easting/Northing) to lat/long in R - r

I am a new user of R and I have a project where some coordinates are provided in British National Grid coordinates. These are not recognised by our preferred mapping program, so I would prefer to convert to lat/long. I have found some resources on how to do this using the code below:
require("rgdal") || install.packages("rgdal")
require("sp") || install.packages("sp")
library(rgdal)
# prepare UTM coordinates matrix
utmcoor<-SpatialPoints(cbind(knime.in$"Easting",knime.in$"Northing"), proj4string=CRS("+proj=utm +zone=30"))
#utmdata$X and utmdata$Y are corresponding to UTM Easting and Northing, respectively.
#zone= UTM zone
# converting
longlatcoor<-spTransform(utmcoor,CRS("+proj=longlat"))
However, the results are not what I was expecting. I tried an Easting/Northing for somewhere in Greenwich as an example and the point eventually mapped was somewhere off the Ivory Coast! I don't think UTM here is the correct system to specify, but I do not know how to specify BNG. I don't think I'm too far off solving the problem, but I could do with a bit of assistance. Thank you!
Example:
Tried the above code with East./North. 537869 , 178976. Was expecting 51.492927 , -0.015449524, but got the result -2.6595478348955846, 1.6192189249637292 instead.

Related

How can I edit the values of coordinates within a geojson file?

I am trying to map a geojson file (A map of Alaska precincts, downloaded from the division of elections and then converted online to geojson) onto a choropleth map using folium, the problem is the coordinates are in 7-digit numbers like this:
[ -16624764.227, 8465801.1497 ]
I read on a similar post that this was most likely a US coordinate system like UTM or State Plane, and recommended using an API to reproject it. Is it also possible to access the coordinates directly such as with geopandas and divide them by 100000?
The data is most likely in a specific cartographic projection. You don't just want to divide by 100k - the data will likely have nonlinear transformations which have a different effect on the position depending on the location. See the GeoPandas docs on working with projections.
If the CRS of the data is correctly encoded, you can re-project the dataframe into lat/lons (e.g. WGS84, which has the EPSG Code 4326) using geopandas.GeoDataFrame.to_crs, e.g.:
df_latlon = df.to_crs("epsg:4326")

Strange coordinate units

I received a .gdb file that I need to do analysis on. This is an ESRI file, but I don't have access to any ESRI products. I opened it in R using the "rgdal" package, but the coordinates it displays seem very off.
For example, (-9288065, 4604652) should map to Kentucky. My initial thought was that there are missing decimals, so it should be -92.88 and -46.04, but this maps to somewhere north of Kentucky, which is wrong.
Do these coordinate units look familiar? How might I convert them to latitude and longitude?
Solved with help from #camille.
For future users:
library(rgdal)
library(raster)
myFeatureClass<-readOGR('your_data_here')
#checks the projection type
sp::proj4string(myFeatureClass)
#convert projection to longlat
final_data <- as.data.frame(spTransform(myFeatureClass, CRS("+proj=longlat +datum=WGS84")))

Create buffer around spatial data in R

I have a spatial dataset of shopping centers that I would like to create buffers around in R.
I think these packages will be useful:
require(maptools)
require(geosphere)
I was able to do so for a set of coordinates, but not for spatial data. The code looks like this:
coordinates(locs) <- c("Longitude", "Latitude") # set spatial coordinates
fivekm <- cbind(coordinates(locs), X=rowSums(distm (coordinates(locs)[,1:2], fun = distHaversine) / 1000 <= 5)) # number of points within 5 km
But I don't know what function/package to use for a set of polygons. Can someone please advise on the function (or code) and I will go from there?
Thanks!
In library rgeos, there is the gBuffer function that works with SpatialPoints or SpatialPolygons.
The width parameter allows to set the distance to which you want to buffer. However, be careful, this distance is in the scale of the coordinates system used. Thus, in degrees and not in meters with non-projected data. As suggested by #Ege Rubak, you will have to project your data with spTransform first (be sure to use the appropriate CRS according to your location).
As for now, rgeos library works with library sp, but not (yet?) with the recent sf.
I think the only option at the moment is to project your longitude and latitude points to a flat map and then do everything there. As far as I know there are no packages for doing polygonal geometry on the sphere yet (I'm working on one, but there is no ETA).
Projection used to be done with spTransform from the sp package, but now it may be more convenient to use the more modern simple features package sf which has the function st_transform. The vignette https://cran.r-project.org/web/packages/sf/vignettes/sf1.html has a section called "Coordinate reference systems and transformations" to help you with this part. The buffering is described in the section "Geometrical operations".
The two previous post have covered the details but I thought it might be helpful to provide a workflow. This is assuming you have you are using points of lat and long. What is your original spatial data format?
Convert your coordinates into a Spatial Points Dataframe SpatialPointsDataFrame and assign it a geographic CRS (proj4) that matches your coordinate data (probably WGS84)
Change the projection to a local projected CRS with preferred units
Apply buffer to spatial point data frame, the width will now be in more usable units

Geospatial data: weird longitude/latitude to the shapefile

I am just getting started with geospatial data and have a question related to longitude/latitude. I downloaded the shapefile for the French departments (http://professionnels.ign.fr/geofla) and used R to read in the data:
library(rgdal)
library(maptools)
map <- readOGR(dsn="...",layer="DEPARTEMENT")
map_f <- fortify(map)
head(map_f)
If I look at the longitude-latitude, it are numbers like long = 885661.8, lat = 6679942. In contrast, the long-lat combination for Paris is 2.352222, 48.85661 (geocode("Paris")). So I don't really get why the numbers are so different in magnitude? Can somebody help me or give some good references?
Thanks in advance!
The coordinates look projected (perhaps UTM) and not lat,long. I think that the fields are just mislabeled. I do not read French but in quick scan of the site you linked this caught my eye:
En métropole : (RGF 93) projection Lambert93
En outre-mer : (système légal)- Projections UTM
Neither of which are geographic coordinate systems. Please track down the metadata and check the coordinate system. You can also check proj4string(map) to see it there is a coordinate system defined.

Can ggmap base map be converted to UTM?

I am wondering whether the reference coordinates of the base map acquired using get_googlemap() of ggmap can be converted to UTM coordinate system ?
I would like to display a heatmap on a base map of a city acquired by get_googlemap(). The data of my heatmap is referenced in UTMs (i.e. metres) as I find these coordinates more meaningful at the city scale than decimal degrees. I have researched SO but it seems the solutions are to go the other way i.e. to convert the heat map UTM data into lat long system, but this is not my preference.
Has anyone a solution for this ? Any guidance and help is greatly appreciated.
Many thanks !

Resources