Python and the DK API
Python is one of the world's most popular programming languages. Occasionally TatukGIS is asked about the possibility of using Python with its Developer Kernel (SDK) to develop custom GIS applications. After recently investing some effort on this, we can confirm that using Python with the DK for ActiveX edition is highly possible.
Though TatukGIS does not support the Python platform natively (as a module), the “Python for Windows Extensions” package known as pywin32 allows easy access to, and use of, the DK for ActiveX edition via Python.
Pywin32 is a very thin wrapper of Python that allows interacting with COM objects. This approach is very powerful, but the downside is that you must run a script on a Windows system.
Prerequisites
Before looking at examples, the following software must be installed:
- Python 3.8.5 (x86) - https://www.python.org/downloads/
- Pywin32 extension (win32-py3.8) - https://github.com/mhammond/pywin32/releases
- Developer Kernel for ActiveX edition
Getting Started
Having the software installed, we can start building our python script. But first, let’s generate a static COM proxy of the Developer Kernel for ActiveX (DK).
Run a Python console and execute the following script:
from win32com.client import makepy
()
You should see a window with a list of available COM libraries from where you can select the DK library:
Confirm the selection using the OK button and, after successful import, the module should report:
Generating to C:\Users\YOUR_USER\AppData\Local\Temp\gen_py\3.8\4E0C09B1-25E9-46BD-9CD2-F03788996E26x0x1x0.py
Building definitions from type library...
Generating...
Importing module
To verify the result and generated files, let’s go to the output directory quickly by pressing the Win-R keys and entering “%TEMP%\gen_py\3.8” path.
Then, after opening the file 4E0C09B1-25E9-46BD-9CD2-F03788996E26x0x1x0.py in a text editor, you should see a context like this:
# Created by makepy.py version 0.5.01
# By python version 3.8.5 [MSC v.1926 32 bit (Intel)]
# From type library 'TatukGIS_DK11_x86.ocx' 'TatukGIS Developers Kernel for ActiveX 11.0'
from win32com.client import Dispatch
CLSID = IID('{4E0C09B1-25E9-46BD-9CD2-F03788996E26}')
MajorVersion = 1
MinorVersion = 0
LibraryFlags = 10
LCID = 0x0
The header information will be required to ensure the module loading and dispatching COM interfaces.
First script – Hello world
Now let’s create our first python script using the DK API:
import win32com.client as wc
#ensure loading DK module using CLSID and version from the header above
xdk11 = wc.gencache.EnsureModule('{4E0C09B1-25E9-46BD-9CD2-F03788996E26}', 0, 1, 0, 10)
print(xdk11.__file__)
#activate the license
utils = wc.Dispatch("TatukGIS_XDK11.TGIS_Utils")
utils.SetLicense(YOUR ACTICATION CODE)
#create a point
ptg = utils.GisPoint(10.0, 10.0)
print("ptg : {0} {1}".format(ptg.X, ptg.Y))
#create a vector layer
lv = wc.Dispatch("TatukGIS_XDK11.TGIS_LayerVector")
lv.Open()
#create a point shape
shp = lv.CreateShape(wc.constants.Point)
shp.AddPart()
shp.AddPoint(ptg)
print(shp.ExportToWKT())
#create another point shape from WKT
shpwkt = utils.GisCreateShapeFromWKT("POINT(20 20)")
lv.AddShape(shpwkt)
The syntax is very similar to other platforms except for the class constructors that must be dispatched using a full name. To extract available classes, you can add this code after ensuring the xdk11 module:
with open(xdk11.__file__, "r") as f:
for line in f.readlines():
if line.startswith("# This CoClass is known by the name"):
print(line)
Second Script - Map Viewer
In this example, we will go a step farther by opening a map in the viewer showing the visual output in the window. To do this, we must install a few extra modules using pip - the package installer for Python.
The first is the Python interface to Tcl/Tk called tkinter. This is the standard Python interface to the Tk GUI toolkit and should be available by default. Otherwise, you can install the module using “pip install tk” command.
We also need the PIL module called pillow to support different image formats such as bmp, jpg, or png that can be installed using “pip install pillow” command.
Having all modules available, let’s run our next script:
import win32com.client as wc
from tkinter import *
from PIL import Image, ImageTk
xdk11 = wc.gencache.EnsureModule('{4E0C09B1-25E9-46BD-9CD2-F03788996E26}', 0, 1, 0, 10)
utils = wc.Dispatch("TatukGIS_XDK11.TGIS_Utils")
utils.SetLicense("YOUR ACTIVATION CODE")
#create a bitmap viewer
vbmp = wc.Dispatch("TatukGIS_XDK11.TGIS_ViewerBmp")
vbmp.SetSize(1024, 768)
#open a project from DK samples
vbmp.Open("C:\\Users\\Public\\Documents\\TatukGIS\\Data\\Samples11\\World\\Countries\\Poland\\DCW\\poland.ttkproject")
vbmp.Draw_()
#create a bitmap, assign a viewer output, and save to a file
bmp = wc.Dispatch("TatukGIS_XDK11.TGIS_Bitmap")
bmp.NativeBitmap = vbmp.Bitmap
bmp.SaveToFile("c:\\tmp\\viewer.png")
#create a window and load the image
root = Tk()
load = Image.open("c:\\tmp\\viewer.png")
load = load.resize((1024, 768), Image.LANCZOS)
#show the map
img = ImageTk.PhotoImage(load)
label = Label(root, image=img)
label.image = img
label.pack()
root.wm_title("Hello TatukGIS from Python")
root.mainloop()
To use another API, refer to the source code of the DK samples.