Skip to content

Inputs

The Input_Reader class is the base object to read different file formats

Input_Reader

Source code in python\engine\inputs.py
 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
class Input_Reader:
    logger = get_logger()

    def wfs(uri):
        """
        A function that reads a WFS service.

        Args:
            uri (string): The uri can be a HTTP url to a WFS server (http://foobar/wfs?TYPENAME=xxx&SRSNAME=yyy[&FILTER=zzz) or a URI constructed using the QgsDataSourceURI class with the following parameters: - url=string (mandatory): HTTP url to a WFS server endpoint. e.g http://foobar/wfs - typename=string (mandatory): WFS typename - srsname=string (recommended): SRS like ‘EPSG:XXXX’ - username=string - password=string - authcfg=string - version=auto/1.0.0/1.1.0/2.0.0 -sql=string: full SELECT SQL statement with optional WHERE, ORDER BY and possibly with JOIN if supported on server - filter=string: QGIS expression or OGC/FES filter - restrictToRequestBBOX=1: to download only features in the view extent (or more generally in the bounding box of the feature iterator) - maxNumFeatures=number - IgnoreAxisOrientation=1: to ignore EPSG axis order for WFS 1.1 or 2.0 - InvertAxisOrientation=1: to invert axis order - hideDownloadProgressDialog=1: to hide the download progress dialog

        Returns:
            layer (QgsVectorLayer): A QgsVectorLayer object containing data from the WFS service.
        """

        try:
            logger.info(f'Reading WFS layer: {uri}')
            layer = QgsVectorLayer(uri, "WFS_Layer" , 'WFS')
            logger.info("Finished reading the WFS service")
            return layer
        except Exception as error:
            logger.error(f'An error occured reading the WFS {uri}')
            logger.error(f'{type(error).__name__}{str(error)}')
            logger.critical("Program terminated")
            script_failed()

    def shapefile(filepath):
        """
        A function that reads a shapefile

        Args:
            filepath (string): The path to the shapefile to read

        Returns:
            layer (QgsVectorLayer): A QgsVectorLayer containing data from the shapefile.
        """
        logger.info(f'Reading file: {filepath}')
        try: 
            layer =  QgsVectorLayer(filepath, f'QgsLayer_ {str(randrange(1000))}', "ogr")
            logger.info("Finished reading file")
            return layer
        except Exception as error:
            logger.error(f'An error occured opening file {filepath}')
            logger.error(f'{type(error).__name__}{str(error)}')
            logger.critical("Program terminated")
            script_failed()


    def geojson(filepath):
        """
        A function that reads a GeoJson file.

        Args:
            filepath (string): The path to the GeoJson file to read

        Returns:
            layer (QgsVectorLayer): A QgsVectorLayer object containing data from the GeoJson file.
        """

        logger.info(f'Reading file: {filepath}')
        try:
            layer =  QgsVectorLayer(filepath, f'QgsLayer_ {str(randrange(1000))}', "ogr")
            logger.info("Finished reading file")
            return layer
        except Exception as error:
            logger.info(f'An error occured opening file {filepath}')
            logger.error(f'{type(error).__name__}{str(error)}')
            logger.critical("Program terminated")
            script_failed()

    def fileBasedDB(file, layername, format):

        if Path(file).exists() == False:
            logger.error(f'{file} does not exist')
            script_failed()

        logger.info(f'Reading {format}: {file}')
        try:
            uri = f'{file}|layername={layername}'
            layer = QgsVectorLayer(uri, f'QgsLayer_{str(randrange(1000))}', 'ogr')
            logger.info(f'Finished reading {format}')
            return layer
        except Exception as error:
            logger.info(f'An error occured opening {format}: {file}')
            logger.error(type(error).__name__ + " – " + str(error))
            logger.critical("Program terminated")
            script_failed()

    def geopackage(file, layername):
        """
        A function that reads alayer from a Geopackage file.

        Args: 
            file (string) : The path to the geopackage file to read
            Layername (string): The layer to load from the Geopackage

        Returns:
            layer (QgsVectorLayer): A QgsVectorLayer object containing data from the geopackage.
        """

        layer = Input_Reader.fileBasedDB(file, layername, 'Geopackage')
        return layer

    def filegdb(file, layername):
        """
        A function that read a layer from an ESRI File Geodatabase using the OpenFileGDB driver.

        Args:
            file (string): The path to the file geodatabase to read.
            layername (string): The layer to load from the database.

        Returns:
            layer (QgsVectorLayer): A QgsVectorLayer object containing the data
        """
        layer = Input_Reader.fileBasedDB(file, layername, 'ESRI File Geodatabase')
        return layer

    def postGIS(connection, dbname, schema, table, geometryname='geom'):
        """        
        A function that reads a layer from a PostGIS database

        Args:
            connection (string): connection name in settings
            dbname (string): database name
            schema (string): schema name
            table (string): table name
            geometryname (string,optional): name of geometry column. defaults to "geom"

        Returns:
            A QgsVectorLayer object containing data from the postgis database
        """
        layer = Input_Reader.sqlDB('Postgres', connection, dbname, schema, table, geometryname)
        return layer

    def mssql(connection, dbname, schema, table, geometryname='Geometri'):
        """        
        A function that reads a layer from a MSSQL database

        Args:
            connection (string): connection name in settings
            dbname (string): database name
            schema (string): schema name
            table (string): table name
            geometryname (string,optional): name of geometry column. defaults to "geom"

        Returns:
            A QgsVectorLayer object containing data from the postgis database
        """
        layer = Input_Reader.sqlDB('MSSQL', connection, dbname, schema, table, geometryname)
        return layer

    def sqlDB(db_type, connection, dbname, schema, table, geometryname="geom"):
        """        
        A function that reads a layer from a SQL database

        Args:
            db_type (string): Type of SQL database. Postgres and MSSQL supported.
            connection (string): connection name in settings
            dbname (string): database name
            schema (string): schema name
            table (string): table name
            geometryname (string,optional): name of geometry column. defaults to "geom"

        Returns:
            A QgsVectorLayer object containing data from the postgis database
        """

        logger.info(f'Importing {schema}.{table} layer from {db_type}')

        try:
            config = get_config()
            dbConnection = config['DatabaseConnections'][connection]
            uri = QgsDataSourceUri()
            logger.info(f'Reading from {db_type} database {dbname}, table {schema}.{table}')

            uri.setConnection(dbConnection["host"], dbConnection["port"], dbname, dbConnection["user"], dbConnection["password"])
            uri.setDataSource(schema, table, geometryname)

            layer = QgsVectorLayer(uri.uri(False), "layer", f"{db_type.lower()}")

            logger.info(f'Import from {db_type} completed')
            if layerHasFeatures(layer):
                logger.info(f'Imported {str(layer.featureCount())} features from {db_type}')
            return layer    

        except Exception as error:
            logger.error("An error occured importing from {db_type}")
            logger.error(f'{type(error).__name__}{str(error)}')
            logger.critical("Program terminated")
            script_failed()

