Skip to content

Instantly share code, notes, and snippets.

@dylanpinn
Last active July 3, 2019 06:32
Show Gist options
  • Save dylanpinn/2eb3944d1294c9c04dd9369dec913e17 to your computer and use it in GitHub Desktop.
Save dylanpinn/2eb3944d1294c9c04dd9369dec913e17 to your computer and use it in GitHub Desktop.
CPPC: Convert KML to GeoJSON
#!/usr/bin/env bash
echo 'Converting kml files in directory'
echo 'Installing required package'
npm install -g @tmcw/togeojson-cli
for filename in ./kml/*.kml; do
code=$(awk -F/ '{print $3}' <<< $filename)
code=$(awk -F. '{print $1}' <<< $code)
togeojson $filename > geojson/$code.geojson
done
#!/usr/bin/env ruby
require 'find'
require 'json'
require 'aws-sdk'
if ARGV.count != 1
p 'Need to pass table name as argument to script.'
return
end
table_name = ARGV.first
p "Importing data into #{table_name}."
client = Aws::DynamoDB::Client.new
Find.find('./geojson') do |file_path|
unless file_path.include?('.geojson')
next
end
file = File.open(file_path, 'r')
data = file.read
data = JSON.parse(data)
file.close
code = file_path.split('/')
code = code.last.split('.').first
puts code
resp = client.put_item({
item: {
"code" => code,
"geojson" => "#{data.to_json.to_s}",
},
return_consumed_capacity: "TOTAL",
table_name: table_name,
})
puts resp
end
#!/usr/bin/env ruby
require 'csv'
if ARGV.count != 1
p 'Need to pass filename as argument to script.'
return
end
filename = ARGV.first
p "Splitting CSV: #{filename} into KML files"
table = CSV.read(filename, headers: true)
table.each do |row|
code = row['code']
geometry = row['geometry']
kml_start = '<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2"><Placemark>'
kml_end = '</Placemark></kml>'
File.open("kml/#{code}.kml", 'a') { |f| f.write("#{kml_start}#{geometry}#{kml_end}") }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment