Skip to content

Instantly share code, notes, and snippets.

@iwillig
Created August 27, 2010 23:52
Show Gist options
  • Select an option

  • Save iwillig/554402 to your computer and use it in GitHub Desktop.

Select an option

Save iwillig/554402 to your computer and use it in GitHub Desktop.
(defn make-postgis
;; hack to make setting up a postgis database easier
;; should clean up with regex
[params]
(let [conn (.split params "@")
host (first (.split (second conn) ":"))
port-table (second (.split (second conn) ":"))
port (first (.split port-table "/"))
database (second (.split port-table "/"))
user (first (.split (first conn) ":"))
password (second (.split (first conn) ":"))]
(make-datastore {"dbtype" "postgis" "host" host "database" database "port" port "user" user "passwd" password })))
(defn find-datastore
;; "shp://path"
;; "pg://user:pass@localhost:port/db"
;; "h2://dbname"
[params]
(let [type (first (seq (.split params "://")))
params (second (seq (.split params "://")))]
(if (= type "shp") (make-datastore {"url" (-> params java.io.File. .toURL)})
(if (= type "pg") (make-postgis params)
(if (= type "h2") (make-datastore {"dbtype" "h2" "dbname" params}))))))
(defn write->layer
;; writes features to a datastore
[datastore layer features]
(let [transaction (DefaultTransaction. "add")
feature-schema (.getSchema (.getFeatureSource datastore layer))
feature-source (.getFeatureSource datastore (.getTypeName feature-schema))
collection (FeatureCollections/newCollection)
feature-builder (SimpleFeatureBuilder. feature-schema)]
(doseq [feature features] (.set feature-builder (name (key feature)) (val feature)))
(.add collection (.buildFeature feature-builder nil))
(.setTransaction feature-source transaction)
(try
(.addFeatures feature-source collection)
(.commit transaction)
(catch Exception _ (.rollback transaction ))
(finally (.close transaction)))))
(defn create-layer
;; allows the user to add layers that follow this pattern
;; {:name "Layer" :schema"name:String,type:Shapefile"}
[datastore feature]
(let [schema (make-schema (:name feature) (:schema feature))]
(.createSchema datastore schema)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment