-
Notifications
You must be signed in to change notification settings - Fork 3
/
m93a_osm_importer.rb
73 lines (61 loc) · 2.13 KB
/
m93a_osm_importer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# (C) 2015 by Aqualung (Jim Hamilton)
# (C) 2017 by m93a (Michal Grňo)
# Permission granted to freely use this code as long as this statement
# and the list of authors are included in any derivative work(s).
#
# m93a_osm_importer.rb
#
# Import OSM-type 2D ways
#
# version 1.1 - February 18, 2017 - Uses Sketchup's internal geo coordinates
#----------------------------------------------------------------------------------------
require 'sketchup.rb'
require 'rexml/document'
include REXML
#----------------------------------------------------------------------------------------
module M93A
module OSM_Importer
class << self
def m93a_osm_import
model = Sketchup.active_model
imported = model.entities.add_group
# Check for georeferencing
unless model.georeferenced?
UI.messagebox("The model needs to be georeferenced!")
return
end
# Start the operation
model.start_operation("Import OSM File",true)
# Ask user for the OSM file
base = UI.openpanel("Select OSM File", "~", "OSM Files|*.osm;||")
path = File.dirname(base) + '/' + File.basename(base,".*") + '.osm'
# Parse the file
doc = Document.new File.new path
nodes = Hash.new
# Read positions of nodes
doc.elements.each("osm/node") { |n|
id = n.attributes["id"]
lat = n.attributes["lat"].to_f
lon = n.attributes["lon"].to_f
# BEWARE! Longitude comes first!
nodes[id] = model.latlong_to_point [lon,lat]
}
# Add ways to the model
doc.elements.each("osm/way") { |w|
points = []
group = imported.entities.add_group
w.elements.each("nd") { |n|
points.push nodes[n.attributes["ref"]]
}
group.entities.add_edges points
}
# Commit the operation
model.commit_operation
end #m93a_osm_import
end #class
unless file_loaded?("m93a_osm_importer.rb")
UI.menu("PlugIns").add_item("Import OSM File") { m93a_osm_import }
file_loaded("m93a_osm_importer.rb")
end
end #OSM_Importer
end #M93A