Skip to content

Instantly share code, notes, and snippets.

@radcliff
Last active August 29, 2015 14:04
Show Gist options
  • Save radcliff/89e1c0619330c1d0b3cf to your computer and use it in GitHub Desktop.
Save radcliff/89e1c0619330c1d0b3cf to your computer and use it in GitHub Desktop.
drought-monitor-api helpers
module GeojsonHelper
# this method takes in a RGeo geometry object and returns a GeoJSON object
def to_geojson(geometry_collection)
geojson_object = {}
geojson_object["type"] = "FeatureCollection"
geojson_object["features"] = []
geometry_collection.each_with_index do |record, index|
feature = RGeo::GeoJSON::Feature.new(record.shape, index, { :DM => record.dm } )
geojson_object["features"].push(RGeo::GeoJSON.encode(feature))
end
return geojson_object
end
# this method takes in a GeoJSON object and saves it to disk
def json_to_file(object, file_path_and_name)
open("#{file_path_and_name}.json", "wb") do |file|
file.write(object.to_json)
true
end
end
end
module TopojsonHelper
# this method converts a GeoJSON object to a TopoJSON object
def convert_to_topojson(geojson)
json_to_file(geojson, "drought")
`topojson -o drought-topo.json drought.json` # executes topojson command (node package) in a separate thread
topojson = File.read ('drought-topo.json')
`rm drought.json` # remove temp files
`rm drought-topo.json` # remove temp files
return topojson
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment