0

First time working with shape files and new to mapping so please forgive my potential ignorance here. Also, I am unable to create MWE due to access to shape file in question.

I am creating a leaflet map within Shiny and using the SF package for reading shape files. I am trying to import a shape file to display as a map overlay.

Shape file:

Simple feature collection with 13 features and 20 fields
Geometry type: MULTIPOLYGON
Dimension:     XYZ
Bounding box:  xmin: 389792.7 ymin: 2295313 xmax: 431035.9 ymax: 2376519
z_range:       zmin: 0 zmax: 0
Projected CRS: WGS 84 / UTM zone 46N

Structure:

Classes ‘sf’ and 'data.frame':  13 obs. of  21 variables:
 $ Join_Count: num  0 0 0 0 0 0 0 0 0 0 ...
 $ TARGET_FID: num  0 0 0 0 0 0 0 0 0 0 ...
 $ JOIN_FID  : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Id        : num  0 0 0 0 0 0 0 0 0 0 ...
 $ OBJECTID  : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Name      : chr  "Teknaf Beach Deposits" "Tipam" "Girujan Clay" "Upper Bokabil sst-shl alteration" ...
 $ FolderPath: chr  NA "Geology_Interpretation.kmz/Geology_Interpretation" "Geology_Interpretation.kmz/Geology_Interpretation" "Geology_Interpretation.kmz/Geology_Interpretation" ...
 $ SymbolID  : num  0 0 1 2 4 5 6 5 5 6 ...
 $ AltMode   : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Base      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Clamped   : num  0 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
 $ Extruded  : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Snippet   : chr  NA NA NA NA ...
 $ PopupInfo : chr  NA NA NA NA ...
 $ Shape_Leng: num  0 2.485 1.368 0.163 0.157 ...
 $ Shape_Le_1: num  0 2.493 1.426 0.892 0.812 ...
 $ Shape_Area: num  0 0.04115 0.00439 0.00418 0.00205 ...
 $ OBJECTID_1: num  0 1 2 3 5 9 11 12 14 15 ...
 $ layer     : chr  NA "Clip_all" "Clip_all" "Clip_all" ...
 $ path      : chr  NA "MultiPolygonZ?crs=EPSG:3857&field=OBJECTID_1:long(10,0)&field=Name:string(254,0)&field=FolderPath:string(254,0)"| __truncated__ "MultiPolygonZ?crs=EPSG:3857&field=OBJECTID_1:long(10,0)&field=Name:string(254,0)&field=FolderPath:string(254,0)"| __truncated__ "MultiPolygonZ?crs=EPSG:3857&field=OBJECTID_1:long(10,0)&field=Name:string(254,0)&field=FolderPath:string(254,0)"| __truncated__ ...
 $ geometry  :sfc_MULTIPOLYGON of length 13; first list element: List of 1
  ..$ :List of 1
  .. ..$ : num [1:832, 1:3] 424728 424746 424763 424781 424799 ...
  ..- attr(*, "class")= chr [1:3] "XYZ" "MULTIPOLYGON" "sfg"
 - attr(*, "sf_column")= chr "geometry"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
  ..- attr(*, "names")= chr [1:20] "Join_Count" "TARGET_FID" "JOIN_FID" "Id" ...

Previous shapes have been simply added using addPolygons and function perfectly. However, I am unable to map this particular file which I assume stems from two things. 1) There are no lng or lat data stored as far as I can see. 2) The file uses WGS 84 CRS whereas leaflet needs ESRI 3857?

I've attempted to transform the shapefile using shape <- shape %>% st_transform(3857) but receive the following: addPolygons must be called with both lng and lat, or with neither. So I guess this confirms that the missing lng and lat data are the issue here?

My question is, is there anything I can do to resolve this or is there a more fundamental issue with the creation of the shape file itself?

Advice appreciated!

r0bt
  • 383
  • 3
  • 12

1 Answers1

0

Your object is in a projected CRS (see the last line of your first printout). Specifically it seems to be in https://epsg.io/32646

To make it work with leaflet consider transforming (= sf::st_transform()) it to unprojected CRS - "pure" WGS 84 = EPSG::4326 should be good.

Also make double certain that you are adding your object via the data argument of leaflet::addPolygons() - and not via the first (or technically the second, since we are in a pipe) argument, which is not data but lng - thus causing your polygons object to have "defined" longitude (wrongly, but I digress) but no latitude. This is the most common root cause of the error you mention.

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44
  • Thank you for your reply. I'm already using `st_transform` to attempt to transform though not to EPSG::4326. I tried this and also specified the `data = ` argument with `addPolygons` (which I missed before). However, I receive the following: `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` – r0bt Aug 03 '22 at 16:32
  • That is an unfamiliar error... Unlike the lng & lat one which pops around quite often. I am afraid it will be hard to help any further without seeing your actual code and (a sample of) data – Jindra Lacko Aug 03 '22 at 19:10