georuby_remake 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. data/.gitignore +6 -0
  2. data/History.txt +4 -0
  3. data/LICENSE +21 -0
  4. data/README.txt +118 -0
  5. data/Rakefile +47 -0
  6. data/VERSION +1 -0
  7. data/georuby_remake.gemspec +132 -0
  8. data/lib/geo_ruby.rb +5 -0
  9. data/lib/geo_ruby/gpx4r.rb +9 -0
  10. data/lib/geo_ruby/gpx4r/gpx.rb +104 -0
  11. data/lib/geo_ruby/shp4r.rb +57 -0
  12. data/lib/geo_ruby/shp4r/dbf.rb +38 -0
  13. data/lib/geo_ruby/shp4r/shp_file.rb +297 -0
  14. data/lib/geo_ruby/shp4r/shp_record.rb +14 -0
  15. data/lib/geo_ruby/shp4r/shp_transaction.rb +345 -0
  16. data/lib/geo_ruby/simple_features.rb +20 -0
  17. data/lib/geo_ruby/simple_features/envelope.rb +167 -0
  18. data/lib/geo_ruby/simple_features/ewkb_parser.rb +216 -0
  19. data/lib/geo_ruby/simple_features/ewkt_parser.rb +336 -0
  20. data/lib/geo_ruby/simple_features/geometry.rb +230 -0
  21. data/lib/geo_ruby/simple_features/geometry_collection.rb +136 -0
  22. data/lib/geo_ruby/simple_features/geometry_factory.rb +81 -0
  23. data/lib/geo_ruby/simple_features/georss_parser.rb +135 -0
  24. data/lib/geo_ruby/simple_features/helper.rb +18 -0
  25. data/lib/geo_ruby/simple_features/line_string.rb +206 -0
  26. data/lib/geo_ruby/simple_features/linear_ring.rb +12 -0
  27. data/lib/geo_ruby/simple_features/multi_line_string.rb +51 -0
  28. data/lib/geo_ruby/simple_features/multi_point.rb +46 -0
  29. data/lib/geo_ruby/simple_features/multi_polygon.rb +52 -0
  30. data/lib/geo_ruby/simple_features/point.rb +364 -0
  31. data/lib/geo_ruby/simple_features/polygon.rb +157 -0
  32. data/spec/data/gpx/fells_loop.gpx +1077 -0
  33. data/spec/data/gpx/long.gpx +1642 -0
  34. data/spec/data/gpx/long.kml +31590 -0
  35. data/spec/data/gpx/long.nmea +2220 -0
  36. data/spec/data/gpx/short.gpx +13634 -0
  37. data/spec/data/gpx/short.kml +130 -0
  38. data/spec/data/gpx/tracktreks.gpx +706 -0
  39. data/spec/data/multipoint.dbf +0 -0
  40. data/spec/data/multipoint.shp +0 -0
  41. data/spec/data/multipoint.shx +0 -0
  42. data/spec/data/point.dbf +0 -0
  43. data/spec/data/point.shp +0 -0
  44. data/spec/data/point.shx +0 -0
  45. data/spec/data/polygon.dbf +0 -0
  46. data/spec/data/polygon.shp +0 -0
  47. data/spec/data/polygon.shx +0 -0
  48. data/spec/data/polyline.dbf +0 -0
  49. data/spec/data/polyline.shp +0 -0
  50. data/spec/data/polyline.shx +0 -0
  51. data/spec/geo_ruby/gpx4r/gpx_spec.rb +106 -0
  52. data/spec/geo_ruby/shp4r/shp_spec.rb +240 -0
  53. data/spec/geo_ruby/simple_features/envelope_spec.rb +45 -0
  54. data/spec/geo_ruby/simple_features/ewkb_parser_spec.rb +158 -0
  55. data/spec/geo_ruby/simple_features/ewkt_parser_spec.rb +179 -0
  56. data/spec/geo_ruby/simple_features/geometry_collection_spec.rb +55 -0
  57. data/spec/geo_ruby/simple_features/geometry_factory_spec.rb +11 -0
  58. data/spec/geo_ruby/simple_features/geometry_spec.rb +32 -0
  59. data/spec/geo_ruby/simple_features/georss_parser_spec.rb +218 -0
  60. data/spec/geo_ruby/simple_features/line_string_spec.rb +245 -0
  61. data/spec/geo_ruby/simple_features/linear_ring_spec.rb +14 -0
  62. data/spec/geo_ruby/simple_features/multi_line_string_spec.rb +54 -0
  63. data/spec/geo_ruby/simple_features/multi_point_spec.rb +35 -0
  64. data/spec/geo_ruby/simple_features/multi_polygon_spec.rb +50 -0
  65. data/spec/geo_ruby/simple_features/point_spec.rb +356 -0
  66. data/spec/geo_ruby/simple_features/polygon_spec.rb +108 -0
  67. data/spec/geo_ruby_spec.rb +27 -0
  68. data/spec/spec.opts +6 -0
  69. data/spec/spec_helper.rb +65 -0
  70. metadata +185 -0
@@ -0,0 +1,6 @@
1
+ .DS_Store
2
+ pkg/*.gem
3
+ pkg/*.tgz
4
+ pkg/**/*
5
+ coverage/*
6
+ doc/*
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-08-14
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ GeoRuby License
2
+
3
+ Copyright (c) 2006 Guilhem Vellut <guilhem.vellut+georuby@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to
7
+ deal in the Software without restriction, including without limitation the
8
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9
+ permit persons to whom the Software is furnished to do so, subject to the
10
+ following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,118 @@
1
+ # GeoRuby
2
+
3
+ This is a remake of [nofxx/georuby](http://github.com/nofxx/georuby), enabling autoloading
4
+
5
+ It is intended as a holder for data returned from PostGIS and the Spatial Extensions of MySql. 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). It also supports various output and input formats (GeoRSS, KML, Shapefile).
6
+
7
+ ## Available data types
8
+
9
+ The following geometric data types are provided :
10
+ - Point
11
+ - Line string
12
+ - Linear ring
13
+ - Polygon
14
+ - Multi point
15
+ - Multi line string
16
+ - Multi polygon
17
+ - Geometry collection
18
+
19
+ They can be in 2D, 3DZ, 3DM, and 4D.
20
+
21
+ On top of this an Envelope class is available, to contain the bounding box of a geometry.
22
+
23
+ ## Input and output
24
+
25
+ These geometries can be input and output in WKB/EWKB/WKT/EWKT format (as well as the related HexWKB and HexEWKB formats). HexEWKB and WKB are the default form under which geometric data is returned respectively from PostGIS and MySql.
26
+
27
+ GeoRSS Simple, GeoRSS W3CGeo, GeoRSS GML can also be input and output. Note that they will not output valid RSS, but just the part strictly concerning the geometry as outlined in http://www.georss.org/1/ . Since the model does not allow multiple geometries, for geometry collections, only the first geometry will be output. Similarly, for polygons, the GeoRSS output will only contain the outer ring. As for W3CGeo output, only points can be output, so the first point of the geometry is chosen. By default the Simple format is output. Envelope can also be output in these formats: The box geometric type is chosen (except for W3CGeo, where the center of the envelope is chose). These formats can also be input and a GeoRuby geometry will be created. Note that it will not read a valid RSS file, only a geometry string.
28
+
29
+ On top of that, there is now support for KML output and input. As for GeoRSS, a valid KML file will not be output, but only the geometric data. Options <tt>:id</tt>, <tt>:extrude</tt>, <tt>:tesselate</tt> and <tt>:altitude_mode</tt> can be given. Note that if the <tt>:altitude_mode</tt> option is not passed or set to <tt>clampToGround</tt>, the altitude data will not be output even if present. Envelopes output a LatLonAltBox instead of a geometry. For the output, the following geometric types are supported : Point, LineString, Polygon.
30
+
31
+
32
+ ## SHP reading et writing
33
+
34
+ Georuby has support for reading ESRI shapefiles (http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf). A tool called <tt>shp2sql.rb</tt> is also provided : it shows how to use the SHP reading functionality together with the spatial adapter plugin for Rails to import spatial features into MySQL and PostGIS.
35
+
36
+ Here is an example of Shapefile reading, that goes through all the geometries in a file and disaply the values of the attributes :
37
+
38
+ require 'geo_ruby/shp'
39
+
40
+ ShpFile.open(shpfile) do |shp|
41
+ shp.each do |shape|
42
+ geom = shape.geometry #a GeoRuby SimpleFeature
43
+ att_data = shape.data #a Hash
44
+ shp.fields.each do |field|
45
+ puts att_data[field.name]
46
+ end
47
+ end
48
+ end
49
+
50
+ Support for ESRI shapefile creation and modification has been added as well. New shapefiles can be created given a geometry type and specifications for the DBF fields. Data can be added and removed from an existing shapefile. An update operation is also provided for convenience : it just performs a 'delete' and an 'add', which means the index of the modified record will change. Note that once a shapefile has been created, GeoRuby does not allow the modification of the schema (it will probably be done in a subsequent version).
51
+
52
+ Here is an example of how to create a new Shapefile with 2 fields :
53
+
54
+ shpfile = ShpFile.create('hello.shp',ShpType::POINT,[Dbf::Field.new("Hoyoyo","C",10),Dbf::Field.new("Boyoul","N",10,0)])
55
+
56
+ The file is then open for reading and writing.
57
+
58
+ Here is an example of how to write to a shapefile (created or not with GeoRuby) :
59
+
60
+ shpfile = ShpFile.open('places.shp')
61
+ shpfile.transaction do |tr|
62
+ tr.add(ShpRecord.new(Point.from_x_y(123.4,123.4),'Hoyoyo' => "AEZ",'Bouyoul' => 45))
63
+ tr.update(4,ShpRecord.new(Point.from_x_y(-16.67,16.41),'Hoyoyo' => "EatMe",'Bouyoul' => 42))
64
+ tr.delete(1)
65
+ end
66
+ shpfile.close
67
+
68
+ Note the transaction is just there so the operations on the files can be buffered. Nothing happens on the original files until the block has finished executing. Calling <tt>tr.rollback</tt> at anytime during the execution will prevent the modifications.
69
+
70
+ Also currently, error reporting is minimal and it has not been tested that thoroughly so caveat emptor and backup before performing any destructive operation.
71
+
72
+
73
+ === GPX Reading
74
+
75
+ You can read and convert GPX Files to LineString/Polygon:
76
+
77
+ gpxfile = GpxFile.open(tour.gpx')
78
+ gpxfile.as_line_string
79
+ => GeoRuby::SimpleFeatures::LineString..
80
+
81
+
82
+ === Installation
83
+
84
+ To install the latest version, just type :
85
+
86
+ gem install nofxx-georuby
87
+
88
+
89
+ === Changes since the last version
90
+
91
+ - Writing of ESRI shapefiles
92
+ - Reading of ESRI shapefiles
93
+ - Addition of a small tool to import spatial features in MySQL and PostGIS from a SHP file
94
+
95
+
96
+ === Coming in the next versions
97
+
98
+ - Schema modification of existing shapefiles
99
+ - More error reporting when writing shapefiles
100
+ - More tests on writing shapefiles ; tests on real-world shapefiles
101
+ - Better shp2sql import tool
102
+ - Documentation
103
+
104
+
105
+ === Acknowledgement
106
+
107
+ The SHP reading part uses the DBF library (http://rubyforge.org/projects/dbf/) by Keith Morrison (http://infused.org).
108
+ Thanks also to Pramukta Kumar and Pete Schwamb for their contributions.
109
+
110
+
111
+ === License
112
+
113
+ GeoRuby is released under the MIT license.
114
+
115
+ === Support
116
+
117
+ Any questions, enhancement proposals, bug notifications or corrections
118
+ can be sent to mailto:guilhem.vellut@gmail.com.
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "georuby_remake"
8
+ gem.summary = "Ruby data holder for OGC Simple Features"
9
+ gem.description = "GeoRuby provides geometric data types from the OGC 'Simple Features' specification."
10
+ gem.email = "juniorz@gmail.com"
11
+ gem.homepage = "http://github.com/juniorz/georuby_remake"
12
+ gem.authors = ["Guilhem Vellut", "Marcos Piccinini"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "dbf", ">= 1.1.2"
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "geo_ruby #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,132 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{georuby_remake}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Guilhem Vellut", "Marcos Piccinini"]
12
+ s.date = %q{2010-09-20}
13
+ s.description = %q{GeoRuby provides geometric data types from the OGC 'Simple Features' specification.}
14
+ s.email = %q{juniorz@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.txt"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "History.txt",
22
+ "LICENSE",
23
+ "README.txt",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "georuby_remake.gemspec",
27
+ "lib/geo_ruby.rb",
28
+ "lib/geo_ruby/gpx4r.rb",
29
+ "lib/geo_ruby/gpx4r/gpx.rb",
30
+ "lib/geo_ruby/shp4r.rb",
31
+ "lib/geo_ruby/shp4r/dbf.rb",
32
+ "lib/geo_ruby/shp4r/shp_file.rb",
33
+ "lib/geo_ruby/shp4r/shp_record.rb",
34
+ "lib/geo_ruby/shp4r/shp_transaction.rb",
35
+ "lib/geo_ruby/simple_features.rb",
36
+ "lib/geo_ruby/simple_features/envelope.rb",
37
+ "lib/geo_ruby/simple_features/ewkb_parser.rb",
38
+ "lib/geo_ruby/simple_features/ewkt_parser.rb",
39
+ "lib/geo_ruby/simple_features/geometry.rb",
40
+ "lib/geo_ruby/simple_features/geometry_collection.rb",
41
+ "lib/geo_ruby/simple_features/geometry_factory.rb",
42
+ "lib/geo_ruby/simple_features/georss_parser.rb",
43
+ "lib/geo_ruby/simple_features/helper.rb",
44
+ "lib/geo_ruby/simple_features/line_string.rb",
45
+ "lib/geo_ruby/simple_features/linear_ring.rb",
46
+ "lib/geo_ruby/simple_features/multi_line_string.rb",
47
+ "lib/geo_ruby/simple_features/multi_point.rb",
48
+ "lib/geo_ruby/simple_features/multi_polygon.rb",
49
+ "lib/geo_ruby/simple_features/point.rb",
50
+ "lib/geo_ruby/simple_features/polygon.rb",
51
+ "spec/data/gpx/fells_loop.gpx",
52
+ "spec/data/gpx/long.gpx",
53
+ "spec/data/gpx/long.kml",
54
+ "spec/data/gpx/long.nmea",
55
+ "spec/data/gpx/short.gpx",
56
+ "spec/data/gpx/short.kml",
57
+ "spec/data/gpx/tracktreks.gpx",
58
+ "spec/data/multipoint.dbf",
59
+ "spec/data/multipoint.shp",
60
+ "spec/data/multipoint.shx",
61
+ "spec/data/point.dbf",
62
+ "spec/data/point.shp",
63
+ "spec/data/point.shx",
64
+ "spec/data/polygon.dbf",
65
+ "spec/data/polygon.shp",
66
+ "spec/data/polygon.shx",
67
+ "spec/data/polyline.dbf",
68
+ "spec/data/polyline.shp",
69
+ "spec/data/polyline.shx",
70
+ "spec/geo_ruby/gpx4r/gpx_spec.rb",
71
+ "spec/geo_ruby/shp4r/shp_spec.rb",
72
+ "spec/geo_ruby/simple_features/envelope_spec.rb",
73
+ "spec/geo_ruby/simple_features/ewkb_parser_spec.rb",
74
+ "spec/geo_ruby/simple_features/ewkt_parser_spec.rb",
75
+ "spec/geo_ruby/simple_features/geometry_collection_spec.rb",
76
+ "spec/geo_ruby/simple_features/geometry_factory_spec.rb",
77
+ "spec/geo_ruby/simple_features/geometry_spec.rb",
78
+ "spec/geo_ruby/simple_features/georss_parser_spec.rb",
79
+ "spec/geo_ruby/simple_features/line_string_spec.rb",
80
+ "spec/geo_ruby/simple_features/linear_ring_spec.rb",
81
+ "spec/geo_ruby/simple_features/multi_line_string_spec.rb",
82
+ "spec/geo_ruby/simple_features/multi_point_spec.rb",
83
+ "spec/geo_ruby/simple_features/multi_polygon_spec.rb",
84
+ "spec/geo_ruby/simple_features/point_spec.rb",
85
+ "spec/geo_ruby/simple_features/polygon_spec.rb",
86
+ "spec/geo_ruby_spec.rb",
87
+ "spec/spec.opts",
88
+ "spec/spec_helper.rb"
89
+ ]
90
+ s.homepage = %q{http://github.com/juniorz/georuby_remake}
91
+ s.rdoc_options = ["--charset=UTF-8"]
92
+ s.require_paths = ["lib"]
93
+ s.rubygems_version = %q{1.3.7}
94
+ s.summary = %q{Ruby data holder for OGC Simple Features}
95
+ s.test_files = [
96
+ "spec/geo_ruby_spec.rb",
97
+ "spec/spec_helper.rb",
98
+ "spec/geo_ruby/shp4r/shp_spec.rb",
99
+ "spec/geo_ruby/gpx4r/gpx_spec.rb",
100
+ "spec/geo_ruby/simple_features/geometry_factory_spec.rb",
101
+ "spec/geo_ruby/simple_features/multi_polygon_spec.rb",
102
+ "spec/geo_ruby/simple_features/multi_point_spec.rb",
103
+ "spec/geo_ruby/simple_features/geometry_collection_spec.rb",
104
+ "spec/geo_ruby/simple_features/multi_line_string_spec.rb",
105
+ "spec/geo_ruby/simple_features/envelope_spec.rb",
106
+ "spec/geo_ruby/simple_features/polygon_spec.rb",
107
+ "spec/geo_ruby/simple_features/geometry_spec.rb",
108
+ "spec/geo_ruby/simple_features/linear_ring_spec.rb",
109
+ "spec/geo_ruby/simple_features/point_spec.rb",
110
+ "spec/geo_ruby/simple_features/georss_parser_spec.rb",
111
+ "spec/geo_ruby/simple_features/ewkb_parser_spec.rb",
112
+ "spec/geo_ruby/simple_features/line_string_spec.rb",
113
+ "spec/geo_ruby/simple_features/ewkt_parser_spec.rb"
114
+ ]
115
+
116
+ if s.respond_to? :specification_version then
117
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
118
+ s.specification_version = 3
119
+
120
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
121
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
122
+ s.add_development_dependency(%q<dbf>, [">= 1.1.2"])
123
+ else
124
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
125
+ s.add_dependency(%q<dbf>, [">= 1.1.2"])
126
+ end
127
+ else
128
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
129
+ s.add_dependency(%q<dbf>, [">= 1.1.2"])
130
+ end
131
+ end
132
+
@@ -0,0 +1,5 @@
1
+ module GeoRuby
2
+ autoload :SimpleFeatures, 'geo_ruby/simple_features'
3
+ autoload :Shp4r, 'geo_ruby/shp4r'
4
+ autoload :Gpx4r, 'geo_ruby/gpx4r'
5
+ end
@@ -0,0 +1,9 @@
1
+ require "rubygems"
2
+ require "nokogiri"
3
+
4
+ module GeoRuby::Gpx4r
5
+ autoload :GpxFile, 'geo_ruby/gpx4r/gpx'
6
+
7
+ class MalformedGpxException < StandardError
8
+ end
9
+ end
@@ -0,0 +1,104 @@
1
+ #An interface to GPX files
2
+ class GeoRuby::Gpx4r::GpxFile
3
+ attr_reader :record_count, :file_root #:xmin, :ymin, :xmax, :ymax, :zmin, :zmax, :mmin, :mmax, :file_length
4
+
5
+ include Enumerable
6
+
7
+ # Opens a GPX file. Both "abc.shp" and "abc" are accepted.
8
+ def initialize(file, *opts) #with_z = true, with_m = true)
9
+ @file_root = file.gsub(/\.gpx$/i,"")
10
+ raise MalformedGpxException.new("Missing GPX File") unless
11
+ File.exists? @file_root + ".gpx"
12
+ @points, @envelope = [], nil
13
+ @gpx = File.open(@file_root + ".gpx", "rb")
14
+ opt = opts.inject({}) { |o, h| h.merge(o) }
15
+ parse_file(opt[:with_z], opt[:with_m])
16
+ end
17
+
18
+ #force the reopening of the files compsing the shp. Close before calling this.
19
+ def reload!
20
+ initialize(@file_root)
21
+ end
22
+
23
+ #opens a GPX "file". If a block is given, the GpxFile object is yielded to it and is closed upon return. Else a call to <tt>open</tt> is equivalent to <tt>GpxFile.new(...)</tt>.
24
+ def self.open(file, *opts)
25
+ gpxfile = GpxFile.new(file, *opts)
26
+ if block_given?
27
+ yield gpxfile
28
+ gpxfile.close
29
+ else
30
+ gpxfile
31
+ end
32
+ end
33
+
34
+ #Closes a gpxfile
35
+ def close
36
+ @gpx.close
37
+ end
38
+
39
+ #Tests if the file has no record
40
+ def empty?
41
+ record_count == 0
42
+ end
43
+
44
+ #Goes through each record
45
+ def each
46
+ (0...record_count).each do |i|
47
+ yield get_record(i)
48
+ end
49
+ end
50
+ alias :each_record :each
51
+
52
+ #Returns record +i+
53
+ def [](i)
54
+ get_record(i)
55
+ end
56
+
57
+ #Returns all the records
58
+ def records
59
+ @points
60
+ end
61
+
62
+ # Return the GPX file as LineString
63
+ def as_line_string
64
+ LineString.from_points(@points)
65
+ end
66
+ alias :as_polyline :as_line_string
67
+
68
+ # Return the GPX file as a Polygon
69
+ # If the GPX isn't closed, a line from the first
70
+ # to the last point will be created to close it.
71
+ def as_polygon
72
+ Polygon.from_points([@points[0] == @points[-1] ? @points : @points.push(@points[0].clone)])
73
+ end
74
+
75
+ # Return GPX Envelope
76
+ def envelope
77
+ @envelope ||= as_polygon.envelope
78
+ end
79
+
80
+ private
81
+
82
+ def get_record(i)
83
+ @points[i]
84
+ end
85
+
86
+ # wpt => waypoint => TODO?
87
+ # rte(pt) => route
88
+ # trk(pt) => track /
89
+ def parse_file(with_z, with_m)
90
+ data = @gpx.read
91
+ @file_mode = data =~ /trkpt/ ? "//trkpt" : "//rtept"
92
+ Nokogiri.HTML(data).search(@file_mode).each do |tp|
93
+ z = z.inner_text.to_i if with_z && z = tp.at("ele")
94
+ m = m.inner_text if with_m && m = tp.at("time")
95
+ @points << Point.from_coordinates([tp["lon"].to_f, tp["lat"].to_f, z, m],nil,with_z, with_m)
96
+ end
97
+ close
98
+ @record_count = @points.length
99
+ rescue => e
100
+ raise MalformedGpxException.new("Bad GPX. Error: #{e}")
101
+ # trackpoint.at("gpxdata:hr").nil? # heartrate
102
+ end
103
+
104
+ end