lundi 14 juin 2021

Python - using AND in an IF statement for coordinate conversion

I'm trying to get my python function to recognise which coordinate conversion to execute based on the last part of the filename - either 27700 for British National Grid or 29903 for Irish Grid. The filename is extracted and broken up using

                base_filename = os.path.basename(key)
                filename, extension = os.path.splitext(base_filename)
                device_parts = filename.split("_")
                self.device_name = device_parts[1]
                self.device_type = device_parts[2]

and the filename used is always either test_data_27700 or test_data_29903

Initially I tried:

for row in rows:
                
                fid = uuid.uuid4() # assign new UUID to each row

                
                if line_count == 0:
                    keys = row
                    print('keys', keys)
                    line_count += 1
                elif self.device_type == 27700:
                    inProj = Proj(init='epsg:27700') # British National Grid
                    outProj = Proj(init='epsg:4326') # WGS84
                    x1, y1 = row[1], row[2]  # easting, northing
                    x2, y2 = transform(inProj, outProj, x1, y1)
                    row[1], row[2] = y2, x2
                else:
                    inProj = Proj(init='epsg:29903') # Irish Grid
                    outProj = Proj(init='epsg:4326') # WGS84
                    x1, y1 = row[1], row[2]  # easting, northing
                    x2, y2 = transform(inProj, outProj, x1, y1)
                    row[1], row[2] = y2, x2
            
                    
                    json_doc = {}

But I realised the elif statement was being skipped due to the if being true. Is there a way I can have if line_count == 0 AND self.device_type == 27700: ??

For example, something like this:

for row in rows:
                
                fid = uuid.uuid4() # assign new UUID to each row

                
                if line_count == 0 and self.device_type == 27700:
                    keys = row
                    print('keys', keys)
                    line_count += 1
                    inProj = Proj(init='epsg:27700') # British National Grid
                    outProj = Proj(init='epsg:4326') # WGS84
                    x1, y1 = row[1], row[2]  # easting, northing
                    x2, y2 = transform(inProj, outProj, x1, y1)
                    row[1], row[2] = y2, x2
                else:
                    inProj = Proj(init='epsg:29903') # Irish Grid
                    outProj = Proj(init='epsg:4326') # WGS84
                    x1, y1 = row[1], row[2]  # easting, northing
                    x2, y2 = transform(inProj, outProj, x1, y1)
                    row[1], row[2] = y2, x2
            
                    
                    json_doc = {}

Note - I have also tried if self.device_type == 27700 to check it is working but it throws me the error that the input must be a list, array, tuple or scalar

How can I get python to recognise that if self.device_type is equal to 27700 or 29903, to then run ____ or ____ in terms of coordinate conversion?

Aucun commentaire:

Enregistrer un commentaire