Right. It still doesn't work, but I now have a whole bunch of unit tests

This commit is contained in:
Simon Brooke 2018-06-28 15:12:39 +01:00
parent b83f737b7e
commit a1383c78a6
2 changed files with 34 additions and 29 deletions

View file

@ -44,7 +44,7 @@
(declare geopoint-to-osgrid vector3d-to-geopoint geopoint-to-vector3d osgrid-to-geopoint) (declare geopoint->osgrid vector3d->geopoint geopoint->vector3d osgrid->geopoint)
(def ellipsoids (def ellipsoids
@ -115,7 +115,8 @@
(ellipsoid [x] (:ellipsoid (datum x))) (ellipsoid [x] (:ellipsoid (datum x)))
;; I already am vector3d; return myself ;; I already am vector3d; return myself
(vector3d [x] x) (vector3d [x] x)
(geopoint [this datum] (vector3d-to-geopoint this datum)) (geopoint [this datum] (vector3d->geopoint this datum))
(osgrid [this datum] (geopoint->osgrid (vector3d->geopoint this datum) datum))
Transformable Transformable
(apply-transform [this transform] (apply-transform [this transform]
;; tests say this works ;; tests say this works
@ -194,7 +195,7 @@
(:key (:datum location)))) (:key (:datum location))))
(ellipsoid [location] (ellipsoid [location]
(:ellipsoid (datum [location]))) (:ellipsoid (datum [location])))
(vector3d [this] (geopoint-to-vector3d this)) (vector3d [this] (geopoint->vector3d this))
(geopoint [location new-datum] (geopoint [location new-datum]
(println (str "(geopoint " location " " new-datum ")")) (println (str "(geopoint " location " " new-datum ")"))
(if (if
@ -202,10 +203,10 @@
location location
(let [od (datum location) (let [od (datum location)
nd (datums new-datum) nd (datums new-datum)
c (geopoint-to-vector3d location)] c (geopoint->vector3d location)]
(cond (cond
(= od nd) location (= od nd) location
(= (:key od) :WGS84) (vector3d-to-geopoint (= (:key od) :WGS84) (vector3d->geopoint
(apply-transform c (:transform nd)) (apply-transform c (:transform nd))
(:datum location)) (:datum location))
(= (:key nd) :WGS84) (geopoint (= (:key nd) :WGS84) (geopoint
@ -225,7 +226,7 @@
(grid-y [location] (grid-y [location]
(:n (osgrid location (:datum location)))) (:n (osgrid location (:datum location))))
(osgrid [location datum] (osgrid [location datum]
(geopoint-to-osgrid location datum))) (geopoint->osgrid location datum)))
(defrecord OsGrid (defrecord OsGrid
@ -239,18 +240,18 @@
(grid-y [location] n) (grid-y [location] n)
(latitude [location] (latitude (geopoint location :WGS84))) (latitude [location] (latitude (geopoint location :WGS84)))
(longitude [location] (longitude (geopoint location :WGS84))) (longitude [location] (longitude (geopoint location :WGS84)))
(vector3d [location] (geopoint-to-vector3d (osgrid-to-geopoint location :WGS84))) (vector3d [location] (geopoint->vector3d (osgrid->geopoint location :WGS84)))
(geopoint [location datum] (geopoint [location datum]
(osgrid-to-geopoint location datum)) (osgrid->geopoint location datum))
(osgrid [location datum] location)) (osgrid [location datum] location))
(defn raw-vector3d-to-geopoint (defn raw-vector3d->geopoint
;; My initial design decision to allow datums to be passed as keywords or as maps ;; My initial design decision to allow datums to be passed as keywords or as maps
;; was WRONG: a datum SHALL BE passed as a key ;; was WRONG: a datum SHALL BE passed as a key
;; Tests say this works ;; Tests say this works
([v datum] ([v datum]
(vector3d-to-geopoint (:x v) (:y v) (:z v) datum)) (vector3d->geopoint (:x v) (:y v) (:z v) datum))
([x y z d] ([x y z d]
(let (let
[a (:a (:ellipsoid (datums d))) [a (:a (:ellipsoid (datums d)))
@ -276,12 +277,12 @@
;; memoisation here is used more to break mutual recursion than to speed things ;; memoisation here is used more to break mutual recursion than to speed things
;; up, although of course it will also speed things up a bit. ;; up, although of course it will also speed things up a bit.
(def vector3d-to-geopoint (memoize raw-vector3d-to-geopoint)) (def vector3d->geopoint (memoize raw-vector3d->geopoint))
(defn raw-geopoint-to-osgrid (defn raw-geopoint->osgrid
([gp datum] ([gp datum]
(geopoint-to-osgrid (:latitude gp) (:longitude gp) (:datum gp) datum)) (geopoint->osgrid (:latitude gp) (:longitude gp) (:datum gp) datum))
([latitude longitude from-datum to-datum] ([latitude longitude from-datum to-datum]
;; for bizarrely over-complicated trigonometry, look no further. ;; for bizarrely over-complicated trigonometry, look no further.
;; This doesn't work. But, to be brutally honest, I don't need it to. ;; This doesn't work. But, to be brutally honest, I don't need it to.
@ -349,14 +350,14 @@
(OsGrid. E N)))) (OsGrid. E N))))
(def geopoint-to-osgrid (memoize raw-geopoint-to-osgrid)) (def geopoint->osgrid (memoize raw-geopoint->osgrid))
(defn raw-geopoint-to-vector3d (defn raw-geopoint->vector3d
;; Tests say this no longer works ;; Tests say this no longer works
([gp] ([gp]
(println (str "(geopoint-to-vector3d " geopoint ")")) (println (str "(geopoint->vector3d " geopoint ")"))
(geopoint-to-vector3d (:latitude gp) (:longitude gp) (datum gp))) (geopoint->vector3d (:latitude gp) (:longitude gp) (datum gp)))
([latitude longitude datum] ([latitude longitude datum]
(let [φ (radians latitude) (let [φ (radians latitude)
λ (radians longitude) λ (radians longitude)
@ -375,14 +376,14 @@
(*' v (+' h (-' 1 e²)) sinφ))))) (*' v (+' h (-' 1 e²)) sinφ)))))
(def geopoint-to-vector3d (memoize raw-geopoint-to-vector3d)) (def geopoint->vector3d (memoize raw-geopoint->vector3d))
(defn raw-osgrid-to-geopoint (defn raw-osgrid->geopoint
([osgrid] ([osgrid]
(osgrid-to-geopoint osgrid :WGS84)) (osgrid->geopoint osgrid :WGS84))
([osgrid datum] ([osgrid datum]
(osgrid-to-geopoint (:e osgrid) (:n osgrid) datum)) (osgrid->geopoint (:e osgrid) (:n osgrid) datum))
([E N datum] ([E N datum]
;; this currently doesn't work ;; this currently doesn't work
(let (let
@ -460,10 +461,10 @@
(GeoPoint. (degrees φ) (degrees λ) :WGS84)))) (GeoPoint. (degrees φ) (degrees λ) :WGS84))))
(def osgrid-to-geopoint (memoize raw-osgrid-to-geopoint)) (def osgrid->geopoint (memoize raw-osgrid->geopoint))
(defn post-code-data-to-addresses (defn post-code-data->addresses
"`filename` is expected to be the path name of a JSON file containing records as documented "`filename` is expected to be the path name of a JSON file containing records as documented
[here](https://apidocs.os.uk/docs/os-places-dpa-output)." [here](https://apidocs.os.uk/docs/os-places-dpa-output)."
[filename] [filename]
@ -508,6 +509,6 @@
;; (inverse-transform { :tx 89.5, :ty 93.8 :tz 123.1 :s -1.2 :rx 0.0 :ry 0.0 :rz 0.156 }) ;; (inverse-transform { :tx 89.5, :ty 93.8 :tz 123.1 :s -1.2 :rx 0.0 :ry 0.0 :rz 0.156 })
;; (geopoint (osgrid-to-geopoint hazelfield :froboz) :WGS84) ;; (geopoint (osgrid->geopoint hazelfield :froboz) :WGS84)

View file

@ -22,7 +22,7 @@
(str "Components should be close to 1: z: " (:z transformed)))))) (str "Components should be close to 1: z: " (:z transformed))))))
(deftest osgrid-to-geopoint-tests (deftest osgrid->geopoint-tests
(testing "osgrid to geopoint one way" (testing "osgrid to geopoint one way"
(let (let
[hazelfield (yyy_data.core.OsGrid. 277656 549165) [hazelfield (yyy_data.core.OsGrid. 277656 549165)
@ -35,7 +35,7 @@
(str "Longitude should be -3.906009: " (:longitude actual)))))) (str "Longitude should be -3.906009: " (:longitude actual))))))
(deftest geopoint-to-osgrid-tests (deftest geopoint->osgrid-tests
(testing "geopoint to osgrid one way" (testing "geopoint to osgrid one way"
(let (let
[hazelfield (yyy_data.core.GeoPoint. 54.822218 -3.906009 :WGS84) [hazelfield (yyy_data.core.GeoPoint. 54.822218 -3.906009 :WGS84)
@ -48,7 +48,7 @@
(str "Easting should be 277656: " (:e actual)))))) (str "Easting should be 277656: " (:e actual))))))
(deftest osgrid-to-geopoint-round-trip-tests (deftest osgrid->geopoint-round-trip-tests
(testing "osgrid to geopoint round trip" (testing "osgrid to geopoint round trip"
(let (let
[hazelfield (yyy_data.core.OsGrid. 277656 549165) [hazelfield (yyy_data.core.OsGrid. 277656 549165)
@ -64,7 +64,7 @@
(:n hazelfield) ", " (:n actual)))))) (:n hazelfield) ", " (:n actual))))))
(deftest geopoint-to-vector-round-trip-tests (deftest geopoint->vector-round-trip-tests
(testing "round trip geopoint->vector->geopoint" (testing "round trip geopoint->vector->geopoint"
(let (let
[hazelfield (yyy_data.core.GeoPoint. 54.822218 -3.906009 :WGS84) [hazelfield (yyy_data.core.GeoPoint. 54.822218 -3.906009 :WGS84)
@ -78,7 +78,7 @@
(str "Longitude should be -3.906009: " (:longitude actual)))))) (str "Longitude should be -3.906009: " (:longitude actual))))))
(deftest osgrid-to-vector-round-trip-tests (deftest osgrid->vector-round-trip-tests
(testing "round trip osgrid->vector->osgrid" (testing "round trip osgrid->vector->osgrid"
(let (let
[hazelfield (yyy_data.core.OsGrid. 277656 549165) [hazelfield (yyy_data.core.OsGrid. 277656 549165)
@ -93,6 +93,10 @@
(str "Components should be close to one another: northing: " (str "Components should be close to one another: northing: "
(:n hazelfield) ", " (:n actual)))))) (:n hazelfield) ", " (:n actual))))))
;; (def hazelfield (yyy_data.core.OsGrid. 277656 549165))
;; (def v (vector3d hazelfield))
;; (osgrid->geopoint hazelfield :WGS84)
;; Currently blows up horribly (mutual recursion/stack). But I currently ;; Currently blows up horribly (mutual recursion/stack). But I currently
;; don't need it to work. ;; don't need it to work.