Data alignment using 'spTransform'

I'm transforming data from OSGB36 (epsg:27700) to WGS84 (epsg:4326). I'm experiencing a data alignment problem. The transformation requires a 300 ft shift west of current position and I've tried many other codes but none of them place the data on OSM other than '4326'. It may be I've not correctly stated the crs. Any help most appreciated. Data is coming from a 'Postgres/Postgis' table.

library(shiny)
library(shinythemes)
library(rsconnect)
library(RPostgreSQL)
library(rpostgis)
library(sp)
library(rgdal)
library(leaflet)
library(leaflet.extras)
# Specify Driver
pg <- dbDriver("PostgreSQL")
# Connection Details
con <- dbConnect(pg, user ='######', password ="######", host = "######" , port = 5432, dbname="######")
# List all Tables in database (to test the connection)
dbListTables(con)
# Read the PostGIS table "np_owned_land" into a 'data.frame' 
dbReadTable(con, "np_owned_land", schema = "land_management") %>% head(3)
# list the Fields in the table 
dbListFields(con, "np_owned_land", schema = "land_management")
# Query the tables for each layer you require
query <- "SELECT * FROM np_owned_land"
np_owned_land <- pgGetGeom(con, c("land_management","np_owned_land"), geom = "wkb_geometry", gid = "ogc_fid", other.cols = TRUE)
query <- "SELECT * FROM land_ownership"
land_ownership <- pgGetGeom(con, c("land_management","land_ownership"), geom = "wkb_geometry", gid = "ogc_fid", other.cols = TRUE)
# Change the projection from OSGB36 (epsg:27700) to WGS 84 (epsg:4326)
np_owned_land <- spTransform(np_owned_land, CRS(SRS_string ="EPSG:4326"))
land_ownership <- spTransform(land_ownership, CRS(SRS_string ="EPSG:4326"))
ui <- fluidPage(theme = shinytheme("cerulean"),
                tags$head(HTML("<title>Land Ownership</title> <link rel='icon'type='image/png' href='logo.png'>")),
                titlePanel(title=div(img(src="BBNPA Logo_small_RGB_transparent.png", style = "padding:5px", height = 130, width = 95, align = "left","Land Ownership"))),
                h4("The land does not include management agreements and other legal interests."),
                p("The layers consist of owned land
![alignment|690x332](upload://izjUfuqMifGiv2HXeWmdZA8lpyy.png)
."),
                leafletOutput("np_owned_land", height = 700))

# Server Function:
server <- function(input, output, session)

  output$np_owned_land <- renderLeaflet ({
    map <- leaflet(np_owned_land) %>%
      setView(-3.4710, 51.8633, zoom = 10) %>%
      addTiles(group="OpenStreetMap.Mapnik") %>%
      addProviderTiles("Esri.WorldImagery", group = "Satellite") %>%
      addProviderTiles("OpenStreetMap.HOT", group = "OpenStreetMap.HOT") %>%
      addProviderTiles("Stamen.TerrainBackground", group = "Stamen.TerrainBackground") %>%
      addMiniMap(tiles = "OpenStreetMap.HOT", toggleDisplay = TRUE) %>%
      addSearchOSM() %>%
      addEasyButton(easyButton(icon="fa-globe", title="Zoom to Level 1", onClick=JS("function(btn, map){ map.setZoom(1); }"))) %>%
      addEasyButton(easyButton(icon="fa-crosshairs", title="Locate Me", onClick=JS("function(btn, map){ map.locate({setView: true}); }"))) %>%
      addPolygons(data=np_owned_land,
                  fillColor = "#e41a1c",
                  fillOpacity = 0.4,
                  stroke = "#690E0E",
                  dashArray = "4",
                  weight = 0.7,
                  label = ~name_of_area,
                  labelOptions = labelOptions(style = list("font-weight" = "normal", padding = "3px 8px"), textsize = "13px", direction = "auto"),
                  group = "np_owned_land",
                  highlight = highlightOptions(color = "black", weight = 4, bringToFront = TRUE)) %>%
      addPolygons(data=land_ownership,
                  fillColor = "#377eb8",
                  fillOpacity = 0.4,
                  stroke = "#20445F",
                  dashArray = "4",
                  weight = 0.7,
                  label = ~owner,
                  labelOptions = labelOptions(style = list("font-weight" = "normal", padding = "3px 8px"), textsize = "13px", direction = "auto"),
                  group = "land_ownership",
                  highlight = highlightOptions(color = "black", weight = 4, bringToFront = TRUE)) %>%
      addLegend("bottomleft", title = "Map Legend", colors = c("#e41a1c","#377eb8"), labels = c("NP Owned Land","Land Ownership")) %>%
      addFullscreenControl() %>%
      addMeasure() %>%
      addScaleBar(position = c("bottomleft")) %>%
      addLayersControl(baseGroups = c("OpenStreetMap.Mapnik","Satellite","OpenStreetMap.HOT","Stamen.TerrainBackground"),
                       overlayGroups = c("np_owned_land","land_ownership"),
                       options = layersControlOptions(collapsed = TRUE))
    map
  })
# ShinyApp Function:
shinyApp(ui = ui, server = server)

I am sorry not to be able to offer any help, but the code you provide is not quite reproducible - and vast majority of it is not directly related to the problem on hand (the database, leaflet and shiny parts).

Would it be possible to simplify your piece of code somewhat, at the same time making it reproducible for others to run?

Such as what coordinates you have, and which are you expecting after transformation?

On a general note I would suggest to avoid using {rgdal} because of its fast approaching retirement, and focus on {sf} instead, which 1) has all the methods you need and 2) is here to stay

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.