Skip to content

Commit

Permalink
Merge pull request #3 from radaczynski/master
Browse files Browse the repository at this point in the history
Geojson
  • Loading branch information
sunng87 committed Oct 1, 2012
2 parents 1ba835b + 204f90f commit ecb2494
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions project.clj
Expand Up @@ -2,6 +2,8 @@
:description "Clojure wrapper of JTS, implements the Simple Feature Spec of Open Geospatial Consortium (OGC)."
:dependencies [[org.clojure/clojure "1.4.0"]
[com.vividsolutions/jts "1.12"]
[org.geotools/gt-geojson "8.2"]
[midje "1.4.0"]]
:repositories [["opengeo" "http://repo.opengeo.org/"]]
:profiles {:dev {:plugins [[lein-midje "2.0.0-SNAPSHOT"]]}}
:warn-on-reflection true)
19 changes: 18 additions & 1 deletion src/cljts/io.clj
Expand Up @@ -11,7 +11,10 @@
WKBWriter
WKTReader
WKTWriter
]))
])
(:import [org.geotools.geojson.geom
GeometryJSON])
)

(defn read-wkt
"read geometry object from a reader contains well-known text"
Expand Down Expand Up @@ -91,3 +94,17 @@
wkb-writer (WKBWriter. dimension bo include-srid?)]
(.write wkb-writer geo)))

(defn read-geojson
"read geometry object from a reader that contains a geojson text"
[input & {:keys [decimals]}]
(let [reader (GeometryJSON.)]
(.read reader input)))

(defn write-geojson
"write geometry as geojson to output"
([geo]
(let [writer (GeometryJSON.)]
(.toString writer geo)))
([geo output]
(let [writer (GeometryJSON.)]
(.write writer geo output))))
18 changes: 18 additions & 0 deletions test/cljts/test/io.clj
Expand Up @@ -47,3 +47,21 @@
(WKBWriter/toHex (.toByteArray bos)) =>
"0000000002000000024049000000000000403400000000000040440000000000004008000000000000"))

(fact
(write-geojson (line-string [(c 50 20) (c 40 3)]))
=> "{\"type\":\"LineString\",\"coordinates\":[[50,20],[40,3]]}")

(fact
(read-geojson "{\"type\":\"LineString\",\"coordinates\":[[50,20],[40,3]]}")
=> (line-string [(c 50 20) (c 40 3)]))

(fact
(let [sw (StringWriter.)
geo (line-string [(c 50 20) (c 40 3)])]
(write-geojson geo sw)
(.toString sw) => "{\"type\":\"LineString\",\"coordinates\":[[50,20],[40,3]]}"))

(fact
(let [sr (StringReader. "{\"type\":\"LineString\",\"coordinates\":[[50,20],[40,3]]}")
geo (read-geojson sr)]
geo => (line-string [(c 50 20) (c 40 3)])))

0 comments on commit ecb2494

Please sign in to comment.