shp2geocouch 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
File without changes
@@ -4,7 +4,9 @@ This is an executable rubygem that creates "GeoCouch":http://www.github.com/vmx/
4
4
 
5
5
  h2. Installation
6
6
 
7
- You will need access to a GeoCouch server. For instructions on setting up your own local GeoCouch, check out "my related blog post":http://maxogden.com/#/2010-08-14-loading-shapefiles-into-geocouch.html.
7
+ This has been verified to work on Unix/MacOSX. Your mileage may vary if you are on Windows.
8
+
9
+ You will need access to a GeoCouch server. For instructions on setting up your own local GeoCouch, check out "my related blog post":http://maxogden.com/#/blog/installing-geocouch. The easiest way to get GeoCouch is the "binary distribution from Couchbase":http://www.couchbase.com/downloads.
8
10
 
9
11
  Install dependencies:
10
12
  @unzip@, @sed@, @ogr2ogr@
@@ -26,10 +28,12 @@ h2. Usage
26
28
  @shp2geocouch <path_to_shapefile> [geocouch-url (optional, default: http://localhost:5984/zip_filename)]@
27
29
 
28
30
  example: @shp2geocouch /sweet_geo_data/Portland_Oregon_Public_Libraries.shp@
29
- another possible example: @shp2geocouch /zipped_shapfiles/US_Railroads.zip http://myusername.couchone.com/railroads@
31
+ another possible example: @shp2geocouch /zipped_shapfiles/US_Railroads.zip http://admin:password@helloworld.couchone.com/railroads@
30
32
 
31
33
  Once completed, you can run bounding box queries against your data! Visit the following link to receive a dump of your entire dataset as GeoJSON (a bounding box that covers the entire planet):
32
- @http://localhost:5984/your_db_name/_design/geojson/_spatial/points?bbox=-180,-90,180,90@
34
+ @http://localhost:5984/your_db_name/_design/geo/_spatial/ful?bbox=-180,-90,180,90@
35
+
36
+ @shp2geocouch@ will try to install "geocouch-utils":http://github.com/maxogden/geocouch-utils into your database by replicating it from "my public couch":http://max.couchone.com/apps/_design/geo. @geocouch-utils@ includes a basic web based map browser that will allow you to view the data from your Shapefile.
33
37
 
34
38
  If you uploaded a large dataset, you may experience a delay on the first ever spatial query as GeoCouch builds it's spatial index. You can check the status of the indexer by visiting @http://localhost:5984/_utils/status.html@
35
39
 
@@ -39,6 +43,7 @@ h3. Arguments:
39
43
  * @-v@ for verbose
40
44
  * @--no-cleanup@ if you want @shp2geocouch@ to leave you a copy of unzipped Shapefile and converted JSON.
41
45
  * @--chunksize@ to customize the number of docs that it sends in each PUT to CouchDB's @_bulk_docs@ interface (default is 50)
46
+ * @-i@ to set the CouchDB IDs (@_id@) based on the given column in the Shapefile. The column is assumed to be unique; no error checking is performed.
42
47
 
43
48
  h2. What?
44
49
 
data/Rakefile CHANGED
File without changes
data/VERSION CHANGED
File without changes
@@ -1,13 +1,16 @@
1
1
  #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
2
4
  require 'httparty'
3
5
  require 'couchrest'
4
6
  require 'optparse'
5
7
  require 'iconv'
6
8
  require 'net/http'
7
9
  require 'uri'
10
+ require 'json'
8
11
 
9
12
  class ShapefileToGeoCouch
10
- attr_accessor :path, :extension, :name, :database_url, :cleanup, :verbose, :chunksize, :uri
13
+ attr_accessor :path, :extension, :name, :database_url, :cleanup, :verbose, :chunksize, :uri, :id
11
14
 
12
15
  def initialize(options)
13
16
  set_accessors(options)
@@ -78,19 +81,40 @@ class ShapefileToGeoCouch
78
81
  %x!ogr2ogr -t_srs EPSG:4326 -a_srs EPSG:4326 -f "GeoJSON" #{json} #{shapefile}!
79
82
  puts "Reformatting json for bulk import, saving as #{bulk}..." if @verbose
80
83
  %x!sed -e '/^\"type\": \"FeatureCollection\",$/d' -e '/^\"features\": \\[$/d' -e '/^{$/d' -e '/^,$/d' -e '/^}$/d' -e '/^]$/d' -e '/^$/d' -e 's/$/,/' #{json} > #{bulk}!
84
+
85
+ # the user wants to use a predefined ID field
86
+ if @id
87
+ puts "Inserting _id attributes based on col '#{id}'"
88
+ end
81
89
  end
82
-
90
+
83
91
  def post(string)
84
92
  ic = Iconv.new('UTF-8//IGNORE', 'UTF-8') # disregard all UTF8 characters
85
93
  valid_string = ic.iconv(string[0..-3] + ' ')[0..-3] # http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/
86
- HTTParty.post(@database_url + '/_bulk_docs', :body => '{"docs": [' + valid_string + "]}", :headers => {'content-type' => "application/json"})
94
+ body = '{"docs": [' + valid_string + ']}'
95
+ HTTParty.post(@database_url + '/_bulk_docs', :body => body, :headers => {'content-type' => "application/json"})
87
96
  end
88
-
97
+
89
98
  def upload
90
99
  puts "Bulk loading data into GeoCouch... view progress at #{couch_url}/_utils" if @verbose
91
100
  group = []
92
101
  length = File.open(bulk).lines.count - 1
93
102
  File.open(bulk).each_with_index do |line, index|
103
+ # check that the line is non-blank and not invalid JSON (e.g. rogue
104
+ # punctuation characters)
105
+ stripped_line = line.strip
106
+ if stripped_line.empty? or stripped_line == ","
107
+ next
108
+ end
109
+ # create an _id attr if requested
110
+ if @id
111
+ # The slice is to get rid of ,\n at the end of the line
112
+ parsedLine = JSON.parse(line[0...-2])
113
+ # Cast it to a string, so that Couch will take an integer id
114
+ parsedLine['_id'] = parsedLine['properties'][@id].to_s
115
+ line = parsedLine.to_json + ",\n"
116
+ end
117
+
94
118
  if (index % @chunksize == 0)
95
119
  post(group.join(''))
96
120
  group = [line]
@@ -155,11 +179,16 @@ OptionParser.new do |opts|
155
179
  opts.on('-v') do |v|
156
180
  defaults[:verbose] = true
157
181
  end
182
+
183
+ opts.on('-i [ID]', 'ID column to use for the database load') do |id|
184
+ defaults[:id] = id
185
+ end
158
186
  end.parse!
159
187
 
160
188
  raise "You must specify a Shapefile to convert." if ARGV[0].nil?
161
189
 
162
- extension = ARGV[0].split('.')[1]
190
+ # Use -1, not 1, in case user has specified ../
191
+ extension = ARGV[0].split('.')[-1]
163
192
  raise "You must specify a .shp or a .zip" unless extension =~ /zip|shp/i
164
193
 
165
194
  name = ARGV[0].split('/')[-1].split('.')[0]
@@ -167,4 +196,4 @@ name = ARGV[0].split('/')[-1].split('.')[0]
167
196
  options = {:path => ARGV[0], :extension => extension}.merge(defaults)
168
197
  options[:database_url] = ARGV[1] || "http://localhost:5984/#{name.downcase}"
169
198
 
170
- ShapefileToGeoCouch.new(options)
199
+ ShapefileToGeoCouch.new(options)
Binary file
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{shp2geocouch}
8
- s.version = "0.0.8"
8
+ s.version = "0.0.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Max Ogden"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shp2geocouch
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 8
10
- version: 0.0.8
9
+ - 9
10
+ version: 0.0.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Max Ogden
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-23 00:00:00 -07:00
19
- default_executable: shp2geocouch
18
+ date: 2011-04-23 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: httparty
@@ -63,7 +62,6 @@ files:
63
62
  - bin/shp2geocouch
64
63
  - pkg/shp2geocouch-0.0.8.gem
65
64
  - shp2geocouch.gemspec
66
- has_rdoc: true
67
65
  homepage: http://github.com/maxogden/shp2geocouch
68
66
  licenses: []
69
67
 
@@ -93,9 +91,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
91
  requirements: []
94
92
 
95
93
  rubyforge_project:
96
- rubygems_version: 1.4.2
94
+ rubygems_version: 1.8.24
97
95
  signing_key:
98
96
  specification_version: 3
99
97
  summary: rubygem that converts Shapefiles into GeoCouch databases
100
98
  test_files: []
101
99
 
100
+ has_rdoc: