Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #3 from radaczynski/master

Geojson
  • Loading branch information...
commit ecb2494f793895c00b94cbd78600672d7ddcac92 2 parents 1ba835b + 204f90f
@sunng87 authored
Showing with 38 additions and 1 deletion.
  1. +2 −0  project.clj
  2. +18 −1 src/cljts/io.clj
  3. +18 −0 test/cljts/test/io.clj
View
2  project.clj
@@ -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)
View
19 src/cljts/io.clj
@@ -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"
@@ -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))))
View
18 test/cljts/test/io.clj
@@ -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)])))
Please sign in to comment.
Something went wrong with that request. Please try again.