Skip to content

Commit

Permalink
Merge pull request #1114 from transitland/remove-old-gtfs-graph
Browse files Browse the repository at this point in the history
Remove old GTFSGraph and dependencies
  • Loading branch information
irees committed Jun 27, 2017
2 parents b4049b8 + 2b2b6b9 commit c1a947d
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 796 deletions.
8 changes: 0 additions & 8 deletions app/models/concerns/from_gtfs.rb

This file was deleted.

32 changes: 0 additions & 32 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,38 +259,6 @@ def import_status
end
end

##### FromGTFS ####
include FromGTFS
def self.from_gtfs(entity, attrs={})
# Entity is a feed.
visited_stops = Set.new
entity.agencies.each { |agency| visited_stops |= agency.stops }
coordinates = Stop::GEOFACTORY.collection(
visited_stops.map { |stop| Stop::GEOFACTORY.point(*stop.coordinates) }
)
geohash = GeohashHelpers.fit(coordinates)
geometry = RGeo::Cartesian::BoundingBox.create_from_geometry(coordinates)
# Generate third Onestop ID component
feed_id = nil
if entity.file_present?('feed_info.txt')
feed_info = entity.feed_infos.first
feed_id = feed_info.feed_id if feed_info
end
name_agencies = entity.agencies.select { |agency| agency.stops.size > 0 }.map(&:agency_name).join('~')
name_url = Addressable::URI.parse(attrs[:url]).host.gsub(/[^a-zA-Z0-9]/, '') if attrs[:url]
name = feed_id.presence || name_agencies.presence || name_url.presence || 'unknown'
# Create Feed
attrs[:geometry] = geometry.to_geometry
attrs[:onestop_id] = OnestopId.handler_by_model(self).new(
geohash: geohash,
name: name
)
feed = Feed.new(attrs)
feed.tags ||= {}
feed.tags[:feed_id] = feed_id if feed_id
feed
end

private

def set_default_values
Expand Down
32 changes: 0 additions & 32 deletions app/models/operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,38 +125,6 @@ def recompute_convex_hull_around_stops
Operator.convex_hull(self.stops, projected: false)
end

##### FromGTFS ####
include FromGTFS
def self.from_gtfs(entity, attrs={})
# GTFS Constructor
# Convert to TL Stops so geometry projection works properly...
tl_stops = entity.stops.map { |stop| Stop.new(geometry: Stop::GEOFACTORY.point(*stop.coordinates)) }
geohash = GeohashHelpers.fit(
Stop::GEOFACTORY.collection(tl_stops.map { |stop| stop[:geometry] })
)
# Generate third Onestop ID component
name = [entity.agency_name, entity.id, "unknown"]
.select(&:present?)
.first
# Create Operator
attrs[:geometry] = Operator.convex_hull(tl_stops, projected: false)
attrs[:name] = name
attrs[:onestop_id] = OnestopId.handler_by_model(self).new(
geohash: geohash,
name: name
)
operator = Operator.new(attrs)
operator.tags ||= {}
operator.tags[:agency_phone] = entity.agency_phone
operator.tags[:agency_lang] = entity.agency_lang
operator.tags[:agency_fare_url] = entity.agency_fare_url
operator.tags[:agency_id] = entity.id
operator.tags[:agency_email] = entity.agency_email
operator.timezone = entity.agency_timezone
operator.website = entity.agency_url
operator
end

private

def set_default_values
Expand Down
31 changes: 0 additions & 31 deletions app/models/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,37 +239,6 @@ def generate_onestop_id
)
onestop_id.to_s
end

include FromGTFS
def self.from_gtfs(entity, attrs={})
# GTFS Constructor
coordinates = Stop::GEOFACTORY.collection(
entity.stops.map { |stop| Stop::GEOFACTORY.point(*stop.coordinates) }
)
geohash = GeohashHelpers.fit(coordinates)
name = [entity.route_short_name, entity.route_long_name, entity.id, "unknown"]
.select(&:present?)
.first
onestop_id = OnestopId.handler_by_model(self).new(
geohash: geohash,
name: name
)
route = Route.new(
name: name,
onestop_id: onestop_id.to_s,
vehicle_type: entity.route_type.to_i
)
route.color = Route.color_from_gtfs(entity.route_color)
# Copy over GTFS attributes to tags
route.tags ||= {}
route.tags[:route_long_name] = entity.route_long_name
route.tags[:route_desc] = entity.route_desc
route.tags[:route_url] = entity.route_url
route.tags[:route_color] = entity.route_color
route.tags[:route_text_color] = entity.route_text_color
route
end

end

class OldRoute < BaseRoute
Expand Down
30 changes: 0 additions & 30 deletions app/models/stop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ def self.conflate_with_osm(stops)
end
end

##### FromGTFS ####
def generate_onestop_id
fail Exception.new('geometry required') if geometry.nil?
fail Exception.new('name required') if name.nil?
Expand All @@ -355,35 +354,6 @@ def generate_onestop_id
onestop_id.to_s
end

