GeoRuby 0.0.4 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,30 +4,33 @@ module GeoRuby
4
4
  module SimpleFeatures
5
5
  #Represents a group of line strings (see LineString).
6
6
  class MultiLineString < GeometryCollection
7
- def initialize(srid = DEFAULT_SRID)
7
+ def initialize(srid = DEFAULT_SRID,with_z=false,with_m=false)
8
8
  super(srid)
9
9
  end
10
10
  def binary_geometry_type
11
11
  5
12
12
  end
13
+
13
14
  #Text representation of a multi line string
14
- def text_representation(dimension = 2)
15
- @geometries.collect{|line_string| "(" + line_string.text_representation + ")" }.join(",")
15
+ def text_representation(allow_z=true,allow_m=true)
16
+ @geometries.collect{|line_string| "(" + line_string.text_representation(allow_z,allow_m) + ")" }.join(",")
16
17
  end
17
18
  #WKT geometry type
18
19
  def text_geometry_type
19
20
  "MULTILINESTRING"
20
21
  end
22
+
21
23
  #Creates a new multi line string from an array of line strings
22
- def self.from_line_strings(line_strings,srid=DEFAULT_SRID)
23
- multi_line_string = MultiLineString::new(srid)
24
+ def self.from_line_strings(line_strings,srid=DEFAULT_SRID,with_z=false,with_m=false)
25
+ multi_line_string = MultiLineString::new(srid,with_z,with_m)
24
26
  multi_line_string.concat(line_strings)
25
27
  multi_line_string
26
28
  end
29
+
27
30
  #Creates a new multi line string from sequences of points : (((x,y)...(x,y)),((x,y)...(x,y)))
28
- def self.from_raw_point_sequences(point_sequences,srid=DEFAULT_SRID)
29
- multi_line_string = MultiLineString::new(srid)
30
- multi_line_string.concat(point_sequences.collect {|point_sequence| LineString.from_raw_point_sequence(point_sequence,srid) })
31
+ def self.from_coordinates(point_sequences,srid=DEFAULT_SRID,with_z=false,with_m=false)
32
+ multi_line_string = MultiLineString::new(srid,with_z,with_m)
33
+ multi_line_string.concat(point_sequences.collect {|points| LineString.from_coordinates(points,srid,with_z,with_m) })
31
34
  multi_line_string
32
35
  end
33
36
  end
@@ -5,16 +5,17 @@ module GeoRuby
5
5
  #Represents a group of points (see Point).
6
6
  class MultiPoint < GeometryCollection
7
7
 
8
- def initialize(srid= DEFAULT_SRID)
9
- super(srid)
8
+ def initialize(srid= DEFAULT_SRID,with_z=false,with_m=false)
9
+ super(srid,with_z,with_m)
10
10
  end
11
-
11
+
12
12
  def binary_geometry_type
13
13
  4
14
14
  end
15
+
15
16
  #Text representation of a MultiPoint
16
- def text_representation(dimension=2)
17
- @geometries.collect{|point| point.text_representation(dimension)}.join(",")
17
+ def text_representation(allow_z=true,allow_m=true)
18
+ @geometries.collect{|point| point.text_representation(allow_z,allow_m)}.join(",")
18
19
  end
19
20
  #WKT geoemtry type
20
21
  def text_geometry_type
@@ -22,16 +23,16 @@ module GeoRuby
22
23
  end
23
24
 
24
25
  #Creates a new multi point from an array of points
25
- def self.from_points(points,srid= DEFAULT_SRID)
26
- multi_point= MultiPoint::new(srid)
26
+ def self.from_points(points,srid= DEFAULT_SRID,with_z=false,with_m=false)
27
+ multi_point= MultiPoint::new(srid,with_z,with_m)
27
28
  multi_point.concat(points)
28
29
  multi_point
29
30
  end
30
31
 
31
32
  #Creates a new multi point from a list of point coordinates : ((x,y)...(x,y))
