1

I have some site boundary data as follow:

UNB_BDSB_SiBdry_v08_201028lico_25831.cpq
UNB_BDSB_SiBdry_v08_201028lico_25831.dbf
UNB_BDSB_SiBdry_v08_201028lico_25831.prj
UNB_BDSB_SiBdry_v08_201028lico_25831.sbn
UNB_BDSB_SiBdry_v08_201028lico_25831.sbx
UNB_BDSB_SiBdry_v08_201028lico_25831.shp
UNB_BDSB_SiBdry_v08_201028lico_25831.shp.xml
UNB_BDSB_SiBdry_v08_201028lico_25831.shx

Reading those is R via rgdal package:

myfile = readOGR(dsn = dsn
                 layer = "UNB_BDSB_SiBdry_v08_201028lico_25831")

I'm getting a warning:

OGR data source with driver: ESRI Shapefile 
Source: dsn, layer: "UNB_BDSB_SiBdry_v08_201028lico_25831"
with 1 features
It has 4 fields
Warning message:
In OGRSpatialRef(dsn, layer, morphFromESRI = morphFromESRI, dumpSRS = dumpSRS,  :
  Discarded datum European_Terrestrial_Reference_System_1989 in Proj4 definition: +proj=utm +zone=31 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs

And the data looks like:

head(myfile@data)
            NAME                              GISFILE SHAPE_Leng SHAPE_Area
0 Norfolk Boreas UNB_BDSB_SiBdry_v08_201028lico_25831   90513.37  442457610

Now if I use the addpolygons function from leaflet, I can not see any added polygon!

leaflet::leaflet(data = myfile) %>% addTiles() %>%
addPolygons(
            fillColor = "green",
            highlight = highlightOptions(color = "red", weight = 5,
                                         fillOpacity = 0.7,
                                         bringToFront = TRUE))

I'm wondering if there is something wrong with the data or I am doing something wrong!

Also the result using the st_read function from the sf package is as follow :

sf_data = st_read(dsn = "\\\\sv52hcn8934.corp.vattenfall.com/bgf93$/Documents/Map/220420_AH_BdyShp",
+         layer = "UNB_BDSB_SiBdry_v08_201028lico_25831")


summary(sf_data)
     NAME             GISFILE            SHAPE_Leng      SHAPE_Area                 geometry
 Length:1           Length:1           Min.   :90513   Min.   :442457610   POLYGON      :1  
 Class :character   Class :character   1st Qu.:90513   1st Qu.:442457610   epsg:25831   :0  
 Mode  :character   Mode  :character   Median :90513   Median :442457610   +proj=utm ...:0  
                                       Mean   :90513   Mean   :442457610                    
                                       3rd Qu.:90513   3rd Qu.:442457610                    
                                       Max.   :90513   Max.   :442457610 
2
  • Can you share the data? Can you use the sf package and function st_read to read the shapefile, and show us the summary or the normal print value of the object?
    – Spacedman
    May 9, 2022 at 20:53
  • @Spacedman, thanks for the reply, the result of using sf package added to the question !
    – Haribo
    May 10, 2022 at 6:59

1 Answer 1

2

Here's a sample sp-class points data frame in a projected coordinate system like your data is:

> ps
class       : SpatialPointsDataFrame 
features    : 4 
extent      : 1, 4, 1, 4  (xmin, xmax, ymin, ymax)
crs         : +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs 
variables   : 0

If I view it in leaflet, its not in the right place...

> leaflet(ps) %>% addTiles() %>% addMarkers()

and leaflet doesn't complain. But if I convert it to an sf spatial data frame and use leaflet:

> leaflet(st_as_sf(ps)) %>% addTiles() %>% addMarkers()
Warning messages:
1: sf layer is not long-lat data 
2: sf layer has inconsistent datum (+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs).
Need '+proj=longlat +datum=WGS84' 

...I get those warnings. This is telling me that leaflet needs data in lat-long coordinates, so I can transform them to coordinate system 4326:

> leaflet(st_transform(st_as_sf(ps),4326)) %>% addTiles() %>% addMarkers()

and that gets me the points in the right place with no errors.

I don't know why leaflet doesn't warn with an sp class object but does with the equivalent sf object - its another good argument for dropping sp objects and using sf as much as possible.

4
  • st_transform(st_as_sf(sf_data),4326) Simple feature collection with 1 feature and 4 fields Geometry type: POLYGON Dimension: XY Bounding box: xmin: 2.75969 ymin: 52.95313 xmax: 3.112287 ymax: 53.24133 Geodetic CRS: WGS 84 NAME GISFILE SHAPE_Leng SHAPE_Area geometry 1 Norfolk Boreas UNB_BDSB_SiBdry_v08_201028lico_25831 90513.37 442457610 POLYGON ((3.058682 53.24133...
    – Haribo
    May 10, 2022 at 11:18
  • leaflet(st_transform(sf_data,4326)) %>% addTiles() %>% addMarkers() Error in pointData.default(sanitize_sf(obj)) : Don't know how to get location data from object of class sfc_POLYGON,sfc
    – Haribo
    May 10, 2022 at 11:19
  • I still get the error from leaflet and the error is appearing by adding the addMarkers() function, but if I remove that I will just get an empty map.
    – Haribo
    May 10, 2022 at 11:20
  • 1
    I'm only using addMarkers because my test data is points. Use addPolygons for your polygons.
    – Spacedman
    May 10, 2022 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.