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
  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
class Input_Reader:
    logger = get_logger()

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

        Parameters
        ----------
        uri : str
            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
            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

        Parameters
        ----------
        filepath : str
            The path to the shapefile to read

        Returns
        -------
        layer
            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.

        Parameters
        ----------
        filepath : str
            The path to the GeoJson file to read

        Returns
        -------
        layer
            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):
        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.

        Parameters
        ----------
        file : str
            The path to the geopackage file to read

        Layername : str
            The layer to load from the Geopackage

        Returns
        -------
        layer
            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.

        Parameters
        ----------
        file : str
            The path to the file geodatabase to read.
        layername : str
            The layer to load from the database.
        """
        layer = Input_Reader.fileBasedDB(file, layername, 'ESRI File Geodatabase')
        return layer

filegdb(file, layername)

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

Parameters

file : str The path to the file geodatabase to read. layername : str The layer to load from the database.

Source code in python\engine\inputs.py
124
125
126
127
128
129
130
131
132
133
134
135
136
def filegdb(file, layername):
    """
    A function that read a layer from an ESRI File Geodatabase using the OpenFileGDB driver.

    Parameters
    ----------
    file : str
        The path to the file geodatabase to read.
    layername : str
        The layer to load from the database.
    """
    layer = Input_Reader.fileBasedDB(file, layername, 'ESRI File Geodatabase')
    return layer

geojson(filepath)

A function that reads a GeoJson file.

Parameters

filepath : str The path to the GeoJson file to read

Returns

layer A QgsVectorLayer object containing data from the GeoJson file.

Source code in python\engine\inputs.py
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
def geojson(filepath):
    """
    A function that reads a GeoJson file.

    Parameters
    ----------
    filepath : str
        The path to the GeoJson file to read

    Returns
    -------
    layer
        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

file : str The path to the geopackage file to read

str

The layer to load from the Geopackage

Returns

layer A QgsVectorLayer object containing data from the geopackage.

Source code in python\engine\inputs.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def geopackage(file, layername):
    """
    A function that reads alayer from a Geopackage file.

    Parameters
    ----------
    file : str
        The path to the geopackage file to read

    Layername : str
        The layer to load from the Geopackage

    Returns
    -------
    layer
        A QgsVectorLayer object containing data from the geopackage.
    """

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

shapefile(filepath)

A function that reads a shapefile

Parameters

filepath : str The path to the shapefile to read

Returns

layer A QgsVectorLayer containing data from the shapefile.

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

    Parameters
    ----------
    filepath : str
        The path to the shapefile to read

    Returns
    -------
    layer
        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()

wfs(uri)

A function that reads a WFS service.

Parameters

uri : str 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 A QgsVectorLayer object containing data from the WFS service.

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
def wfs(uri):
    """
    A function that reads a WFS service.

    Parameters
    ----------
    uri : str
        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
        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()