GeoRuby 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,5 +1,5 @@
1
1
  =GeoRuby
2
- This is GeoRuby 0.0.2. It 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 http://www.opengis.org/docs/99-049.pdf), although without any kind of advanced functionalities (such as geometric operators or reprojections).
2
+ This is GeoRuby 0.0.3. It 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 http://www.opengis.org/docs/99-049.pdf), although without any kind of advanced functionalities (such as geometric operators or reprojections).
3
3
 
4
4
  ===Available data types
5
5
  The following geometric data types are provided :
@@ -13,12 +13,15 @@ The following geometric data types are provided :
13
13
  - Geometry collection
14
14
 
15
15
  ===Input and output
16
- These geometries can be input and output in WKB/EWKB format (as well as the related HexWKB and HexEWKB formats). Since EWKB and HexEWKB are respectively the canonical binary and ASCII representations of geometries in PostGIS, this functionality can prove useful. Geometries can also be output as WKT/EWKT.
16
+ These geometries can be input and output in WKB/EWKB/WKT/EWKT format (as well as the related HexWKB and HexEWKB formats). Since EWKB and HexEWKB are respectively the canonical binary and ASCII representations of geometries in PostGIS, this functionality can prove useful.
17
17
 
18
18
  ===Installation
19
19
  Just type :
20
20
  gem install GeoRuby
21
21
 
22
+ ===Changes since the last version
23
+ Geometries can be input in WKT/EWKT format
24
+
22
25
  ===License
23
26
  GeoRuby is released under the MIT license.
24
27
 
data/lib/geo_ruby.rb CHANGED
@@ -8,5 +8,6 @@ require 'geo_ruby/simple_features/multi_line_string'
8
8
  require 'geo_ruby/simple_features/multi_polygon'
9
9
  require 'geo_ruby/simple_features/geometry_collection'
10
10
  require 'geo_ruby/simple_features/ewkb_parser'
11
+ require 'geo_ruby/simple_features/ewkt_parser'
11
12
  require 'geo_ruby/simple_features/geometry_factory'
12
13
 
@@ -37,6 +37,7 @@ module GeoRuby
37
37
  @unpack_structure=UnpackStructure::new(ewkb)
38
38
  parse_geometry
39
39
  @unpack_structure.done
40
+ @srid=nil
40
41
  end
41
42
  private
42
43
  def parse_geometry
@@ -67,15 +68,15 @@ module GeoRuby
67
68
  def parse_geometry_collection
68
69
  parse_multi_geometries(GeometryCollection)
69
70
  end
70
- ##must be corrected : endianness + geometry_type present
71
+
71
72
  def parse_multi_polygon
72
73
  parse_multi_geometries(MultiPolygon)
73
74
  end
74
- #must be corrected
75
+
75
76
  def parse_multi_line_string
76
77
  parse_multi_geometries(MultiLineString)
77
78
  end
78
- #must be corrected
79
+
79
80
  def parse_multi_point
80
81
  parse_multi_geometries(MultiPoint)
81
82
  end
@@ -0,0 +1,138 @@
1
+ require 'geo_ruby/simple_features/point'
2
+ require 'geo_ruby/simple_features/line_string'
3
+ require 'geo_ruby/simple_features/linear_ring'
4
+ require 'geo_ruby/simple_features/polygon'
5
+ require 'geo_ruby/simple_features/multi_point'
6
+ require 'geo_ruby/simple_features/multi_line_string'
7
+ require 'geo_ruby/simple_features/multi_polygon'
8
+ require 'geo_ruby/simple_features/geometry_collection'
9
+
10
+ require 'strscan'
11
+
12
+ module GeoRuby
13
+ module SimpleFeatures
14
+ #Parses EWKT strings and notifies of events (such as the beginning of the definition of geometry, the value of the SRID...) the factory passed as argument to the constructor.
15
+ #
16
+ #=Example
17
+ # factory = GeometryFactory::new
18
+ # ewkt_parser = EWKTParser::new(factory)
19
+ # ewkt_parser.parse(<EWKT String>)
20
+ # geometry = @factory.geometry
21
+ class EWKTParser
22
+
23
+ def initialize(factory)
24
+ @factory = factory
25
+ @parse_options ={
26
+ "POINT" => method(:parse_point),
27
+ "LINESTRING" => method(:parse_line_string),
28
+ "POLYGON" => method(:parse_polygon),
29
+ "MULTIPOINT" => method(:parse_multi_point),
30
+ "MULTILINESTRING" => method(:parse_multi_line_string),
31
+ "MULTIPOLYGON" => method(:parse_multi_polygon),
32
+ "GEOMETRYCOLLECTION" => method(:parse_geometry_collection)
33
+ }
34
+ end
35
+
36
+ #Parses the ewkt string passed as argument and notifies the factory of events
37
+ def parse(ewkt)
38
+ @factory.reset
39
+ parse_geometry(ewkt)
40
+ @srid=nil
41
+ end
42
+
43
+ private
44
+ def parse_geometry(ewkt)
45
+ scanner = StringScanner.new(ewkt)
46
+ if scanner.scan(/SRID=(\d+);/)
47
+ @srid = scanner[1].to_i
48
+ else
49
+ #to manage multi geometries : the srid is not present in sub_geometries, therefore we take the srid of the parent ; if it is the parent, we take the default srid
50
+ @srid= @srid || DEFAULT_SRID
51
+ end
52
+
53
+ if scanner.scan(/(\w+)/) and @parse_options.has_key? scanner[1]
54
+ to_call = scanner[1]
55
+ if scanner.scan(/\((.*)\)/)
56
+ @parse_options[to_call].call(scanner[1])
57
+ else
58
+ raise StandardError::new("Bad token : " + scanner.rest)
59
+ end
60
+ else
61
+ raise StandardError::new("Unknown geometry type")
62
+ end
63
+ end
64
+ def parse_geometry_collection(string)
65
+ @factory.begin_geometry(GeometryCollection,@srid)
66
+ scanner = StringScanner.new(string)
67
+ while(scanner.scan(/(.*?),(?=[A-Z])/))
68
+ parse_geometry(scanner[1])
69
+ end
70
+ parse_geometry(scanner.rest)
71
+ @factory.end_geometry
72
+ end
73
+
74
+ def parse_multi_polygon(string)
75
+ @factory.begin_geometry(MultiPolygon,@srid)
76
+ scanner = StringScanner.new(string)
77
+ while(scanner.scan(/\((\((.*?)\))\),?/))
78
+ parse_polygon(scanner[1])
79
+ end
80
+ @factory.end_geometry
81
+ end
82
+
83
+ def parse_multi_line_string(string)
84
+ @factory.begin_geometry(MultiLineString,@srid)
85
+ scanner = StringScanner.new(string)
86
+ while(scanner.scan(/\(([^\)]*)\),?/))
87
+ parse_line_string(scanner[1])
88
+ end
89
+ @factory.end_geometry
90
+ end
91
+
92
+ def parse_polygon(string)
93
+ @factory.begin_geometry(Polygon,@srid)
94
+ scanner = StringScanner.new(string)
95
+ while(scanner.scan(/\(([^\)]*)\),?/))
96
+ parse_linear_ring(scanner[1])
97
+ end
98
+ @factory.end_geometry
99
+ end
100
+
101
+
102
+ def parse_multi_point(string)
103
+ parse_point_list(MultiPoint,string)
104
+ end
105
+ def parse_linear_ring(string)
106
+ parse_point_list(LinearRing,string)
107
+ end
108
+ def parse_line_string(string)
109
+ parse_point_list(LineString,string)
110
+ end
111
+ #used to parse line_strings and linear_rings
112
+ def parse_point_list(geometry_type,string)
113
+ @factory.begin_geometry(geometry_type,@srid)
114
+ scanner = StringScanner.new(string)
115
+ while(scanner.scan(/([^,]*),?/))
116
+ parse_point(scanner[1])
117
+ end
118
+ @factory.end_geometry
119
+ end
120
+
121
+ def parse_point(string)
122
+ @factory.begin_geometry(Point,@srid)
123
+ scanner = StringScanner.new(string)
124
+ coords = Array.new
125
+ while scanner.scan(/\s*([-+]?[\d.]+)\s*/)
126
+ coords << scanner[1].to_f
127
+ end
128
+ if(coords.length == 2)
129
+ @factory.add_point_x_y(*coords)
130
+ else
131
+ @factory.add_point_x_y_z(*coords)
132
+ end
133
+ @factory.end_geometry
134
+ end
135
+ end
136
+
137
+ end
138
+ end
@@ -82,6 +82,13 @@ module GeoRuby#:nodoc:
82
82
  hexewkb_parser.parse(hexewkb)
83
83
  factory.geometry
84
84
  end
85
+
86
+ def self.from_ewkt(ewkt)
87
+ factory = GeometryFactory::new
88
+ ewkt_parser= EWKTParser::new(factory)
89
+ ewkt_parser.parse(ewkt)
90
+ factory.geometry
91
+ end
85
92
  end
86
93
  end
87
94
  end
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.2"
27
+ s.version = "0.0.3"
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)
@@ -0,0 +1,94 @@
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_text(2),ewkt)
32
+ end
33
+
34
+ def test_point3d
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_text(3,false),ewkt)
41
+ end
42
+
43
+ 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)
48
+ end
49
+
50
+ def test_polygon
51
+ @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
+ polygon = @factory.geometry
53
+ 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)
55
+ end
56
+
57
+ def test_multi_point
58
+ @ewkt_parser.parse("SRID=444;MULTIPOINT(12.4 -123.3,-65.1 123.4,123.55555555 123)")
59
+ multi_point = @factory.geometry
60
+ 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)
62
+ assert_equal(444,multi_point.srid)
63
+ assert_equal(444,multi_point[0].srid)
64
+ end
65
+
66
+ def test_multi_line_string
67
+ @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
+ multi_line_string = @factory.geometry
69
+ 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)
71
+ assert_equal(256,multi_line_string.srid)
72
+ assert_equal(256,multi_line_string[0].srid)
73
+ end
74
+
75
+ def test_multi_polygon
76
+ 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)))"
77
+ @ewkt_parser.parse(ewkt)
78
+ multi_polygon = @factory.geometry
79
+ 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)
81
+ assert_equal(256,multi_polygon.srid)
82
+ assert_equal(256,multi_polygon[0].srid)
83
+ assert_equal(multi_polygon.as_text,ewkt)
84
+ end
85
+
86
+ def test_geometry_collection
87
+ @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
+ geometry_collection = @factory.geometry
89
+ 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)
91
+ assert_equal(256,geometry_collection[0].srid)
92
+ end
93
+
94
+ end
metadata CHANGED
@@ -1,62 +1,66 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.10
2
+ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: GeoRuby
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2006-04-09
6
+ version: 0.0.3
7
+ date: 2006-04-16 00:00:00 +05:00
8
8
  summary: Ruby data holder for OGC Simple Features
9
9
  require_paths:
10
- - lib
10
+ - lib
11
11
  email: guilhem.vellut+georuby@gmail.com
12
12
  homepage: http://thepochisuperstarmegashow.com
13
13
  rubyforge_project:
14
- description: "GeoRuby is intended as a holder for data returned from PostGIS queries.
15
- Therefore, the data model roughly follows the OGC \"Simple Features for SQL\"
16
- specification (see www.opengis.org/docs/99-049.pdf), although without any kind
17
- of advanced functionalities (such as geometric operators or reprojections)"
14
+ description: 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)
18
15
  autorequire:
19
16
  default_executable:
20
17
  bindir: bin
21
18
  has_rdoc: true
22
19
  required_ruby_version: !ruby/object:Gem::Version::Requirement
23
20
  requirements:
24
- -
25
- - ">"
26
- - !ruby/object:Gem::Version
27
- version: 0.0.0
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
28
24
  version:
29
25
  platform: ruby
26
+ signing_key:
27
+ cert_chain:
30
28
  authors:
31
- - Guilhem Vellut
29
+ - Guilhem Vellut
32
30
  files:
33
- - lib/geo_ruby.rb
34
- - lib/geo_ruby/simple_features/ewkb_parser.rb
35
- - lib/geo_ruby/simple_features/geometry.rb
36
- - lib/geo_ruby/simple_features/geometry_collection.rb
37
- - lib/geo_ruby/simple_features/geometry_factory.rb
38
- - lib/geo_ruby/simple_features/linear_ring.rb
39
- - lib/geo_ruby/simple_features/line_string.rb
40
- - lib/geo_ruby/simple_features/multi_line_string.rb
41
- - lib/geo_ruby/simple_features/multi_point.rb
42
- - lib/geo_ruby/simple_features/multi_polygon.rb
43
- - lib/geo_ruby/simple_features/point.rb
44
- - lib/geo_ruby/simple_features/polygon.rb
45
- - test/test_ewkb_parser.rb
46
- - test/test_simple_features.rb
47
- - README
48
- - MIT-LICENSE
49
- - rakefile.rb
31
+ - lib/geo_ruby.rb
32
+ - lib/geo_ruby/simple_features/ewkb_parser.rb
33
+ - lib/geo_ruby/simple_features/ewkt_parser.rb
34
+ - lib/geo_ruby/simple_features/geometry.rb
35
+ - lib/geo_ruby/simple_features/geometry_collection.rb
36
+ - lib/geo_ruby/simple_features/geometry_factory.rb
37
+ - lib/geo_ruby/simple_features/linear_ring.rb
38
+ - lib/geo_ruby/simple_features/line_string.rb
39
+ - lib/geo_ruby/simple_features/multi_line_string.rb
40
+ - lib/geo_ruby/simple_features/multi_point.rb
41
+ - lib/geo_ruby/simple_features/multi_polygon.rb
42
+ - lib/geo_ruby/simple_features/point.rb
43
+ - lib/geo_ruby/simple_features/polygon.rb
44
+ - test/test_ewkb_parser.rb
45
+ - test/test_ewkt_parser.rb
46
+ - test/test_simple_features.rb
47
+ - README
48
+ - MIT-LICENSE
49
+ - rakefile.rb
50
50
  test_files:
51
- - test/test_ewkb_parser.rb
52
- - test/test_simple_features.rb
51
+ - test/test_ewkb_parser.rb
52
+ - test/test_ewkt_parser.rb
53
+ - test/test_simple_features.rb
53
54
  rdoc_options:
54
- - "--main"
55
- - README
55
+ - --main
56
+ - README
56
57
  extra_rdoc_files:
57
- - README
58
+ - README
58
59
  executables: []
60
+
59
61
  extensions: []
62
+
60
63
  requirements:
61
- - none
62
- dependencies: []
64
+ - none
65
+ dependencies: []
66
+