GeoRuby 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/README +16 -13
  2. data/lib/geo_ruby/shp4r/dbf.rb +234 -0
  3. data/lib/geo_ruby/shp4r/shp.rb +301 -0
  4. data/lib/geo_ruby/simple_features/geometry.rb +1 -0
  5. data/lib/geo_ruby.rb +1 -1
  6. data/rakefile.rb +4 -4
  7. data/test/data/point.dbf +0 -0
  8. data/test/data/point.shp +0 -0
  9. data/test/data/point.shx +0 -0
  10. data/test/data/polygon.dbf +0 -0
  11. data/test/data/polygon.shp +0 -0
  12. data/test/data/polygon.shx +0 -0
  13. data/test/data/polyline.dbf +0 -0
  14. data/test/data/polyline.shp +0 -0
  15. data/test/data/polyline.shx +0 -0
  16. data/test/test_shp.rb +77 -0
  17. data/tools/db.yml +6 -0
  18. data/tools/lib/spatial_adapter/MIT-LICENSE +7 -0
  19. data/tools/lib/spatial_adapter/README +116 -0
  20. data/tools/lib/spatial_adapter/init.rb +10 -0
  21. data/tools/lib/spatial_adapter/lib/common_spatial_adapter.rb +175 -0
  22. data/tools/lib/spatial_adapter/lib/mysql_spatial_adapter.rb +143 -0
  23. data/tools/lib/spatial_adapter/lib/post_gis_adapter.rb +333 -0
  24. data/tools/lib/spatial_adapter/rakefile.rb +35 -0
  25. data/tools/lib/spatial_adapter/test/access_mysql_test.rb +87 -0
  26. data/tools/lib/spatial_adapter/test/access_postgis_test.rb +151 -0
  27. data/tools/lib/spatial_adapter/test/common/common_mysql.rb +18 -0
  28. data/tools/lib/spatial_adapter/test/common/common_postgis.rb +19 -0
  29. data/tools/lib/spatial_adapter/test/db/database_mysql.yml +5 -0
  30. data/tools/lib/spatial_adapter/test/db/database_postgis.yml +4 -0
  31. data/tools/lib/spatial_adapter/test/find_mysql_test.rb +64 -0
  32. data/tools/lib/spatial_adapter/test/find_postgis_test.rb +65 -0
  33. data/tools/lib/spatial_adapter/test/migration_mysql_test.rb +136 -0
  34. data/tools/lib/spatial_adapter/test/migration_postgis_test.rb +170 -0
  35. data/tools/lib/spatial_adapter/test/models/models_mysql.rb +25 -0
  36. data/tools/lib/spatial_adapter/test/models/models_postgis.rb +41 -0
  37. data/tools/lib/spatial_adapter/test/schema/schema_mysql.rb +40 -0
  38. data/tools/lib/spatial_adapter/test/schema/schema_postgis.rb +69 -0
  39. data/tools/shp2sql.rb +91 -0
  40. metadata +47 -4
data/tools/shp2sql.rb ADDED
@@ -0,0 +1,91 @@
1
+ $:.unshift("lib/spatial_adapter/","lib/spatial_adapter/lib")
2
+
3
+ require 'geo_ruby'
4
+ include GeoRuby::Shp4r
5
+
6
+ require 'active_record'
7
+ require 'yaml'
8
+
9
+ def shp_field_type_2_rails(type)
10
+ case type
11
+ when 'N' then :integer
12
+ when 'F' then :float
13
+ when 'D' then :date
14
+ else
15
+ :string
16
+ end
17
+ end
18
+
19
+ def shp_geom_type_2_rails(type)
20
+ case type
21
+ when ShpType::POINT then :point
22
+ when ShpType::POLYLINE then :mlti_line_string
23
+ when ShpType::POLYGON then :multi_polygon
24
+ when ShpType::MULTIPOINT then :multi_point
25
+ end
26
+ end
27
+
28
+ #create an active record connection to a database
29
+ ActiveRecord::Base.establish_connection(YAML.load_file('db.yml'))
30
+
31
+ #add options depending on the type of database
32
+ if ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::MysqlAdapter)
33
+ options = "TYPE=MyISAM" #for MySQL <= 5.0.16 : only MyISAM tables support geometric types
34
+ else
35
+ options = ""
36
+ end
37
+
38
+ #load the correct spatial adapter depending on the type of the selected database : the required file is called init since it is originally a rails plugin and that's what the entry file for the plugin has to be called
39
+ require 'init'
40
+
41
+ #go through all the shp files passed as argument
42
+ ARGV.each do |shp|
43
+ shp_basename = File.basename(shp)
44
+ if shp_basename =~ /(.*)\.shp/
45
+ table_name = $1.downcase
46
+
47
+ #drop in case it already exists
48
+ begin
49
+ ActiveRecord::Schema.drop_table(table_name)
50
+ rescue
51
+ end
52
+
53
+ #empty block : the columns will be added afterwards
54
+ ActiveRecord::Schema.create_table(table_name,:options => options){}
55
+
56
+ ShpFile.open(shp) do |shp|
57
+ shp.fields.each do |field|
58
+ ActiveRecord::Schema.add_column(table_name, field.name.downcase, shp_field_type_2_rails(field.type))
59
+ end
60
+
61
+ #add the geometric column in the_geom
62
+ ActiveRecord::Schema.add_column(table_name,"the_geom",shp_geom_type_2_rails(shp.shp_type),:null => false)
63
+ #add an index
64
+ ActiveRecord::Schema.add_index(table_name,"the_geom",:spatial => true)
65
+
66
+ #add the data
67
+ #create a subclass of ActiveRecord::Base wired to the table just created
68
+ arTable = Class.new(ActiveRecord::Base) do
69
+ set_table_name table_name
70
+ end
71
+
72
+ #go though all the shapes in the file
73
+ shp.each do |shape|
74
+ #create an ActiveRecord object
75
+ record = arTable.new
76
+
77
+ #fill the fields
78
+ shp.fields.each do |field|
79
+ record[field.name.downcase] = shape.data[field.name]
80
+ end
81
+
82
+ #fill the geometry
83
+ record.the_geom = shape.geometry
84
+
85
+ #save to the database
86
+ record.save
87
+ end
88
+ end
89
+ end
90
+ end
91
+
metadata CHANGED
@@ -3,13 +3,13 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: GeoRuby
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.1.2
7
- date: 2007-01-05 00:00:00 +01:00
6
+ version: 1.2.0
7
+ date: 2007-01-20 00:00:00 +01:00
8
8
  summary: Ruby data holder for OGC Simple Features
9
9
  require_paths:
10
10
  - lib
11
- email: guilhem.vellut+georuby@gmail.com
12
- homepage: http://thepochisuperstarmegashow.com/projects
11
+ email: guilhem.vellut@gmail.com
12
+ homepage: http://thepochisuperstarmegashow.com/projects/
13
13
  rubyforge_project:
14
14
  description: 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)
15
15
  autorequire:
@@ -30,6 +30,8 @@ authors:
30
30
  - Guilhem Vellut
31
31
  files:
32
32
  - lib/geo_ruby.rb
33
+ - lib/geo_ruby/shp4r/dbf.rb
34
+ - lib/geo_ruby/shp4r/shp.rb
33
35
  - lib/geo_ruby/simple_features/envelope.rb
34
36
  - lib/geo_ruby/simple_features/ewkb_parser.rb
35
37
  - lib/geo_ruby/simple_features/ewkt_parser.rb
@@ -48,14 +50,55 @@ files:
48
50
  - test/test_ewkb_parser.rb
49
51
  - test/test_ewkt_parser.rb
50
52
  - test/test_georss_kml.rb
53
+ - test/test_shp.rb
51
54
  - test/test_simple_features.rb
52
55
  - README
53
56
  - MIT-LICENSE
54
57
  - rakefile.rb
58
+ - test/data/point.shp
59
+ - test/data/polygon.shp
60
+ - test/data/polyline.shp
61
+ - test/data/point.dbf
62
+ - test/data/polygon.dbf
63
+ - test/data/polyline.dbf
64
+ - test/data/point.shx
65
+ - test/data/polygon.shx
66
+ - test/data/polyline.shx
67
+ - tools/db.yml
68
+ - tools/lib/spatial_adapter/test/db/database_mysql.yml
69
+ - tools/lib/spatial_adapter/test/db/database_postgis.yml
70
+ - tools/shp2sql.rb
71
+ - tools/lib/spatial_adapter/init.rb
72
+ - tools/lib/spatial_adapter/rakefile.rb
73
+ - tools/lib/spatial_adapter/lib/common_spatial_adapter.rb
74
+ - tools/lib/spatial_adapter/lib/mysql_spatial_adapter.rb
75
+ - tools/lib/spatial_adapter/lib/post_gis_adapter.rb
76
+ - tools/lib/spatial_adapter/test/access_mysql_test.rb
77
+ - tools/lib/spatial_adapter/test/access_postgis_test.rb
78
+ - tools/lib/spatial_adapter/test/find_mysql_test.rb
79
+ - tools/lib/spatial_adapter/test/find_postgis_test.rb
80
+ - tools/lib/spatial_adapter/test/migration_mysql_test.rb
81
+ - tools/lib/spatial_adapter/test/migration_postgis_test.rb
82
+ - tools/lib/spatial_adapter/test/common/common_mysql.rb
83
+ - tools/lib/spatial_adapter/test/common/common_postgis.rb
84
+ - tools/lib/spatial_adapter/test/models/models_mysql.rb
85
+ - tools/lib/spatial_adapter/test/models/models_postgis.rb
86
+ - tools/lib/spatial_adapter/test/schema/schema_mysql.rb
87
+ - tools/lib/spatial_adapter/test/schema/schema_postgis.rb
88
+ - tools/lib/spatial_adapter
89
+ - tools/lib/spatial_adapter/lib
90
+ - tools/lib/spatial_adapter/MIT-LICENSE
91
+ - tools/lib/spatial_adapter/README
92
+ - tools/lib/spatial_adapter/test
93
+ - tools/lib/spatial_adapter/test/common
94
+ - tools/lib/spatial_adapter/test/db
95
+ - tools/lib/spatial_adapter/test/models
96
+ - tools/lib/spatial_adapter/test/schema
55
97
  test_files:
56
98
  - test/test_ewkb_parser.rb
57
99
  - test/test_ewkt_parser.rb
58
100
  - test/test_georss_kml.rb
101
+ - test/test_shp.rb
59
102
  - test/test_simple_features.rb
60
103
  rdoc_options:
61
104
  - --main