Skip to content

Instantly share code, notes, and snippets.

@spara
Created July 31, 2011 18:01
Show Gist options
  • Save spara/1117027 to your computer and use it in GitHub Desktop.
Save spara/1117027 to your computer and use it in GitHub Desktop.
Convert shapefile to elasticsearch bulk load format
#
# converts a shapefile to an elasticsearch geo_point type with this mapping:
#
# {
# "zipcode" : {
# "properties" : {
# "geometry": {
# "properties": {
# "coordinates": {
# "type": "geo_point"
# }
# }
# }
# }
# }
# }
#
# writes to stdout, can redirect to a file:
#
# ruby shapefile2elasticsearch.rb > outfile.json
#
require 'rubygems'
require 'rgeo'
require 'rgeo/shapefile'
iter = 0
RGeo::Shapefile::Reader.open('zipcode10.shp') do |file|
file.each do |record|
geom = record.geometry
attr = record.attributes
load = {
"index" => {
"_index"=>"geo",
"_type"=>"zipcode",
"_id" => iter
}
}
data = {
"geometry" => {
"coordinates" => {
"lat" => geom.y(),
"lon" => geom.x()
}
}
}
data.merge!(attr)
iter +=1
puts load.to_json
puts data.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment