Shapefile Renders Incorrecly in Leaflet

I have a shapefile which renders properly in ArcMap and sf, but does not render properly in rmd leaflet. I use leaflet to render other shapefiles with no problem. Attached is the shapefile which does not render properly, it simply shows a solid blue line in space. I get no error and cannot figure out if there's something wrong with project (shouldnt be since it works in other tools) or some other conflict. I'd welcome some suggestions on how to resolve what seems to be a simple issue. I'm labeled as a new user and am not allowed to upload the shapefile in any format :frowning: . Note that I can render other shapefiles with no issues, and this particular shapefile works fine in other applications,

library(shiny)
library(leaflet)
library(sf)
library(readxl)
library(raster)
library(rgdal)

  sf1 <- shapefile('./Instance/Course/shapeFiles/Demo_Pipeline_Route.shp') # this shapefile does not show up properly

  map <- leaflet() %>%
    addTiles(group = "Open Street")%>% 
    addPolylines(data = sf1, group = "Pipeline",  color = "blue", opacity = 1)%>% 
    addLayersControl(
      baseGroups = c("Open Street", "World Imagery")
    )
  map

What does sf::st_crs(sf1) tell you?

Note that {leaflet} expects your data in unprojected coordinates (degrees). This is due to the need to reconcile your data with the numerous basemaps.

If your shapefile is in different coordinate reference system it be unusable for leaflet purposes without transforming the CRS.

1 Like

Coordinate Reference System:
User input: USA_Contiguous_Albers_Equal_Area_Conic
wkt:
PROJCRS["USA_Contiguous_Albers_Equal_Area_Conic",
BASEGEOGCRS["NAD83",
DATUM["North American Datum 1983",
ELLIPSOID["GRS 1980",6378137,298.257222101,
LENGTHUNIT["metre",1]],
ID["EPSG",6269]],
PRIMEM["Greenwich",0,
ANGLEUNIT["Degree",0.0174532925199433]]],
CONVERSION["unnamed",
METHOD["Albers Equal Area",
ID["EPSG",9822]],
PARAMETER["Latitude of false origin",37.5,
ANGLEUNIT["Degree",0.0174532925199433],
ID["EPSG",8821]],
PARAMETER["Longitude of false origin",-96,
ANGLEUNIT["Degree",0.0174532925199433],
ID["EPSG",8822]],
PARAMETER["Latitude of 1st standard parallel",29.5,
ANGLEUNIT["Degree",0.0174532925199433],
ID["EPSG",8823]],
PARAMETER["Latitude of 2nd standard parallel",45.5,
ANGLEUNIT["Degree",0.0174532925199433],
ID["EPSG",8824]],
PARAMETER["Easting at false origin",0,
LENGTHUNIT["foot",0.3048],
ID["EPSG",8826]],
PARAMETER["Northing at false origin",0,
LENGTHUNIT["foot",0.3048],
ID["EPSG",8827]]],
CS[Cartesian,2],
AXIS["(E)",east,
ORDER[1],
LENGTHUNIT["foot",0.3048,
ID["EPSG",9002]]],
AXIS["(N)",north,
ORDER[2],
LENGTHUNIT["foot",0.3048,
ID["EPSG",9002]]]]

OK, then we have got your culprit! :slight_smile:

Albers projection is not compatible with {leaflet}.

Consider this in your code, and using sf1_trans in your leaflet calls

sf1_trans <- sf::st_transform(sf1, 4326)

What this code does is that it transforms your data from Albers projection to WGS84, which is in degrees and compatible with leaflet.

1 Like

I ran the code (and have sf package installed) and get the following error:

Error in UseMethod("st_transform") :
no applicable method for 'st_transform' applied to an object of class "c('SpatialLinesDataFrame', 'SpatialLines', 'Spatial', 'SpatialVector')"

  sf1 <- shapefile('./Instance/Course/shapefiles/Demo_Pipeline_Route.shp') # this shapefile does not show up properly
  sf1_trans <- sf::st_transform(sf1, 4326)

  map <- leaflet() %>%
    addTiles(group = "Open Street")%>% 
    addPolylines(data = sf1_trans, group = "Pipeline",  color = "blue", opacity = 1)%>% 
    addLayersControl(
      baseGroups = c("Open Street", "World Imagery")
    )
  map

btw, as my first experience w\RStudio community you are quite helpful after I've spent hours researching & testing without success...

You are kind, thank you!

Consider a slight change in your code - reading via {sf} package, so the sf1 will be a native sf package object.

  sf1 <- sf::st_read('./Instance/Course/shapefiles/Demo_Pipeline_Route.shp') # this shapefile does not show up properly
  sf1_trans <- sf::st_transform(sf1, 4326)

  map <- leaflet() %>%
    addTiles(group = "Open Street")%>% 
    addPolylines(data = sf1_trans, group = "Pipeline",  color = "blue", opacity = 1)%>% 
    addLayersControl(
      baseGroups = c("Open Street", "World Imagery")
    )
  map

I get:

Error in if (length(nms) != n || any(nms == "")) stop("'options' must be a fully named list, or have no names (NULL)") : missing value where TRUE/FALSE needed

strange...
does this occur in the st_read call, or st_transform call?

you may also consider reading your shapefile as it was, and transforming it from a {sp} package to a {sf} package object in your R session (should make no difference, but you may try it - if reading via shapefile() worked before....)

sf1 <- shapefile('./Instance/Course/shapefiles/Demo_Pipeline_Route.shp') %>%
    sf::st_as_sf()

I run this:

sf1 <- shapefile('./Instance/Course/shapefiles/Demo_Pipeline_Route.shp')%>%sf::st_as_sf()
sf1_trans <- sf::st_transform(sf1, 4326)

no issues, then run this:
map <- leaflet() %>%
addTiles(group = "Open Street")%>%
addPolylines(data = sf1, group = "Pipeline", color = "blue", opacity = 1)%>%
addLayersControl(
baseGroups = c("Open Street", "World Imagery")
)
map

and get this error:

sf layer is not long-lat datasf layer has inconsistent datum (+proj=aea +lat_0=37.5 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=NAD83 +units=ft +no_defs).
Need '+proj=longlat +datum=WGS84'

my bad! this works now!

I forgot to change the name....awesome job on a Sunday!

1 Like

OK, whatever that was, I am glad you managed to fix it
Take care!

it was the transform function and learning how to get info about the CRS...I would never have found this and intend to research more around this topic.

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.