32
- def self.from_raw_point_sequence(point_sequence,srid= DEFAULT_SRID)
33
- multi_point= MultiPoint::new(srid)
34
- multi_point.concat(point_sequence.collect {|point| Point.from_coordinates(point,srid)})
33
+ def self.from_coordinates(points,srid= DEFAULT_SRID,with_z=false,with_m=false)
34
+ multi_point= MultiPoint::new(srid,with_z,with_m)
35
+ multi_point.concat(points.collect {|point| Point.from_coordinates(point,srid,with_z,with_m)})
35
36
  multi_point
36
37
  end
37
38
 
@@ -5,15 +5,15 @@ module GeoRuby
5
5
  module SimpleFeatures
6
6
  #Represents a group of polygons (see Polygon).
7
7
  class MultiPolygon < GeometryCollection
8
- def initialize(srid = DEFAULT_SRID)
8
+ def initialize(srid = DEFAULT_SRID,with_z=false,with_m=false)
9
9
  super(srid)
10
10
  end
11
11
  def binary_geometry_type
12
12
  6
13
13
  end
14
14
  #Text representation of a MultiPolygon
15
- def text_representation(dimension=2)
16
- @geometries.collect{|polygon| "(" + polygon.text_representation(dimension) + ")"}.join(",")
15
+ def text_representation(allow_z=true,allow_m=true)
16
+ @geometries.collect{|polygon| "(" + polygon.text_representation(allow_z,allow_m) + ")"}.join(",")
17
17
  end
18
18
  #WKT geometry type
19
19
  def text_geometry_type
@@ -21,18 +21,17 @@ module GeoRuby
21
21
  end
22
22
 
23
23
  #Creates a multi polygon from an array of polygons
24
- def self.from_polygons(polygons,srid=DEFAULT_SRID)
25
- multi_polygon = MultiPolygon::new(srid)
24
+ def self.from_polygons(polygons,srid=DEFAULT_SRID,with_z=false,with_m=false)
25
+ multi_polygon = MultiPolygon::new(srid,with_z,with_m)
26
26
  multi_polygon.concat(polygons)
27
27
  multi_polygon
28
28
  end
29
29
  #Creates a multi polygon from sequences of points : ((((x,y)...(x,y)),((x,y)...(x,y)),((x,y)...(x,y)))
30
- def self.from_raw_point_sequences(point_sequences, srid= DEFAULT_SRID)
31
- multi_polygon = MultiPolygon::new(srid)
32
- multi_polygon.concat( point_sequences.collect {|point_sequence| Polygon.from_raw_point_sequences(point_sequence,srid) } )
30
+ def self.from_coordinates(point_sequence_sequences,srid= DEFAULT_SRID,with_z=false,with_m=false)
31
+ multi_polygon = MultiPolygon::new(srid,with_z,with_m)
32
+ multi_polygon.concat( point_sequence_sequences.collect {|point_sequences| Polygon.from_coordinates(point_sequences,srid,with_z,with_m) } )
33
33
  multi_polygon
34
34
  end
35
-
36
35
  end
37
36
  end
38
37
  end
@@ -4,16 +4,17 @@ module GeoRuby
4
4
  module SimpleFeatures
5
5
  #Represents a point. It is in 3D if the Z coordinate is not +nil+.
6
6
  class Point < Geometry
7
- #Coordinates of the point
8
- attr_accessor :x,:y,:z
9
7
 
10
- def initialize(srid=DEFAULT_SRID)
11
- super(srid)
8
+ attr_accessor :x,:y,:z,:m
9
+
10
+ def initialize(srid=DEFAULT_SRID,with_z=false,with_m=false)
11
+ super(srid,with_z,with_m)
12
12
  @x=0.0
13
13
  @y=0.0
14
- @z=nil
14
+ @z=0.0 #default value : meaningful if with_z
15
+ @m=0.0 #default value : meaningful if with_m
15
16
  end
16
- #sets all coordinates in one call
17
+ #sets all coordinates in one call. Use the +m+ accessor to set the m.
17
18
  def set_x_y_z(x,y,z)