filegdb(file, layername)

A function that read a layer from an ESRI File Geodatabase using the OpenFileGDB driver.

Parameters:

Name Type Description Default
file string

The path to the file geodatabase to read.

required
layername string

The layer to load from the database.

required

Returns:

Name Type Description
layer QgsVectorLayer

A QgsVectorLayer object containing the data

Source code in python\engine\inputs.py
113
114
115
116
117
118
119
120
121
122
123
124
125
def filegdb(file, layername):
    """
    A function that read a layer from an ESRI File Geodatabase using the OpenFileGDB driver.

    Args:
        file (string): The path to the file geodatabase to read.
        layername (string): The layer to load from the database.

    Returns:
        layer (QgsVectorLayer): A QgsVectorLayer object containing the data
    """
    layer = Input_Reader.fileBasedDB(file, layername, 'ESRI File Geodatabase')
    return layer

geojson(filepath)

A function that reads a GeoJson file.

Parameters:

Name Type Description Default
filepath string

The path to the GeoJson file to read

required

Returns:

Name Type Description
layer QgsVectorLayer

A QgsVectorLayer object containing data from the GeoJson file.

Source code in python\engine\inputs.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def geojson(filepath):
    """
    A function that reads a GeoJson file.

    Args:
        filepath (string): The path to the GeoJson file to read

    Returns:
        layer (QgsVectorLayer): A QgsVectorLayer object containing data from the GeoJson file.
    """

    logger.info(f'Reading file: {filepath}')
    try:
        layer =  QgsVectorLayer(filepath, f'QgsLayer_ {str(randrange(1000))}', "ogr")
        logger.info("Finished reading file")
        return layer
    except Exception as error:
        logger.info(f'An error occured opening file {filepath}')
        logger.error(f'{type(error).__name__}{str(error)}')
        logger.critical("Program terminated")
        script_failed()

