Sample program in Python / Spider

 

 

# -*- coding: utf-8 -*-

"""

Python 3.7.11 [MSC v.1927 64 bit (AMD64)]

Created on Mon May 23 09:16:09 2022

(c) Dipl.-Ing. Andreas Neukirchen M.A.

 

 

/*****************************************************************************/

/*                                                                           */

/* Template of a Python 3.7.11 [MSC v.1927 64 bit (AMD64)] program as a      */

/* Spyder 3.3.6 project for some function calls of the 64 bit Geodetic       */

/* Development Kit GeoDLL.                                                   */

/* The template can be extended for other GeoDLL function calls if           */

/* necessary.                                                                */

/*---------------------------------------------------------------------------*/

/*                                                                           */

/* Required external files:                                                  */

/*   installation from GeoDLL in directory 'C:\\geodll\geodll\'              */

/*   1. "C:\\geodll\geodll\GEODLL64.DLL"                                     */

/*   2. "C:\\geodll\geodll\GEODLLBN.BIN"                                     */

/*   3. Python installation: https://www.anaconda.com/products/distribution  */

/*   4. Spyder installation: https://anaconda.org/anaconda/spyder            */

/*                                                                           */

/* Online user manual with all function declarations:                        */

/*   https://www.killetsoft.de/h_geodll_e/handbuch_e.htm in English language */

/*   https://www.killetsoft.de/h_geodll_e/handbuch_d.htm in German language  */

/*                                                                           */

/* Included help files with all function declarations see GeoDLL download:   */

/*   GeoDLL_e.chm in English language                                        */

/*   GeoDLL_e.chm in German language                                         */

/*                                                                           */

/* Author of this source:                                                    */

/*   Dipl.-Ing. Andreas Neukirchen M.A.                                      */

/*   Ziviltechnikerkanzlei Baurat h.c. Dipl.-Ing. Andreas Neukirchen M.A.    */

/*   A-2531 Gaaden, Skodagasse 14                                            */

/*   a.neukirchen@neuk.at                                                    */

/*                                                                           */

/* Author of GeoDLL:                                                         */

/*   Dipl.-Ing. Fred Killet                                                  */

/*   Killet GeoSoftware Ing.-GbR                                             */

/*   https://www.killetsoft.de                                               */

/*                                                                           */

/*****************************************************************************/

"""

 

import ctypes

 

#------------------------------------------------------------------------------

# used class prototype

#------------------------------------------------------------------------------

 

class clsgeopoint:

    def __init__(self,x,y,z,epsg):

        self.X_dbl = ctypes.c_double(x)

        self.Y_dbl = ctypes.c_double(y)

        self.H_dbl = ctypes.c_double(z)

        self.EPSG = ctypes.c_ushort(epsg)

        self.X_float = self.X_dbl.value

        self.Y_float = self.Y_dbl.value

        self.H_float = self.H_dbl.value

 

#------------------------------------------------------------------------------

# used functions prototypes

#------------------------------------------------------------------------------

 

def c_str(string):

    #Create ctypes char * from a Python string.

    return ctypes.c_char_p(string.encode('utf-8'))

 

def c_dbl(float2double):

    #Create ctypes double from a Python float.

    return ctypes.c_double(float2double)

 

def c_ushort(int2ushort):

    #Create ctypes ushort from a Python int.

    return ctypes.c_ushort(int2ushort)

 

#------------------------------------------------------------------------------

# loading GeoDLL environment

#------------------------------------------------------------------------------

 

geodll = ctypes.CDLL("C:\\geodll\geodll\GEODLL64.DLL")

#print(geodll)

 

# Set GeoDLL internal language to English

geodll.setlanguage(ctypes.c_ushort(2))

 

 

# With no unlockkey set  no_unlockkey = 1 otherwise 0

no_unlockkey = 1

 

# Calling setunlockcode()

# Set the original unlock parameters you got from Killetsoft here:

unlockcode = c_str("123456789-987654321")

username = c_str("MicroModia GmbH")

ppszError = c_str("")

 

if no_unlockkey != 1:

    geodll.setunlockcode.argtypes = [ctypes.c_char_p,ctypes.c_char_p]

    nRet = geodll.setunlockcode(unlockcode,username)

 

# Explanation

print("\n")

print("You can use all features of GeoDLL in your own application initially free.")

print("However, the number of function calls per program run is limited to 100")

print("geodetic calculations. Also in the free test version a message appears")

print("on the screen or is written to the EventLog and a tone sequence is sounded.")

print("\n")

print("After WIN10 PRO DLL installation:")

print("load DLL: geodll = ctypes.CDLL('C:\\geodll\geodll\GEODLL64.DLL')\n")

print("\n")

print("By purchasing a licence the restrictions will be canceled. For unlocking")

print("you get an user name and an unlock code from KilletSoft.")

