Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShapeLine Collection #56

Merged
merged 5 commits into from
Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/gtfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require 'gtfs/zip_source'
require 'gtfs/url_source'
require 'gtfs/service_period'
require 'gtfs/shape_line'
require 'gtfs/wide_time'
require 'gtfs/fetch'

Expand Down
39 changes: 39 additions & 0 deletions lib/gtfs/shape_line.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module GTFS
class ShapeLine
include Enumerable
attr_accessor :shape_id, :shapes

def self.from_shapes(shapes)
self.new(shapes.first.shape_id, shapes.sort_by { |i| i.shape_pt_sequence.to_i })
end

def initialize(shape_id=nil, shapes=nil)
@shape_id = shape_id
@shapes = shapes || []
end

def shape(index)
@shapes[index]
end

def each_shape(&block)
@shapes.each(&block)
end

def coordinates
@shapes.map { |i| [i.shape_pt_lon.to_f, i.shape_pt_lat.to_f] }
end

def shape_dist_traveled
@shapes.map { |i| i.shape_dist_traveled.to_f }
end

def each(&block)
coordinates.each(&block)
end

def size
@shapes.size
end
end
end
4 changes: 1 addition & 3 deletions lib/gtfs/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ def load_shapes
shapes_merge = Hash.new { |h,k| h[k] = [] }
self.each_shape { |e| shapes_merge[e.shape_id] << e }
shapes_merge.each do |k,v|
@shape_lines[k] = v
.sort_by { |i| i.shape_pt_sequence.to_i }
.map { |i| [i.shape_pt_lon.to_f, i.shape_pt_lat.to_f] }
@shape_lines[k] = ShapeLine.from_shapes(v)
end
@shape_lines
end
Expand Down
90 changes: 90 additions & 0 deletions spec/gtfs/shape_line_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe GTFS::ShapeLine do
let(:valid_local_source) do
File.expand_path(File.dirname(__FILE__) + '/../fixtures/valid_gtfs.zip')
end
let(:shapes) {[
GTFS::Shape.new(shape_id: '63542', shape_pt_sequence: '5', shape_pt_lon: "-76.662018", shape_pt_lat: "39.350739", shape_dist_traveled: "0.1631"),
GTFS::Shape.new(shape_id: '63542', shape_pt_sequence: '4', shape_pt_lon: "-76.661949", shape_pt_lat: "39.35067", shape_dist_traveled: "0.1539"),
GTFS::Shape.new(shape_id: '63542', shape_pt_sequence: '3', shape_pt_lon: "-76.660767", shape_pt_lat: "39.350719", shape_dist_traveled: "0.0517"),
GTFS::Shape.new(shape_id: '63542', shape_pt_sequence: '2', shape_pt_lon: "-76.6605", shape_pt_lat: "39.350742", shape_dist_traveled: "0.0286"),
GTFS::Shape.new(shape_id: '63542', shape_pt_sequence: '1', shape_pt_lon: "-76.660172", shape_pt_lat: "39.350792", shape_dist_traveled: "0.0"),
]}
let(:shape_line) { GTFS::ShapeLine.from_shapes(shapes) }

context 'load_shapes' do
let(:data_source) {valid_local_source}
let(:opts) {{}}
it 'has a ShapeLine' do
source = GTFS::ZipSource.new(data_source, opts)
source.load_shapes
sl = source.shape_line('63542')
sl.shape_id.should eq('63542')
end
end

context '.from_shapes' do
it 'creates ShapeLine from shapes' do
sl = GTFS::ShapeLine.from_shapes(shapes)
sl.shape_id.should eq('63542')
sl.coordinates.size.should eq(5)
sl.coordinates[0][0].should be_within(0.01).of(-76.660172)
sl.coordinates[0][1].should be_within(0.01).of(39.350792)
sl.shape_dist_traveled.size.should eq(5)
sl.shape_dist_traveled[0].should be_within(0.01).of(0.0)
sl.shape_dist_traveled[-1].should be_within(0.01).of(0.1631)
end
end

context '#size' do
it 'returns shape size' do
shape_line.size.should eq(5)
end
end

context '#coordinates' do
it 'returns shape coordinates' do
shape_line.coordinates.size.should eq(5)
shape_line.coordinates[0][0].should be_within(0.01).of(-76.660172)
shape_line.coordinates[0][1].should be_within(0.01).of(39.350792)
end
end

context '#shape_dist_traveled' do
it 'returns shape_dist_traveled' do
shape_line.shape_dist_traveled.size.should eq(5)
shape_line.shape_dist_traveled[0].should be_within(0.01).of(0.0)
shape_line.shape_dist_traveled[-1].should be_within(0.01).of(0.1631)
end
end

context '#shape' do
it 'returns shape index' do
shape_line.shape(0).should eq(shapes.sort_by { |i| i.shape_pt_sequence.to_i }[0])
end
end

context '#each_shape' do
it 'iterates through shapes' do
count = 0
shape_line.each_shape { |i| count += 1 }
count.should eq(5)
end
end

context '#each' do
it 'iterates through coordinates' do
count = 0
shape_line.each { |i| count += 1 }
count.should eq(5)
end
end

context 'Enumerable' do
it 'supports map' do
result = shape_line.map { |i| true }
result.should eq([true]*5)
end
end
end