Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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