Last active
January 1, 2016 05:28
-
-
Save bertspaan/8098311 to your computer and use it in GitHub Desktop.
Ruby script to add Amsterdam parking data to CitySDK
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
require 'citysdk' | |
$email = ARGV[1] | |
$password = ARGV[2] | |
@api = CitySDK::API.new("api.citysdk.waag.org") | |
@api.authenticate(email, passw) | |
layer = 'test.divv.parking.zone' | |
# 1. Parse the input zones from file using json | |
data = File.open './data/parkeerzones.geojson', 'r:UTF-8' | |
zones_raw = JSON.parse data.read | |
# Process raw data make it useful for CitySDK | |
# Each node should have unique id, GeoJSON geometry and key/value data | |
zones = zones_raw["features"].map do | zone_raw | | |
{ | |
:id => "#{zone_raw["id"]}", | |
:name => "zone #{zone_raw["id"]}", | |
:geom => zone_raw["geometry"], | |
:data => | |
{ | |
:code => zone_raw["properties"]["GEBIED_COD"], | |
:date => zone_raw["properties"]["B_DAT_GEBI"] | |
} | |
} | |
end | |
#2. Put stuff in citysdk | |
begin | |
# Create layer | |
# (can be omitted if layer already exists) | |
begin | |
api.put('/layers',{:data => { | |
:name => layer, | |
:description => 'Amsterdam parking data', | |
:organization => 'Bert Spaan', | |
:category => 'mobility.test' | |
}}) | |
rescue CitySDK::HostException => e | |
#import parsed data | |
puts e.message | |
end | |
# Add parsed data to layer | |
api.set_layer layer | |
zones.each do |zone| | |
#puts zone | |
api.create_node zone | |
end | |
ensure | |
# Always release api, no matter what happens | |
api.release | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment