Skip to content

Instantly share code, notes, and snippets.

@mnyrop
Last active March 23, 2020 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnyrop/fbe7c51133d6a8d8a8307b33414b00ed to your computer and use it in GitHub Desktop.
Save mnyrop/fbe7c51133d6a8d8a8307b33414b00ed to your computer and use it in GitHub Desktop.
require 'fileutils'
require 'json'
class Record
def initialize(path)
@source = path
@json = parse_json
@name = @source.sub '.geojson', ''
@before = @json.dig 'name'
@features = @json.dig 'features'
end
def parse_json
content = File.read @source
JSON.parse content
rescue StandardError => e
puts "JSON for source file #{@source} is invalid"
abort
end
def rename!
puts "renaming: #{@before} ~> #{@name}"
@json['name'] = @name
end
def transform_features!
@json['features'] = @features.map do |feature|
url = feature.dig('properties', 'websiteUrl')
id = url.sub 'https://geo.nyu.edu/catalog/', ''
feature['properties']['available'] = false if @name.eql? id
feature
end
end
def write_to_file!
outfile = "#{OUTPUT_DIR}/#{@source}"
pretty = JSON.pretty_generate @json
File.open(outfile, 'w') { |f| f.write pretty }
end
end
OUTPUT_DIR = 'output'
FILES = Dir.glob("*\.geojson")
puts "Found #{FILES.length} .geojson records\n\n"
FileUtils.mkdir_p OUTPUT_DIR unless Dir.exist? OUTPUT_DIR
FILES.each do |path|
record = Record.new path
record.rename!
record.transform_features!
record.write_to_file!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment