File: libpdalpython.pyx

package info (click to toggle)
python-pdal 2.1.8%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 568 kB
  • sloc: python: 358; cpp: 349; makefile: 9
file content (182 lines) | stat: -rw-r--r-- 5,173 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# distutils: language = c++
# cython: c_string_type=unicode, c_string_encoding=utf8

from libcpp.vector cimport vector
from libcpp.string cimport string
from libc.stdint cimport uint32_t, int64_t
from libcpp cimport bool
from cpython.version cimport PY_MAJOR_VERSION
cimport numpy as np
np.import_array()

from cpython cimport PyObject, Py_INCREF
from cython.operator cimport dereference as deref, preincrement as inc

cdef extern from "pdal/pdal_config.hpp" namespace "pdal::Config":
    cdef int versionMajor() except +
    cdef int versionMinor() except +
    cdef int versionPatch() except +
    cdef string sha1() except+
    cdef string debugInformation() except+
    cdef string pluginInstallPath() except+
    cdef string versionString() except+

def getVersionString():
    return versionString()

def getVersionMajor():
    return versionMajor()
def getVersionMinor():
    return versionMinor()
def getVersionPatch():
    return versionPatch()
def getSha1():
    return sha1()
def getDebugInformation():
    return debugInformation()
def getPluginInstallPath():
    return pluginInstallPath()

cdef extern from "PyArray.hpp" namespace "pdal::python":
    cdef cppclass Array:
        Array(object) except +
        void* getPythonArray() except+

cdef extern from "PyPipeline.hpp" namespace "libpdalpython":
    cdef cppclass Pipeline:
        Pipeline(const char* ) except +
        Pipeline(const char*, vector[Array*]& ) except +
        int64_t execute() except +
        bool validate() except +
        string getPipeline() except +
        string getMetadata() except +
        string getSchema() except +
        string getLog() except +
        vector[Array*] getArrays() except +
        int getLogLevel()
        void setLogLevel(int)



cdef class PyArray:
    cdef Array *thisptr
    def __cinit__(self, object array):
        self.thisptr = new Array(array)
    def __dealloc__(self):
        del self.thisptr

cdef extern from "PyDimension.hpp":
    ctypedef struct Dimension:
        string name;
        string description;
        int size;
        string type;
##         string units; // Not defined by PDAL yet

    cdef vector[Dimension] getValidDimensions() except +


def getDimensions():
        cdef vector[Dimension] c_dims;
        c_dims = getValidDimensions()
        output = []
        cdef vector[Dimension].iterator it = c_dims.begin()
        while it != c_dims.end():
            ptr = deref(it)
            d = {}
            d['name'] = ptr.name
            d['description'] = ptr.description
            kind = ptr.type + str(ptr.size)
            d['dtype'] = np.dtype(kind)
            ptr = deref(it)
            output.append(d)
            inc(it)
        return output


cdef class PyPipeline:
    cdef Pipeline *thisptr      # hold a c++ instance which we're wrapping


    def __cinit__(self, unicode json, list arrays=None):
        cdef char* x = NULL
        cdef int n_arrays;
        if arrays:
            n_arrays = len(arrays)

        cdef vector[Array*] c_arrays;
        cdef np.ndarray np_array;
        cdef Array* a

        if arrays is not None:
            for array in arrays:
                a = new Array(array)
                c_arrays.push_back(a)

        if PY_MAJOR_VERSION >= 3:
            if arrays:
                self.thisptr = new Pipeline(json.encode('UTF-8'), c_arrays)
            else:
                self.thisptr = new Pipeline(json.encode('UTF-8'))
        else:
            if arrays:
                self.thisptr = new Pipeline(json, c_arrays)
            else:
                self.thisptr = new Pipeline(json)
#        if arrays:
#            self.thisptr = new Pipeline(json.encode('UTF-8'), c_arrays)
#        else:
#            self.thisptr = new Pipeline(json.encode('UTF-8'))

    def __dealloc__(self):
        del self.thisptr

    property pipeline:
        def __get__(self):
            return self.thisptr.getPipeline()

    property metadata:
        def __get__(self):
            return self.thisptr.getMetadata()

    property loglevel:
        def __get__(self):
            return self.thisptr.getLogLevel()
        def __set__(self, v):
            self.thisptr.setLogLevel(v)

    property log:
        def __get__(self):

            return self.thisptr.getLog()

    property schema:
        def __get__(self):
            import json

            j = self.thisptr.getSchema()
            return json.loads(j)

    property arrays:
        def __get__(self):
            v = self.thisptr.getArrays()
            output = []
            cdef vector[Array*].iterator it = v.begin()
            cdef Array* a
            while it != v.end():
                ptr = deref(it)
                a = ptr#.get()
                o = a.getPythonArray()
                output.append(<object>o)
                inc(it)
            return output

    def execute(self):
        if not self.thisptr:
            raise Exception("C++ Pipeline object not constructed!")
        return self.thisptr.execute()

    def validate(self):
        if not self.thisptr:
            raise Exception("C++ Pipeline object not constructed!")
        return self.thisptr.validate()