Class: ActiveRoad::SimulationTool

Inherits:
Object
  • Object
show all
Defined in:
lib/active_road/simulation_tool.rb

Class Method Summary collapse

Class Method Details

.j_by_objectidObject



43
44
45
46
47
48
49
# File 'lib/active_road/simulation_tool.rb', line 43

def self.j_by_objectid
    hash = {}
    ActiveRoad::Junction.all.each do |j|
        hash[ j.objectid ] = j.id
    end
    hash
end

.pr_by_objectidObject



50
51
52
53
54
55
56
# File 'lib/active_road/simulation_tool.rb', line 50

def self.pr_by_objectid
    hash = {}
    ActiveRoad::PhysicalRoad.all.each do |pr|
        hash[ pr.objectid ] = pr.id
    end
    hash
end

.relation_sqlsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_road/simulation_tool.rb', line 57

def self.relation_sqls
    result = []
    pr_h = pr_by_objectid
    j_h = j_by_objectid
    pr_h.keys.each do |pr_objectid|
        parts = pr_objectid.split("-")
        j_start_objectid = "#{parts[0]}-#{parts[1]}"
        j_end_objectid = "#{parts[2]}-#{parts[3]}"

        result << "INSERT INTO junctions_physical_roads ( physical_road_id, junction_id ) values ( #{pr_h[ pr_objectid ]}, #{j_h[ j_start_objectid ]} );"
        result << "INSERT INTO junctions_physical_roads ( physical_road_id, junction_id ) values ( #{pr_h[ pr_objectid ]}, #{j_h[ j_end_objectid ]} );"
    end
    result
end

.save_simulated_square(size) ⇒ Object



14
15
16
17
# File 'lib/active_road/simulation_tool.rb', line 14

def self.save_simulated_square( size )
    ActiveRoad::PhysicalRoad.connection.execute square_sqls( size ).join("\n")
    ActiveRoad::PhysicalRoad.connection.execute relation_sqls.join("\n")
end

.sql_insert_junction(x, y) ⇒ Object



2
3
4
5
6
# File 'lib/active_road/simulation_tool.rb', line 2

def self.sql_insert_junction( x, y)
    x = (( x * 100000 ).to_i)/100000.0
    y = (( y * 100000 ).to_i)/100000.0
    "INSERT INTO junctions (objectid, created_at, updated_at, geometry) values ( '#{x}-#{y}', '2014-03-14 14:32:23.362164', '2014-03-14 14:32:23.362164', ST_geometryFromText('POINT( #{x} #{y})', 4326) );"
end

.sql_insert_physical_road(x1, y1, x2, y2) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/active_road/simulation_tool.rb', line 7

def self.sql_insert_physical_road( x1, y1, x2, y2)
    x2 = (( x2 * 100000 ).to_i)/100000.0
    y2 = (( y2 * 100000 ).to_i)/100000.0
    x1 = (( x1 * 100000 ).to_i)/100000.0
    y1 = (( y1 * 100000 ).to_i)/100000.0
    "INSERT INTO physical_roads (objectid, created_at, updated_at, geometry) values ( '#{x1}-#{y1}-#{x2}-#{y2}', '2014-03-14 14:32:23.362164', '2014-03-14 14:32:23.362164', ST_geometryFromText('LINESTRING( #{x1} #{y1}, #{x2} #{y2})', 4326) );"
end

.square_sqls(size) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_road/simulation_tool.rb', line 20

def self.square_sqls( size)
    result = []
    step = (1.0 / size)
    index_x = 0
    0.upto( size) do |i_x|
        index_y = 0
        0.upto( size) do |i_y|
            result << sql_insert_junction( index_x, index_y)

            if i_y < size
                result << sql_insert_physical_road( index_x, index_y, index_x, index_y+step)
            end
            if i_x < size
                result << sql_insert_physical_road( index_x, index_y, index_x+step, index_y)
            end
            index_y += step
        end
        index_x += step
    end
    result
end