                 value)
               (pr-str color)))))
 
-(defn point-cartocss [point-size point-color-column point-color-mapping layer-index]
-  (format "#s {
-              marker-allow-overlap: true;
-              marker-fill-opacity: 0.8;
-              marker-fill: %s;
-              marker-line-color: #fff;
-              marker-width: %s;
-              %s
-           }"
-          (layer-point-color layer-index)
-          (marker-width point-size)
-          (str/join " " (point-color-css point-color-column point-color-mapping))))
-
-(defn shape-cartocss [layer-index layer]
-  (cond-> (format "#s {
-              polygon-opacity: 0.8;
-              polygon-fill: transparent;
-              line-width: 2;
-              line-color: rgba(0,0,0,0.3);
-           }
-           "
-          (layer-point-color layer-index))
-          (get layer "shapeLabelColumn")
-          (str (format "#s::labels {
-            text-name: [%s];
-            text-face-name: 'DejaVu Sans Book';
-            text-size: 10;
-            text-fill: #000;
-          }"
-          (get layer "shapeLabelColumn"))))
-)
-
-(defn shape-aggregation-cartocss [layer-index layer]
-  (cond-> (format "#s {
-              polygon-opacity: 0.8;
-              polygon-fill: [shapefill];
-              line-width: 0.5;
-              line-color: rgba(0,0,0,0.3);
-           }
-           "
-          (layer-point-color layer-index))
-          (get layer "shapeLabelColumn")
-          (str (format "#s::labels {
-            text-name: [%s];
-            text-face-name: 'DejaVu Sans Book';
-            text-size: 10;
-            text-fill: #000;
-          }"
-          (get layer "shapeLabelColumn"))))
-)
-
-(defn cartocss [layer layer-index metadata-array]
+(defn point-cartocss
+  [point-size point-color-column point-color-mapping layer-index]
+  (let [s-template "
+#s {
+    marker-allow-overlap: true;
+    marker-fill-opacity: 0.8;
+    marker-fill: %s;
+    marker-line-color: #fff;
+    marker-width: %s;
+    %s
+}\n"]
+    (format s-template
+            (layer-point-color layer-index)
+            (marker-width point-size)
+            (str/join " "
+                      (point-color-css point-color-column point-color-mapping)))))
+
+(defn shape-cartocss [layer-index {:strs [shapeLabelColumn] :as layer}]
+  (let [s-template "
+#s {
+    polygon-opacity: 0.8;
+    polygon-fill: transparent;
+    line-width: 2;
+    line-color: rgba(0,0,0,0.3);
+}\n"
+        labels-template "
+#s::labels {
+    text-name: [%s];
+    text-face-name: 'DejaVu Sans Book';
+    text-size: 10;
+    text-fill: #000;
+}\n"]
+    (cond-> (format s-template (layer-point-color layer-index))
+      shapeLabelColumn (str (format labels-template shapeLabelColumn)))))
+
+(defn shape-aggregation-cartocss [layer-index {:strs [shapeLabelColumn]}]
+  (let [s-template "
+#s {
+    polygon-opacity: 0.8;
+    polygon-fill: [shapefill];
+    line-width: 0.5;
+    line-color: rgba(0,0,0,0.3);
+}\n"
+        labels-template "
+#s::labels {
+    text-name: [%s];
+    text-face-name: 'DejaVu Sans Book';
+    text-size: 10;
+    text-fill: #000;
+}\n"]
+    (cond-> (format s-template (layer-point-color layer-index))
+      shapeLabelColumn
+      (str (format labels-template shapeLabelColumn)))))
+
+#_(defn cartocss [layer layer-index metadata-array]
+    (cond
+      (and (get layer "aggregationDataset")
+           (get layer "aggregationColumn")
+           (get layer "aggregationGeomColumn"))
+      (shape-aggregation-cartocss layer-index layer)
+
+      (= (get layer "layerType") "geo-shape")
+      (shape-cartocss layer-index layer)
+
+      :else
+      (point-cartocss (get layer "pointSize")
+                      (get layer "pointColorColumn")
+                      (get (nth metadata-array layer-index) "pointColorMapping")
+                      layer-index)))
+
+(defn cartocss
+  [{:strs [aggregationColumn aggregationDataset aggregationGeomColumn
+           layerType pointColorColumn pointSize] :as layer}
+   layer-index metadata-array]
   (cond
-    (and (get layer "aggregationDataset")(get layer "aggregationColumn")(get layer "aggregationGeomColumn"))
-    (shape-aggregation-cartocss layer-index layer)
-
-    (= (get layer "layerType") "geo-shape")
-    (shape-cartocss layer-index layer)
-
-    :else
-    (point-cartocss (get layer "pointSize") (get layer "pointColorColumn") (get (nth metadata-array layer-index) "pointColorMapping") layer-index)
-  )
-)
+    (and aggregationColumn
+         aggregationDataset
+         aggregationGeomColumn) (shape-aggregation-cartocss layer-index layer)
+
+    (= layerType "geo-shape") (shape-cartocss layer-index layer)
+
+    :else (point-cartocss pointSize
+                          pointColorColumn
+                          (get (nth metadata-array layer-index)
+                               "pointColorMapping")
+                          layer-index)))
 
 (defn trim-css [s]
   (-> s

                                     (str c "::text")
                                     c)) popup-columns) geom-column)
                point-color-column (conj point-color-column)))
    hue (color-to-hue (clojure.string/upper-case (get current-layer "gradientColor" "#FF0000")))
    extra-cols (popup-and-label-cols (get current-layer "popup") (get current-layer "shapeLabelColumn"))
    ]

    (format "with temp_table as
              (select

(hugsql/def-db-fns "akvo/lumen/lib/dataset.sql")

(defn layer-point-color [layer-index]
  ({0 "#2ca409"
    1 "#096ba4"
    2 "#7a09a4"
    3 "#a4092a"
    4 "#fff721"} layer-index "#000000"))

(defn marker-width [point-size]
  (let [point-size (if (string? point-size)
