-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add conversion methods to ease usage (#56)
- Loading branch information
Showing
10 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module RGeo | ||
# This module serves to provide handy methods when using GeoJSON. The methods | ||
# provided eases the passage between entities and GeoJSON String/Hash. | ||
module GeoJSON::ConversionMethods | ||
# Convert a geometry to a GeoJSON Hash | ||
def as_geojson | ||
GeoJSON.encode(self) | ||
end | ||
alias as_json as_geojson | ||
|
||
# Convert a geometry to a GeoJSON String | ||
def to_geojson | ||
::MultiJson.dump(as_geojson) | ||
end | ||
alias to_json to_geojson | ||
end | ||
|
||
# These convenience methods are added directly into the module rather than | ||
# including the module above into the Feature::Instance module which is in | ||
# every geometry implementation. This is due to a behavior in ruby versions | ||
# <3.0.2 where dynamically included modules will not be included automatically | ||
# in the ancestor tree. | ||
# See https://bugs.ruby-lang.org/issues/9573 for more information. | ||
module Feature | ||
module Instance | ||
# Convert a geometry to a GeoJSON Hash | ||
def as_geojson | ||
GeoJSON.encode(self) | ||
end | ||
alias as_json as_geojson | ||
|
||
# Convert a geometry to a GeoJSON String | ||
def to_geojson | ||
::MultiJson.dump(as_geojson) | ||
end | ||
alias to_json to_geojson | ||
end | ||
|
||
module Factory::Instance | ||
# Parses a GeoJSON String/Hash, or an IO object from which | ||
# the JSON string is read and returns the corresponding | ||
# feature. Returns nil if unable to parse. | ||
def parse_geojson(input) | ||
GeoJSON.decode(input, geo_factory: self) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module RGeo | ||
module GeoJSON # :nodoc: | ||
module Tests # :nodoc: | ||
module Common # :nodoc: | ||
module ConversionTests # :nodoc: | ||
def test_geometry_to_geojson | ||
pt = @factory.point(1, 2) | ||
assert_equal( | ||
{ "type" => "Point", "coordinates" => [1.0, 2.0] }, | ||
pt.as_geojson | ||
) | ||
assert_equal( | ||
'{"type":"Point","coordinates":[1.0,2.0]}', | ||
pt.to_geojson | ||
) | ||
end | ||
|
||
def test_parse_geojson | ||
pt = @factory.point(1, 2) | ||
assert_equal(pt, @factory.parse_geojson(pt.as_geojson)) | ||
assert_equal(pt, @factory.parse_geojson(pt.to_geojson)) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require "minitest/autorun" | ||
require_relative "../../lib/rgeo-geojson" | ||
require_relative "../common/conversion_test" | ||
|
||
class GeosConversionTest < Minitest::Test # :nodoc: | ||
include RGeo::GeoJSON::Tests::Common::ConversionTests | ||
|
||
def setup | ||
@factory = RGeo::Geos.factory | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require "minitest/autorun" | ||
require_relative "../../lib/rgeo-geojson" | ||
require_relative "../common/conversion_test" | ||
|
||
class GeosFFIConversionTest < Minitest::Test # :nodoc: | ||
include RGeo::GeoJSON::Tests::Common::ConversionTests | ||
|
||
def setup | ||
@factory = RGeo::Geos.factory(native_interface: :ffi) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require "minitest/autorun" | ||
require_relative "../../lib/rgeo-geojson" | ||
require_relative "../common/conversion_test" | ||
|
||
class CartesianConversionTest < Minitest::Test # :nodoc: | ||
include RGeo::GeoJSON::Tests::Common::ConversionTests | ||
|
||
def setup | ||
@factory = RGeo::Cartesian.simple_factory | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require "minitest/autorun" | ||
require_relative "../../lib/rgeo-geojson" | ||
require_relative "../common/conversion_test" | ||
|
||
class MercatorConversionTest < Minitest::Test # :nodoc: | ||
include RGeo::GeoJSON::Tests::Common::ConversionTests | ||
|
||
def setup | ||
@factory = RGeo::Geographic.simple_mercator_factory | ||
end | ||
end |