jsl-GeoRuby 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. data/MIT-LICENSE +7 -0
  2. data/README +83 -0
  3. data/georuby.gemspec +37 -0
  4. data/lib/geo_ruby.rb +16 -0
  5. data/lib/geo_ruby/shp4r/dbf.rb +180 -0
  6. data/lib/geo_ruby/shp4r/shp.rb +701 -0
  7. data/lib/geo_ruby/simple_features/envelope.rb +134 -0
  8. data/lib/geo_ruby/simple_features/ewkb_parser.rb +216 -0
  9. data/lib/geo_ruby/simple_features/ewkt_parser.rb +336 -0
  10. data/lib/geo_ruby/simple_features/geometry.rb +225 -0
  11. data/lib/geo_ruby/simple_features/geometry_collection.rb +136 -0
  12. data/lib/geo_ruby/simple_features/geometry_factory.rb +81 -0
  13. data/lib/geo_ruby/simple_features/georss_parser.rb +135 -0
  14. data/lib/geo_ruby/simple_features/helper.rb +18 -0
  15. data/lib/geo_ruby/simple_features/line_string.rb +166 -0
  16. data/lib/geo_ruby/simple_features/linear_ring.rb +12 -0
  17. data/lib/geo_ruby/simple_features/multi_line_string.rb +39 -0
  18. data/lib/geo_ruby/simple_features/multi_point.rb +41 -0
  19. data/lib/geo_ruby/simple_features/multi_polygon.rb +38 -0
  20. data/lib/geo_ruby/simple_features/point.rb +236 -0
  21. data/lib/geo_ruby/simple_features/polygon.rb +150 -0
  22. data/rakefile.rb +44 -0
  23. data/test/data/multipoint.dbf +0 -0
  24. data/test/data/multipoint.shp +0 -0
  25. data/test/data/multipoint.shx +0 -0
  26. data/test/data/point.dbf +0 -0
  27. data/test/data/point.shp +0 -0
  28. data/test/data/point.shx +0 -0
  29. data/test/data/polygon.dbf +0 -0
  30. data/test/data/polygon.shp +0 -0
  31. data/test/data/polygon.shx +0 -0
  32. data/test/data/polyline.dbf +0 -0
  33. data/test/data/polyline.shp +0 -0
  34. data/test/data/polyline.shx +0 -0
  35. data/test/test_ewkb_parser.rb +171 -0
  36. data/test/test_ewkt_parser.rb +193 -0
  37. data/test/test_georss_kml.rb +231 -0
  38. data/test/test_shp.rb +76 -0
  39. data/test/test_shp_write.rb +150 -0
  40. data/test/test_simple_features.rb +506 -0
  41. data/tools/db.yml +6 -0
  42. data/tools/shp2sql.rb +92 -0
  43. metadata +95 -0
