   to point to the new (reduced) shapefile"
   [request]
   (when (= "true" (get-in request [:params "simplify-shapefile"]))
-    (if-let [tmp-file (get-in request [:params "shapefile" :tempfile])]
+    (if (get-in request [:params "shapefile" :tempfile])
       (simplify (get-in request [:params "shapefile"]))
       (errors/throw-service-error :bad-request "Missing shapefile"))))
 

                    temp-dir)
          data-store (FileDataStoreFinder/getDataStore shp-file)
          feature-source (.getFeatureSource data-store)
          feature-type (.getSchema feature-source)
          features (.getFeatures feature-source)
          feature-count (shapefile/error-if (.size features)
                                            #(< % 1)
                                            "Shapefile has no features"
                                            temp-dir)
          _ (shapefile/error-if feature-count
                                #(> % (shapefile/max-shapefile-features))
                                (format "Shapefile feature count [%d] exceeds the %d feature limit"
                                        feature-count
                                        (shapefile/max-shapefile-features))
                                nil)
          _ (debug (format "Found [%d] features" feature-count))
          iterator (.features features)]
      (try
        (loop [features (ArrayList.)]
          (if (.hasNext iterator)
            (let [feature (.next iterator)]
              (.add features feature)
              (recur features))
            (simplify-data filename features feature-type mt/shapefile)))
        (finally (do
                   (.close iterator)
                   (-> data-store .getFeatureReader .close)
                   (.delete temp-dir)
                   (.delete file)))))
    (catch Exception e
      (let [{:keys [type errors]} (ex-data e)]
        (if (and type errors)

  "Adds shapefile simplification header to response when shapefile simplication was
  requested."
  [handler default-format-fn]
  (fn [{_context :request-context :as request}]
    (try
      (if-let [[[original-point-count new-point-count] result] (simplify-shapefile request)]
        (-> (assoc-in request [:params "shapefile"] result)
