Thursday, July 14, 2011

Fun with DC's gis data, part 1

It looks like DC has kindly released quite a bit of gis data for public consumption. One of the more interesting sets is the regularly updated Owner Polygon dataset available from data.dc.gov. This is a shapefile containing current property records for everything in the District. Unfortunately, it's not available kml for easy display in google's tools. However the 70MB esri shapefile is available. Using Open Layers, PostGIS, and and GeoServer, we can get start displaying everything, but what if we want to use google maps and do things the hard way? To solve that, there's a few simple steps to allow polygon querying, selection, and display on google maps.
  1. Import data into PostGIS
  2. Create GIS servlet
  3. Draw the data on google maps
  4. Query PostGIS for google's lat/long
  5. Select Properties from the map
We're going to work on step one today, import your data into PostGIS.
Prepare PostGIS
I'm running Ubuntu 11.04, PostgreSQL 8.4 with postGIS 1.5.1 installed from the default software repo.
  1. PostGIS 1.5 manual
  2. nad 83, maryland projection
psql (8.4.8)
Type "help" for help.

postgres=# create database propertymap;
CREATE DATABASE
postgres=# \q
~$ createlang plpgsql propertymap;
~$ cd /usr/share/postgresql/8.4/contrib/postgis-1.5/
postgis-1.5$ psql -f postgis.sql propertymap
postgis-1.5$ psql -f ../postgis_comments.sql propertymap;
postgis-1.5$ psql -f spatial_ref_sys.sql propertymap;
Convert Shapefile
Create a ton of insert statements using shp2pgsql:
poly$ shp2pgsql -s 926985 OwnerPly.shp ownertable > inserts.sql
Shapefile type: Polygon
Postgis type: MULTIPOLYGON[2]
If we look at the .prj file included, we see that the projection for the data is NAD_1983_StatePlane_Maryland_FIPS_1900. We need to add the projection from spatialreference.org in to our database
propertymap=# INSERT into spatial_ref_sys (srid, auth_name, .......66666666],UNIT["Meter",1.0]]');
INSERT 0 1
propertymap=# \i inserts.sql
Run your first query
propertymap=# select ownername,square,lot,premiseadd from ownertable where premiseadd like '%1600 PENNSYLVANIA%';
        ownername         | square | lot  |       premiseadd        
--------------------------+--------+------+-------------------------
 UNITED STATES OF AMERICA | 0187   | 0800 | 1600 PENNSYLVANIA AV NW
 UNITED STATES OF AMERICA | 0187   | 0802 | 1600 PENNSYLVANIA AV NW
 UNITED STATES OF AMERICA | 0187   | 0801 | 1600 PENNSYLVANIA AV NW
First, a little background on what we asked for. DC property records are based on square, suffix, and lot. Square generally refers to a city block and goes all the way back to the original city planning in the old part of the city. Lot is a lot within a square/suffix. For the most part, you can ignore suffix as it's rarely used. Next time, create a simple servlet to expose all of this.