Friday 28 March 2014

Python - List Comprehensions

"List Comprehensions" in python is a concept used to construct lists in a easy way, like a mathematician does on a piece of paper. These listcomp (I'll be using listcomp  instead of List Comprehensions in the remaining of my description) are useful when there is a requirement of doing certain kind of operations on all the list elements. Say that you are having list 'a' as shown below:

  >>>a = [1,2,3,4,5,6]
  >>>
You want to create another list b which contains squares of elements in list a. We can do that using the below python script:
  a = [1,2,3,4,5,6]
  for x in a:
     b.append(x*x)  
  print(b)



Using listcomp we can do that easily.
  a = [1,2,3,4,5,6]
  b = [ x*x for x in a]
  print(b)
Listcomp conssts of an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression  in the context of the for and if clauses which follow it.

Let us take another example. Say we have 2 lists 'a' and 'b' and wish to build another list 'c' containing tuples of 'a' and 'b' i.e., 

  >>>a = [1,2,3]
  >>>b = [10,20,30]
  >>>c = [(1,10),(1,20),(1,30),(2,20),(2,30),(2,30),(3,10),(3,20),(3,30)]
This can be done using listcomps as below:
  a = [1,2,3]
  b = [10,20,30]
  c = [(x,y) for x in a for y in b if x!= y ]
  print(c)
Let us look into some complex listcomps. Observe the below piece of code
  nonprimes = [j for i in range(2, 8) for j in range(i*2,50,i)]
  #above listcomp creates a list with all nonprime numbers between 2 and 50

  primes = [x for x in range(2, 50) if x not in noprimes]
  #above listcomp creates a list all prime numbers between 2 and 50 by checking
  # into nonprimes list

  print(primes)
The comments in the above code snippet explains the liscomp generated. We can even make the above code simple by using nested listcomps:
  primes = [x for x in range(2, 50) if x not in [j for i in range(2, 8) for j in range(i*2, 50, i)]]

  print(primes)
As shown above we can build very complex lists using listcomps. In coming post I'll discuss how to convert the listcomp to iterator loops. Happy coding....:)

Note: I've used 3.3 python interpreter to run the code 

Saturday 22 March 2014

Configuring python for GIS operations

There are good open source APIs and libraries to work on Geospatial Information System(GIS) using Python. In this post I'll go through the installation of Geospatial Data Abstraction Library and its python bindings. I'll also go through PROJ.4 and pyProj APi which is used to convert geodetic data from one datum/projection to other. I'll be configuring this environment on windows-64 bit PC. Here you go


  • Install 2.7 (don't install 3.x) python from https://www.python.org/downloads/
  • Now go to this link
  • Download and install appropriate "Generic installer for the GDAL core components" MSI for the version of windows you are using.
  • Also download and install "Installer for the GDAL python bindingsMSI for the version of python you have installed.
  • Add a GDAL_DATA environmental variable with value “C:\Program Files\GDAL\gdal-data”
  • Also add the “C:\Program Files\GDAL” to PATH environmental variable.
  • Now go to your python prompt and test whether the installed GDAL modules are working by using the following command
  >>>import osgeo
  >>>
  • If the command prompt appears without any message then you are successful in setting the GDAL API for python
  • Now we need to install PROJ.4
  • Download PROJ.4
  • Extract and copy "proj" folder to C:\
  • Add "C:\proj\bin" PATH environmental variable.
  • Also add PROJ_LIB enviromental variable with value "C:\proj\nad".
  • Add PROJ_DIR environmental variable with value "C:\proj"
  • Now download and install appropriate pyproj exe from here (if you get any error related to registry during installation, just export python registry keys from LOCAL_MACHINE and change the key names from LOCAL_MACHINE to CURRENT_USER and try the installation again)
  • After the installation check the PyProj module is installed correctly or not by using the following command.
  >>>import pyproj
  >>>
  • If the command prompt appears without any message then you are successful in installing PYPROJ API.
Now everything is set work on Geospatial data. Happy coding... :)