I have a dataframe df that has a Geometry Column which contains WKT. Currently there is a mix of POINT, LINESTRING AND MULTILINESTRING data in there but in the future it might also contain all the WKT types like POLYGON and the others.
If I remove all but the LINESTRING and MULTILINESTRING from my dataframe then my code below works, I get all polylines plotted.
How do I plot Geometry in R Leaflet irrespective if it is a line, polygon, point, etc all from within one column in the dataframe?
I was thinking I would need to either filter the Geometry column into a new dataframe for each type or have a series of if statements. Is this thinking right or is there a simpler way?
library(leaflet)
library(sf)
setwd("C:/Users/XXXX/Documents/R Programming Training/Leaflet Learning")
df <- read.csv("CLAWKT2.csv") # ordinary data frame
df <- st_as_sf(df, wkt = "Geometry") # convert to spatial data frame
m <- leaflet(df) %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
addProviderTiles('Esri.WorldImagery', group = "Sat View") %>%
addPolylines(
data = df$Geometry,
color = "red",
weight = 5,
group = "Polylines"
) %>%
addMiniMap(
minimized = FALSE,
toggleDisplay = TRUE,
position = "bottomleft"
) %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite", "Sat View"),
overlayGroups = c("Polylines"),
position = "topleft",
options = layersControlOptions(collapsed = TRUE)
)
m
dput(df):
structure(list(Asset_ID = 1000001:1000006, System_Name = c("System Name 1",
"System Name 2", "System Name 4", "System Name 4", "System Name 1",
"System Name 3"), Asset_Description = c("Fork", "Spoon", "Fork",
"Fork", "Knife", "Knife"), Asset_Name = c("Asset Name 1", "Asset Name 2",
"Asset Name 3", "Asset Name 4", "Asset Name 5", "Asset Name 6"
), Condition = c(1L, 3L, 5L, 5L, 2L, 3L), Geometry = structure(list(
structure(c(-1.35198486751484, -1.35234525303538, -1.35248940653067,
-1.35260441494862, -1.35263555353458, -1.35258473300277,
-1.35250559836976, -1.35232875476765, -1.35192607680683,
-1.35180865355401, -1.35179195040429, -1.35187451322375,
-1.35185429834619, -1.35178329473176, -1.35126955152303,
-1.35055959559126, -1.34978502894559, -1.34989107305621,
-1.35021695298201, -1.350317183135, -1.35065915038905, -1.35079706278466,
-1.35105996018697, -1.3516517187018, -1.35249688275413, -1.35271774874895,
-1.35292405499795, -1.35366647372453, -1.35375889918796,
-1.354010774701, -1.35412851846741, -1.35462439479792, -1.35471726841881,
-1.35474040831931, -1.35469627169117, -1.35478798316437,
-1.35490481493925, -1.35528387086618, -1.35571095347439,
-1.35577322047125, -1.35581587026861, -1.35590792899297,
-1.35600943689386, -1.3562208820016, -1.3563546466869, -1.35716562572165,
-1.35806128168316, -1.35853813024667, -1.35905337577199,
-1.35985958967845, -1.36013907704664, -1.36251821458615,
-1.36265792115304, -1.36318262266271, -1.36334686922989,
-1.36345340367592, -1.36348827561781, -1.3635893807038, -1.36373218569516,
-1.36422067066856, -1.36435848664855, -1.36440064030488,
-1.36436463781278, -1.36408942106959, -1.36283962792941,
-1.36237310571868, -1.36225206144648, -1.36190504118253,
-1.36153848956157, -1.36141888422639, -1.36124536097223,
-1.3607750850719, -1.36054500378561, -1.36050578814311, -1.36056320617949,
-1.36053267567725, -1.36032906026332, -1.36004399212011,
-1.35874793687755, -1.35834434617168, -1.35795835965026,
-1.35755662752073, -1.35723694491421, -1.35699018364039,
-1.35657916982805, -1.35613059279507, -1.35491430369687,
-1.35428681819522, -1.3535467929927, -1.35333832296981, -1.35280783616645,
-1.35264422520212, -1.35222048431593, -1.3510345432854, -1.35090226316381,
-1.35061671758139, -1.35019375996963, -1.35001911996661,
-1.34986768054462, -1.34954215333471, -1.34938071152082,
-1.34927853682671, -1.3490119966149, -1.34886654105233, -1.34868588491475,
-1.34858222009029, -1.34842466818215, -1.34839532338292,
-1.34842931519985, -1.34838809888369, -1.34827604030248,
-1.34791677906444, -1.34781032734578, -1.34736504577421,
-1.34726622053787, -1.3470650675856, -1.34692453899414, -1.34616525509612,
-1.34597184823335, -1.34580532993775, -1.34559325200233,
-1.3453051671889, -1.34499490126712, -1.34475921505699, -1.34427536175846,
-1.34431764056143, -1.34430733181747, -1.34423416754179,
-1.34408156080249, -1.34367572525899, -1.3434969621119, -1.34317611324474,
-1.34316072830446, -1.34326282023099, -1.34338800930314,
-1.34347021609699, -1.34377275650412, -1.34450422347519,
-1.34484695009675, -1.34532013050026, -1.34563518136781,
-1.34586726368103, -1.34637188063967, -1.34693883347381,
-1.34756878756897, -1.35083266612587, -1.3518708456867, 53.3665188040405,
53.3670648592674, 53.3673765930719, 53.3677833937721, 53.368177381022,
53.3684630599995, 53.3686645977258, 53.3689605490667, 53.3692825246462,
53.3694380677381, 53.3695174717774, 53.3698648273811, 53.3699899982912,
53.3700884896613, 53.37036239008, 53.3705357847352, 53.3712821369098,
53.3716905766854, 53.3722212777486, 53.37245637116, 53.3730419328704,
53.3733631859107, 53.3737744412375, 53.3744916907916, 53.3758470977014,
53.3760272219281, 53.37614518974, 53.3764713881183, 53.3765409873118,
53.3774987928917, 53.3776339773286, 53.3778638955976, 53.377966361556,
53.3781451352678, 53.3785485423452, 53.3788215045086, 53.3789864580418,
53.3792387771138, 53.3797023057244, 53.3798411223343, 53.380190784622,
53.3804727363201, 53.3806887281319, 53.3809668275634, 53.3810374901495,
53.3812480251129, 53.3819834380591, 53.3821711044499, 53.3823362216559,
53.3827733939237, 53.3830906135445, 53.3869018259181, 53.3870171748615,
53.3871517049941, 53.3872284194866, 53.3873944321715, 53.387671577772,
53.3877920559123, 53.387857420131, 53.3879954049968, 53.3880804044196,
53.38818427798, 53.3882986910424, 53.3886376716585, 53.3897048808548,
53.3902984523894, 53.3904118503725, 53.3906481991627, 53.3910594395905,
53.3911391373374, 53.3912106824521, 53.3913300811699, 53.3914442996329,
53.3915213362836, 53.3917980414656, 53.3919855152756, 53.3921835828977,
53.3923292497264, 53.3928638858022, 53.3929844764941, 53.3930590923509,
53.3931808139183, 53.3933147711706, 53.3933805813408, 53.3934317435688,
53.3934495585554, 53.3936132372758, 53.3936531142405, 53.3938010868269,
53.3937892874904, 53.3936920413448, 53.3936970564744, 53.393812180053,
53.3942344053924, 53.3942494193738, 53.3942377586893, 53.3941458576011,
53.3941342355709, 53.394173020098, 53.3943181601263, 53.3943655970331,
53.3943785247569, 53.3943532000783, 53.3943692628634, 53.3944747393774,
53.3947087240509, 53.3949323022387, 53.3950383212475, 53.3952438416935,
53.3953579416292, 53.3954640722243, 53.3956938549797, 53.3958019824157,
53.3966784804635, 53.3967793448177, 53.3968720674158, 53.3969033232058,
53.3968604159691, 53.3968767752972, 53.3969171578455, 53.3970171216256,
53.397200657016, 53.3974205878186, 53.3976499137485, 53.3982781581855,
53.3990752927601, 53.3998384308461, 53.400103758036, 53.4002473029698,
53.400445078686, 53.4005778063306, 53.4011476749186, 53.4013534882906,
53.4016529235755, 53.4018258008763, 53.4018900149294, 53.4020225714569,
53.4022302285786, 53.4022559797624, 53.4022037926322, 53.402195120724,
53.4022331865993, 53.4023688058688, 53.4024547639985, 53.4025090393943,
53.4024180668628, 53.4025186346169), dim = c(147L, 2L), class = c("XY",
"LINESTRING", "sfg")), structure(c(-3.36187363816841, -3.36189102805219,
54.6614176695771, 54.6613396436674), dim = c(2L, 2L), class = c("XY",
"LINESTRING", "sfg")), structure(list(structure(c(-1.68034047322624,
-1.68038289179782, -1.68045906926982, -1.68048512895859,
-1.68053513999523, -1.68058793251902, -1.6806304470928, -1.68068494033555,
-1.68080486828078, -1.68090160208891, -1.68098744619037,
-1.681059243053, -1.68120417228514, -1.68130363603522, -1.68144793922962,
-1.68149890595872, -1.68158988492711, -1.6816730354649, -1.68176169843031,
-1.68178287840857, -1.68180681439964, -1.68184330248185,
-1.68200852094169, -1.68211520890884, -1.68229203857493,
-1.68246049811455, -1.68265612784607, -1.68275449461309,
-1.68286754214383, -1.68296114395775, -1.68301728003276,
55.1690912809677, 55.1691070637455, 55.1691323332199, 55.169140494827,
55.1691515333531, 55.16916064358, 55.1691674400557, 55.1691754997488,
55.1691873362278, 55.1692364993597, 55.1692634651086, 55.1692745764327,
55.1692974575058, 55.1693124957266, 55.1693322244598, 55.1693390426607,
55.169338928231, 55.1693570893883, 55.1693777287495, 55.1693813027131,
55.1693859396785, 55.1693971186269, 55.1695004809808, 55.1695393201381,
55.1696303433464, 55.1697228191642, 55.1698040413541, 55.1698414188748,
55.1698879828757, 55.1699450529148, 55.1699800665141), dim = c(31L,
2L)), structure(c(-1.67991922565183, -1.67992292824692, -1.67992871281608,
-1.67994732968346, -1.67996521661522, -1.67998904788041,
-1.6800139147732, -1.68004425890084, -1.68007241769249, -1.68010884196028,
-1.68012858924333, -1.6801329331177, 55.168869598948, 55.1688759382411,
55.1688828149423, 55.1688994020405, 55.1689122671015, 55.1689300988942,
55.1689468146592, 55.1689633192048, 55.1689780828675, 55.168996915352,
55.169006643819, 55.1690087472255), dim = c(12L, 2L))), class = c("XY",
"MULTILINESTRING", "sfg")), structure(list(structure(c(-1.68034047322624,
-1.68038289179782, -1.68045906926982, -1.68048512895859,
-1.68053513999523, -1.68058793251902, -1.6806304470928, -1.68068494033555,
-1.68080486828078, -1.68090160208891, -1.68098744619037,
-1.681059243053, -1.68120417228514, -1.68130363603522, -1.68144793922962,
-1.68149890595872, -1.68158988492711, -1.6816730354649, -1.68176169843031,
-1.68178287840857, -1.68180681439964, -1.68184330248185,
-1.68200852094169, -1.68211520890884, -1.68229203857493,
-1.68246049811455, -1.68265612784607, -1.68275449461309,
-1.68286754214383, -1.68296114395775, -1.68301728003276,
55.1690912809677, 55.1691070637455, 55.1691323332199, 55.169140494827,
55.1691515333531, 55.16916064358, 55.1691674400557, 55.1691754997488,
55.1691873362278, 55.1692364993597, 55.1692634651086, 55.1692745764327,
55.1692974575058, 55.1693124957266, 55.1693322244598, 55.1693390426607,
55.169338928231, 55.1693570893883, 55.1693777287495, 55.1693813027131,
55.1693859396785, 55.1693971186269, 55.1695004809808, 55.1695393201381,
55.1696303433464, 55.1697228191642, 55.1698040413541, 55.1698414188748,
55.1698879828757, 55.1699450529148, 55.1699800665141), dim = c(31L,
2L)), structure(c(-1.67991922565183, -1.67992292824692, -1.67992871281608,
-1.67994732968346, -1.67996521661522, -1.67998904788041,
-1.6800139147732, -1.68004425890084, -1.68007241769249, -1.68010884196028,
-1.68012858924333, -1.6801329331177, 55.168869598948, 55.1688759382411,
55.1688828149423, 55.1688994020405, 55.1689122671015, 55.1689300988942,
55.1689468146592, 55.1689633192048, 55.1689780828675, 55.168996915352,
55.169006643819, 55.1690087472255), dim = c(12L, 2L))), class = c("XY",
"MULTILINESTRING", "sfg")), structure(c(-1.3612916916826,
53.3912536935424), class = c("XY", "POINT", "sfg")), structure(c(-1.4217339865527,
53.2123973008381), class = c("XY", "POINT", "sfg"))), n_empty = 0L, crs = structure(list(
input = NA_character_, wkt = NA_character_), class = "crs"), class = c("sfc_GEOMETRY",
"sfc"), precision = 0, bbox = structure(c(xmin = -3.36189102805219,
ymin = 53.2123973008381, xmax = -1.34316072830446, ymax = 55.1699800665141
), class = "bbox"), classes = c("LINESTRING", "LINESTRING", "MULTILINESTRING",
"MULTILINESTRING", "POINT", "POINT"))), row.names = c(NA, -6L
), class = c("sf", "data.frame"), sf_column = "Geometry", agr = structure(c(Asset_ID = NA_integer_,
System_Name = NA_integer_, Asset_Description = NA_integer_, Asset_Name = NA_integer_,
Condition = NA_integer_), class = "factor", levels = c("constant",
"aggregate", "identity")))
This works for me:
library(leaflet)
library(sf)
setwd("C:/Users/XXXX/Documents/R Programming Training/Leaflet Learning")
df <- read.csv("datasource.csv", stringsAsFactors = TRUE) # ordinary data frame with strings converted to FACTORS which are essentially categories. see Summary() it has grouped the geometry
df <- st_as_sf(df, wkt = "Geometry") # convert to spatial data frame
df$geometry_types <- st_geometry_type(df) # creates a new column in df data frame called geometry_types where the function st_geometry_type() has worked out the geometry type
all.lines.geometry <- df[df$geometry_types %in% c("LINESTRING","MULTILINESTRING"),] # use the new geometry_type column to filter only for LINESTRING and MULTILINESTRING
all.point.geometry <- df[df$geometry_types == "POINT",] # use the new geometry_type column to filter only for POINT
all.polygons.geometry <- df[df$geometry_types %in% c("POLYGON","MULTIPOLYGON"),] # use the new geometry_type column to filter only for POLYGON and MULTIPOLYGON
m <- leaflet(df) %>%
addTiles(group = "OSM (default)") %>%
addCircles(
data = all.point.geometry, # data source is the filtered vector we created called all.point.geometry
color = "blue",
) %>%
addPolylines(
data = all.lines.geometry, # data source is the filtered vector we created called all.lines.geometry
color = "red",
weight = 5
) %>%
addPolygons(
data = all.polygons.geometry, # data source is the filtered vector we created called all.polygons.geometry
color = "green",
)
m