@@ -0,0 +1,150 @@
1
+ require 'geo_ruby/simple_features/geometry'
2
+
3
+ module GeoRuby
4
+ module SimpleFeatures
5
+ #Represents a polygon as an array of linear rings (see LinearRing). No check is performed regarding the validity of the geometries forming the polygon.
6
+ class Polygon < Geometry
7
+ #the list of rings forming the polygon
8
+ attr_reader :rings
9
+
10
+ def initialize(srid = DEFAULT_SRID,with_z=false,with_m=false)
11
+ super(srid,with_z,with_m)
12
+ @rings = []
13
+ end
14
+
15
+ #Delegate the unknown methods to the rings array
16
+ def method_missing(method_name,*args,&b)
17
+ @rings.send(method_name,*args,&b)
18
+ end
19
+
20
+ #Bounding box in 2D/3D. Returns an array of 2 points
21
+ def bounding_box
22
+ unless with_z
23
+ @rings[0].bounding_box
24
+ else
25
+ result = @rings[0].bounding_box #valid for x and y
26
+ max_z, min_z = result[1].z, result[0].z
27
+ 1.upto(size - 1) do |index|
28
+ bbox = @rings[index].bounding_box
29
+ sw = bbox[0]
30
+ ne = bbox[1]
31
+ max_z = ne.z if ne.z > max_z
32
+ min_z = sw.z if sw.z < min_z
33
+ end
34
+ result[1].z, result[0].z = max_z, min_z
35
+ result
36
+ end
37
+ end
38
+
39
+ def m_range
40
+ if with_m
41
+ max_m, min_m = -Float::MAX, Float::MAX
42
+ each do |lr|
43
+ lrmr = lr.m_range
44
+ max_m = lrmr[1] if lrmr[1] > max_m
45
+ min_m = lrmr[0] if lrmr[0] < min_m
46
+ end
47
+ [min_m,max_m]
48
+ else
49
+ [0,0]
50
+ end
51
+ end
52
+
53
+ #tests for other equality. The SRID is not taken into account.
54
+ def ==(other_polygon)
55
+ if other_polygon.class != self.class or
56
+ length != other_polygon.length
57
+ false
58
+ else
59
+ index=0
60
+ while index<length
61
+ return false if self[index] != other_polygon[index]
62
+ index+=1
63
+ end
64
+ true
65
+ end
66
+ end
67
+ #binary representation of a polygon, without the headers neccessary for a valid WKB string
68
+ def binary_representation(allow_z=true,allow_m=true)
69
+ rep = [length].pack("V")
70
+ each {|linear_ring| rep << linear_ring.binary_representation(allow_z,allow_m)}
71
+ rep
72
+ end
73
+ #WKB geometry type
74
+ def binary_geometry_type
75
+ 3
76
+ end
77
+
78
+ #Text representation of a polygon
79
+ def text_representation(allow_z=true,allow_m=true)
80
+ @rings.collect{|line_string| "(" + line_string.text_representation(allow_z,allow_m) + ")" }.join(",")
81
+ end
82
+ #WKT geometry type
83
+ def text_geometry_type
84
+ "POLYGON"
85
+ end
86
+
87
+ #georss simple representation : outputs only the outer ring
88
+ def georss_simple_representation(options)
89
+ georss_ns = options[:georss_ns] || "georss"
90
+ geom_attr = options[:geom_attr]
91
+ "<#{georss_ns}:polygon#{geom_attr}>" + self[0].georss_poslist + "</#{georss_ns}:polygon>\n"
92
+ end
93
+ #georss w3c representation : outputs the first point of the outer ring
94
+ def georss_w3cgeo_representation(options)
95
+ w3cgeo_ns = options[:w3cgeo_ns] || "geo"
96
+
97
+ "<#{w3cgeo_ns}:lat>#{self[0][0].y}</#{w3cgeo_ns}:lat>\n<#{w3cgeo_ns}:long>#{self[0][0].x}</#{w3cgeo_ns}:long>\n"
98
+ end
99
+ #georss gml representation
100
+ def georss_gml_representation(options)
101
+ georss_ns = options[:georss_ns] || "georss"
102
+ gml_ns = options[:gml_ns] || "gml"
103
+
104
+ result = "<#{georss_ns}:where>\n<#{gml_ns}:Polygon>\n<#{gml_ns}:exterior>\n<#{gml_ns}:LinearRing>\n<#{gml_ns}:posList>\n" + self[0].georss_poslist + "\n</#{gml_ns}:posList>\n</#{gml_ns}:LinearRing>\n</#{gml_ns}:exterior>\n</#{gml_ns}:Polygon>\n</#{georss_ns}:where>\n"
105
+ end
106
+
107
+ #outputs the geometry in kml format : options are <tt>:id</tt>, <tt>:tesselate</tt>, <tt>:extrude</tt>,
108
+ #<tt>:altitude_mode</tt>. If the altitude_mode option is not present, the Z (if present) will not be output (since
109
+ #it won't be used by GE anyway: clampToGround is the default)
110
+ def kml_representation(options = {})
111
+ result = "<Polygon#{options[:id_attr]}>\n"
112
+ result += options[:geom_data] if options[:geom_data]
113
+ rings.each_with_index do |ring, i|
114
+ if i == 0
115
+ boundary = "outerBoundaryIs"
116
+ else
117
+ boundary = "innerBoundaryIs"
118
+ end
119
+ result += "<#{boundary}><LinearRing><coordinates>\n"
120
+ result += ring.kml_poslist(options)
121
+ result += "\n</coordinates></LinearRing></#{boundary}>\n"
122
+ end
123
+ result += "</Polygon>\n"
124
+ end
125
+
126
+ #creates a new polygon. Accepts an array of linear strings as argument
127
+ def self.from_linear_rings(linear_rings,srid = DEFAULT_SRID,with_z=false,with_m=false)
128
+ polygon = new(srid,with_z,with_m)
129
+ polygon.concat(linear_rings)
130
+ polygon
131
+ end
132
+
133
+ #creates a new polygon. Accepts a sequence of points as argument : ((x,y)....(x,y)),((x,y).....(x,y))
134
+ def self.from_coordinates(point_sequences,srid=DEFAULT_SRID,with_z=false,with_m=false)
135
+ polygon = new(srid,with_z,with_m)
136
+ polygon.concat( point_sequences.collect {|points| LinearRing.from_coordinates(points,srid,with_z,with_m) } )
137
+ polygon
138
+ end
139
+
140
+ #creates a new polygon from a list of Points (pt1....ptn),(pti....ptj)
141
+ def self.from_points(point_sequences, srid=DEFAULT_SRID,with_z=false,with_m=false)
142
+ polygon = new(srid,with_z,with_m)
143
+ polygon.concat( point_sequences.collect {|points| LinearRing.from_points(points,srid,with_z,with_m) } )
144
+ polygon
145
+
146
+ end
147
+
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,44 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ task :default => :test
6
+
7
+ desc "Run the tests"
8
+ Rake::TestTask::new do |t|
9
+ t.test_files = FileList['test/test*.rb']
10
+ t.verbose = true
11
+ end
12
+
13
+ desc "Generate the documentation"
14
+ Rake::RDocTask::new do |rdoc|
15
+ rdoc.rdoc_dir = 'georuby-doc/'
16
+ rdoc.title = "GeoRuby Documentation"
17
+ rdoc.options << '--line-numbers' << '--inline-source'
18
+ rdoc.rdoc_files.include('README')
19
+ rdoc.rdoc_files.include('lib/**/*.rb')
20
+ end
21
+
22
+ spec = Gem::Specification::new do |s|
23
+ s.platform = Gem::Platform::RUBY
24
+
25
+ s.name = 'GeoRuby'
26
+ s.version = "1.3.3"
27
+ s.summary = "Ruby data holder for OGC Simple Features"
28
+ s.description = <<EOF
29
+ GeoRuby is intended as a holder for data returned from PostGIS and MySQL Spatial queries. The data model roughly follows the OGC "Simple Features for SQL" specification (see www.opengis.org/docs/99-049.pdf), although without any kind of advanced functionalities (such as geometric operators or reprojections)
30
+ EOF
31
+ s.author = 'Guilhem Vellut'
32
+ s.email = 'guilhem.vellut@gmail.com'
33
+ s.homepage = "http://thepochisuperstarmegashow.com/projects/"
34
+
35
+ s.requirements << 'none'
36
+ s.require_path = 'lib'
37
+ s.files = FileList["lib/**/*.rb", "test/**/*.rb", "README","MIT-LICENSE","rakefile.rb","test/data/*.shp","test/data/*.dbf","test/data/*.shx","tools/**/*.yml","tools/**/*.rb","tools/lib/**/*"]
38
+ s.test_files = FileList['test/test*.rb']
39
+
40
+ s.has_rdoc = true
41
+ s.extra_rdoc_files = ["README"]
42
+ s.rdoc_options.concat ['--main', 'README']
43
+ end
44
+
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,171 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'geo_ruby'
4
+ require 'test/unit'
5
+
6
+ include GeoRuby::SimpleFeatures
7
+
8
+ class TestEWKBParser < Test::Unit::TestCase
9
+
10
+ def setup
11
+ @factory = GeometryFactory::new
12
+ @hex_ewkb_parser = HexEWKBParser::new(@factory)
13
+ end
14
+
15
+ def test_point2d
16
+ @hex_ewkb_parser.parse("01010000207B000000CDCCCCCCCCCC28406666666666A64640")
17
+ point = @factory.geometry
18
+ assert(point.instance_of?(Point))
19
+ assert_equal(Point.from_x_y(12.4,45.3,123),point)
20
+ end
21
+
22
+ def test_point2d_BigEndian
23
+ @hex_ewkb_parser.parse("00000000014013A035BD512EC7404A3060C38F3669")
24
+ point = @factory.geometry
25
+ assert(point.instance_of?(Point))
26
+ assert_equal(Point.from_x_y(4.906455,52.377953),point)
27
+ end
28
+
29
+
30
+
31
+ def test_point3dz
32
+ @hex_ewkb_parser.parse("01010000A07B000000CDCCCCCCCCCC28406666666666A646400000000000000CC0")
33
+ point = @factory.geometry
34
+ assert(point.instance_of?(Point))
35
+ assert_equal(Point.from_x_y_z(12.4,45.3,-3.5,123),point)
36
+ end
37
+
38
+ def test_point4d
39
+ @hex_ewkb_parser.parse("01010000E07B000000CDCCCCCCCCCC28406666666666A646400000000000000CC00000000000002E40")
40
+ point = @factory.geometry
41
+ assert(point.instance_of?(Point))
42
+ assert_equal(Point.from_x_y_z_m(12.4,45.3,-3.5,15,123),point)
43
+ end
44
+
45
+ def test_line_string
46
+ @hex_ewkb_parser.parse("01020000200001000002000000CDCCCCCCCCCC28406666666666A646C03333333333B34640CDCCCCCCCCCC4440")
47
+ line_string = @factory.geometry
48
+ assert(line_string.instance_of?(LineString))
49
+ assert_equal(LineString.from_coordinates([[12.4,-45.3],[45.4,41.6]],256),line_string)
50
+
51
+ @hex_ewkb_parser.parse("01020000A00001000002000000CDCCCCCCCCCC28406666666666A646C06666666666A641403333333333B34640CDCCCCCCCCCC44409A99999999992840")
52
+ line_string = @factory.geometry
53
+ assert(line_string.instance_of?(LineString))
54
+ assert_equal(LineString.from_coordinates([[12.4,-45.3,35.3],[45.4,41.6,12.3]],256,true),line_string)
55
+
56
+ @hex_ewkb_parser.parse("01020000E00001000002000000CDCCCCCCCCCC28406666666666A646C06666666666A64140CDCCCCCCCC8C46403333333333B34640CDCCCCCCCCCC44409A999999999928403D0AD7A3701D4440")
57
+ line_string = @factory.geometry
58
+ assert(line_string.instance_of?(LineString))
59
+ assert_equal(LineString.from_coordinates([[12.4,-45.3,35.3,45.1],[45.4,41.6,12.3,40.23]],256,true,true),line_string)
60
+
61
+ end
62
+
63
+ def test_polygon
64
+ @hex_ewkb_parser.parse("0103000020000100000200000005000000000000000000000000000000000000000000000000001040000000000000000000000000000010400000000000001040000000000000000000000000000010400000000000000000000000000000000005000000000000000000F03F000000000000F03F0000000000000840000000000000F03F00000000000008400000000000000840000000000000F03F0000000000000840000000000000F03F000000000000F03F")
65
+ polygon = @factory.geometry
66
+ assert(polygon.instance_of?(Polygon))
67
+ assert_equal(Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256),polygon)
68
+
69
+ @hex_ewkb_parser.parse("01030000A000010000020000000500000000000000000000000000000000000000000000000000004000000000000010400000000000000000000000000000004000000000000010400000000000001040000000000000004000000000000000000000000000001040000000000000004000000000000000000000000000000000000000000000004005000000000000000000F03F000000000000F03F00000000000000400000000000000840000000000000F03F0000000000000040000000000000084000000000000008400000000000000040000000000000F03F00000000000008400000000000000040000000000000F03F000000000000F03F0000000000000040")
70
+ polygon = @factory.geometry
71
+ assert(polygon.instance_of?(Polygon))
72
+ assert_equal(Polygon.from_coordinates([[[0,0,2],[4,0,2],[4,4,2],[0,4,2],[0,0,2]],[[1,1,2],[3,1,2],[3,3,2],[1,3,2],[1,1,2]]],256,true),polygon)
73
+
74
+ @hex_ewkb_parser.parse("010300006000010000020000000500000000000000000000000000000000000000000000000000004000000000000010400000000000000000000000000000004000000000000010400000000000001040000000000000004000000000000000000000000000001040000000000000004000000000000000000000000000000000000000000000004005000000000000000000F03F000000000000F03F00000000000000400000000000000840000000000000F03F0000000000000040000000000000084000000000000008400000000000000040000000000000F03F00000000000008400000000000000040000000000000F03F000000000000F03F0000000000000040")
75
+ polygon = @factory.geometry
76
+ assert(polygon.instance_of?(Polygon))
77
+ assert_equal(Polygon.from_coordinates([[[0,0,2],[4,0,2],[4,4,2],[0,4,2],[0,0,2]],[[1,1,2],[3,1,2],[3,3,2],[1,3,2],[1,1,2]]],256,false,true),polygon)
78
+
79
+
80
+ @hex_ewkb_parser.parse("01030000E0000100000200000005000000000000000000000000000000000000000000000000000040CDCCCCCCCC8C46C00000000000001040000000000000000000000000000000400000000000001440000000000000104000000000000010400000000000000040AE47E17A14AE1240000000000000000000000000000010400000000000000040713D0AD7A370F53F000000000000000000000000000000000000000000000040CDCCCCCCCC8C46C005000000000000000000F03F000000000000F03F00000000000000409A999999999928400000000000000840000000000000F03F00000000000000400000000000C05E400000000000000840000000000000084000000000000000406666666666662840000000000000F03F000000000000084000000000000000400000000000002840000000000000F03F000000000000F03F00000000000000409A99999999992840")
81
+ polygon = @factory.geometry
82
+ assert(polygon.instance_of?(Polygon))
83
+ assert_equal(Polygon.from_coordinates([[[0,0,2,-45.1],[4,0,2,5],[4,4,2,4.67],[0,4,2,1.34],[0,0,2,-45.1]],[[1,1,2,12.3],[3,1,2,123],[3,3,2,12.2],[1,3,2,12],[1,1,2,12.3]]],256,true,true),polygon)
84
+
85
+
86
+ end
87
+
88
+ def test_geometry_collection
89
+ @hex_ewkb_parser.parse("010700002000010000020000000101000000AE47E17A14AE12403333333333B34640010200000002000000CDCCCCCCCCCC16406666666666E628403333333333E350400000000000004B40")
90
+ geometry_collection = @factory.geometry
91
+ assert(geometry_collection.instance_of?(GeometryCollection))
92
+ assert_equal(GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4,256),LineString.from_coordinates([[5.7,12.45],[67.55,54]],256)],256),geometry_collection)
93
+ assert_equal(256,geometry_collection[0].srid)
94
+
95
+ @hex_ewkb_parser.parse("01070000E0000100000200000001010000C0AE47E17A14AE12403333333333B34640F6285C8FC2D54640666666666666024001020000C002000000CDCCCCCCCCCC16406666666666E628403D0AD7A3703D124033333333339358403333333333E350400000000000004B4066666666666628403333333333330B40")
96
+ geometry_collection = @factory.geometry
97
+ assert(geometry_collection.instance_of?(GeometryCollection))
98
+ assert_equal(GeometryCollection.from_geometries([Point.from_x_y_z_m(4.67,45.4,45.67,2.3,256),LineString.from_coordinates([[5.7,12.45,4.56,98.3],[67.55,54,12.2,3.4]],256,true, true)],256,true, true),geometry_collection)
99
+ assert_equal(256,geometry_collection[0].srid)
100
+
101
+
102
+ end
103
+
104
+ def test_multi_point
105
+ @hex_ewkb_parser.parse("0104000020BC010000030000000101000000CDCCCCCCCCCC28403333333333D35EC0010100000066666666664650C09A99999999D95E4001010000001F97DD388EE35E400000000000C05E40")
106
+ multi_point = @factory.geometry
107
+ assert(multi_point.instance_of?(MultiPoint))
108
+ assert_equal(MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]],444),multi_point)
109
+ assert_equal(444,multi_point.srid)
110
+ assert_equal(444,multi_point[0].srid)
111
+
112
+ @hex_ewkb_parser.parse("01040000A0BC010000030000000101000080CDCCCCCCCCCC28403333333333D35EC00000000000001240010100008066666666664650C09A99999999D95E40333333333333F33F01010000801F97DD388EE35E400000000000C05E406666666666660240")
113
+ multi_point = @factory.geometry
114
+ assert(multi_point.instance_of?(MultiPoint))
115
+ assert_equal(MultiPoint.from_coordinates([[12.4,-123.3,4.5],[-65.1,123.4,1.2],[123.55555555,123,2.3]],444,true),multi_point)
116
+ assert_equal(444,multi_point.srid)
117
+ assert_equal(444,multi_point[0].srid)
118
+
119
+
120
+ end
121
+
122
+ def test_multi_line_string
123
+ @hex_ewkb_parser.parse("01050000200001000002000000010200000002000000000000000000F83F9A99999999994640E4BD6A65C20F4BC0FA7E6ABC749388BF010200000003000000000000000000F83F9A99999999994640E4BD6A65C20F4BC0FA7E6ABC749388BF39B4C876BE8F46403333333333D35E40")
124
+ multi_line_string = @factory.geometry
125
+ assert(multi_line_string.instance_of?(MultiLineString))
126
+ assert_equal(MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012]],256),LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]],256)],256),multi_line_string)
127
+ assert_equal(256,multi_line_string.srid)
128
+ assert_equal(256,multi_line_string[0].srid)
129
+
130
+ @hex_ewkb_parser.parse("0105000020000100000200000001020000C002000000000000000000F83F9A99999999994640CDCCCCCCCCCCF43F333333333333F33FE4BD6A65C20F4BC0FA7E6ABC749388BF333333333333F33F000000000000124001020000C003000000000000000000F83F9A99999999994640666666666666144000000000000012C0E4BD6A65C20F4BC0FA7E6ABC749388BF3333333333331BC03333333333330B4039B4C876BE8F46403333333333D35E40000000000000124033333333333315C0")
131
+ multi_line_string = @factory.geometry
132
+ assert(multi_line_string.instance_of?(MultiLineString))
133
+ assert_equal(MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2,1.3,1.2],[-54.12312,-0.012,1.2,4.5]],256,true,true),LineString.from_coordinates([[1.5,45.2,5.1,-4.5],[-54.12312,-0.012,-6.8,3.4],[45.123,123.3,4.5,-5.3]],256,true,true)],256,true,true),multi_line_string)
134
+ assert_equal(256,multi_line_string.srid)
135
+ assert_equal(256,multi_line_string[0].srid)
136
+
137
+
138
+ end
139
+
140
+ def test_multi_polygon
141
+ @hex_ewkb_parser.parse("0106000020000100000200000001030000000200000004000000CDCCCCCCCCCC28406666666666A646C03333333333B34640CDCCCCCCCCCC44406DE7FBA9F1D211403D2CD49AE61DF13FCDCCCCCCCCCC28406666666666A646C004000000333333333333034033333333333315409A999999999915408A8EE4F21FD2F63FEC51B81E85EB2C40F6285C8FC2F5F03F3333333333330340333333333333154001030000000200000005000000000000000000000000000000000000000000000000001040000000000000000000000000000010400000000000001040000000000000000000000000000010400000000000000000000000000000000005000000000000000000F03F000000000000F03F0000000000000840000000000000F03F00000000000008400000000000000840000000000000F03F0000000000000840000000000000F03F000000000000F03F")
142
+ multi_polygon = @factory.geometry
143
+ assert(multi_polygon.instance_of?(MultiPolygon))
144
+ assert_equal(MultiPolygon.from_polygons([Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]],256),Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)],256),multi_polygon)
145
+ assert_equal(256,multi_polygon.srid)
146
+ assert_equal(256,multi_polygon[0].srid)
147
+
148
+ @hex_ewkb_parser.parse("0106000020000100000200000001030000400200000004000000CDCCCCCCCCCC28406666666666A646C0333333333333F33F3333333333B34640CDCCCCCCCCCC4440333333333333F33F6DE7FBA9F1D211403D2CD49AE61DF13F333333333333F33FCDCCCCCCCCCC28406666666666A646C0333333333333F33F0400000033333333333303403333333333331540333333333333F33F9A999999999915408A8EE4F21FD2F63F333333333333F33FEC51B81E85EB2C40F6285C8FC2F5F03F333333333333F33F33333333333303403333333333331540333333333333F33F0103000040020000000500000000000000000000000000000000000000333333333333F33F00000000000010400000000000000000333333333333F33F00000000000010400000000000001040666666666666024000000000000000000000000000001040333333333333F33F00000000000000000000000000000000333333333333F33F05000000000000000000F03F000000000000F03F9A999999999901400000000000000840000000000000F03F6666666666660A40000000000000084000000000000008409A9999999999F13F000000000000F03F00000000000008403333333333330340000000000000F03F000000000000F03F9A99999999990140")
149
+ multi_polygon = @factory.geometry
150
+ assert(multi_polygon.instance_of?(MultiPolygon))
151
+ assert_equal(MultiPolygon.from_polygons([Polygon.from_coordinates([[[12.4,-45.3,1.2],[45.4,41.6,1.2],[4.456,1.0698,1.2],[12.4,-45.3,1.2]],[[2.4,5.3,1.2],[5.4,1.4263,1.2],[14.46,1.06,1.2],[2.4,5.3,1.2]]],256,false,true),Polygon.from_coordinates([[[0,0,1.2],[4,0,1.2],[4,4,2.3],[0,4,1.2],[0,0,1.2]],[[1,1,2.2],[3,1,3.3],[3,3,1.1],[1,3,2.4],[1,1,2.2]]],256,false,true)],256,false,true),multi_polygon)
152
+ assert_equal(256,multi_polygon.srid)
153
+ assert_equal(256,multi_polygon[0].srid)
154
+ end
155
+
156
+
157
+ def test_failure_trailing_data
158
+ #added A345 at the end
159
+ assert_raise(EWKBFormatError){@hex_ewkb_parser.parse("01010000207B000000CDCCCCCCCCCC28406666666666A64640A345")}
160
+ end
161
+ def test_failure_unknown_geometry_type
162
+ assert_raise(EWKBFormatError){@hex_ewkb_parser.parse("01090000207B000000CDCCCCCCCCCC28406666666666A64640")}
163
+ end
164
+ def test_failure_m
165
+ assert_raise(EWKBFormatError){@hex_ewkb_parser.parse("01010000607B000000CDCCCCCCCCCC28406666666666A64640")}
166
+ end
167
+ def test_failure_truncated_data
168
+ assert_raise(EWKBFormatError){@hex_ewkb_parser.parse("01010000207B000000CDCCCCCCCCCC2840666666")}
169
+ end
170
+
171
+ end
@@ -0,0 +1,193 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'geo_ruby'
4
+ require 'test/unit'
5
+
6
+
7
+ include GeoRuby::SimpleFeatures
8
+
9
+ class TestEWKTParser < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @factory = GeometryFactory::new
13
+ @ewkt_parser = EWKTParser::new(@factory)
14
+ end
15
+
16
+ def test_point
17
+ ewkt="POINT( 3.456 0.123)"
18
+ @ewkt_parser.parse(ewkt)
19
+ point = @factory.geometry
20
+ assert(point.instance_of?(Point))
21
+ assert_equal(Point.from_x_y(3.456,0.123),point)
22
+ end
23
+
24
+ def test_point_with_srid
25
+ ewkt="SRID=245;POINT(0.0 2.0)"
26
+ @ewkt_parser.parse(ewkt)
27
+ point = @factory.geometry
28
+ assert(point.instance_of?(Point))
29
+ assert_equal(Point.from_x_y(0,2,245),point)
30
+ assert_equal(245,point.srid)
31
+ assert_equal(point.as_ewkt(true,false),ewkt)
32
+ end
33
+
34
+ def test_point3dz
35
+ ewkt="POINT(3.456 0.123 123.667)"
36
+ @ewkt_parser.parse(ewkt)
37
+ point = @factory.geometry
38
+ assert(point.instance_of?(Point))
39
+ assert_equal(Point.from_x_y_z(3.456,0.123,123.667),point)
40
+ assert_equal(point.as_ewkt(false),ewkt)
41
+ end
42
+
43
+ def test_point3dm
44
+ ewkt="POINTM(3.456 0.123 123.667)"
45
+ @ewkt_parser.parse(ewkt)
46
+ point = @factory.geometry
47
+ assert(point.instance_of?(Point))
48
+ assert_equal(Point.from_x_y_m(3.456,0.123,123.667),point)
49
+ assert_equal(point.as_ewkt(false),ewkt)
50
+ end
51
+
52
+ def test_point4d
53
+ ewkt="POINT(3.456 0.123 123.667 15.0)"
54
+ @ewkt_parser.parse(ewkt)
55
+ point = @factory.geometry
56
+ assert(point.instance_of?(Point))
57
+ assert_equal(Point.from_x_y_z_m(3.456,0.123,123.667,15.0),point)
58
+ assert_equal(point.as_ewkt(false),ewkt)
59
+ end
60
+
61
+ def test_linestring
62
+ @ewkt_parser.parse("LINESTRING(3.456 0.123,123.44 123.56,54555.22 123.3)")
63
+ line_string = @factory.geometry
64
+ assert(line_string.instance_of?(LineString))
65
+ assert_equal(LineString.from_coordinates([[3.456,0.123],[123.44,123.56],[54555.22,123.3]]),line_string)
66
+
67
+ @ewkt_parser.parse("SRID=256;LINESTRING(12.4 -45.3,45.4 41.6)")
68
+ line_string = @factory.geometry
69
+ assert(line_string.instance_of?(LineString))
70
+ assert_equal(LineString.from_coordinates([[12.4,-45.3],[45.4,41.6]],256),line_string)
71
+
72
+ @ewkt_parser.parse("SRID=256;LINESTRING(12.4 -45.3 35.3,45.4 41.6 12.3)")
73
+ line_string = @factory.geometry
74
+ assert(line_string.instance_of?(LineString))
75
+ assert_equal(LineString.from_coordinates([[12.4,-45.3,35.3],[45.4,41.6,12.3]],256,true),line_string)
76
+
77
+ @ewkt_parser.parse("SRID=256;LINESTRINGM(12.4 -45.3 35.3,45.4 41.6 12.3)")
78
+ line_string = @factory.geometry
79
+ assert(line_string.instance_of?(LineString))
80
+ assert_equal(LineString.from_coordinates([[12.4,-45.3,35.3],[45.4,41.6,12.3]],256,false,true),line_string)
81
+
82
+ @ewkt_parser.parse("SRID=256;LINESTRING(12.4 -45.3 35.3 25.2,45.4 41.6 12.3 13.75)")
83
+ line_string = @factory.geometry
84
+ assert(line_string.instance_of?(LineString))
85
+ assert_equal(LineString.from_coordinates([[12.4,-45.3,35.3,25.2],[45.4,41.6,12.3,13.75]],256,true,true),line_string)
86
+
87
+ end
88
+
89
+ def test_polygon
90
+ @ewkt_parser.parse("POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,3 1,3 3,1 3,1 1))")
91
+ polygon = @factory.geometry
92
+ assert(polygon.instance_of?(Polygon))
93
+ assert_equal(Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256),polygon)
94
+
95
+ @ewkt_parser.parse("SRID=256;POLYGON( ( 0 0 2,4 0 2,4 4 2,0 4 2,0 0 2),(1 1 2,3 1 2,3 3 2,1 3 2,1 1 2))")
96
+ polygon = @factory.geometry
97
+ assert(polygon.instance_of?(Polygon))
98
+ assert_equal(Polygon.from_coordinates([[[0,0,2],[4,0,2],[4,4,2],[0,4,2],[0,0,2]],[[1,1,2],[3,1,2],[3,3,2],[1,3,2],[1,1,2]]],256,true),polygon)
99
+
100
+ @ewkt_parser.parse("SRID=256;POLYGONM((0 0 2,4 0 2,4 4 2,0 4 2,0 0 2),(1 1 2,3 1 2,3 3 2,1 3 2,1 1 2))")
101
+ polygon = @factory.geometry
102
+ assert(polygon.instance_of?(Polygon))
103
+ assert_equal(Polygon.from_coordinates([[[0,0,2],[4,0,2],[4,4,2],[0,4,2],[0,0,2]],[[1,1,2],[3,1,2],[3,3,2],[1,3,2],[1,1,2]]],256,false,true),polygon)
104
+
105
+
106
+ @ewkt_parser.parse("SRID=256;POLYGON((0 0 2 -45.1,4 0 2 5,4 4 2 4.67,0 4 2 1.34,0 0 2 -45.1),(1 1 2 12.3,3 1 2 123,3 3 2 12.2,1 3 2 12,1 1 2 12.3))")
107
+ polygon = @factory.geometry
108
+ assert(polygon.instance_of?(Polygon))
109
+ assert_equal(Polygon.from_coordinates([[[0,0,2,-45.1],[4,0,2,5],[4,4,2,4.67],[0,4,2,1.34],[0,0,2,-45.1]],[[1,1,2,12.3],[3,1,2,123],[3,3,2,12.2],[1,3,2,12],[1,1,2,12.3]]],256,true,true),polygon)
110
+
111
+
112
+ end
113
+
114
+ def test_multi_point
115
+ #Form output by the current version of PostGIS. Future versions will output the one in the specification
116
+ @ewkt_parser.parse("SRID=444;MULTIPOINT(12.4 -123.3,-65.1 123.4,123.55555555 123)")
117
+ multi_point = @factory.geometry
118
+ assert(multi_point.instance_of?(MultiPoint))
119
+ assert_equal(MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]],444),multi_point)
120
+ assert_equal(444,multi_point.srid)
121
+ assert_equal(444,multi_point[0].srid)
122
+
123
+ @ewkt_parser.parse("SRID=444;MULTIPOINT(12.4 -123.3 4.5,-65.1 123.4 6.7,123.55555555 123 7.8)")
124
+ multi_point = @factory.geometry
125
+ assert(multi_point.instance_of?(MultiPoint))
126
+ assert_equal(MultiPoint.from_coordinates([[12.4,-123.3,4.5],[-65.1,123.4,6.7],[123.55555555,123,7.8]],444,true),multi_point)
127
+ assert_equal(444,multi_point.srid)
128
+ assert_equal(444,multi_point[0].srid)
129
+
130
+ #Form in the EWKT specification (from the OGC)
131
+ @ewkt_parser.parse("SRID=444;MULTIPOINT( ( 12.4 -123.3 4.5 ) , (-65.1 123.4 6.7),(123.55555555 123 7.8))")
132
+ multi_point = @factory.geometry
133
+ assert(multi_point.instance_of?(MultiPoint))
134
+ assert_equal(MultiPoint.from_coordinates([[12.4,-123.3,4.5],[-65.1,123.4,6.7],[123.55555555,123,7.8]],444,true),multi_point)
135
+ assert_equal(444,multi_point.srid)
136
+ assert_equal(444,multi_point[0].srid)
137
+ end
138
+
139
+ def test_multi_line_string
140
+ @ewkt_parser.parse("SRID=256;MULTILINESTRING((1.5 45.2,-54.12312 -0.012),(1.5 45.2,-54.12312 -0.012,45.123 123.3))")
141
+ multi_line_string = @factory.geometry
142
+ assert(multi_line_string.instance_of?(MultiLineString))
143
+ assert_equal(MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012]],256),LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]],256)],256),multi_line_string)
144
+ assert_equal(256,multi_line_string.srid)
145
+ assert_equal(256,multi_line_string[0].srid)
146
+
147
+ @ewkt_parser.parse("SRID=256;MULTILINESTRING((1.5 45.2 1.3 1.2,-54.12312 -0.012 1.2 4.5),(1.5 45.2 5.1 -4.5,-54.12312 -0.012 -6.8 3.4,45.123 123.3 4.5 -5.3))")
148
+ multi_line_string = @factory.geometry
149
+ assert(multi_line_string.instance_of?(MultiLineString))
150
+ assert_equal(MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2,1.3,1.2],[-54.12312,-0.012,1.2,4.5]],256,true,true),LineString.from_coordinates([[1.5,45.2,5.1,-4.5],[-54.12312,-0.012,-6.8,3.4],[45.123,123.3,4.5,-5.3]],256,true,true)],256,true,true),multi_line_string)
151
+ assert_equal(256,multi_line_string.srid)
152
+ assert_equal(256,multi_line_string[0].srid)
153
+
154
+
155
+ end
156
+
157
+ def test_multi_polygon
158
+ ewkt="SRID=256;MULTIPOLYGON(((12.4 -45.3,45.4 41.6,4.456 1.0698,12.4 -45.3),(2.4 5.3,5.4 1.4263,14.46 1.06,2.4 5.3)),((0.0 0.0,4.0 0.0,4.0 4.0,0.0 4.0,0.0 0.0),(1.0 1.0,3.0 1.0,3.0 3.0,1.0 3.0,1.0 1.0)))"
159
+ @ewkt_parser.parse(ewkt)
160
+ multi_polygon = @factory.geometry
161
+ assert(multi_polygon.instance_of?(MultiPolygon))
162
+ assert_equal(MultiPolygon.from_polygons([Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]],256),Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)],256),multi_polygon)
163
+ assert_equal(256,multi_polygon.srid)
164
+ assert_equal(256,multi_polygon[0].srid)
165
+ assert_equal(multi_polygon.as_ewkt,ewkt)
166
+
167
+ ewkt="MULTIPOLYGON(((12.4 -45.3 2,45.4 41.6 3,4.456 1.0698 4,12.4 -45.3 2),(2.4 5.3 1,5.4 1.4263 3.44,14.46 1.06 4.5,2.4 5.3 1)),((0 0 5.6,4 0 5.4,4 4 1,0 4 23,0 0 5.6),(1 1 2.3,3 1 4,3 3 5,1 3 6,1 1 2.3)))"
168
+ @ewkt_parser.parse(ewkt)
169
+ multi_polygon = @factory.geometry
170
+ assert(multi_polygon.instance_of?(MultiPolygon))
171
+ assert_equal(MultiPolygon.from_polygons([Polygon.from_coordinates([[[12.4,-45.3,2],[45.4,41.6,3],[4.456,1.0698,4],[12.4,-45.3,2]],[[2.4,5.3,1],[5.4,1.4263,3.44],[14.46,1.06,4.5],[2.4,5.3,1]]],DEFAULT_SRID,true),Polygon.from_coordinates([[[0,0,5.6],[4,0,5.4],[4,4,1],[0,4,23],[0,0,5.6]],[[1,1,2.3],[3,1,4],[3,3,5],[1,3,6],[1,1,2.3]]],DEFAULT_SRID,true)],DEFAULT_SRID,true),multi_polygon)
172
+ assert_equal(-1,multi_polygon.srid)
173
+ assert_equal(-1,multi_polygon[0].srid)
174
+
175
+ end
176
+
177
+ def test_geometry_collection
178
+ @ewkt_parser.parse("SRID=256;GEOMETRYCOLLECTION(POINT(4.67 45.4),LINESTRING(5.7 12.45,67.55 54),POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,3 1,3 3,1 3,1 1)))")
179
+ geometry_collection = @factory.geometry
180
+ assert(geometry_collection.instance_of?(GeometryCollection))
181
+ assert_equal(GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4,256),LineString.from_coordinates([[5.7,12.45],[67.55,54]],256),Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)],256),geometry_collection)
182
+ assert_equal(256,geometry_collection[0].srid)
183
+
184
+ @ewkt_parser.parse("SRID=256;GEOMETRYCOLLECTIONM(POINTM(4.67 45.4 45.6),LINESTRINGM(5.7 12.45 5.6,67.55 54 6.7))")
185
+ geometry_collection = @factory.geometry
186
+ assert(geometry_collection.instance_of?(GeometryCollection))
187
+ assert_equal(GeometryCollection.from_geometries([Point.from_x_y_m(4.67,45.4,45.6,256),LineString.from_coordinates([[5.7,12.45,5.6],[67.55,54,6.7]],256,false,true)],256,false,true),geometry_collection)
188
+ assert_equal(256,geometry_collection[0].srid)
189
+
190
+
191
+ end
192
+
193
+ end