May 20

As may be evident from my other posts in the past, I have a tendency to use Python to solve a lot of the problems I face every day doing research in Geography. In that context, analyzing the 2002 Input Output Model for Washington State, I decided that Graph Theory needs to come into play (Economic Geograhy seems to need more Python(s) around).

In order to achieve this, I used the Excel Spreadsheet provided by the Washington State Office of Financial Management (OFM) and Python in the following way:

  1. Using the excellent xlrd python module to read Excel data into Python
  2. Create a multi-node directed graph from the data using NetworkX
  3. With the amazing power of Python, calculate the Adjacency and Distance Matrices of the graph
  4. Use xlwd to write back information in an Excel sheet

The results were stunning. The 50×50 matrix of the Washington I/O Model with 2,050 actual transactions takes a little more than a second to generate all the outputs given the Excel spreadsheet. Python to the rescue again, performing the work of geographers in a simple, fast way.

Here is a sample image, generating a directed grap (DiGraph) of the first 10 sectors reported in the Washington State Input Output Model for 2002 (the choice of 10 was to ensure a small, less complex graph so people can see the results better). All transactions are considered, with direction preserved.

The graph above is further analyzed using an adjacency matrix and a distance matrix, reported out as an Excel file.

I will be posting more results soon, and if anyone asks for it, I will clean up the script for release into the wild.

iograph10

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

Related posts:

  1. The Essential Python Modules for GIS
  2. Manipulating Excel files using Python part 1: Reading Excel Files
  3. Manipulating Excel files using Python part 2: Writing Excel Files
  4. Python Geoprocessing in ArcGIS 9.2 vs. 9.3
  5. MakeFeatureLayer in Python

3 Responses to “Python and Geography: Input Output Models and Graph Theory”

  1. MO says:

    I have a data set of almost 45000 lines and i want to read it from excel worksheet to python and then want to compute the adjacency matrix. as i have tried this in Matlab and i failed because of the memory issues with Matlab. is it possible in Python.

    • Michalis Avraam says:

      MO,

      This should be possible in multiple methods. Are your data in an Excel sheet already? Then using xlrd to read them would be the way to go. As for the adjacency matrix calculation… I assume you have 45,000 nodes. I believe NetworkX can handle this. It also allows you to export a network graph to a numpy matrix populated with adjacency values. Which is very very good indeed, so you can avoid any calculations.

      If you want to contact me personally, maybe I can help with your code as well.

      Michalis

  2. Ruslan says:

    Hi Michalis,

    I just came across your blog today from a review in GIS Development magazine. This is really a fantastic & resourceful blog. You surely have contributed a lot to the GIS community.

    Regards
    Ruslan

Leave a Reply

preload preload preload
Easy AdSense by Unreal