print("\n")

print("The GeoDLL function groups required by your application must be unlocked")

print("in the initialization part of your program with the function setunlockcode().")

print("The function gets the user name and the unlock code as parameters.")

print("\n")

print("How to do that I will show you here. Of course, these unlock parameters")

print("are fictitious and do not work. Therefore the error message and following")

print("tone sequence are correct in this case!")

print("\n")

print("nRet = geodll.setunlockcode(\"123456789-987654321\",\"MicroModia GmbH\");")

print("if(!= nRet)")

print("  geodll.geterrorcode(ppszError) with stringError = "" and ")

print("ppszError = ctypes.c_char_p(stringError.encode('utf-8'))")

print("strError = ppszError.value.decode('utf-8')\n")

 

if no_unlockkey != 1:

    if nRet == 1:

        print("GeoDLL functions unlocked!")

        #print("Funktionsgruppe Koordinatentransformation freigeschaltet!")

        print(username.value.decode("utf-8"))

    else:

        ppszError = c_str("")

        #geodll.geterrorcode(ctypes.byref(ppszError))

        #print("type of ppszError: " + str(type(ppszError)))

        #print(ppszError)

        strp = ppszError.value.decode("utf-8")

        print("GeoDLL functions remain locked!")

        print("Error Message from GeodDLL: " + strp)

 

# Wait for Key Press or Mouse Click.

input("Press Enter to continue...")

 

 

#------------------------------------------------------------------------------

# Coordinate Transformations with the function coordtrans()

#------------------------------------------------------------------------------

 

#Explanation

print("\n")

print("Now we start transforming coordinates. Here only the use of the GeoDLL")

print("function coordtrans() is demonstrated. But you can also use any other")

print("function for coordinate transformations available in GeoDLL. The required")

print("Coordinate Systems and Reference Systems you find sorted by countries in")

print("the User Manual GeoDLL_e.chm, chapter \"Coodinate Transformations\", in the")

print("file README.DOC or in the Online User Manual.")

print("\n")

 

# Gauss-Kueger, DHDN --> UTM, ETRS89

# origin 

dCoordXQ = c_dbl(2531418.55)  # double dCoordXQ

dCoordYQ = c_dbl(5695935.71)  # double dCoordYQ

pszDummy = c_str("")          # char *pszCoordQ 

nCoordSysQ = c_ushort(2) # unsigned short Gauss-Krueger

nRefSysQ = c_ushort(17)  # unsigned short DHDN

# destination

dCoordXZ = c_dbl(0.0)    # double *dCoordXZ

dCoordYZ = c_dbl(0.0)    # double *dCoordYZ

ppszDummy = c_str("")    # char **pszCoordZ

nCoordSysZ = c_ushort(3) # unsigned short UTM

nRefSysZ = c_ushort(4)   # unsigned short ETRS89

nStripZ = c_ushort(0)    # unsigned short

 

nRet =  geodll.coordtrans(\

                          dCoordXQ,\

                          dCoordYQ,\

                          pszDummy,\

                          nCoordSysQ,\

                          nRefSysQ,\

                          ctypes.byref(dCoordXZ),\

                          ctypes.byref(dCoordYZ),\

                          ctypes.byref(ppszDummy),\

                          nCoordSysZ,\

                          nRefSysZ,\

                          nStripZ)

 

# Output of the results.

if nRet == 1:

    print("Gauss-Kueger, DHDN --> UTM, ETRS89")

    str1 = "{0} {1:12.6f}".format("Gauss-Krueger Easting  =  ",dCoordXQ.value)

    str2 = "{0} {1:12.6f}".format("Gauss-Krueger Northing =  ",dCoordYQ.value)

    str3 = "{0} {1:12.6f}".format("UTM Easting =            ",dCoordXZ.value)

    str4 = "{0} {1:12.6f}".format("UTM Easting =            ",dCoordYZ.value)

    print(str1)

    print(str2)

    print(str3)

    print(str4)

else:

    ppszError = c_str("")

    geodll.geterrorcode(ctypes.byref(ppszError))

    strp = ppszError.value.decode("utf-8")

    print("GeoDLL coordinate transformation failed!")

    print("Error: " + strp)

 

print("\n")

 

#input("Press Enter to continue...")

 

# UTM, ETRS89 -- > UTMRef (Centimeter Grid), ED50 (Europe) 

# Note:

# GeoDLL is able to allocate memory for returnable PSZ parameters internally

# and makes them accessible to the calling program by global stored addresses.

# In the calling program then neither memory must allocated nor memory must

# released later. For more information see function setstringallocate().

dCoordXQ = dCoordXZ

dCoordYQ = dCoordYZ

pszDummy = c_str("")     # char *pszCoordQ 

nCoordSysQ = c_ushort(3) # unsigned short UTM

