From f89ad2e1e388f8c54c7da244c70f54d833ae2aa4 Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Tue, 31 Dec 2024 13:27:47 -0600 Subject: [PATCH] Add WKT --- spec/geo/coord_spec.cr | 10 ++++++++++ src/geo/coord.cr | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/spec/geo/coord_spec.cr b/spec/geo/coord_spec.cr index 742e5bf..fc13373 100644 --- a/spec/geo/coord_spec.cr +++ b/spec/geo/coord_spec.cr @@ -111,6 +111,16 @@ describe Geo::Coord do geojson.should be_a(GeoJSON::Coordinates) end + describe "#to_wkt" do + it "generates a Well Known Text format" do + coord = Geo::Coord.new(50.004444, 36.231389) + + ewkt = coord.to_wkt + + ewkt.should eq "POINT(36.231389 50.004444)" + end + end + describe "#to_ewkt" do it "generates an Extended Well Known Text format" do coord = Geo::Coord.new(50.004444, 36.231389) diff --git a/src/geo/coord.cr b/src/geo/coord.cr index c037d47..aa436dc 100644 --- a/src/geo/coord.cr +++ b/src/geo/coord.cr @@ -177,9 +177,8 @@ module Geo def to_ewkt(io : IO) : Nil # SRID 4326 is used for latitude and longitude # https://epsg.org/crs_4326/WGS-84.html - io << "SRID=4326;POINT(" - io << lng << ' ' << lat - io << ')' + io << "SRID=4326;" + to_wkt io end def to_ewkb : Bytes @@ -197,6 +196,16 @@ module Geo bytes end + def to_wkt : String + String.build { |str| to_wkt str } + end + + def to_wkt(io : IO) : Nil + io << "POINT(" + io << lng << ' ' << lat + io << ')' + end + def ll {lat, lng} end