 (defn geometry->iso-xml
   "Returns an individual ISO MENDS geographic extent element for a UMM
   spatial coverage geometry record."
-  [coordinate-system geom]
+  [coordinate-system geom gen-id]
   ;; set the coordinate system based on the spatial coverage for output
-  (let [geom (d/calculate-derived (umm-s/set-coordinate-system coordinate-system geom))]
+  (let [geom     (d/calculate-derived (umm-s/set-coordinate-system coordinate-system geom))]
     (list
-     (x/element :gmd:geographicElement {} (geometry->iso-geom (r/mbr geom)))
-     (x/element :gmd:geographicElement {} (geometry->iso-geom geom)))))
+     (x/element :gmd:geographicElement {} (geometry->iso-geom (r/mbr geom) gen-id))
+     (x/element :gmd:geographicElement {} (geometry->iso-geom geom gen-id)))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Public Functions

  [n]
  (x/element :gco:Decimal {} (util/double->string n)))

(def gml-id-state (atom 0))

(defn gen-id
  []
  (str "geo-" (swap! gml-id-state inc)))

(defmulti geometry->iso-geom
  "Returns content of a ISO MENDS extent element for an individual UMM
  spatial coverage geometry record. Dispatches on type."
  type)

;;; Rendering gmd:geographicElement content

(defmethod geometry->iso-geom cmr.spatial.point.Point
  [point]
  (gmd-poly (x/element :gml:Point {:gml:id (gen-id)} (gml-pos point))))

(defmethod geometry->iso-geom cmr.spatial.line_string.LineString
  [{:keys [points]}]
  (gmd-poly (x/element :gml:LineString {:gml:id (gen-id)}
                       (gml-poslist points))))

(defmethod geometry->iso-geom cmr.spatial.polygon.Polygon
  [polygon]
  (let [exterior (poly/boundary polygon)
        interior (poly/holes polygon)]
    (gmd-poly

                  (map #(x/element :gml:interior {} (gml-linear-ring (:points %))) interior))))))

(defmethod geometry->iso-geom cmr.spatial.mbr.Mbr
  [mbr gen-id]
  (x/element :gmd:EX_GeographicBoundingBox {}
             (x/element :gmd:westBoundLongitude {} (gco-decimal (:west mbr)))
             (x/element :gmd:eastBoundLongitude {} (gco-decimal (:east mbr)))