nRefSysQ = c_ushort(4)   # unsigned short ETRS89

# destination

dCoordXZ = c_dbl(0.0)    # double *dCoordXZ

dCoordYZ = c_dbl(0.0)    # double *dCoordYZ

ppszCoordZ = c_str("")   # char **pszCoordZ

nCoordSysZ = c_ushort(661) # unsigned short UTMref

nRefSysZ = c_ushort(2)   # unsigned short ED50

nStripZ = c_ushort(0)    # unsigned short

nRet =  geodll.coordtrans(\

                          dCoordXQ,\

                          dCoordYQ,\

                          pszDummy,\

                          nCoordSysQ,\

                          nRefSysQ,\

                          ctypes.byref(dCoordXZ),\

                          ctypes.byref(dCoordYZ),\

                          ctypes.byref(ppszCoordZ),\

                          nCoordSysZ,\

                          nRefSysZ,\

                          nStripZ)

 

# Output of the results.

if nRet == 1:

    print("UTM, ETRS89 -- > UTMRef (Centimeter Grid), ED50 (Europe)")

    print("UTMRef grid coordinate =  ",ppszCoordZ.value.decode("utf-8"))

else:

    ppszError = c_str("")

    geodll.geterrorcode(ctypes.byref(ppszError))

    strp = ppszError.value.decode("utf-8")

    print("GeoDLL coordinate transformation failed!")

    print("Error: " + strp)

 

print("\n")

 

#input("Press Enter to continue...")

 

# UTMRef (Centimeter Grid), ED50 (Europe) --> Geographic Coordinates, WGS84

dCoordXQ = c_dbl(0.0)

dCoordYQ = c_dbl(0.0)

pszCoordQ = c_str(ppszCoordZ.value.decode('utf-8')) # hard copy

nCoordSysQ = c_ushort(661) # unsigned short UTMref

nRefSysQ = c_ushort(2)  # unsigned short ED50

# destination

dCoordXZ = c_dbl(0.0)    # double *dCoordXZ

dCoordYZ = c_dbl(0.0)    # double *dCoordYZ

ppszDummy = c_str("")    # char **pszCoordZ

nCoordSysZ = c_ushort(6) # unsigned short Geo. Coord

nRefSysZ = c_ushort(10)   # unsigned short WGS84

nStripZ = c_ushort(0)    # unsigned short

nRet =  geodll.coordtrans(\

                          dCoordXQ,\

                          dCoordYQ,\

                          pszCoordQ,\

                          nCoordSysQ,\

                          nRefSysQ,\

                          ctypes.byref(dCoordXZ),\

                          ctypes.byref(dCoordYZ),\

                          ctypes.byref(ppszDummy),\

                          nCoordSysZ,\

                          nRefSysZ,\

                          nStripZ)

 

# Output of the results.

if nRet == 1:

    print("UTMRef (Centimeter Grid), ED50 (Europe) --> Geographic Coordinates, WGS84")

    str3 = "{0} {1:12.9f}".format("Longitude = ",dCoordXZ.value)

    str4 = "{0} {1:12.9f}".format("Latitude  = ",dCoordYZ.value)

    print(str3)

    print(str4)

else:

    ppszError = c_str("")

    geodll.geterrorcode(ctypes.byref(ppszError))

    strp = ppszError.value.decode("utf-8")

    print("GeoDLL coordinate transformation failed!")

    print("Error: " + strp)

 

print("\n")

 

input("Press Enter to continue...")

 

#------------------------------------------------------------------------------

# Coordinate Transformations with the function coordtransepsg()

#------------------------------------------------------------------------------    

 

# Explanation

print("\n")

print("Coordinate Transformations with the function coordtransepsg():")

print("For simplicity, it is possible to perform all required settings for the")

print("parameters Coordinate System, Reference System, Meridian Strip, Meridian")

print("Strip Suffix and 2D/3D Modus by using so-called EPSG codes. EPSG is the")

print("acronym for \"European Petroleum Survey Group Geodesy\". This is a working")

print("group of the European oil and gas exploration companies. The EPSG and")

print("their successors OGP \"International Association of Oil and Gas Producers\"")

print("have made it to their task to build a system with globally unique")

print("identifiers (EPSG codes) for geodetic data, such as Coordinate Reference")

print("Systems, Reference Ellipsoids or Map Projections.")

print("\n")

print("The required EPSG Codes you find in the User Manual GeoDLL_e.chm, chapter")

print("\"Coordinate Transformations\", in the Online Manual or in the EPSG database.")

print("\n")

print("This EPSG Codes for Coordinate Reference Systems are used:")

print("4326: EPSG code for WGS 84 -- WGS84 - World Geodetic System 1984")

print("31254: EPSG code for MGI/Austria GK West")

print("\n")

 

# use class for conversion to ctypes

coordQ = clsgeopoint(11.3439685833333,47.2602099166667,0.0,4326) # origin coord

dCoordXQ = coordQ.X_dbl

dCoordYQ = coordQ.Y_dbl

dEllHgtQ = coordQ.H_dbl

epsgQ = coordQ.EPSG

sysorigin = "EPSG 4326 WGS84 World Geodetic System 1984"

 

# or you can do it by hand

#dCoordXQ = ctypes.c_double(11.3439685833333)

#dCoordYQ = ctypes.c_double(47.2602099166667)

#dEllHgtQ = ctypes.c_double(0.0)

#epsgQ = ctypes.c_ushort(4326)

 

coordZ = clsgeopoint(0.0,0.0,0.0,31254) # initialyze destination coord

 

dCoordXZ = coordZ.X_dbl

dCoordYZ = coordZ.Y_dbl

dEllHgtZ = coordQ.H_dbl

epsgZ = coordZ.EPSG

# you can do it by hand

#dCoordXZ = ctypes.c_double()

#dCoordYZ = ctypes.c_double()

#dEllHgtZ = ctypes.c_double()

#epsgZ = ctypes.c_ushort(31254)

sysdestination = "EPSG 31254 MGI/GK Austria West"

 

nRet = geodll.coordtransepsg(dCoordXQ,\

                             dCoordYQ,\

                             dEllHgtQ,\

                             epsgQ,\

                             ctypes.byref(dCoordXZ),\

                             ctypes.byref(dCoordYZ),\

                             ctypes.byref(dEllHgtZ),\

                             epsgZ)

 

#Output of the results.

if (nRet == 1):

    print("origin: " + sysorigin + " --> ")

    print("destination: " + sysdestination)

    str1 = "WGS84 Longitude =              {0:6.9f}".format(dCoordXQ.value)

    str2 = "WGS84 Latitude =               {0:6.9f}".format(dCoordYQ.value)

    str3 = "MGI GK Austria West Easting =  {0:12.3f}".format(dCoordXZ.value)

    str4 = "MGI GK Austria West Northing = {0:12.3f}".format(dCoordYZ.value)

    print(str1)

    print(str2)

    print(str3)

    print(str4)

else:

    ppszError = c_str("")

    geodll.geterrorcode(ctypes.byref(ppszError))

    strp = ppszError.value.decode("utf-8")

    print("GeoDLL coordinate transformation failed!")

    print("Error: " + strp)

 

input("Press Enter to continue...")

 

print("\n")

print("Coordinate Transformations with the function coordtransepsg():")

print("This EPSG Codes for Coordinate Reference Systems are used:")

print("31466: EPSG code for \"DHDN / 3-deg. Gauss-Krueger zone 2\"")

print(" 4647: EPSG code for \"ETRS89 / UTM zone 32N (zE-N)\"")

print("\n");

 

# Gauss-Kueger, DHDN --> UTM, ETRS89

# use class for conversion to ctypes

coordQ = clsgeopoint(2531418.55,5695935.71,0.0,31466) # origin coord

dCoordXQ = coordQ.X_dbl

dCoordYQ = coordQ.Y_dbl

dEllHgtQ = coordQ.H_dbl  # double dEllHgtQ  Ell. Height not usesed

epsgQ = coordQ.EPSG

 

# initialyze destination coord

coordZ = clsgeopoint(0.0,0.0,0.0,4647)

dCoordXZ = coordZ.X_dbl

dCoordYZ = coordZ.Y_dbl

dEllHgtZ = coordQ.H_dbl  # double* dEllHgtZ  Ell. Height not usesed

epsgZ = coordZ.EPSG 

 

nRet = geodll.coordtransepsg(dCoordXQ,\

                             dCoordYQ,\

                             dEllHgtQ,\

                             epsgQ,\

                             ctypes.byref(dCoordXZ),\

                             ctypes.byref(dCoordYZ),\

                             ctypes.byref(dEllHgtZ),\

                             epsgZ)

 

#Output of the results.

if (nRet == 1):

    print("Gauss-Kueger, DHDN --> UTM, ETRS89");

    str1 = "Gauss-Krueger Easting =  {0:12.6f}".format(dCoordXQ.value)

    str2 = "Gauss-Krueger Northing = {0:12.6f}".format(dCoordYQ.value)

    str3 = "UTM Easting =            {0:12.6f}".format(dCoordXZ.value)

    str4 = "UTM Northing =           {0:12.6f}".format(dCoordYZ.value)

    print(str1)

    print(str2)

    print(str3)

    print(str4)

else:

    ppszError = c_str("")

    geodll.geterrorcode(ctypes.byref(ppszError))

    strp = ppszError.value.decode("utf-8")

    print("GeoDLL coordinate transformation failed!")

    print("Error: " + strp)

    

input("Press Enter to continue...")