QGIS Fast SQL Layer

Lee Hachadoorian on May 7th 2012

PostGIS and SpatiaLite offer a large number of spatial query operations, including buffers, intersections, and spatial joins. It’s really useful, especially when you’re still experimenting, to visualize your results without having to create a new spatial table, spatial view, or exporting to a shapefile or other GIS data format. Fortunately, there are several ways to do this, including at least three plugins for Quantum GIS: Fast SQL Layer, PostGIS Query Editor, and RT SQL Layer. In this blog post I will walk you through using Fast SQL Layer. I’ll assume you already know how to install a QGIS plugin (but see the link in the next section if you don’t), and that you’re somewhat familiar with SQL. Don’t worry if you’re not—SQL is a short step beyond the kind of querying that GIS users are already familiar with. The examples should be understandable, and maybe it will encourage you try it yourself. If you already are familiar with SQL, this post will give you a quick introduction to the plugin, as well as point out some gotchas that I haven’t seen documented elsewhere.

Installing Fast SQL Layer

Fast SQL Layer is installed like any other plugin. Fast SQL Layer has a number of dependencies which, unfortunately, are poorly documented. On Windows, it worked out of the box. (Note that I am using the full OSGeo4W stack, which probably took care of the dependencies.) On Ubuntu, I had to find the unmet dependencies by trial-and-error:

  1. Install the plugin.
  2. Relaunch QGIS, checking the error message for the missing package.
  3. Install the dependency.
  4. Repeat steps 2-4 until there are no more error messages.

The ones I found are: pygments, psycopg2, pyspatialite. pyspatialite cannot be installed via apt, but to compile it you need to have python-dev, so you might as The first two can be installed with:

sudo apt-get install python-pygments python-psycopg2

