                 :coordinates coordinates}})
 
 (defn dump-dups [seq]
-  (for [[id freq] (frequencies seq)  ;; get the frequencies, destructure
-        :when (= freq 1)]            ;; this is the filter condition
-   id))
+  (for [[group els] (group-by :coords seq)  ;; get the frequencies, destructure
+        :when (= (count els) 1)]            ;; this is the filter condition
+   (first els)))
+
 
 ;; perform a mongodb aggregation of $geoNear (just to get the document distances, increasing) and $geoIntersects
 ;; take the closest result of two requests, since shapes were separated in two collections
-(defn closest-shape [perp radius]
+(defn closest-shape [perp id radius]
   (let [ q
          (fn [coll]
            (mc/aggregate

  (let [nearQuery { :geometry { $near
                             { "$geometry" {
                                :type "Point"
                                :coordinates [2.3795807361602783 48.84644395021793]
                                }
                               "$maxDistance" 100} } }
        ;; get some bati geojson
        results (mc/find-maps db "v" nearQuery)
        ;; get their coordinates
        polygons (map (fn [{{coordinates :coordinates} :geometry}] coordinates) results)
        ;; collect all the polygon segments
        all-segments (mapcat
                       (fn [polygon]
                         (mapcat
                           (fn [ring]
                             (partition 2 1 ring))
                           polygon))
                       polygons)
        ;; remove shared (duplicate) segments, since they can't be near a trottoir
        lonely-segments (dump-dups all-segments)
        ;; remove short segments, they usually are corners of bati. This is a problem with round structures TODO
        filtered-segments (filter (fn [s] (> (segment-length s) 5)) lonely-segments)
        ;; compute perpendicular segments
        perps (map (fn [lseg] [lseg (compute-perp lseg 20)]) filtered-segments)
        ;; check whether this perpendicular's first intersection is a bordure trottoir
        hits (mapcat (fn [[lseg perp]]
                       (let [closest (closest-shape perp 60)
                             valid (= "BOR" (get-in closest [:properties :info]))]
                         [valid]
                         (if valid [(compute-perp lseg (:calculated_distance closest))] [])
                         ))
                  perps)
        ]
    ;(json/write-str hits :value-fn objectId-reader)
    (json/write-str (map perp-lineString hits) :value-fn objectId-reader)
    ))


                          :spherical true
                          :maxDistance radius
                          :distanceField "calculated_distance"
                          :query {:_id {"$ne" id }
                                  :geometry { "$geoIntersects" { "$geometry" (:geometry (perp-lineString perp)) } }}
                          }}]))]
    (apply min-key
           #(if (nil? %) 10000 ;; this because I don't know how to code
             (:calculated_distance %))
           (map (comp first q) ["v" "t"]))
    ))

(apply min-key identity (remove nil? [ nil 1]))