geopackage(file, layername)

A function that reads alayer from a Geopackage file.

Parameters:

Name Type Description Default
file string)

The path to the geopackage file to read

required
Layername string

The layer to load from the Geopackage

required

Returns:

Name Type Description
layer QgsVectorLayer

A QgsVectorLayer object containing data from the geopackage.

Source code in python\engine\inputs.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def geopackage(file, layername):
    """
    A function that reads alayer from a Geopackage file.

    Args: 
        file (string) : The path to the geopackage file to read
        Layername (string): The layer to load from the Geopackage

    Returns:
        layer (QgsVectorLayer): A QgsVectorLayer object containing data from the geopackage.
    """

    layer = Input_Reader.fileBasedDB(file, layername, 'Geopackage')
    return layer

mssql(connection, dbname, schema, table, geometryname='Geometri')

A function that reads a layer from a MSSQL database

Parameters:

Name Type Description Default
connection string

connection name in settings

required
dbname string

database name

required
schema string

schema name

required
table string

table name

required
geometryname (string, optional)

name of geometry column. defaults to "geom"

'Geometri'

Returns:

Type Description

A QgsVectorLayer object containing data from the postgis database

Source code in python\engine\inputs.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def mssql(connection, dbname, schema, table, geometryname='Geometri'):
    """        
    A function that reads a layer from a MSSQL database

    Args:
        connection (string): connection name in settings
        dbname (string): database name
        schema (string): schema name
        table (string): table name
        geometryname (string,optional): name of geometry column. defaults to "geom"

    Returns:
        A QgsVectorLayer object containing data from the postgis database
    """
    layer = Input_Reader.sqlDB('MSSQL', connection, dbname, schema, table, geometryname)
    return layer

postGIS(connection, dbname, schema, table, geometryname='geom')

A function that reads a layer from a PostGIS database

Parameters:

Name Type Description Default
connection string

connection name in settings

required
dbname string

database name

required
schema string

schema name

required
table string

table name

required
geometryname (string, optional)

name of geometry column. defaults to "geom"

'geom'

Returns:

Type Description

A QgsVectorLayer object containing data from the postgis database

Source code in python\engine\inputs.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def postGIS(connection, dbname, schema, table, geometryname='geom'):
    """        
    A function that reads a layer from a PostGIS database

    Args:
        connection (string): connection name in settings
        dbname (string): database name
        schema (string): schema name
        table (string): table name
        geometryname (string,optional): name of geometry column. defaults to "geom"

    Returns:
        A QgsVectorLayer object containing data from the postgis database
    """
    layer = Input_Reader.sqlDB('Postgres', connection, dbname, schema, table, geometryname)
    return layer

shapefile(filepath)

A function that reads a shapefile

Parameters:

Name Type Description Default
filepath string

The path to the shapefile to read

required

Returns:

Name Type Description
layer QgsVectorLayer

A QgsVectorLayer containing data from the shapefile.

Source code in python\engine\inputs.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def shapefile(filepath):
    """
    A function that reads a shapefile

    Args:
        filepath (string): The path to the shapefile to read

    Returns:
        layer (QgsVectorLayer): A QgsVectorLayer containing data from the shapefile.
    """
    logger.info(f'Reading file: {filepath}')
    try: 
        layer =  QgsVectorLayer(filepath, f'QgsLayer_ {str(randrange(1000))}', "ogr")
        logger.info("Finished reading file")
        return layer
    except Exception as error:
        logger.error(f'An error occured opening file {filepath}')
        logger.error(f'{type(error).__name__}{str(error)}')
        logger.critical("Program terminated")
        script_failed()

sqlDB(db_type, connection, dbname, schema, table, geometryname='geom')

A function that reads a layer from a SQL database

Parameters:

Name Type Description Default
db_type string

Type of SQL database. Postgres and MSSQL supported.

required
connection string

connection name in settings

required
dbname string

database name

required
schema string

schema name

required
table string

table name

required
geometryname (string, optional)

name of geometry column. defaults to "geom"

'geom'

Returns:

Type Description

A QgsVectorLayer object containing data from the postgis database

