@@ -105,15 +105,7 @@ GeomSf <- ggproto("GeomSf", Geom,
105
105
106
106
# Need to refactor this to generate one grob per geometry type
107
107
coord <- coord $ transform(data , panel_params )
108
- grobs <- lapply(1 : nrow(data ), function (i ) {
109
- sf_grob(
110
- coord [i , , drop = FALSE ],
111
- lineend = lineend ,
112
- linejoin = linejoin ,
113
- linemitre = linemitre
114
- )
115
- })
116
- do.call(" gList" , grobs )
108
+ sf_grob(coord , lineend = lineend , linejoin = linejoin , linemitre = linemitre )
117
109
},
118
110
119
111
draw_key = function (data , params , size ) {
@@ -138,33 +130,37 @@ default_aesthetics <- function(type) {
138
130
}
139
131
}
140
132
141
- sf_grob <- function (row , lineend = " butt" , linejoin = " round" , linemitre = 10 ) {
133
+ sf_grob <- function (x , lineend = " butt" , linejoin = " round" , linemitre = 10 ) {
142
134
# Need to extract geometry out of corresponding list column
143
- geometry <- row $ geometry [[1 ]]
144
-
145
- if (inherits(geometry , c(" POINT" , " MULTIPOINT" ))) {
146
- row <- modify_list(default_aesthetics(" point" ), row )
147
- gp <- gpar(
148
- col = alpha(row $ colour , row $ alpha ),
149
- fill = alpha(row $ fill , row $ alpha ),
150
- # Stroke is added around the outside of the point
151
- fontsize = row $ size * .pt + row $ stroke * .stroke / 2 ,
152
- lwd = row $ stroke * .stroke / 2
153
- )
154
- sf :: st_as_grob(geometry , gp = gp , pch = row $ shape )
155
- } else {
156
- row <- modify_list(default_aesthetics(" poly" ), row )
157
- gp <- gpar(
158
- col = row $ colour ,
159
- fill = alpha(row $ fill , row $ alpha ),
160
- lwd = row $ size * .pt ,
161
- lty = row $ linetype ,
162
- lineend = lineend ,
163
- linejoin = linejoin ,
164
- linemitre = linemitre
165
- )
166
- sf :: st_as_grob(geometry , gp = gp )
167
- }
135
+ geometry <- x $ geometry
136
+ type <- sf_types [sf :: st_geometry_type(geometry )]
137
+ is_point <- type %in% " point"
138
+ type_ind <- match(type , c(" point" , " line" , " other" ))
139
+ defaults <- list (
140
+ GeomPoint $ default_aes ,
141
+ GeomLine $ default_aes ,
142
+ modify_list(GeomPolygon $ default_aes , list (fill = " grey90" , colour = " grey35" ))
143
+ )
144
+ default_names <- unique(unlist(lapply(defaults , names )))
145
+ defaults <- lapply(setNames(default_names , default_names ), function (n ) {
146
+ unlist(lapply(defaults , function (def ) def [[n ]] %|| % NA ))
147
+ })
148
+ alpha <- x $ alpha %|| % defaults $ alpha [type_ind ]
149
+ col <- x $ colour %|| % defaults $ colour [type_ind ]
150
+ col [is_point ] <- alpha(col [is_point ], alpha [is_point ])
151
+ fill <- x $ fill %|| % defaults $ fill [type_ind ]
152
+ fill <- alpha(fill , alpha )
153
+ size <- x $ size %|| % defaults $ size [type_ind ]
154
+ stroke <- (x $ stroke %|| % defaults $ stroke [1 ]) * .stroke / 2
155
+ fontsize <- size * .pt + stroke
156
+ lwd <- ifelse(is_point , stroke , size * .pt )
157
+ pch <- x $ shape %|| % defaults $ shape [type_ind ]
158
+ lty <- x $ linetype %|| % defaults $ linetype [type_ind ]
159
+ gp <- gpar(
160
+ col = col , fill = fill , fontsize = fontsize , lwd = lwd , lty = lty ,
161
+ lineend = lineend , linejoin = linejoin , linemitre = linemitre
162
+ )
163
+ sf :: st_as_grob(geometry , pch = pch , gp = gp )
168
164
}
169
165
170
166
# ' @export
@@ -282,3 +278,11 @@ geom_sf_text <- function(mapping = aes(), data = NULL,
282
278
layer_class = LayerSf
283
279
)
284
280
}
281
+
282
+ sf_types <- c(GEOMETRY = " other" , POINT = " point" , LINESTRING = " line" ,
283
+ POLYGON = " other" , MULTIPOINT = " point" , MULTILINESTRING = " line" ,
284
+ MULTIPOLYGON = " other" , GEOMETRYCOLLECTION = " other" ,
285
+ CIRCULARSTRING = " line" , COMPOUNDCURVE = " other" , CURVEPOLYGON = " other" ,
286
+ MULTICURVE = " other" , MULTISURFACE = " other" , CURVE = " other" ,
287
+ SURFACE = " other" , POLYHEDRALSURFACE = " other" , TIN = " other" ,
288
+ TRIANGLE = " other" )
0 commit comments