Our talented graphics guy, Mike Fisher, came to me a few weeks ago with a PDF of the above image. The image showed all the annexations into the City of San Antonio since 1936. (The big square in the middle of the image shows the extent of San Antonio in 1936.)
Fisher asked me if I could separate out all the annexations by year so that he could build an animation that showed how San Antonio has grown through the years. I told him I could if he could get me a shapefile. A few phone calls later, shapefile in hand, he returned.
I previewed the shapefile in QGIS and took a look at the file’s attributes. Each shape had a field called “annexation” which indicated the year that particular geography was brought into the city. I also noticed that there was a “type” field which classified an annexation as an addition (‘Annexation’) to the city or a subtraction (‘Disannexation’). (How this actually figured in to the final graphic is a longer story….)
I used the handy shp2pgsql command to load the shapefile into a spatially enabled postgresql database.
Then, I wrote a Python script that looped over the database twice. In each pass, the script selected either all the annexations or disannexations and grouped them by year.
Next, the script looped over each year in the resultset and selected, then exported, those shapes that were annexed (or deannexed) in that particular year. Shp2pgsql’s sibling, pgsql2shp, made this export easy.
I exported each shape along this file path: Annexation(or Disannexation) directory/year/year.shp. Then, I exported each resulting shapefile as a .dxf file to a DXF directory within the Annexation or Disannexation folder. The .dxf file was the final desired product. That format is readable by Illustrator and what our graphic’s guy needed to make the final animation.
The most challenging part of this script, really, was manipulating the files and filepaths. I had a definite idea of how I wanted the results to be structured: Annexation/year/year.shp, Disannexation/year/year.shp and then Annexation/DXF/year.dxf and Disannexation/DXF/year.dxf, and moving between directories from within a script can be confusing. My patience, though, was rewarded by a portable script with a structured file output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55