Last active
August 29, 2015 14:04
-
-
Save radcliff/89e1c0619330c1d0b3cf to your computer and use it in GitHub Desktop.
drought-monitor-api helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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