18
19
  @x=x
19
20
  @y=y
@@ -24,21 +25,20 @@ module GeoRuby
24
25
  @x=x
25
26
  @y=y
26
27
  end
27
- #tests the equality of points
28
+ #tests the equality of the position of points + m
28
29
  def ==(other_point)
29
30
  if other_point.class != self.class
30
31
  false
31
32
  else
32
- @x == other_point.x and @y == other_point.y and @z == other_point.z
33
+ @x == other_point.x and @y == other_point.y and @z == other_point.z and @m == other_point.m
33
34
  end
34
35
  end
35
36
  #binary representation of a point. It lacks some headers to be a valid EWKB representation.
36
- def binary_representation(dimension=2)
37
- if dimension == 2
38
- [@x,@y].pack("EE")
39
- else
40
- [@x,@y,@z || 0].pack("EEE")
41
- end
37
+ def binary_representation(allow_z=true,allow_m=true)
38
+ bin_rep = [@x,@y].pack("EE")
39
+ bin_rep += [@z].pack("E") if @with_z and allow_z #Default value so no crash
40
+ bin_rep += [@m].pack("E") if @with_m and allow_m #idem
41
+ bin_rep
42
42
  end
43
43
  #WKB geometry type of a point
44
44
  def binary_geometry_type
@@ -46,12 +46,11 @@ module GeoRuby
46
46
  end
47
47
 
48
48
  #text representation of a point
49
- def text_representation(dimension=2)
50
- if dimension == 2
51
- "#{@x} #{@y}"
52
- else
53
- "#{@x} #{@y} #{@z || 0}"
54
- end
49
+ def text_representation(allow_z=true,allow_m=true)
50
+ tex_rep = "#{@x} #{@y}"
51
+ tex_rep += " #{@z}" if @with_z and allow_z
52
+ tex_rep += " #{@m}" if @with_m and allow_m
53
+ tex_rep
55
54
  end
56
55
  #WKT geometry type of a point
57
56
  def text_geometry_type
@@ -59,14 +58,16 @@ module GeoRuby
59
58
  end
60
59
 
61
60
  #creates a point from an array of coordinates
62
- def self.from_coordinates(coords,srid=DEFAULT_SRID)
63
- point= Point::new(srid)
64
- if coords.length == 2
65
- point.set_x_y(*coords)
61
+ def self.from_coordinates(coords,srid=DEFAULT_SRID,with_z=false,with_m=false)
62
+ if ! (with_z or with_m)
63
+ from_x_y(coords[0],coords[1],srid)
64
+ elsif with_z and with_m
65
+ from_x_y_z_m(coords[0],coords[1],coords[2],coords[3],srid)
66
+ elsif with_z
67
+ from_x_y_z(coords[0],coords[1],coords[2],srid)
66
68
  else
67
- point.set_x_y_z(*coords)
69
+ from_x_y_m(coords[0],coords[1],coords[2],srid)
68
70
  end
69
- point
70
71
  end
71
72
  #creates a point from the X and Y coordinates
72
73
  def self.from_x_y(x,y,srid=DEFAULT_SRID)
@@ -76,8 +77,22 @@ module GeoRuby
76
77
  end
77
78
  #creates a point from the X, Y and Z coordinates
78
79
  def self.from_x_y_z(x,y,z,srid=DEFAULT_SRID)
79
- point= Point::new(srid)
80
+ point= Point::new(srid,true)
81
+ point.set_x_y_z(x,y,z)
82
+ point
83
+ end
84
+ #creates a point from the X, Y and M coordinates
85
+ def self.from_x_y_m(x,y,m,srid=DEFAULT_SRID)
86
+ point= Point::new(srid,false,true)
87
+ point.set_x_y(x,y)
88
+ point.m=m
89
+ point
90
+ end
91
+ #creates a point from the X, Y, Z and M coordinates
92
+ def self.from_x_y_z_m(x,y,z,m,srid=DEFAULT_SRID)
93
+ point= Point::new(srid,true,true)
80
94
  point.set_x_y_z(x,y,z)
