Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: zoocasa/geos-extensions
base: 51437b1780
...
head fork: zoocasa/geos-extensions
compare: c73b605923
  • 9 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
1  .gitignore
View
@@ -12,3 +12,4 @@ doc/
*.patch
.vimrc_local
debug.log
+test/local_database.yml
4 Rakefile
View
@@ -2,6 +2,9 @@
# -*- ruby -*-
require 'rubygems'
+
+gem 'rdoc', '~> 3.12'
+
require 'rubygems/package_task'
require 'rake/testtask'
require 'rdoc/task'
@@ -41,7 +44,6 @@ end
desc 'Build docs'
Rake::RDocTask.new do |t|
- require 'rdoc'
t.main = 'README.rdoc'
t.title = "Geos Extensions #{version}"
t.rdoc_dir = 'doc'
2  VERSION
View
@@ -1 +1 @@
-0.1.5
+0.1.6.dev
17 lib/geos/active_record_extensions/connection_adapters/postgresql_adapter.rb
View
@@ -11,7 +11,7 @@ def initialize(name, srid = nil, coord_dimension = nil)
end
end
- class PostgreSQLColumn < Column
+ class PostgreSQLColumn
def simplified_type_with_geometry_type(field_type)
if field_type == 'geometry'
:geometry
@@ -22,27 +22,22 @@ def simplified_type_with_geometry_type(field_type)
alias_method_chain :simplified_type, :geometry_type
end
- class PostgreSQLAdapter < AbstractAdapter
+ class PostgreSQLAdapter
# Returns the geometry columns for the table.
def geometry_columns(table_name, name = nil)
return [] if !table_exists?(table_name)
columns(table_name, name).select { |c| c.sql_type == 'geometry' }.collect do |c|
- res = execute(
- "SELECT * FROM geometry_columns WHERE f_table_name = #{quote(table_name)} AND f_geometry_column = #{quote(c.name)}",
+ res = select_rows(
+ "SELECT srid, coord_dimension FROM geometry_columns WHERE f_table_name = #{quote(table_name)} AND f_geometry_column = #{quote(c.name)}",
"Geometry column load for #{table_name}"
)
PostgreSQLGeometryColumn.new(c.name).tap do |g|
# since we're too stupid at the moment to understand
# PostgreSQL schemas, let's just go with this:
- if res.ntuples == 1
- coord_dimension_idx, srid_idx =
- res.fields.index('coord_dimension'),
- res.fields.index('srid')
-
- g.srid = res.getvalue(0, srid_idx).to_i
- g.coord_dimension = res.getvalue(0, coord_dimension_idx).to_i
+ if res.length == 1
+ g.srid, g.coord_dimension = res.first.collect(&:to_i)
end
end
end
103 lib/geos_extensions.rb
View
@@ -17,7 +17,7 @@ module Geos
autoload :ActiveRecord, File.join(GEOS_EXTENSIONS_BASE, *%w{ geos active_record_extensions })
autoload :GoogleMaps, File.join(GEOS_EXTENSIONS_BASE, *%w{ geos google_maps })
- REGEXP_FLOAT = /(-?\d+(?:\.\d+)?)/
+ REGEXP_FLOAT = /(-?\d*(?:\.\d+)?|-?\d*(?:\.\d+?)[eE][-+]?\d+)/
REGEXP_LAT_LNG = /#{REGEXP_FLOAT}\s*,\s*#{REGEXP_FLOAT}/
REGEXP_WKT = /^\s*(?:SRID=(-?[0-9]+);)?(\s*[PLMCG].+)/im
@@ -329,6 +329,10 @@ def to_flickr_bbox(options = {})
(self.north * precision).ceil / precision
].join(',')
end
+
+ def to_geojson(options = {})
+ self.to_geojsonable(options).to_json
+ end
end
@@ -406,6 +410,17 @@ def to_jsonable options = {}
}
end
end
+
+ def to_geojsonable(options = {})
+ {
+ :type => 'LineString',
+ :coordinates => self.to_a
+ }
+ end
+
+ def to_geojson(options = {})
+ self.to_geojsonable(options).to_json
+ end
end
@@ -509,6 +524,13 @@ def to_jsonable options = {}
{ :type => 'point', :lat => cs.get_y(0), :lng => cs.get_x(0) }
end
end
+
+ def to_geojsonable(options = {})
+ {
+ :type => 'Point',
+ :coordinates => self.to_a
+ }
+ end
end
@@ -516,6 +538,10 @@ class LineString
def to_jsonable(options = {})
self.coord_seq.to_jsonable(options)
end
+
+ def to_geojsonable(options = {})
+ self.coord_seq.to_geojsonable(options)
+ end
end
class Polygon
@@ -646,6 +672,29 @@ def to_jsonable options = {}
ret
end
end
+
+ # Options:
+ #
+ # * :interior_rings - whether to include any interior rings in the output.
+ # The default is true.
+ def to_geojsonable(options = {})
+ options = {
+ :interior_rings => true
+ }.merge(options)
+
+ ret = {
+ :type => 'Polygon',
+ :coordinates => [ self.exterior_ring.coord_seq.to_a ]
+ }
+
+ if options[:interior_rings] && self.num_interior_rings > 0
+ ret[:coordinates].concat self.interior_rings.collect { |r|
+ r.coord_seq.to_a
+ }
+ end
+
+ ret
+ end
end
@@ -695,5 +744,57 @@ def to_kml *args
def to_georss *args
self.exterior_ring.to_georss(*args)
end
+
+ def to_geojsonable(options = {})
+ {
+ :type => 'GeometryCollection',
+ :geometries => self.to_a.collect { |g| g.to_geojsonable(options) }
+ }
+ end
+ end
+
+ class MultiPolygon < GeometryCollection
+ def to_geojsonable(options = {})
+ options = {
+ :interior_rings => true
+ }.merge(options)
+
+ {
+ :type => 'MultiPolygon',
+ :coordinates => self.to_a.collect { |polygon|
+ coords = [ polygon.exterior_ring.coord_seq.to_a ]
+
+ if options[:interior_rings] && polygon.num_interior_rings > 0
+ coords.concat polygon.interior_rings.collect { |r|
+ r.coord_seq.to_a
+ }
+ end
+
+ coords
+ }
+ }
+ end
+ end
+
+ class MultiLineString < GeometryCollection
+ def to_geojsonable(options = {})
+ {
+ :type => 'MultiLineString',
+ :coordinates => self.to_a.collect { |linestring|
+ linestring.coord_seq.to_a
+ }
+ }
+ end
+ end
+
+ class MultiPoint < GeometryCollection
+ def to_geojsonable(options = {})
+ {
+ :type => 'MultiPoint',
+ :coordinates => self.to_a.collect { |point|
+ point.to_a
+ }
+ }
+ end
end
end
17 test/database.yml
View
@@ -0,0 +1,17 @@
+---
+# To modify the test database parameters, create a new file called
+# local_database.yml and go nuts with settings. The "jdbc" settings
+# are merged into the "arunit" settings as JDBC works over a TCP
+# socket and those sorts of connections generally require some user
+# credentials.
+
+arunit:
+ adapter: "postgresql"
+ database: "geos_extensions_unit_tests"
+ min_messages: "warning"
+ schema_search_path: "public"
+
+jdbc:
+ host: "localhost"
+ adapter: "jdbcpostgresql"
+
172 test/google_maps_api_3_tests.rb
View
@@ -56,22 +56,41 @@ def test_to_jsonable
end
if defined?(JSON)
- def test_to_g_polygon
- assert_equal(
- %{new google.maps.Polygon({"paths": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})},
- @polygon.to_g_polygon
- )
+ def poly_tester(type, expected, poly)
+ json = poly.gsub(/new google.maps.LatLng\(([^)]+)\)/, '[ \1 ]').gsub(/"map":\s*map/, %{"map": "map"})
- assert_equal(
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
- @polygon.to_g_polygon(
- :stroke_color => '#b00b1e',
- :stroke_weight => 5,
- :stroke_opacity => 0.5,
- :fill_color => '#b00b1e',
- :map => 'map'
- )
- )
+ assert(json =~ /^new google.maps.#{type}\((.+)\)$/)
+
+ assert_equal(expected, JSON.load($~[1]))
+ end
+
+ def test_to_g_polygon
+ poly_tester('Polygon', {
+ "paths" => [
+ [0.0, 0.0], [1.0, 1.0], [2.5, 2.5], [5.0, 5.0], [0.0, 0.0]
+ ]
+ }, @polygon.to_g_polygon)
+
+ poly_tester('Polygon', {
+ 'strokeColor' => '#b00b1e',
+ 'strokeWeight' => 5,
+ 'strokeOpacity' => 0.5,
+ 'fillColor' => '#b00b1e',
+ 'map' => 'map',
+ 'paths' => [
+ [ 0.0, 0.0 ],
+ [ 1.0, 1.0 ],
+ [ 2.5, 2.5 ],
+ [ 5.0, 5.0 ],
+ [ 0.0, 0.0 ]
+ ]
+ }, @polygon.to_g_polygon(
+ :stroke_color => '#b00b1e',
+ :stroke_weight => 5,
+ :stroke_opacity => 0.5,
+ :fill_color => '#b00b1e',
+ :map => 'map'
+ ))
end
def test_to_g_polygon_with_multi_polygon
@@ -82,6 +101,7 @@ def test_to_g_polygon_with_multi_polygon
((20 20, 20 25, 25 25, 25 20, 20 20))
)'
)
+
options = {
:stroke_color => '#b00b1e',
:stroke_weight => 5,
@@ -90,59 +110,83 @@ def test_to_g_polygon_with_multi_polygon
:map => 'map'
}
- assert_equal(
- ["new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(5.0, 0.0), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [new google.maps.LatLng(10.0, 10.0), new google.maps.LatLng(15.0, 10.0), new google.maps.LatLng(15.0, 15.0), new google.maps.LatLng(10.0, 15.0), new google.maps.LatLng(10.0, 10.0)]})",
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [new google.maps.LatLng(20.0, 20.0), new google.maps.LatLng(25.0, 20.0), new google.maps.LatLng(25.0, 25.0), new google.maps.LatLng(20.0, 25.0), new google.maps.LatLng(20.0, 20.0)]})"],
- multi_polygon.to_g_polygon(
- :stroke_color => '#b00b1e',
- :stroke_weight => 5,
- :stroke_opacity => 0.5,
- :fill_color => '#b00b1e',
- :map => 'map'
- )
- )
-
- assert_equal(
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [[new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(5.0, 0.0), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 5.0), new google.maps.LatLng(0.0, 0.0)], [new google.maps.LatLng(10.0, 10.0), new google.maps.LatLng(15.0, 10.0), new google.maps.LatLng(15.0, 15.0), new google.maps.LatLng(10.0, 15.0), new google.maps.LatLng(10.0, 10.0)], [new google.maps.LatLng(20.0, 20.0), new google.maps.LatLng(25.0, 20.0), new google.maps.LatLng(25.0, 25.0), new google.maps.LatLng(20.0, 25.0), new google.maps.LatLng(20.0, 20.0)]]})",
- multi_polygon.to_g_polygon({
- :stroke_color => '#b00b1e',
- :stroke_weight => 5,
- :stroke_opacity => 0.5,
- :fill_color => '#b00b1e',
- :map => 'map'
- }, {
- :single => true
- })
- )
+ expected = [ {
+ "paths" => [[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]],
+ "strokeColor" => "#b00b1e",
+ "strokeOpacity" => 0.5,
+ "fillColor" => "#b00b1e",
+ "strokeWeight" => 5,
+ "map" => "map"
+ }, {
+ "paths" => [[10.0, 10.0], [15.0, 10.0], [15.0, 15.0], [10.0, 15.0], [10.0, 10.0]],
+ "strokeColor" => "#b00b1e",
+ "strokeOpacity" => 0.5,
+ "fillColor" => "#b00b1e",
+ "strokeWeight" => 5,
+ "map" => "map"
+ }, {
+ "paths" => [[20.0, 20.0], [25.0, 20.0], [25.0, 25.0], [20.0, 25.0], [20.0, 20.0]],
+ "strokeColor" => "#b00b1e",
+ "strokeOpacity" => 0.5,
+ "fillColor" => "#b00b1e",
+ "strokeWeight" => 5,
+ "map" => "map"
+ } ]
+
+ multi_polygon.to_g_polygon(options).each_with_index do |polygon, i|
+ poly_tester('Polygon', expected[i], polygon)
+ end
- assert_equal(
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [[new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(5.0, 0.0), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 5.0), new google.maps.LatLng(0.0, 0.0)], [new google.maps.LatLng(10.0, 10.0), new google.maps.LatLng(15.0, 10.0), new google.maps.LatLng(15.0, 15.0), new google.maps.LatLng(10.0, 15.0), new google.maps.LatLng(10.0, 10.0)], [new google.maps.LatLng(20.0, 20.0), new google.maps.LatLng(25.0, 20.0), new google.maps.LatLng(25.0, 25.0), new google.maps.LatLng(20.0, 25.0), new google.maps.LatLng(20.0, 20.0)]]})",
- multi_polygon.to_g_polygon_single(
- :stroke_color => '#b00b1e',
- :stroke_weight => 5,
- :stroke_opacity => 0.5,
- :fill_color => '#b00b1e',
- :map => 'map'
- )
- )
+ poly_tester("Polygon", {
+ "paths" => [
+ [[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]],
+ [[10.0, 10.0], [15.0, 10.0], [15.0, 15.0], [10.0, 15.0], [10.0, 10.0]],
+ [[20.0, 20.0], [25.0, 20.0], [25.0, 25.0], [20.0, 25.0], [20.0, 20.0]]
+ ],
+ "strokeColor" => "#b00b1e",
+ "strokeOpacity" => 0.5,
+ "fillColor" => "#b00b1e",
+ "strokeWeight" => 5,
+ "map" => "map"
+ }, multi_polygon.to_g_polygon(options, {
+ :single => true
+ }))
+
+ poly_tester("Polygon", {
+ "paths" => [
+ [[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]],
+ [[10.0, 10.0], [15.0, 10.0], [15.0, 15.0], [10.0, 15.0], [10.0, 10.0]],
+ [[20.0, 20.0], [25.0, 20.0], [25.0, 25.0], [20.0, 25.0], [20.0, 20.0]]
+ ],
+ "strokeColor" => "#b00b1e",
+ "strokeOpacity" => 0.5,
+ "fillColor" => "#b00b1e",
+ "strokeWeight" => 5,
+ "map" => "map"
+ }, multi_polygon.to_g_polygon_single(options))
end
def test_to_g_polyline
- assert_equal(
- "new google.maps.Polyline({\"path\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
- @polygon.to_g_polyline
- )
-
- assert_equal(
- "new google.maps.Polyline({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"map\": map, \"path\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
- @polygon.to_g_polyline(
- :stroke_color => '#b00b1e',
- :stroke_weight => 5,
- :stroke_opacity => 0.5,
- :map => 'map'
- )
- )
+ poly_tester("Polyline", {
+ "path" => [
+ [0.0, 0.0], [1.0, 1.0], [2.5, 2.5], [5.0, 5.0], [0.0, 0.0]
+ ]
+ }, @polygon.to_g_polyline)
+
+ poly_tester("Polyline", {
+ "strokeColor" => "#b00b1e",
+ "strokeWeight" => 5,
+ "strokeOpacity" => 0.5,
+ "map" => "map",
+ "path" => [
+ [0.0, 0.0], [1.0, 1.0], [2.5, 2.5], [5.0, 5.0], [0.0, 0.0]
+ ]
+ }, @polygon.to_g_polyline(
+ :stroke_color => '#b00b1e',
+ :stroke_weight => 5,
+ :stroke_opacity => 0.5,
+ :map => 'map'
+ ))
end
def test_to_g_marker
9 test/reader_tests.rb
View
@@ -132,4 +132,13 @@ def test_read_wkt_with_newlines
assert_equal('POLYGON ((0 0, 10 10, 0 10, 0 0))', geom.to_wkt(:trim => true))
end
+
+ def test_read_e_notation
+ assert_equal('POINT (60081.5 -0.000858307)', Geos.read('-8.58307e-04, 6.00815E+4').to_wkt(:trim => true))
+ assert_equal('POINT (60081.5 -0.000858307)', Geos.read('-8.58307e-04, 6.00815e4').to_wkt(:trim => true))
+ end
+
+ def test_no_leading_digits
+ assert_equal('POINT (0.01 0.02)', Geos.read('.02, .01').to_wkt(:trim => true))
+ end
end
58 test/test_helper.rb
View
@@ -3,7 +3,7 @@
require 'test/unit'
if ENV['TEST_ACTIVERECORD']
- ACTIVERECORD_GEM_VERSION = ENV['ACTIVERECORD_GEM_VERSION'] || '~> 3.0.3'
+ ACTIVERECORD_GEM_VERSION = ENV['ACTIVERECORD_GEM_VERSION'] || '~> 3.2.0'
gem 'activerecord', ACTIVERECORD_GEM_VERSION
POSTGIS_PATHS = [
@@ -19,6 +19,10 @@
require 'active_record'
require 'active_record/fixtures'
require 'logger'
+
+ if ActiveRecord::VERSION::STRING < '3.0'
+ require 'fake_arel'
+ end
end
require File.join(File.dirname(__FILE__), %w{ .. lib geos_extensions })
@@ -35,33 +39,47 @@
if ENV['TEST_ACTIVERECORD']
ActiveRecord::Base.logger = Logger.new("debug.log")
ActiveRecord::Base.configurations = {
- 'arunit' => {
- :adapter => 'postgresql',
- :database => 'geos_extensions_unit_tests',
- :min_messages => 'warning',
- :schema_search_path => 'public'
- }
+ 'arunit' => {}
}
+ %w{
+ database.yml
+ local_database.yml
+ }.each do |file|
+ file = File.join('test', file)
+
+ next unless File.exists?(file)
+
+ configuration = YAML.load(File.read(file))
+
+ if configuration['arunit']
+ ActiveRecord::Base.configurations['arunit'].merge!(configuration['arunit'])
+ end
+
+ if defined?(JRUBY_VERSION) && configuration['jdbc']
+ ActiveRecord::Base.configurations['arunit'].merge!(configuration['jdbc'])
+ end
+ end
+
ActiveRecord::Base.establish_connection 'arunit'
ARBC = ActiveRecord::Base.connection
- if postgresql_version = ARBC.query('SELECT version()').flatten.to_s
+ if postgresql_version = ARBC.select_rows('SELECT version()').first.first
puts "PostgreSQL info from version(): #{postgresql_version}"
end
puts "Checking for PostGIS install"
2.times do
begin
- if postgis_version = ARBC.query('SELECT postgis_full_version()').flatten.to_s
+ if postgis_version = ARBC.select_rows('SELECT postgis_full_version()').first.first
puts "PostGIS info from postgis_full_version(): #{postgis_version}"
break
end
rescue ActiveRecord::StatementInvalid
puts "Trying to install PostGIS. If this doesn't work, you'll have to do this manually!"
- plpgsql = ARBC.query(%{SELECT count(*) FROM pg_language WHERE lanname = 'plpgsql'}).to_s
- if plpgsql == '0'
+ plpgsql = ARBC.select_rows(%{SELECT count(*) FROM pg_language WHERE lanname = 'plpgsql'}).first.first.to_i
+ if plpgsql == 0
ARBC.execute(%{CREATE LANGUAGE plpgsql})
end
@@ -138,6 +156,24 @@ module TestHelper
POLYGON_WITH_INTERIOR_RING = "POLYGON((0 0, 5 0, 5 5, 0 5, 0 0),(4 4, 4 1, 1 1, 1 4, 4 4))"
+ LINESTRING_WKT = "LINESTRING (0 0, 5 5, 5 10, 10 10)"
+
+ GEOMETRYCOLLECTION_WKT = 'GEOMETRYCOLLECTION (
+ MULTIPOLYGON (
+ ((0 0, 1 0, 1 1, 0 1, 0 0)),
+ (
+ (10 10, 10 14, 14 14, 14 10, 10 10),
+ (11 11, 11 12, 12 12, 12 11, 11 11)
+ )
+ ),
+ POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)),
+ POLYGON ((0 0, 5 0, 5 5, 0 5, 0 0), (4 4, 4 1, 1 1, 1 4, 4 4)),
+ MULTILINESTRING ((0 0, 2 3), (10 10, 3 4)),
+ LINESTRING (0 0, 2 3),
+ MULTIPOINT ((0 0), (2 3)),
+ POINT (9 0)
+ )'
+
BOUNDS_G_LAT_LNG = "((0.1, 0.1), (5.2, 5.2))"
BOUNDS_G_LAT_LNG_URL_VALUE = '0.1,0.1,5.2,5.2'
211 test/writer_tests.rb
View
@@ -127,4 +127,215 @@ def test_to_wkt_handles_binary_geos_arity
end
end
end
+
+ if defined?(JSON)
+ def test_to_geojson_coord_seq
+ coord_seq = Geos.read(POLYGON_WKT).exterior_ring.coord_seq
+ json = coord_seq.to_geojson
+
+ assert_equal({
+ "type" => "LineString",
+ "coordinates" => [
+ [0.0, 0.0],
+ [1.0, 1.0],
+ [2.5, 2.5],
+ [5.0, 5.0],
+ [0.0, 0.0]
+ ]
+ }, JSON.load(json))
+ end
+
+ def test_to_geojson_polygon
+ polygon = Geos.read(POLYGON_WKT)
+ json = polygon.to_geojson
+
+ assert_equal({
+ "type" => "Polygon",
+ "coordinates" => [
+ [
+ [0.0, 0.0],
+ [1.0, 1.0],
+ [2.5, 2.5],
+ [5.0, 5.0],
+ [0.0, 0.0]
+ ]
+ ]
+ }, JSON.load(json))
+ end
+
+ def test_to_geojson_polygon_with_interior_ring
+ polygon = Geos.read(POLYGON_WITH_INTERIOR_RING)
+
+ assert_equal({
+ "type" => "Polygon",
+ "coordinates" => [
+ [
+ [0.0, 0.0],
+ [5.0, 0.0],
+ [5.0, 5.0],
+ [0.0, 5.0],
+ [0.0, 0.0]
+ ], [
+ [4.0, 4.0],
+ [4.0, 1.0],
+ [1.0, 1.0],
+ [1.0, 4.0],
+ [4.0, 4.0]
+ ]
+ ]
+ }, JSON.load(polygon.to_geojson))
+
+ assert_equal({
+ "type" => "Polygon",
+ "coordinates" => [
+ [
+ [0.0, 0.0],
+ [5.0, 0.0],
+ [5.0, 5.0],
+ [0.0, 5.0],
+ [0.0, 0.0]
+ ]
+ ]
+ }, JSON.load(polygon.to_geojson(:interior_rings => false)))
+ end
+
+ def test_to_geojson_point
+ point = Geos.read(POINT_WKT)
+
+ assert_equal({
+ "type" => "Point",
+ "coordinates" => [10.0, 10.01]
+ }, JSON.load(point.to_geojson))
+ end
+
+ def test_to_geojson_line_string
+ linestring = Geos.read(LINESTRING_WKT)
+
+ assert_equal({
+ "type" => "LineString",
+ "coordinates" => [
+ [0.0, 0.0],
+ [5.0, 5.0],
+ [5.0, 10.0],
+ [10.0, 10.0]
+ ]}, JSON.load(linestring.to_geojson))
+ end
+
+ def test_to_geojson_geometry_collection
+ collection = Geos.read(GEOMETRYCOLLECTION_WKT)
+
+ assert_equal({
+ "type" => "GeometryCollection",
+ "geometries" => [ {
+ "type" => "MultiPolygon",
+ "coordinates" => [
+ [
+ [
+ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]
+ ]
+ ], [
+ [
+ [10.0, 10.0], [10.0, 14.0], [14.0, 14.0], [14.0, 10.0], [10.0, 10.0]
+ ], [
+ [11.0, 11.0], [11.0, 12.0], [12.0, 12.0], [12.0, 11.0], [11.0, 11.0]
+ ]
+ ]
+ ]
+ }, {
+ "type" => "Polygon",
+ "coordinates" => [
+ [
+ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]
+ ]
+ ]
+ }, {
+ "type" => "Polygon",
+ "coordinates" => [
+ [
+ [0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]
+ ], [
+ [4.0, 4.0], [4.0, 1.0], [1.0, 1.0], [1.0, 4.0], [4.0, 4.0]
+ ]
+ ]
+ }, {
+ "type" => "MultiLineString",
+ "coordinates" => [
+ [
+ [0.0, 0.0], [2.0, 3.0]
+ ], [
+ [10.0, 10.0], [3.0, 4.0]
+ ]
+ ]
+ }, {
+ "type" => "LineString",
+ "coordinates" => [
+ [0.0, 0.0], [2.0, 3.0]
+ ]
+ }, {
+ "type" => "MultiPoint",
+ "coordinates" => [
+ [0.0, 0.0], [2.0, 3.0]
+ ]
+ }, {
+ "type" => "Point",
+ "coordinates" => [9.0, 0.0]
+ }
+ ] }, JSON.load(collection.to_geojson))
+
+ assert_equal({
+ "type" => "GeometryCollection",
+ "geometries" => [ {
+ "type" => "MultiPolygon",
+ "coordinates" => [
+ [
+ [
+ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]
+ ]
+ ], [
+ [
+ [10.0, 10.0], [10.0, 14.0], [14.0, 14.0], [14.0, 10.0], [10.0, 10.0]
+ ]
+ ]
+ ]
+ }, {
+ "type" => "Polygon",
+ "coordinates" => [
+ [
+ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]
+ ]
+ ]
+ }, {
+ "type" => "Polygon",
+ "coordinates" => [
+ [
+ [0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]
+ ]
+ ]
+ }, {
+ "type" => "MultiLineString",
+ "coordinates" => [
+ [
+ [0.0, 0.0], [2.0, 3.0]
+ ],
+ [
+ [10.0, 10.0], [3.0, 4.0]
+ ]
+ ]
+ }, {
+ "type" => "LineString",
+ "coordinates" => [
+ [0.0, 0.0], [2.0, 3.0]
+ ]
+ }, {
+ "type" => "MultiPoint",
+ "coordinates" => [
+ [0.0, 0.0], [2.0, 3.0]
+ ]
+ }, {
+ "type" => "Point",
+ "coordinates" => [9.0, 0.0]
+ } ]
+ }, JSON.load(collection.to_geojson(:interior_rings => false)))
+ end
+ end
end

No commit comments for this range

Something went wrong with that request. Please try again.