Skip to content

Commit

Permalink
add geocrystal/geojson and to_geojson methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Apr 11, 2024
1 parent caa2a85 commit 0ea4fd7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dependencies:
github: geocrystal/geo_bearing
ring_area:
github: geocrystal/ring_area
geojson:
github: geocrystal/geojson

development_dependencies:
ameba:
Expand Down
8 changes: 8 additions & 0 deletions spec/geo/coord_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ describe Geo::Coord do
end
end

describe "#to_geojson" do
coord = Geo::Coord.new(-15, 125)

geojson = coord.to_geojson

geojson.should be_a(GeoJSON::Coordinates)
end

describe "comparisons" do
describe "equality" do
pos1 = Geo::Coord.new(45.3142533036254, -93.47527313511819)
Expand Down
18 changes: 18 additions & 0 deletions spec/geo/polygon_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ describe Geo::Polygon do
it { polygon.area.should eq(7748891609977.457) }
end

describe "#to_geojson" do
coords = [
Geo::Coord.new(-15, 125),
Geo::Coord.new(-22, 113),
Geo::Coord.new(-37, 117),
Geo::Coord.new(-33, 130),
Geo::Coord.new(-39, 148),
Geo::Coord.new(-27, 154),
Geo::Coord.new(-15, 144),
Geo::Coord.new(-15, 125),
]

polygon = Geo::Polygon.new(coords)
geojson = polygon.to_geojson

geojson.should be_a(GeoJSON::Polygon)
end

describe "comparisons" do
describe "equality" do
polygon1 = Geo::Polygon.new([pos1, pos2])
Expand Down
1 change: 1 addition & 0 deletions src/geo.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require "geohash"
require "geo_bearing"
require "haversine"
require "ring_area"
require "geojson"
require "./geo/utils"
require "./geo/coord"
require "./geo/polygon"
Expand Down
4 changes: 4 additions & 0 deletions src/geo/coord.cr
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ module Geo
Geohash.encode(lat.to_f, lng.to_f, precision)
end

def to_geojson : GeoJSON::Coordinates
GeoJSON::Coordinates.new(lng.to_f64, lat.to_f64)
end

# Returns a string representing coordinates.
#
# ```
Expand Down
6 changes: 6 additions & 0 deletions src/geo/polygon.cr
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ module Geo
size == other.size
end

def to_geojson : GeoJSON::Polygon
coordinates = @coords.map { |coord| GeoJSON::Coordinates.new(coord.lng.to_f64, coord.lat.to_f64) }

GeoJSON::Polygon.new([coordinates])
end

private def calculate_centroid : Geo::Coord
centroid_lat = 0.0
centroid_lng = 0.0
Expand Down

0 comments on commit 0ea4fd7

Please sign in to comment.