pyspatialite is installed via python and has its own dependencies. You may have python installed, but you need python-dev as well (add to the apt command above if you know you need it). pyspatialite has its own built-in spatialite, but Fast SQL Layer won’t work unless you choose the version that matches what’s installed on your computer! To install, download pyspatialite from the Python Package Index (http://pypi.python.org/pypi/pyspatialite/). Make sure to download the version that matches the version of spatialite you have installed. In my case, I dowloaded pyspatialite 3.0.1. Extract the zipped file. In the console, navigate to the  folder you just extracted (e.g. ~/Downloads/pyspatialite-3.0.1), and run

sudo python setyp.py install

Note that while the Python Package Index warns against installing as sudo, this package will fail if it can’t write to /usr/local…

After this, Quantum GIS opened with no errors and the Fast SQL Layer plugin can be opened from the Database menu.

Using Fast SQL Layer

When you open Fast SQL Layer, It will open a floating window, which can also be docked to main QGIS window:

QGIS Fast SQL Layer docked to bottom edge

The interface is pretty spare. There are really only a handful of options. Most of the actual work will be done by your SQL statement.

QGIS Fast SQL Layer floating

The above screenshot shows a closeup of the Fast SQL Layer window. The first dropdown contains the name of any PostGIS or SpatiaLite connections you have defined in QGIS. Fast SQL Layer does not offer any way to create new database connections, relying upon ones that you have already set up. If you need to connect to a new PostGIS server, you must first create by clicking the New button in the Add PostGIS Layer dialog:

Even then, the connection won’t appear in Fast SQL Layer until you restart QGIS. How annoying this is will depend on how frequently you need to add new database connections. In my workflow I’m really only working from two different PostGIS servers. On the other hand, I do create new SpatiaLite databases with some regularity.

Next to the Connections dropdown is the Primary Key combo box (showing “gid” in the screenshot). This is the name of the primary key (unique id) in your spatial table. This only needs to be set for PostGIS tables. The combo box allows you to select between the names gid or id, but if your table uses a different field name, you can type in whatever it is. If you  misspecify the primary key field when adding a PostGIS layer, the layer will appear in the Layers pane but won’t render, and an error message will keep popping up every time the map canvas renders (e.g. if you zoom, pan, or add a new layer). If you make this mistake, you will want to remove the offending layer immediately. When adding a SpatiaLite layer, the key field is not used; not only does it not matter whether you make a mistake in the primary key name, it can even be left blank.

Next is the Geometry Column combo box (“the_geom” in the screenshot), which is where you specify the geometry column of your spatial table. It allows you to select between the common choices the_geom and geom, but as with the Primary Key combo box, you can type in the correct column name if it is not among these options. Note that when you create spatial tables in SpatiaLite, geometry columns are created with Geometry as the default name. It would be great if Fast SQL Layer included this among the options, or if it remembered user entries in the Primary Key and Geometry Column combo boxes, but it does not, so you will have to type in Geometry each time you add a SpatiaLite table that uses this column name.

The next button is the Run button. To actually generate your query layer, you need to type in a SQL query. For this example, I am using a PostGIS server and have typed in a basic SELECT * (select all columns, in plain English) of a small dataset, the five boroughs of New York City (originally downloaded in shapefile format from Bytes of the Big Apple).

SELECT * FROM geo_boundaries.boro_nyc_2010_dcp_epsg2263

When you push Run, you get this:

Notice that the Layers pane on the left contains three layers named QueryLayer. That’s because I’ve done this three times. Each new layer is added with the default name QueryLayer. (Normally when when adding a PostGIS or SpatiaLite layer in QGIS, the layer name will default to the name of the PostGIS or SpatiaLite table). If you intend to add several QueryLayers, I suggest renaming each one in the Layers pane (right-click and choose Rename, or double-click to open the Layer Properties dialog) immediately after adding.

If you make a mistake in your query, such as a typo in the table name, you will get an error message and the layer won’t get added.

The Get Layer button (next) will return the connection string and SQL query that created the active layer (the layer selected in the Layers pane):

You can get the same information by hovering your mouse over the layer name in the Layers pane, or looking on the Metadata tab of the Layer Properties dialog. Note that although the connection string appears in the Fast SQL Layer query window, if you hit the Run button you will get an error message! The query window appears to only accept SQL statements, not full connection strings.

The next dropdown box has the options add layer / replace layer. I have some guesses as to what this should do, but in practice I don’t see any difference. In either case, when you hit Run, a new layer gets added to your map.

So far all we have done is add a data layer, no different from adding it with the normal Add [PostGIS|SpatiaLite] Layer… dialog. The real usefulness of Fast SQL Layer comes with being able to visualize your ad hoc queries. For a simple first example, I’m going to return the centroids of the five boroughs:

SELECT gid, boro_code, boro_name, ST_Centroid(the_geom) as the_geom
FROM geo_boundaries.boro_nyc_2010_dcp_epsg2263

Note some things about the above spatial query. You must include a unique key in your select list, and you must return a geometry column, and the column names in the resultset must match what have in the Primary Key and Geometry Column combo boxes. In fact, you could alias these columns with arbitrary names as long as you use the same names in the combo boxes.

When it gets really interesting is when you spatially join your layers.

SELECT z.gid, name, subboro_name, ST_Buffer(z.the_geom, 100)
FROM
    geo_boundaries.boro_nyc_2010_dcp_epsg2263 b
    JOIN geo_boundaries.zip_nyc_2007_cur_epsg26918 z
    ON ST_Intersects(ST_Centroid(b.the_geom), ST_Transform(z.the_geom, 2263))

There’s a few things going on here. First, I’m using ST_Centroid to return each borough’s centroid (as in the last example). Then I’m using ST_Intersects to find the ZIP code at each borough’s center. In order for ST_Intersects to work, the two geometries must be in the same spatial reference system, so ST_Transform is being used to transform the ZIP code layer from UTM 18N (EPSG 26918) to State Plane New York Long Island (EPSG 2263). Fortunately, I don’t have to look up the SRID of the boroughs layer because my table naming convention includes the SRID. Finally, I’m adding a 100 meter buffer to the each ZIP code that satisfies the spatial join. ST_Buffer takes a geometry and a buffer radius. The units are determined by the spatial reference system, and in this case I know that UTM 18N uses meters, so ST_Buffer(z.the_geom, 100) means give me a 100 meter buffer around each ZIP code polygon. The result looks like this:

Fast SQL Layer does need some work. A couple of queries that I attempted took a long time to run, and it would be great if there were a way to cancel the query if it was taking too long (particularly since sometimes when it takes too long you look at the query text and realize it’s taking too long because you made a mistake). As it is, a long query just hangs QGIS until it completes. It would also be great if the Get Layer button returned just the SQL query, so that it could be edited without having to first strip out the connection string parameters, and if the “replace layer” option actually replaced the selected layer with the new query.

Finally, the plugin really needs some kind of cache, either locally or in a PostGIS temp table. When the map canvas renders, the query gets resubmitted to the server. If your spatial query takes any length of time to execute (a not uncommon occurrence), you spend a lot of time waiting for minor visual changes. I know that the alternative plugin PostGIS Query Editor caches a shapefile locally, so I might try that one next, but on the whole, Fast SQL Layer’s no-frills approach is at least straightforward and easy to use.

Postscript: In writing this blog post I found someone who has taken Fast SQL Layer and turned it into a plugin for pgAdmin3. I haven’t tested it yet, but I do a lot of querying in pgAdmin, so this seems like a useful tool as well.

Filed in Computing,GIS | One response so far

Lenovo IdeaPad Y560p Keyboard Possessed by Demon

Lee Hachadoorian on Apr 19th 2012

Normally I don’t write about hardware on this blog (or pretty much anywhere), but I’m making an exception in this case to shout out a thank you to Theje for a YouTube video showing how to fix a weird keyboard problem on my IdeaPad Y560p. Certain keys quite suddenly started doing strange things: The H key would act like an up arrow, so while you were typing, your words would suddenly start appearing in the middle of the last line. Continue Reading »

Filed in Computing,General | No responses yet

New York State Celebrates the 200th Anniversary of the Gerrymander

Lee Hachadoorian on Mar 30th 2012

Colleagues at Center for Urban Research have posted a fun interactive map for visualizing the various New York State redistricting proposals. New York, like all states, is redrawing state and federal legislative districts based on the 2010 Census. So far, it’s not going so good. The proposals released by the legislature have been widely criticized for not preserving “communities of interest”, a common redistricting desideratum. The legislature was unable to agree on federal districts, and left that up to a federal court. State legislative districts were signed into law by Gov. Cuomo, in exchange for a legislative pledge to create a bipartisan redistricting panel for the next go round (i.e., 2020). But the redistricting reform that is shaping up has been criticized by a former New Yorker and current member of the California Citizens Redistricting Commission for, among other things, leaving final adoption in the hands of the legislature. Continue Reading »

Filed in GIS,Governance | No responses yet

Spatialite GUI

Lee Hachadoorian on Jan 31st 2012


Why SpatiaLite?

I spent this weekend getting the SpatiaLite GUI installed, with help from SpatiaLite Users Google Group. SpatiaLite is an open source source geodatabase built on top of SQLite, itself an open source database. As a lightweight, “server-less” database, SQLite is frequently used as an embedded database by other applications, including Firefox, Google Chrome, Skype, iTunes, Clementine (my preferred FOSS music player), and Dropbox (on the client side). Continue Reading »

Filed in Computing,GIS | Comments Off

Using the Kindle DX as a PDF Reader

Lee Hachadoorian on Dec 14th 2011

I was never particularly interested in reading novels on the Kindle, but as an academic I have reams of journal articles—most in PDF format—that I need to read and consult for my research. Many of you will know what I mean when I say that I can’t stand reading on a computer monitor. But printing the articles is problematic, too. There’s the obvious environmental concern, which I partially mitigate by printing everything 4-up double-sided (usually prompting onlookers to ask “Can you read that?!!”), but even then there’s the problem of actually having the articles with you when you find yourself with downtime on the subway, at the doctor’s office, etc. So when my parents asked me what I wanted as a graduation gift…

Which Kindle?

When I got it last May, Amazon had not yet released the Kindle Touch or the Kindle Fire, so my choice was basically between the 6” screen (now renamed Kindle Keyboard) with WiFi (and optionally with 3G wireless) and the 9.7” Kindle DX with 3G wireless only. Obviously, as with laptops, there’s a portability/usability tradeoff. But not knowing how well PDFs would convert to the Amazon e-reader format (read further for notes on PDF conversion), I picked the larger-screen Kindle DX for full-page PDF viewing. Continue Reading »

Filed in Productivity | 2 responses so far

Online Voting

Lee Hachadoorian on Dec 3rd 2011

Estonia is apparently a leader in secure digital signature and voting. 95% of adults have electronic signature credentials, and the country’s national elections take place completely online. I found this out during Day 1 of the CUNY IT Conference, in the keynote address by Robert D. Atkinson of The Information Technology & Innovation Foundation. Atkinson claims countries with smaller territories, like Estonia, can advance  projects like online voting because all the stakeholders can be brought together to move such a project forward. Continue Reading »

Filed in Governance | 3 responses so far

Urbanized

Lee Hachadoorian on Nov 27th 2011

I recently saw Urbanized, a documentary film about urban design showing at a handful of cinemas around the country. The film examines planning and design at cities around the world, generally focusing on specific projects in each city, such as the High Line in New York City, the TransMilenio bus rapid transit system in Bogotá, and a system of pedestrian paths in a South African township. There are also segments discussing citywide planning, for example in Phoenix and Brasília. While the projects chosen vary, a recurring theme is the impact of planning and design on the day-to-day lives of the lower class. Continue Reading »

Filed in Housing,Planning,Transportation | One response so far

Occupy the Commons

Lee Hachadoorian on Nov 18th 2011

Occupy Wall Street is two months old. While sympathetic with generalities of the critique, I’m a little unsure about what actual changes this will lead to. This is partially because OWS has not specified policy demands. This lack of specificity has been defended by Bernard Harcourt as a form of political disobedience, as a protest not only of the current conditions but also of the political system which has led to this point. In this view, articulating demands is merely buying into the institutions and ideologies that OWS rejects. Further, an alternative view of decision-making is being articulated in the participatory democracy of the General Assembly itself. (See this piece on the intellectual roots of the protest.)

But leaving aside participatory democracy, the essence of the critique captured in “We are the 99%” comes down to economic inequality. What is to be done about inequality depends (at least partly) on your opinion of the cause. Participatory democracy is the solution offered by OWS, because the cause of inequality is taken to be corporate capture of traditional political channels. Continue Reading »

Filed in Governance,Urban | 3 responses so far

Downgrading a Package in Ubuntu

Lee Hachadoorian on Oct 28th 2011

Never satisfied with something that is already working (Firefox), a few months ago I decided to check out Chromium, the open source version of Google’s Chrome web browser. My impression is that it is very snappy at page loads. In looking into which browser would be better on an old, hardware-challenged computer (a Sony VAIO with a 1.2 GHz Pentium M and 512 MB RAM), I found that Tom’s Hardware reports that while Chrome has a heavier memory footprint, it also has faster page loads. On low-end systems that makes it kind of a toss-up—will the increased memory demands translate into a faster feel in your browsing?—but on my 6 GB RAM Dell desktop and my 8 GB RAM Lenovo laptop, the OS can easily afford to throw a couple of GB to Chromium in exchange for a faster browsing experience. Continue Reading »

Filed in Computing,General | One response so far

Federalism and States’ Responsibilities

Lee Hachadoorian on Oct 12th 2011

One of the more interesting issues to emerge during the recent Republican primary debates was the division of powers in our federal system of government. Often this gets called something like “states’ rights”, but that phrase is probably irreversibly entwined with Jim Crow and other state curtailments of civil rights. The present issues perhaps require a new term, along the lines of “states’ responsibilities”, which would emphasize things that states should do for their citizens—in the absence of federal action, or perhaps even in preference to federal action. Continue Reading »

Filed in Federalism | Comments Off

Next »