Skip to content

Instantly share code, notes, and snippets.

@jatorre
Created January 13, 2012 18:22
Show Gist options
  • Save jatorre/1607894 to your computer and use it in GitHub Desktop.
Save jatorre/1607894 to your computer and use it in GitHub Desktop.
Ruby script to export a SimpleGeo Layer to CSV
# The following script allows to export a SimpleGeo Storage layer to a CSV
# You have to change the credentials and the layer that you want to export
# It will generate a CSV file with the name of the layer
#
# Author: Javier de la Torre ([email protected] @jatorre)
#Adapt the following to your SimpleGeo credentials. Get it from the UI.
oauth_token="token"
oauth_secret="secret"
#Layer to be exported
sg_layer="com.your.layer"
#-----------------
require 'rubygems'
require 'simplegeo'
require 'csv'
SimpleGeo::Client.set_credentials(oauth_token, oauth_secret)
CSV.open("#{sg_layer.gsub(".","_")}.csv", "wb") do |csv|
next_cursor=""
header=false
#loop until next_cursor is nil (pagination)
#the query basically look for all records within the whole world as I couldnt find a way to download a full layer
while next_cursor do
puts "."
res = SimpleGeo::Client.get_nearby_records(sg_layer,
:lat => 0,:lon => 0, :bbox=>'-90,-180,90,180', :limit=>500,:cursor=>next_cursor)
next_cursor = res[:next_cursor]
res[:records].each do |row|
#add headers
if !header
csv << ["id", "lat", "lon","created"] + row[:record].properties.keys.map(&:to_s) - ["layer"]
header=true
else
#add records
csv << [row[:record].id,row[:record].lat,row[:record].lon, row[:record].created.strftime("%Y-%m-%d %H:%M:%S")] +
row[:record].properties.values - [sg_layer]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment