Module: ActiveRoad::OsmPbfImporter

Included in:
OsmPbfImporterLevelDb
Defined in:
app/models/active_road/osm_pbf_importer.rb

Defined Under Namespace

Modules: ClassMethods Classes: EndpointToWayMap, Node, Way

Constant Summary collapse

@@relation_required_tags_keys =
["boundary", "admin_level"]
@@relation_selected_tags_keys =
["boundary", "admin_level", "ref:INSEE", "name", "addr:postcode", "type"]
@@way_required_tags_keys =
["highway", "railway", "boundary", "admin_level", "addr:housenumber"]
@@way_for_physical_road_required_tags_keys =
["highway", "railway"]
@@way_for_boundary_required_tags_keys =
["boundary", "admin_level"]
@@way_for_street_number_required_tags_keys =
["addr:housenumber"]
@@way_selected_tags_keys =
[ "name", "maxspeed", "oneway", "boundary", "admin_level", "addr:housenumber" ]
@@way_optionnal_tags_keys =

Add first_node_id and last_node_id

["highway", "maxspeed", "bridge", "tunnel", "toll", "cycleway", "cycleway-right", "cycleway-left", "cycleway-both", "oneway:bicycle", "oneway", "bicycle", "segregated", "foot", "lanes", "lanes:forward", "lanes:forward:bus", "busway:right", "busway:left", "oneway_bus", "boundary", "admin_level", "access", "construction", "junction", "motor_vehicle", "psv", "bus", "addr:city", "addr:country", "addr:state", "addr:street"]
@@nodes_selected_tags_keys =
[ "addr:housenumber", "addr:city", "addr:postcode", "addr:street" ]
@@pg_batch_size =

Not Rails.logger.debug a high value because postgres failed to allocate memory

10000

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



29
30
31
# File 'app/models/active_road/osm_pbf_importer.rb', line 29

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#bike?(tags) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
# File 'app/models/active_road/osm_pbf_importer.rb', line 47

def bike?(tags)
  highway_tag_values = %w{cycleway}
  bike_tags_keys = ["cycleway:left", "cycleway:right", "cycleway", "cycleway:left"]

  if (tags["highway"].present? && highway_tag_values.include?(tags["highway"])) || (bike_tags_keys & tags.keys).present?
    true
  else
    false
  end
end

#car?(tags) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
# File 'app/models/active_road/osm_pbf_importer.rb', line 69

def car?(tags)
  highway_tag_values = %w{motorway trunk trunk_link primary secondary tertiary motorway_link primary_link unclassified service road residential track}
  if tags["highway"].present? && highway_tag_values.include?(tags["highway"])
    true
  else
    false
  end
end

#extract_relation_polygon(outer_geometries, inner_geometries = []) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/active_road/osm_pbf_importer.rb', line 125

def extract_relation_polygon(outer_geometries, inner_geometries = [])
  outer_rings = join_ways(outer_geometries)
  inner_rings = join_ways(inner_geometries)

  # TODO : Fix the case where many outer rings with many inner rings
  polygons = [].tap do |polygons|
    outer_rings.each { |outer_ring|
      polygons << GeoRuby::SimpleFeatures::Polygon.from_linear_rings( [outer_ring] + inner_rings )
    }
  end
end

#join_way(way, other) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/models/active_road/osm_pbf_importer.rb', line 174

def join_way(way, other)
  if way.closed?
    raise StandardError, "Trying to join a closed way to another"
  end
  if other.closed?
    raise StandardError, "Trying to join a way to a closed way"
  end

  if way.points.first == other.points.first
    new_points = other.reverse.points[0..-2] + way.points
  elsif way.points.first == other.points.last
    new_points = other.points[0..-2] + way.points
  elsif way.points.last == other.points.first
    new_points = way.points[0..-2] + other.points
  elsif way.points.last == other.points.last
    new_points = way.points[0..-2] + other.reverse.points
  else
    raise StandardError, "Trying to join two ways with no end point in common"
  end

  GeoRuby::SimpleFeatures::LineString.from_points(new_points)
end

#join_ways(ways) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/active_road/osm_pbf_importer.rb', line 137

def join_ways(ways)
  closed_ways = []
  endpoints_to_ways = EndpointToWayMap.new
  for way in ways
    if way.closed?
      closed_ways << way
      next
    end

    # Are there any existing ways we can join this to?
    to_join_to = endpoints_to_ways.get_from_either_end(way)
    if to_join_to.present?
      joined = way
      for existing_way in to_join_to
        joined = join_way(joined, existing_way)
        endpoints_to_ways.remove_way(existing_way)
        if joined.closed?
          closed_ways << joined
          break
        end
      end

      if !joined.closed?
        endpoints_to_ways.add_way(joined)
      end
    else
      endpoints_to_ways.add_way(way)
    end
  end

  if endpoints_to_ways.number_of_endpoints != 0
    raise StandardError, "Unclosed boundaries"
  end

  closed_ways
end

#pedestrian?(tags) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
# File 'app/models/active_road/osm_pbf_importer.rb', line 36

def pedestrian?(tags)
  highway_tag_values = %w{pedestrian footway path steps}
  if tags["highway"].present? && highway_tag_values.include?(tags["highway"])
    true
  else
    false
  end
end

#physical_road_conditionnal_costs(way) ⇒ Object

def extract_tag_value(tag_value)

case tag_value 
when "yes" : 1 
when "no" : 0
when /[0-9].+/i tag_value.to_f        
else 0    
end

end



116
117
118
119
120
121
122
123
# File 'app/models/active_road/osm_pbf_importer.rb', line 116

def physical_road_conditionnal_costs(way)
  [].tap do |prcc|        
    prcc << [ "car", Float::MAX] if !way.car
    prcc << [ "pedestrian", Float::MAX] if !way.pedestrian
    prcc << [ "bike", Float::MAX] if !way.bike
    prcc << [ "train", Float::MAX] if !way.train
  end
end

#required_relation?(tags) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
# File 'app/models/active_road/osm_pbf_importer.rb', line 87

def required_relation?(tags)
  @@relation_required_tags_keys.each do |require_tag_key|
    if tags.keys.include?(require_tag_key)
      return true
    end
  end
  return false
end

#required_way?(required_tags, tags) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
# File 'app/models/active_road/osm_pbf_importer.rb', line 78

def required_way?(required_tags, tags)
  required_tags.each do |require_tag_key|
    if tags.keys.include?(require_tag_key)
      return true
    end
  end
  return false
end

#selected_tags(tags, selected_tags_keys) ⇒ Object

Return an hash with tag_key => tag_value for osm attributes



97
98
99
100
101
102
103
104
105
# File 'app/models/active_road/osm_pbf_importer.rb', line 97

def selected_tags(tags, selected_tags_keys)
  {}.tap do |selected_tags|
    tags.each do |key, value|
      if selected_tags_keys.include?(key)
        selected_tags[key] = value
      end
    end           
  end
end

#train?(tags) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
# File 'app/models/active_road/osm_pbf_importer.rb', line 59

def train?(tags)
  railway_tag_values = %w{rail tram funicular light_rail subway}
  if tags["railway"].present? && railway_tag_values.include?(tags["railway"])
    true
  else
    false
  end
end