Source code in python\engine\inputs.py
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
def sqlDB(db_type, connection, dbname, schema, table, geometryname="geom"):
    """        
    A function that reads a layer from a SQL database

    Args:
        db_type (string): Type of SQL database. Postgres and MSSQL supported.
        connection (string): connection name in settings
        dbname (string): database name
        schema (string): schema name
        table (string): table name
        geometryname (string,optional): name of geometry column. defaults to "geom"

    Returns:
        A QgsVectorLayer object containing data from the postgis database
    """

    logger.info(f'Importing {schema}.{table} layer from {db_type}')

    try:
        config = get_config()
        dbConnection = config['DatabaseConnections'][connection]
        uri = QgsDataSourceUri()
        logger.info(f'Reading from {db_type} database {dbname}, table {schema}.{table}')

        uri.setConnection(dbConnection["host"], dbConnection["port"], dbname, dbConnection["user"], dbConnection["password"])
        uri.setDataSource(schema, table, geometryname)

        layer = QgsVectorLayer(uri.uri(False), "layer", f"{db_type.lower()}")

        logger.info(f'Import from {db_type} completed')
        if layerHasFeatures(layer):
            logger.info(f'Imported {str(layer.featureCount())} features from {db_type}')
        return layer    

    except Exception as error:
        logger.error("An error occured importing from {db_type}")
        logger.error(f'{type(error).__name__}{str(error)}')
        logger.critical("Program terminated")
        script_failed()

wfs(uri)

A function that reads a WFS service.

Parameters:

Name Type Description Default
uri string

The uri can be a HTTP url to a WFS server (http://foobar/wfs?TYPENAME=xxx&SRSNAME=yyy[&FILTER=zzz) or a URI constructed using the QgsDataSourceURI class with the following parameters: - url=string (mandatory): HTTP url to a WFS server endpoint. e.g http://foobar/wfs - typename=string (mandatory): WFS typename - srsname=string (recommended): SRS like ‘EPSG:XXXX’ - username=string - password=string - authcfg=string - version=auto/1.0.0/1.1.0/2.0.0 -sql=string: full SELECT SQL statement with optional WHERE, ORDER BY and possibly with JOIN if supported on server - filter=string: QGIS expression or OGC/FES filter - restrictToRequestBBOX=1: to download only features in the view extent (or more generally in the bounding box of the feature iterator) - maxNumFeatures=number - IgnoreAxisOrientation=1: to ignore EPSG axis order for WFS 1.1 or 2.0 - InvertAxisOrientation=1: to invert axis order - hideDownloadProgressDialog=1: to hide the download progress dialog

required

Returns:

Name Type Description
layer QgsVectorLayer

A QgsVectorLayer object containing data from the WFS service.

Source code in python\engine\inputs.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def wfs(uri):
    """
    A function that reads a WFS service.

    Args:
        uri (string): The uri can be a HTTP url to a WFS server (http://foobar/wfs?TYPENAME=xxx&SRSNAME=yyy[&FILTER=zzz) or a URI constructed using the QgsDataSourceURI class with the following parameters: - url=string (mandatory): HTTP url to a WFS server endpoint. e.g http://foobar/wfs - typename=string (mandatory): WFS typename - srsname=string (recommended): SRS like ‘EPSG:XXXX’ - username=string - password=string - authcfg=string - version=auto/1.0.0/1.1.0/2.0.0 -sql=string: full SELECT SQL statement with optional WHERE, ORDER BY and possibly with JOIN if supported on server - filter=string: QGIS expression or OGC/FES filter - restrictToRequestBBOX=1: to download only features in the view extent (or more generally in the bounding box of the feature iterator) - maxNumFeatures=number - IgnoreAxisOrientation=1: to ignore EPSG axis order for WFS 1.1 or 2.0 - InvertAxisOrientation=1: to invert axis order - hideDownloadProgressDialog=1: to hide the download progress dialog

    Returns:
        layer (QgsVectorLayer): A QgsVectorLayer object containing data from the WFS service.
    """

    try:
        logger.info(f'Reading WFS layer: {uri}')
        layer = QgsVectorLayer(uri, "WFS_Layer" , 'WFS')
        logger.info("Finished reading the WFS service")
        return layer
    except Exception as error:
        logger.error(f'An error occured reading the WFS {uri}')
        logger.error(f'{type(error).__name__}{str(error)}')
        logger.critical("Program terminated")
        script_failed()