Skip to content

Commit

Permalink
uses Sketchup's internal geo coordinates
Browse files Browse the repository at this point in the history
completely rewritten, improved readability
  • Loading branch information
cshaa committed Feb 18, 2017
1 parent da9e39a commit 0dbe197
Showing 1 changed file with 58 additions and 91 deletions.
149 changes: 58 additions & 91 deletions m93a_osm_importer.rb
Original file line number Diff line number Diff line change
@@ -1,103 +1,70 @@
# (C) 2015 by Aqualung - Permission granted to freely use this code as long
# as this line and the preceding line are included in any derivative work(s)
# (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).
#
# jimhami42_osm_importer.rb
# m93a_osm_importer.rb
#
# Import OSM-type 2D ways
#
# version 1.0 (beta) - December 2, 2015 - Initial release of working version
# version 1.1 - February 18, 2017 - Uses Sketchup's internal geo coordinates
#----------------------------------------------------------------------------------------
require 'sketchup.rb'
require 'rexml/document'
include REXML
#----------------------------------------------------------------------------------------
module Jimhami42
module OSMPolygonImporter
# MAIN BODY -----------------------------------------------------------------------------
class << self
@@model = Sketchup.active_model
# MAIN PROCEDURE ------------------------------------------------------------------------
def jimhami42_osm_import()
jimhami42_osm_import_init()
@@model.start_operation("Import OSM File",true)
jimhami42_osm_import_get_input()
puts "Start Time: " + @@timestamp.to_s
jimhami42_osm_import_create_polygons()
puts "Created: " + @@poly_count.to_s
@@model.commit_operation
puts "End Time: " + Time.now.to_s
end
# INITIALIZE DATA -----------------------------------------------------------------------
def jimhami42_osm_import_init()
@@timestamp = Time.now
@@scale = 1.0
@@poly_count = 0
@@display_bb = "No"
@@name = @@timestamp.strftime("%Y%m%d%H%M%S")
@@bbox = []
@@xnode = Hash.new
@@ynode = Hash.new
end
# GET USER INPUT ------------------------------------------------------------------------
def jimhami42_osm_import_get_input()
base = UI.openpanel("Select OSM File", "~", "OSM Files|*.osm;||")
# base = UI.openpanel("Select OSM File", "%HOMEPATH%", "OSM Files|*.osm;||")
@@basename = File.dirname(base) + '/' + File.basename(base,".*")
puts @@basename
puts "Parsing XML File ..."
jimhami42_osm_import_get_data()
prompts = ["Name: ","Scale: ","Display BB: "]
defaults = [@@name,@@scale,"No"]
list = ["","","No|Yes"]
input = UI.inputbox prompts, defaults, list, "Enter Import Parameters:"
@@name = input[0]
@@scale = input[1].to_f
@@display_bb = input[2]
for i in 0...4
@@bbox[i] *= @@scale
end
end
# CREATE POLYGONS -----------------------------------------------------------------------
def jimhami42_osm_import_create_polygons()
if(@@display_bb == 'Yes')
group = @@model.entities.add_group
group.entities.add_edges(Geom::Point3d.new(0,0,0),Geom::Point3d.new(@@bbox[2] - @@bbox[0],0,0),Geom::Point3d.new(@@bbox[2] - @@bbox[0],@@bbox[3] - @@bbox[1],0),Geom::Point3d.new(0,@@bbox[3] - @@bbox[1],0),Geom::Point3d.new(0,0,0))
end
i = 0
@@xmldoc.elements.each("osm/way") do |w|
pts = []
group = @@model.entities.add_group
w.elements.each("nd") {|n| pts.push(Geom::Point3d.new((@@scale * @@xnode[n.attributes["ref"]]) - @@bbox[0],(@@scale * @@ynode[n.attributes["ref"]]) - @@bbox[1],0))}
group.entities.add_edges(pts)
i += 1
if(@@name == "")
group.name = i.to_s
else
group.name = @@name + '-' + i.to_s
end
@@poly_count += 1
end
end
# GET SHAPEFILE DATA --------------------------------------------------------------------
def jimhami42_osm_import_get_data()
file = @@basename + '.osm'
xmlfile = File.new(file)
@@xmldoc = Document.new(xmlfile)
puts "Version: " + @@xmldoc.elements["osm"].attributes["version"]
@@xmldoc.elements.each("osm/bounds") do |b|
@@bbox[0] = b.attributes["minlon"].to_f
@@bbox[1] = b.attributes["minlat"].to_f
@@bbox[2] = b.attributes["maxlon"].to_f
@@bbox[3] = b.attributes["maxlat"].to_f
end
@@xmldoc.elements.each("osm/node") {|n| @@xnode[n.attributes["id"]] = n.attributes["lon"].to_f; @@ynode[n.attributes["id"]] = n.attributes["lat"].to_f}
module M93A
module OSM_Importer

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
nodes[id] = model.latlong_to_point [lat,lon]
}

# 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
#----------------------------------------------------------------------------------------
unless file_loaded?("jimhami42_osm_importer.rb")
menu = UI.menu("PlugIns").add_item("Import OSM File") { jimhami42_osm_import() }
file_loaded("jimhami42_osm_importer.rb")


unless file_loaded?("m93a_osm_importer.rb")
UI.menu("PlugIns").add_item("Import OSM File (new)") { m93a_osm_import() }
file_loaded("m93a_osm_importer.rb")
end
#----------------------------------------------------------------------------------------
end
end

end #OSM_Importer
end #M93A

0 comments on commit 0dbe197

Please sign in to comment.