include FromGTFS
def self.from_gtfs(entity, attrs={})
# GTFS Constructor
point = Stop::GEOFACTORY.point(*entity.coordinates)
geohash = GeohashHelpers.encode(point)
# Use stop_id as a fallback for an invalid onestop ID name component
onestop_id = OnestopId.handler_by_model(self).new(geohash: geohash, name: entity.stop_name.gsub(/[\>\<]/, ''))
if onestop_id.valid? == false
old_onestop_id = onestop_id.to_s
onestop_id = OnestopId.handler_by_model(self).new(geohash: geohash, name: entity.id)
log "Stop.from_gtfs: Invalid onestop_id: #{old_onestop_id}, trying #{onestop_id.to_s}"
end
onestop_id.validate! # raise OnestopIdException
stop = self.new(
name: entity.stop_name,
onestop_id: onestop_id.to_s,
geometry: point.to_s
)
stop.wheelchair_boarding = GTFSGraph.to_tfn(entity.wheelchair_boarding)
# Copy over GTFS attributes to tags
stop.tags ||= {}
stop.tags[:wheelchair_boarding] = entity.wheelchair_boarding
stop.tags[:stop_desc] = entity.stop_desc
stop.tags[:stop_url] = entity.stop_url
stop.tags[:zone_id] = entity.zone_id
stop.timezone = entity.stop_timezone
stop
end

private

def update_includes_stop_transfers(changeset)
Expand Down
69 changes: 67 additions & 2 deletions app/services/feed_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def parse_feed_and_operators
# Ensure gtfs graph is loaded
@gtfs.load_graph
# feed
feed = Feed.from_gtfs(@gtfs, url: @url)
feed = feed_from_gtfs(@gtfs, url: @url)
feed = Feed.find_by_onestop_id(feed.onestop_id) || Feed.find_by(url: @url) || feed
# TODO: Merge created & found feeds?
# operators
operators = []
@gtfs.agencies.each do |agency|
next if agency.stops.size == 0
operator = Operator.from_gtfs(agency)
operator = operator_from_gtfs(agency)
operator = Operator.find_by_onestop_id(operator.onestop_id) || operator
operators << operator
feed.includes_operators ||= []
Expand All @@ -44,4 +44,69 @@ def parse_feed_and_operators
# done
return [feed, operators]
end

private

# Moved from Feed.from_gtfs
def feed_from_gtfs(entity, attrs={})
# Entity is a feed.
visited_stops = Set.new
entity.agencies.each { |agency| visited_stops |= agency.stops }
coordinates = Stop::GEOFACTORY.collection(
visited_stops.map { |stop| Stop::GEOFACTORY.point(*stop.coordinates) }
)
geohash = GeohashHelpers.fit(coordinates)
geometry = RGeo::Cartesian::BoundingBox.create_from_geometry(coordinates)
# Generate third Onestop ID component
feed_id = nil
if entity.file_present?('feed_info.txt')
feed_info = entity.feed_infos.first
feed_id = feed_info.feed_id if feed_info
end
name_agencies = entity.agencies.select { |agency| agency.stops.size > 0 }.map(&:agency_name).join('~')
name_url = Addressable::URI.parse(attrs[:url]).host.gsub(/[^a-zA-Z0-9]/, '') if attrs[:url]
name = feed_id.presence || name_agencies.presence || name_url.presence || 'unknown'
# Create Feed
attrs[:geometry] = geometry.to_geometry
attrs[:onestop_id] = OnestopId.handler_by_model(Feed).new(
geohash: geohash,
name: name
)
feed = Feed.new(attrs)
feed.tags ||= {}
feed.tags[:feed_id] = feed_id if feed_id
feed
end

# Moved from Operator.from_gtfs
def operator_from_gtfs(entity, attrs={})
# GTFS Constructor
# Convert to TL Stops so geometry projection works properly...
tl_stops = entity.stops.map { |stop| Stop.new(geometry: Stop::GEOFACTORY.point(*stop.coordinates)) }
geohash = GeohashHelpers.fit(
Stop::GEOFACTORY.collection(tl_stops.map { |stop| stop[:geometry] })
)
# Generate third Onestop ID component
name = [entity.agency_name, entity.id, "unknown"]
.select(&:present?)
.first
# Create Operator
attrs[:geometry] = Operator.convex_hull(tl_stops, projected: false)
attrs[:name] = name
attrs[:onestop_id] = OnestopId.handler_by_model(Operator).new(
geohash: geohash,
name: name
)
operator = Operator.new(attrs)
operator.tags ||= {}
operator.tags[:agency_phone] = entity.agency_phone
operator.tags[:agency_lang] = entity.agency_lang
operator.tags[:agency_fare_url] = entity.agency_fare_url
operator.tags[:agency_id] = entity.id
operator.tags[:agency_email] = entity.agency_email
operator.timezone = entity.agency_timezone
operator.website = entity.agency_url
operator
end

end
Loading

0 comments on commit c1a947d

Please sign in to comment.