Beginner GIS: spatial querying to find Census Blocks inside urban areas

I know literally nothing about GIS, but I need to figure it out because I need to do some spatial querying. Specifically, I need to find all the Census Blocks that are in a given urban area. This is a  I'm documenting it here for anyone else who needs to get into GIS and doesn't know where to start.

QGIS is the application of choice here. It's like open source ArcGIS. ArcGIS is the Microsoft Office of GIS.

Setup on OS X

You need matplotlib from this page.

Download QGIS (open source ArcGIS). You need the GDAL and NumPy installer from this page as well.

Opening Census shapefiles

I want to work with this shape file from the Census for urban areas ( Download, unzip. The in QGIS go to Layer > Add Layer > Add Vector Layer...

Looks like it works.

Adding another layer

I want to look at Census Blocks in a specific urban area. For example, Abbeville, LA (UACE=00037) as defined here.

Same deal as before for adding the layer ( Then do this to see them overlaid:

Finding Census Blocks in the urban area

First, find Abeville using the Query Builder (cmd-f):

This will hide all the other urban areas from the map. Go back in to the Query Builder and delete the query to get everything back.

Select the urban area of interest using the "Select Features" toolbar button (yellow square with a mouse pointer).

Now, to find the blocks in the urban area. This is done with the Spatial Query plugin. Enable it by going in the Plugin Manager in QGIS, searching for "spatial" and toggling the checkbox. You should have this icon in the sidebar now:

Then run this query by clicking that button and selecting the appropriate items in the dropdown (in the screenshot, the results are shown too):

Create a new layer based on the spatial query:

Then right click on that layer created from the spatial query, choose "Save As..." and save it as a CSV. This will export the attribute table for the layer, which is essentially a list of all the Census Blocks in the specified urban area!

Now, on to automating this for all the urban areas in the US.

Full text lookup bookmarklet

I often want to find the full text article for a PubMed entry. This simple bookmarklet takes the PubMed page for a specific article (like this one) and goes directly to the list of full text options for my library. It beats the "Find at UMB" button that PubMed sometimes displays because it doesn't open a new window.

I'm posting this here because it could be easily modified for a different library.

You can use this bookmarklet creator to make your own bookmarklet with the code above.

If you're from UMB and want to use my bookmarklet, go to this page.

LiveReload Using Guard and Rack With No Browser Plugins

I sometimes want to use LiveReload to automatically refresh index.html when I'm working on some JavaScript. Yes, you can use the $10 app, but I generally end up using Guard anyway for compiling CoffeeScript or SCSS, and I dislike browser plugins because of the security risks. Turns out that it's pretty easy to get LiveReload working with Guard and Rack sans plugins.

The code is in a Gist below. All files go in a single folder (say, ~/git/myproject). To run it (after bundle installing), open two Terminal tabs in this folder. In the first one, run guard. In the other, run ruby serve.rb. Then hit up http://localhost:4567 in your browser of choice. LiveReload should automatically reload the page when you change index.html (as configured; modify the Guardfile to watch additional files for changes).

(These instructions are a little vague, because if you can't easily fill in the blanks you should probably just use the app. It's not worth learning all about Ruby/Gems/Rack/Guard to save $10.)

MailMate: my custom keybindings

I'm giving MailMate a try again after using for the last six months. I accidentally deleted my custom keybindings 😢 so I had to re-create them. Here's what I came up with, on top of the built-in Gmail keybindings:

Note that some of these are not listed in the official custom keybindings documentation.

So far it's good to be back in MailMate. has been getting slower and slower, especially search, which prompted my switch. So far I like the (fairly minor) changes to MailMate since I used it last.

SAS: Getting 2×2 contingency table variables in the right order with PROC FREQ

By default, SAS will format a 2×2 contingency table like this if you have 1=yes 0=no binary variables:

|           | outcome=0 | outcome=1 |
| exposed=0 |           |           |
| exposed=1 |           |           |

But we want it like this:

|           | outcome=1 | outcome=0 |
| exposed=1 |           |           |
| exposed=0 |           |           |

The following code demonstrates how to do this:

And here's the output from this code:

AppleScript: Save Clipboard to Text File

This AppleScript will open a "Save as" dialog, which lets you specify a folder and filename, using the folder of the front-most Finder window as the default folder. It will then save the (text) contents of your clipboard to that .txt file.

tell application "Finder"
	if (count of windows) > 0 then
		set theDefault to the POSIX path of ((folder of window 1) as alias)
		set theDefault to path to desktop
	end if
end tell

set resultFile to (choose file name with prompt "Save As File" default name "paste.txt" default location theDefault) as text
if resultFile does not end with ".txt" then set resultFile to resultFile & ".txt"
set resultFilePosix to quoted form of the POSIX path of resultFile
do shell script "pbpaste > " & resultFilePosix