Research on Integrated Python Method in Component GIS Development
Mavan
Li Lin
Wang Xiaofui
Abstract: You can increase development efficiency and quality by integrating python in component GIS development. Python can write data import, processing, analysis and other modules in the GIS system development, and spatial analysis, geological modeling and other modules in the business logic layer and scientific research of application systems. Python and component GIS can integrate through Pythoncom interface, calling the COM server components developed using Python in the development language of the VB, which enables the GIS system and Python extended independent development, seamless integration.
Keywords: component type GIS, Python, COM
1 Introduction
With the rapid development of computer and geographic information technology, the gradual maturity of GIS theory and application, component technology has gradually become the mainstream of GIS software, changing the working model of traditional integrated GIS platform, more suitable for users to carry out two-year development and Organic integration of other systems such as MIS, OA (Song Guanfu, 1998; Ding Xin, 2003; Feng Kezhong, 2003). Representative component GIS has ERSI's ArcGIS and Beijing Super Map of Supermap Object. SuperMap Object is a new generation of large component geographic information system platforms developed by Beijing Super Chart and Development of Geography and Resources of Chinese Academy of Sciences.
Since most scripting languages have the simple syntax, easy learning, interpretation, no need to compile, easy deployment, and maintenance, using dynamic scripting language development efficiency than directly using static programming languages C, C , VB , Development and maintenance is low, as system extensions and rapid development languages have the advantage of general static programming languages. Extension or integrate existing systems through scripting languages, in many software platforms have been widely used. In the COM-based component geographic information system platform, ArcGIS, SuperMap Object, etc., can be extended, integrated with other programs, integrating other programs, and writing data, etc., which can be integrated, integrated with the Script language, including Python, etc. The business logic layer (intermediate layer) and the spatial analysis, geological modeling, etc. of the business logic layer (middle layer) and scientific research are more advantageous.
Python is a high-level language (LUTZ, 2001) of interpretative, object-oriented, dynamic semantic feature. Python has high-level built-in data structures, dynamic types, and dynamic binding, rich standards, and third-party extensions (including scientific computing, statistical analysis, visualization modules), simple and easy-to-read syntax, make it very suitable for fast Application development has developed a new system, expands existing systems, or as a glue language, integrating existing parts. For information and grammar about Python, please refer to the Python official website (http://www.python.org).
This article describes how to apply Python to extend the ideas, methods and advantages of Component GIS, such as SuperMap Object.
2. Apply Python to expand component GIS
2.1. Python integrated way
In a general programming language, the integration or call Python has the following ways: (1) In C or C language, the interface provided by Python can be used to call the specific function of Python (Rossum, 2003); (2) through data File mode, through the pipeline mode of the operating system, directly call the Python execution environment and function module to complete a specific function; (3) In the Windows environment, you can pack the object written by Python into a COM object using the Pythoncom extension module for supporting COM programming. Language call (Hammond, 2000). Where (1) and (3) methods are "seamless" integration, the method (2) is loose integration. This article will be completed by the Pythoncom extension module to be called with SuperMap Object. Pythoncom is a Python and COM, ASP-integrated extension module developed by ActiveState, please refer to the ActiveState website (http://www.activestate.com) for information on Pythoncom. The following will be described in detail how to create a Python module that can be called by other programming languages, and how this module interacts a specific task with SuperMap Object.
2.2. Apply Python to implement COM objects
Using Python implements a COM object first needs to define a Python class, implement the required properties and methods; secondly need to apply Pythoncom extensions, implement ProgID, CLSID, etc. Register this Python class, make it a call to be called COM object. This process will be described below by a simple example.
Let's design a COM object that can calculate user expressions. The following is the code:
# SimpleEval.py - Expression Solution
Class Pythoneval:
_public_methods_ = ['myeval']
_REG_PROGID_ = "PythongIsDemos.eval"
# Guid of this com searver
_REG_CLSID_ = "{6288B5B7
-870F
-494E-a
4f
0-99868729804e}
Def Myeval (Self, Source, My_X):
X = my_x
Return Eval (Source)
# 自 注 部分
IF __NAME __ == '__ main__':
Print "Registering COM Server ..."
Import win32com.server.register
Win32com.server.register.usecommandline (PythoneVal)
Figure 1 Code of a simple COM object written by Python
The above file definition has three special properties fields, namely:
_PUBLIC_METHODS_
Method list can be called, this code has only one method "Myeval".
_REG_PROGID_
The name of the object is used to call this object.
_REG_CLSID_
CLSID of the COM object, you can use "Pythoncom.createGuid ()" method to generate, or you can use other tools.
Myeval is the method of our definition, the parameter is a expression (string), and the specific value of X ", and then calls this method, returns the calculated value. The rear portion is a registration module. If this module is called directly in Python's interactive environment, run this code. Save this file in the "Python23 / Lib / Site-packages" directory, first run, so that it is registered in the registry. This object can be applied to some modules that need users to give specific expressions. In the process of writing this class, you can write side tests while you can verify the correctness of the algorithm and logic in Python's interactive environment, guarantee development efficiency and quality.
The COM object developed by Python can also be registered and reverse by the command line. Register an object: "python.exe yourver.py"; reverse registration an object: "Python.exe YourServer.py -unregister". It should be noted that this object must have the code of the registration section of the above program.
Pythoncom also defines other features of the COM object in addition to other special attribute fields other than "_public_methods_". In the code, it is also possible to perform error handling by "Raise ComException". For additional details of Creating a COM object using Python, please refer to the Pythoncom related documentation.
2.3. Call the COM object implemented by Python
Call the COM object written in Python and call other COM object method, the following will explain the calling method through the VB code.
Create a project in VB, put 3 text boxes and a button on the default form, write the initialization code for the COM object in the Form's LOAD event, click the code in the button, write the COM object call, in In the form of the form, this object is destroyed. All code is shown in Figure 2.
Public MyServer As Object
Private submmand1_click ()
Text3.text = myserver.myeval (text1.text, int (text2.text))
End Sub
Private sub flow_load ()
Set myserver = creteObject ("PythongIsDemos.eval")
End Sub
Private Sub Form_Unload (Cancel AS Integer)
Set myserver = Nothing
End Sub
Figure 2 VB is called the code written in Python
There is no difference in the entire code and call other COM objects. The following is the interface of the program run, for X = 2, the expression is "3 * x - x * x", the result is 2.
Figure 3 VB Calls the operation result of the COM object written in Python
We can insert this code into component GIS such as SuperMap Object, develop languages into a specific expression of a specific expression of the specific expression. It should be noted that issues that need to pay attention to parameter delivery, data type, object calls, etc., you need to pay attention to it here. In addition, two problems need to be aware that the first, for the VB development environment, VB is case sensitive, and Python is case sensitive; the second is UNICODE problem, and the string of CoM is Unicode, in Python Convert to Python strings as needed before processing.
3. Application examples
Next, Python will use Python to implement a minimum multiplied module. The following is the code: # Leastsq.py - Minimum multiplication
From scipy import *
From scipy.optimize import leastsq
Class Pythonsq:
_public_methods_ = ['addX', 'addy', 'leastsq', 'getp']
_REG_PROGID_ = "PythongIsDemos.Leastsq"
# Guid of this com searver
_REG_CLSID_ = "{6278B5B7
-870F
-494E-a
4f
0-92368724804E}
# # 程序 程序 需要 需要 值
X = []
y = []
# Enter the value of x and y, because there is no data structure such as a corresponding sequence, so input one by one.
DEF ADDX (Self, XX):
Self.x.Append (xx)
DEF Add (Self, YY):
Self.y.append (yy)
# Solve the remaining variables
Def Residuals (Self, P, Y, X):
A, B, C = P
Y = array (y) # Convert sequence to matrix
x = array (x)
Err = Y - (a * x * x b * x c)
Return ERR
# Solve parameters
DEF Leastsq (Self):
m_p = [1.0, 1.0, 1.0]
Self.p = Leastsq (Self.residuals, M_P, Args = (Self.y, Self.x))
# 值 值
Def getp (self, index):
Return Self.p [0] [Index]
# 自 注 部分
(slightly)
Figure 4 Python Written a simple least squares code code
Explanation of the program code is commented. The program uses the SCIPY extension module [1] (http://www.scipy.org), the code amount is very short, the core code is only 10, mainly processing the input and output, define the solution of the remaining variables, The main minimum secondary fitted code is only 1 line, and the LeastSQ method of the Optimize module is directly called. If you want to change the proposed schedule, you can only modify the part of "Err = Y - (A * x B * x C) in the Residuals method. The data is entered one by one, and then it is read into the LIST by Python [2].
Create a new project in VB or other languages, test this COM object, the following is a simple test code of VB6:
DIM OBJSQ As Object
Set objsq = creteObject ("PythongIsDemos.Leastsq")
DIM I as integer
'Initialization return value
For i = 0 to 100
Objsq.addx (CSNG (i))
Objsq.addy (CSNG (i) ^ 2 rND ())
NEXT
'Call regression method
Objsq.leastsq
MsgBox "a =" & objsq.getp (0)
MsgBox "B =" & objsq.getp (1)
MsgBox "c =" & objsq.getp (2)
Set objsq = Nothing
Figure 4 Test the VB code of the least squares modular module written by Python For nonlinear equations, the initial value of the parameter needs to give the initial value of the parameter (the value of M_P within the LeastSQ method). We can also use the "EVAL" function described above to prepare a minimum secondary fitting module that can solve any expression. For code writing, you can first write edge testing while Python's interactive development environment and IDE (Python comes with idle, pythonwin et al., Determining the syntax and logic without errors, you can complete the module, then in other environments Under testing and use, you can increase development efficiency and quality.
4 Conclusion
With the Pythoncom interface, integrated python in the development and extension of GIS tools based on COM-based component GIS (such as SuperMap Object, ArcGIS), and the development and extension of the GIS tool that can call COM components, apply Python to develop some of the functional components with the following advantages:
1) Get full of Python dynamic language interpretation, simple syntax, easy to read, object-oriented, dynamic semantics, high-rise build data structures, can improve the development efficiency and development quality, shorten the development cycle;
2) Based on COM-based development to ensure the development of the GIS system and the independence of Python extended development, for components developed by Python objects, you can modify at any time (including compiling packaging), which will not affect GIS components. Call;
3) The seamless integration of Python and GIS tools is achieved through calls and integration of COM interfaces;
4) Python excellent scientific calculation, statistical analysis, visualization and other extension modules (SCIPY, see http://www.scipy.org) can be easily applicable to data processing, spatial analysis, and geological model analysis and other fields in GIS;
5) Python's rich standard library and third-party expansion libraries can play a positive role in integrated development of application systems such as MIS, OA.
Attachment:
The code listed herein is tested in Environment such as Python and VB (Windows XP, VB 6.0, Python 2.3 Scipy 0.3). The text code is slightly error handling and capture. In practical applications, the appropriate error handling mechanism should be used. For grammar and usage methods of Python, SCIPY extension modules, COM, VB, etc. in the program, refer to its official website and corresponding document.
references
1. Ding Xin, Su Zhihong, Wang Hua, etc., the practice of quantitative remote sensing model library, Earth Information Science, 2003, 3: 80-85
2. Feng Kezhong, Wan Qing, Qi Hui, Based on Component Technology, GIS Generalized Space Analysis, Earth Information Science, 2003, 1: 62-66
3. Song Guanfu, Zhonglu; Component Geographic Information System Research and Development, China Image Graphics, 1998, 3 (4): 314-317
4.
Hammond
M., Robinson A., Python Programming On Win32, O'Reilly, 2000
5. Lutz M., Programming Python, 2nd Edition, O'Reilly, 2001
6. Rossum G., Extending and Embedding The Python, http://www.python.org, 20037. http://www.python.org
8. http://www.activestate.com
9. http://www.scipy.org
[1] For nonlinear equations, the regression analysis is recommended to use this method, and SCIPY can also specifically have a linear algebra module (constructed to Atlas LAPACK and BLAS), which can perform linear algebraic operations, which have better speed and stability for linear regression.
[2] If the data is required to pass the amount of data, the author recommends using external file mode, create a data file by clients such as VB, then pass this file path to Python COM components, read from Python (Python is very simple File read and write mode), built as internal list, array or matrix