95
+ point.m=m
81
96
  point
82
97
  end
83
98
  end
@@ -7,8 +7,8 @@ module GeoRuby
7
7
  #the list of rings forming the polygon
8
8
  attr_reader :rings
9
9
 
10
- def initialize(srid = DEFAULT_SRID)
11
- super(srid)
10
+ def initialize(srid = DEFAULT_SRID,with_z=false,with_m=false)
11
+ super(srid,with_z,with_m)
12
12
  @rings = []
13
13
  end
14
14
  #add one to the polygon
@@ -66,34 +66,36 @@ module GeoRuby
66
66
  end
67
67
  end
68
68
  #binary representation of a polygon, without the headers neccessary for a valid WKB string
69
- def binary_representation(dimension=2)
69
+ def binary_representation(allow_z=true,allow_m=true)
70
70
  rep = [length].pack("V")
71
- each {|linear_ring| rep << linear_ring.binary_representation(dimension) }
71
+ each {|linear_ring| rep << linear_ring.binary_representation(allow_z,allow_m)}
72
72
  rep
73
73
  end
74
74
  #WKB geometry type
75
75
  def binary_geometry_type
76
76
  3
77
77
  end
78
+
78
79
  #Text representation of a polygon
79
- def text_representation(dimension=2)
80
- @rings.collect{|line_string| "(" + line_string.text_representation(dimension) + ")" }.join(",")
80
+ def text_representation(allow_z=true,allow_m=true)
81
+ @rings.collect{|line_string| "(" + line_string.text_representation(allow_z,allow_m) + ")" }.join(",")
81
82
  end
82
83
  #WKT geometry type
83
84
  def text_geometry_type
84
85
  "POLYGON"
85
86
  end
87
+
86
88
  #creates a new polygon. Accepts an array of linear strings as argument
87
- def self.from_linear_rings(linear_rings,srid = DEFAULT_SRID)
88
- polygon = Polygon::new(srid)
89
+ def self.from_linear_rings(linear_rings,srid = DEFAULT_SRID,with_z=false,with_m=false)
90
+ polygon = Polygon::new(srid,with_z,with_m)
89
91
  polygon.concat(linear_rings)
90
92
  polygon
91
93
  end
92
94
 
93
95
  #creates a new polygon. Accepts a sequence of points as argument : ((x,y)....(x,y)),((x,y).....(x,y))
94
- def self.from_raw_point_sequences(point_sequences,srid=DEFAULT_SRID)
95
- polygon = Polygon::new(srid)
96
- polygon.concat( point_sequences.collect {|point_sequence| LinearRing.from_raw_point_sequence(point_sequence,srid) } )
96
+ def self.from_coordinates(point_sequences,srid=DEFAULT_SRID,with_z=false,with_m=false)
97
+ polygon = Polygon::new(srid,with_z,with_m)
98
+ polygon.concat( point_sequences.collect {|points| LinearRing.from_coordinates(points,srid,with_z,with_m) } )
97
99
  polygon
98
100
  end
99
101
 
data/rakefile.rb CHANGED
@@ -24,7 +24,7 @@ spec = Gem::Specification::new do |s|
24
24
  s.platform = Gem::Platform::RUBY
25
25
 
26
26
  s.name = 'GeoRuby'
27
- s.version = "0.0.4"
27
+ s.version = "0.1.1"
28
28
  s.summary = "Ruby data holder for OGC Simple Features"
29
29
  s.description = <<EOF
30
30
  GeoRuby is intended as a holder for data returned from PostGIS queries. Therefore, 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)
@@ -25,74 +25,147 @@ class TestEWKBParser < Test::Unit::TestCase
25
25
  assert(point.instance_of?(Point))
26
26
  assert_equal(Point.from_x_y(4.906455,52.377953),point)
27
27
  end
28
+
29
+
28
30
 
29
- def test_point3d
31
+ def test_point3dz
30
32
  @hex_ewkb_parser.parse("01010000A07B000000CDCCCCCCCCCC28406666666666A646400000000000000CC0")
31
33
  point = @factory.geometry
32
34
  assert(point.instance_of?(Point))
33
35
  assert_equal(Point.from_x_y_z(12.4,45.3,-3.5,123),point)
34
36
  end
35
-
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
+
36
45
  def test_line_string
37
46
  @hex_ewkb_parser.parse("01020000200001000002000000CDCCCCCCCCCC28406666666666A646C03333333333B34640CDCCCCCCCCCC4440")
38
47
  line_string = @factory.geometry
39
48
  assert(line_string.instance_of?(LineString))
40
- assert_equal(LineString.from_raw_point_sequence([[12.4,-45.3],[45.4,41.6]],256),line_string)
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
+
41
61
  end
42
62
 
43
63
  def test_polygon
44
64
  @hex_ewkb_parser.parse("0103000020000100000200000005000000000000000000000000000000000000000000000000001040000000000000000000000000000010400000000000001040000000000000000000000000000010400000000000000000000000000000000005000000000000000000F03F000000000000F03F0000000000000840000000000000F03F00000000000008400000000000000840000000000000F03F0000000000000840000000000000F03F000000000000F03F")
45
65
  polygon = @factory.geometry
46
66
  assert(polygon.instance_of?(Polygon))
47
- assert_equal(Polygon.from_raw_point_sequences([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256),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
+
48
86
  end
49
87
 
50
88
  def test_geometry_collection
51
89
  @hex_ewkb_parser.parse("010700002000010000020000000101000000AE47E17A14AE12403333333333B34640010200000002000000CDCCCCCCCCCC16406666666666E628403333333333E350400000000000004B40")
52
90
  geometry_collection = @factory.geometry
53
91
  assert(geometry_collection.instance_of?(GeometryCollection))
54
- assert_equal(GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4,256),LineString.from_raw_point_sequence([[5.7,12.45],[67.55,54]],256)],256),geometry_collection)
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)
55
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
+
56
102
  end
57
103
 
58
104
  def test_multi_point
59
105
  @hex_ewkb_parser.parse("0104000020BC010000030000000101000000CDCCCCCCCCCC28403333333333D35EC0010100000066666666664650C09A99999999D95E4001010000001F97DD388EE35E400000000000C05E40")
60
106
  multi_point = @factory.geometry
61
107
  assert(multi_point.instance_of?(MultiPoint))
62
- assert_equal(MultiPoint.from_raw_point_sequence([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]],444),multi_point)
108
+ assert_equal(MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]],444),multi_point)
63
109
  assert_equal(444,multi_point.srid)
64
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
+
65
120
  end
66
121
 
67
122
  def test_multi_line_string
68
123
  @hex_ewkb_parser.parse("01050000200001000002000000010200000002000000000000000000F83F9A99999999994640E4BD6A65C20F4BC0FA7E6ABC749388BF010200000003000000000000000000F83F9A99999999994640E4BD6A65C20F4BC0FA7E6ABC749388BF39B4C876BE8F46403333333333D35E40")
69
124
  multi_line_string = @factory.geometry
70
125
  assert(multi_line_string.instance_of?(MultiLineString))
71
- assert_equal(MultiLineString.from_line_strings([LineString.from_raw_point_sequence([[1.5,45.2],[-54.12312,-0.012]],256),LineString.from_raw_point_sequence([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]],256)],256),multi_line_string)
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)
72
134
  assert_equal(256,multi_line_string.srid)
73
135
  assert_equal(256,multi_line_string[0].srid)
136
+
137
+
74
138
  end
75
139
 
76
140
  def test_multi_polygon
77
141
  @hex_ewkb_parser.parse("0106000020000100000200000001030000000200000004000000CDCCCCCCCCCC28406666666666A646C03333333333B34640CDCCCCCCCCCC44406DE7FBA9F1D211403D2CD49AE61DF13FCDCCCCCCCCCC28406666666666A646C004000000333333333333034033333333333315409A999999999915408A8EE4F21FD2F63FEC51B81E85EB2C40F6285C8FC2F5F03F3333333333330340333333333333154001030000000200000005000000000000000000000000000000000000000000000000001040000000000000000000000000000010400000000000001040000000000000000000000000000010400000000000000000000000000000000005000000000000000000F03F000000000000F03F0000000000000840000000000000F03F00000000000008400000000000000840000000000000F03F0000000000000840000000000000F03F000000000000F03F")
78
142
  multi_polygon = @factory.geometry
79
143
  assert(multi_polygon.instance_of?(MultiPolygon))
80
- assert_equal(MultiPolygon.from_polygons([Polygon.from_raw_point_sequences([[[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_raw_point_sequences([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)],256),multi_polygon)
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)
81
152
  assert_equal(256,multi_polygon.srid)
82
153
  assert_equal(256,multi_polygon[0].srid)
83
154
  end
155
+
156
+
84
157
  def test_failure_trailing_data
85
158
  #added A345 at the end
86
- assert_raise(StandardError){@hex_ewkb_parser.parse("01010000207B000000CDCCCCCCCCCC28406666666666A64640A345")}
159
+ assert_raise(EWKBFormatError){@hex_ewkb_parser.parse("01010000207B000000CDCCCCCCCCCC28406666666666A64640A345")}
87
160
  end
88
161
  def test_failure_unknown_geometry_type
89
- assert_raise(StandardError){@hex_ewkb_parser.parse("01090000207B000000CDCCCCCCCCCC28406666666666A64640")}
162
+ assert_raise(EWKBFormatError){@hex_ewkb_parser.parse("01090000207B000000CDCCCCCCCCCC28406666666666A64640")}
90
163
  end
91
164
  def test_failure_m
92
- assert_raise(StandardError){@hex_ewkb_parser.parse("01010000607B000000CDCCCCCCCCCC28406666666666A64640")}
165
+ assert_raise(EWKBFormatError){@hex_ewkb_parser.parse("01010000607B000000CDCCCCCCCCCC28406666666666A64640")}
93
166
  end
94
167
  def test_failure_truncated_data
95
- assert_raise(StandardError){@hex_ewkb_parser.parse("01010000207B000000CDCCCCCCCCCC2840666666")}
168
+ assert_raise(EWKBFormatError){@hex_ewkb_parser.parse("01010000207B000000CDCCCCCCCCCC2840666666")}
96
169
  end
97
170
 
98
171
  end
@@ -28,48 +28,121 @@ class TestEWKTParser < Test::Unit::TestCase
28
28
  assert(point.instance_of?(Point))
29
29
  assert_equal(Point.from_x_y(0,2,245),point)
30
30
  assert_equal(245,point.srid)
31
- assert_equal(point.as_text(2),ewkt)
31
+ assert_equal(point.as_ewkt(true,false),ewkt)
32
32
  end
33
33
 
34
- def test_point3d
34
+ def test_point3dz
35
35
  ewkt="POINT(3.456 0.123 123.667)"
36
36
  @ewkt_parser.parse(ewkt)
37
37
  point = @factory.geometry
38
38
  assert(point.instance_of?(Point))
39
39
  assert_equal(Point.from_x_y_z(3.456,0.123,123.667),point)
40
- assert_equal(point.as_text(3,false),ewkt)
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)
41
59
  end
42
60
 
43
61
  def test_linestring
44
- @ewkt_parser.parse("LINESTRING(3.456 0.123,123.44 123.56,54555.22 123.3)")
45
- linestring = @factory.geometry
46
- assert(linestring.instance_of?(LineString))
47
- assert_equal(LineString.from_raw_point_sequence([[3.456,0.123],[123.44,123.56],[54555.22,123.3]]),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
+
48
87
  end
49
88
 
50
89
  def test_polygon
51
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))")
52
91
  polygon = @factory.geometry
53
92
  assert(polygon.instance_of?(Polygon))
54
- assert_equal(Polygon.from_raw_point_sequences([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256),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
+
55
112
  end
56
113
 
57
114
  def test_multi_point
58
115
  @ewkt_parser.parse("SRID=444;MULTIPOINT(12.4 -123.3,-65.1 123.4,123.55555555 123)")
59
116
  multi_point = @factory.geometry
60
117
  assert(multi_point.instance_of?(MultiPoint))
61
- assert_equal(MultiPoint.from_raw_point_sequence([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]],444),multi_point)
118
+ assert_equal(MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]],444),multi_point)
62
119
  assert_equal(444,multi_point.srid)
