Developing Importer for .SHP (or SHX) to SketchUp

It looked the same when set to metres.

Interesting effect :wink:

Did you get the right files? This looks like a land use map … make sure you are using MNMapPLUTO.

I got the file from the link you gave in your previous message. Pretty sure I got the right files, because your script puts an error message if the files are missing. I took these files that you mention in the script:


Okay … those are not the right files. I downloaded from that link several days ago and have a different set of files.

Where are the right files?

Sorry about that … I’m not sure when I got my link crossed. But here’s the correct link:

You will need to change the filenames to use “building_0715”

I got the same results you did when I used the files I linked to. I’ve updated the link in the original post as well.

That one works, thanks.

Glad to hear it … I apologize again for any confusion I caused ;(

I’ve got a new version that is closer to prime time. If you are so inclined, I would appreciate your assistance with using it on a Mac. I suspect you will have to change the UI.inputbox lines:

base = (UI.openpanel("Select Shapefile", "%USERPROFILE%", "Shapefiles|*.shp;*.shx;||")).scan(/(.*\\.+)\.[shp|shx]/)
@@pathname = base[0][0].to_s

Maybe just change @@pathname to something like “/tmp/building_0715” - the extensions will be added to this root. This should be essentially the code you have working with a more user-friendly front-end and the option to add height from any height database (for the “building_0715” files, select “HEIGHT_ROO” as the height field).

jimhami42_shapefile_importer.rb (10.6 KB)

I didn’t do anything clever. Where you had put c:\temp\ I changed it to /temp/, and I made a
"temp" named folder on my boot drive root level, to hold the files. That worked nicely.

I tested your new script without making any changes to it. I renamed my temp folder to make sure it wasn’t working just because it had with the old script. The shp, shx, and dbf files all show in the file open dialog, but it doesn’t matter which one I try, I get this error:

Error: #<NoMethodError: undefined method []' for nil:NilClass> /Library/Application Support/SketchUp 2015/SketchUp/Plugins/jimhami42_shapefile_importer.rb:105:injimhami42_shapefile_import_get_input’
/Library/Application Support/SketchUp 2015/SketchUp/Plugins/jimhami42_shapefile_importer.rb:96:in jimhami42_shapefile_import' /Library/Application Support/SketchUp 2015/SketchUp/Plugins/jimhami42_shapefile_importer.rb:237:inblock in module:ShapefilePolygonImporter
SketchUp:1:in `call’

I’m not sure what the problem might be, however I am suspect of the UI.openpanel behaving properly since it uses the Windows ‘%USERPROFILE%’ environment variable in the pathname (not to mention it looks for ''s and not '/'s). Not sure what Macs do with this. This is the line the error message refers to:

@@imax = (@@index[24,4].unpack('N').first * 2 - 100) / 8

You might try replacing the line that reads:

    @@pathname = base[0][0].to_s


    @@pathname = "/temp/building_0715"

That should at least remove any potential UI.openpanel issues. I really appreciate your trying this out for me.

BTW, if you get that working, Los Angeles has a really nice dataset to play with. The 581MB zipfile can be downloaded from:

Select “HEIGHT” as the appropriate data field.

That helped. There’s probably a regex way to filter Mac paths too.

I live in NYC, I’ll stay with the Manhattan model for now!

oops, meant to post this earlier…
it may even work on a PC???

        base = (UI.openpanel("Select Shapefile", "~", "Shapefiles|*.shp;*.shx;||"))
        @@pathname = base.sub(/(\.sh[px])/,'')

Wouldn’t the File processes do this ?

base = (UI.openpanel("Select Shapefile", "~", "Shapefiles|*.shp;*.shx;||"))
return unless base
folder=File.dirname(base) >>> some_path to a folder, extracted from the string
file=File.basename(base) >>> the name of the file, with its file-type-suffix, extracted from the string
fil=File.basename(base, ".*") >>> the name of the file, without its file-type-suffix, extracted from the string
ext=File.extname(base) >>> the file-type, extracted from the string
1 Like

That works fine on Mac, thanks.

Thanks, TIG! That worked like a charm and it’s so clean and neat :slight_smile:

FYI, This is named HOME on the Mac, so:


(Since about SU2014, SketchUp has been creating a %HOME% environment var on PC because Ruby uses it to expand the tilde character in pathstrings. The OR assignment above will work on Mac and even on old SketchUp versions on PC.)

Re the %TEMP% var.
Try to use the new Sketchup::temp_dir method (since SU2014,) with a fallback to ENV["TEMP"], ie:

temp = Sketchup::temp_dir rescue ENV["TEMP"]
if Kernel::test(?d,temp)
  # it exists and is a directory
1 Like

Thanks, Dan … I was wondering about the environmental variables. I don’t actually need the temp directory; it was just a handy place to put the files for reading. With the UI.openpanel, any directory will do … but I’ll keep it in mind if I need to use it sometime. Thanks again :smile:

1 Like

The shapefile importer is mostly ready for primetime. Here’s a video of importing (real-time) a very small section of Los Angeles:

At this point, my code leaves off where it usually does: it works for me and allows me to add shapefiles from around the world (it’s actually a lot of fun to use), but it has no error checking (of course) and is probably rife with many other newbie Ruby coding errors. My last addition was a kludge using a diagonal line as the defining geometry for a bounding box (instead of a rectangle) which saves me the hassle of typing in the bounding numbers each time.

At this point, I would really appreciate a critique of the code and any suggestions you might have (you know who you are). I’m not necessarily looking for any additional coding effort, but I could really use some feedback on Ruby Best Practices to help me learn how to improve writing Ruby programs.

Here’s the latest version: jimhami42_shapefile_importer.rb (12.6 KB)

Looking back through the previous 38 posts reminds me how annoying it must be for everyone to see yet another small step for mankind as I work through my stuff. I posted all of this is the spirit of community effort and hope it may continue to develop into something more professional … I’m one of those who believes that many heads are better than one :wink:

Hey Jim, I just came across this thread last night while searching for ways to import SHPs into SketchUp. Looks like you’ve nailed it. I’m impressed! I could use a little help though. When importing “lariac_buildings_2008.shp” into your “la_grid,” it works like a charm, but I cannot successfully import that same .shp file or any others into a new document. I’m sure there’s something obvious I’m missing. Perhaps you have a tutorial showing this process from scratch (i.e., how to prepare a new SketchUp document before importing a shape file)?

Thanks man! I’m looking forward to using this killer plugin!

Steven Davis