An example of Python with ILWIS
Figure 1, The IDLE editor; it is used for developing applications or writing scripts
For this exercise, you need to download the hello world zip file “ILWISObjects-HelloWorld.zip”. The ILWISObjects-HelloWorld.zip archive contains the Python script (helloworld.py), plus the ILWIS3 feature dataset of the world countries. This data set is used to illustrate the use of Python in ILWIS. If you have not downloaded the file yet, you can download it from:
Once the Python is installed, the IDLE shell can be opened by:
From Windows start tab, select All Program
From the list select Python
From the opened list click the IDLE icon
The IDLE shell window will be opened (Figure 1). This shell allows you to type Python commands or to create a Python scripts file and save it for repetitive use.
In order to use ILWIS objects with Python, you need to import them first. To do that, type at the prompt (command line) in the Python shell:
This imports the ILWIS library. In case, you have your data in one directory, for the convenience define your working catalog (directory) for farther use. This can be done by typing the following at the IDLE shell prompt:
# To set the path to any locations, URL or local:
# To set the path to your local drive:
# To use "\" instead of "/"
The content of the string C:/my directory has to be modified in order to refer to your drive and directory where you have your data (e.g. directory where you have the HelloWorld data). If the path is not correct no data will be loaded and no warning will be given. So, it is important to make sure that the path to working catalog is correct.
The ILWIS data format includes multiple files containing either meta or binary data, just like the ESRI Shapefile format. The multi-polygon geometries representing the world's countries are stored in countries.mpa and countries.mpz# for metadata and the binary data respectively. The feature attributes like name, population, etc. are stored in (attribute-)table files called countries.tbt (metadata) and countries.tb# (binary data). In ILWIS and in Python scripts, each dataset is called by the metadata file name.
The script bellow shows number of commands required to derive information from a polygon map. You can use your own polygon map and its attribute table and adapt this commands to derive information from the map and its attribute table. In this example, a polygon map called countries and its attribute table is used. The following script can be used to derive attribute information:
world = ilwis.FeatureCoverage("countries.shp")
table = world.attributeTable()
Via aforementioned commands you import the polygon map countries and its attribute table to Python with variable names world and table. Use the following commands to derive information related to this polygon map and its attribute table.
world.featureCount() #Gives number of polygons
world.type() #Display the map type
world.coordinateSystem().name() #Gives the name of the coordinate system file
print(world.envelope()) #Display coordinates of the map (min/max X, Y)
table.columnCount() #Gives number of columns
table.columns() #Display names of the columns
len(world.attributeTable().columns()) #Display number of columns
table.recordCount() #Gives number of the records
table.record(4) #Display the records contents
In addition to type, interactively, Python expressions at the IDLE editor prompt, you can use Python files that contains python codes or scripts allowing to implement computations and manipulation of raster and vector data. In the following example we use the Python file HelloWorld.py to derive attribute information from the polygon map countries.mpa and its attributer data. To view the Python script,
please open it in IDLE (Python GUI):
Figure 2, the helloworld.py Python script
From the IDLE shell Menu bar, select File
From the list select Open
Select HelloWorld.py from the directory where you have the HelloWorld data
Click Open to view the file
The contents of the HelloWorld.py file is shown in Figure 2. As you can see in the second line, the working catalog ("file:///D:/<working>/<directory>") is replaced with the directory where the data is available, in this case; "file:///C:/HelloWorld".
To run this script:
Insert the path (unzipped helloworld data folder) into line 2
You need to save the changes before you can run the script, so
From menu bar select File and from the list select Save
Hit F5 key on your keyboard or
From menu bar select Run and from the list select Run module
This script should print the sorted list representing each country abbreviation and population in a rank ordered by its population like:
| Country abbreviation