63
120
  assert_equal(444,multi_point[0].srid)
121
+
122
+ @ewkt_parser.parse("SRID=444;MULTIPOINT(12.4 -123.3 4.5,-65.1 123.4 6.7,123.55555555 123 7.8)")
123
+ multi_point = @factory.geometry
124
+ assert(multi_point.instance_of?(MultiPoint))
125
+ 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)
126
+ assert_equal(444,multi_point.srid)
127
+ assert_equal(444,multi_point[0].srid)
64
128
  end
65
129
 
66
130
  def test_multi_line_string
67
131
  @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))")
68
132
  multi_line_string = @factory.geometry
69
133
  assert(multi_line_string.instance_of?(MultiLineString))
70
- assert_equal(MultiLineString.from_line_strings([LineString.from_raw_point_sequence([[1.5,45.2],[-54.12312,-0.012]],256),LineString.from_raw_point_sequence([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]],256)],256),multi_line_string)
134
+ 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)
135
+ assert_equal(256,multi_line_string.srid)
136
+ assert_equal(256,multi_line_string[0].srid)
137
+
138
+ @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))")
139
+ multi_line_string = @factory.geometry
140
+ assert(multi_line_string.instance_of?(MultiLineString))
141
+ 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)
71
142
  assert_equal(256,multi_line_string.srid)
72
143
  assert_equal(256,multi_line_string[0].srid)
144
+
145
+
73
146
  end
74
147
 
75
148
  def test_multi_polygon
@@ -77,18 +150,35 @@ class TestEWKTParser < Test::Unit::TestCase
77
150
  @ewkt_parser.parse(ewkt)
78
151
  multi_polygon = @factory.geometry
79
152
  assert(multi_polygon.instance_of?(MultiPolygon))
80
- assert_equal(MultiPolygon.from_polygons([Polygon.from_raw_point_sequences([[[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_raw_point_sequences([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)],256),multi_polygon)
153
+ 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)
81
154
  assert_equal(256,multi_polygon.srid)
82
155
  assert_equal(256,multi_polygon[0].srid)
83
- assert_equal(multi_polygon.as_text,ewkt)
156
+ assert_equal(multi_polygon.as_ewkt,ewkt)
157
+
158
+ 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)))"
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,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)
163
+ assert_equal(-1,multi_polygon.srid)
164
+ assert_equal(-1,multi_polygon[0].srid)
165
+
84
166
  end
85
167
 
86
168
  def test_geometry_collection
87
169
  @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)))")
88
170
  geometry_collection = @factory.geometry
89
171
  assert(geometry_collection.instance_of?(GeometryCollection))
90
- assert_equal(GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4,256),LineString.from_raw_point_sequence([[5.7,12.45],[67.55,54]],256),Polygon.from_raw_point_sequences([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)],256),geometry_collection)
172
+ 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)
91
173
  assert_equal(256,geometry_collection[0].srid)
92
- end
174
+
175
+ @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))")
176
+ geometry_collection = @factory.geometry
177
+ assert(geometry_collection.instance_of?(GeometryCollection))
178
+ 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)
179
+ assert_equal(256,geometry_collection[0].srid)
180
+
181
+
182
+ end
93
183
 
94
184
  end