4

I am a total beginner with Python and programming in GIS in general. I have point data and I want to compare in which month people walked the longest distance. It is a relative large dataset and I want to do it relatively automatically. This is my approach:

import processing

layer_anfang = QgsProject.instance().mapLayersByName("MVG_2016_Wetter_Januar")[0]
layer_ende = QgsProject.instance().mapLayersByName("MVG_2016_wetter_januar_ende")[0]

for i in range(0, 5):    
    startlat = layer_anfang.getFeature(i).attribute(3)
    startlon = layer_anfang.getFeature(i).attribute(4)
    endlat = layer_ende.getFeature(i).attribute(5)
    endlon = layer_ende.getFeature(i).attribute(6)

    x = startlon, startlat, endlon, endlat
    print(x)

processing.run("native:shortestpathpointtopoint", \
{'INPUT':'C:\\Users\\.......', \
'STRATEGY':0,'DIRECTION_FIELD':'','VALUE_FORWARD':'','VALUE_BACKWARD':'','VALUE_BOTH':'','DEFAULT_DIRECTION':2,'SPEED_FIELD':'','DEFAULT_SPEED':50,'TOLERANCE':0, \
'START_POINT':'startlon, startlat [EPSG:4326]','END_POINT':' 11.47498, 48.10944 [EPSG:4326]',\
'OUTPUT':'C:/Users/.....'})

The plan is to iterate over the attribute table and get the lon/lat data from my attribute table and then parse it into the parameters for "START POINT" and "END_POINT". As you can see in this example I did it for the Start_Point, the END_Point is as it was before. I always get the error wrong parameter for START_Point, even though, if I print it for example it is in the right style.

Must I cast any datatype or something on the variable or is this all complete wrong?

1
  • Where does Start_Point come from? It's not in your code, as we can't say why it's not working. Also, you have "START POINT", Start_Point and START_Point. Make up your mind :-)
    – wingnut
    May 1, 2021 at 13:35

1 Answer 1

5

For 'START_POINT' parameter (and 'END_POINT' parameter if necessary), use this:

'START_POINT': f"{startlon},{startlat} [EPSG:4326]",
'END_POINT': f"{endlon},{endlat} [EPSG:4326]"

I guess, processing.run should be located in for loop.

import processing

layer_anfang = QgsProject.instance().mapLayersByName("MVG_2016_Wetter_Januar")[0]
layer_ende = QgsProject.instance().mapLayersByName("MVG_2016_wetter_januar_ende")[0]

for i in range(0, 5):    
    startlat = layer_anfang.getFeature(i).attribute(3)
    startlon = layer_anfang.getFeature(i).attribute(4)
    endlat = layer_ende.getFeature(i).attribute(5)
    endlon = layer_ende.getFeature(i).attribute(6)

    x = startlon, startlat, endlon, endlat
    print(x)

    processing.run("native:shortestpathpointtopoint", 
                   {'INPUT':'C:\\Users\\.......', 
                    'STRATEGY':0,
                    'DIRECTION_FIELD':'',
                    'VALUE_FORWARD':'',
                    'VALUE_BACKWARD':'',
                    'VALUE_BOTH':'',
                    'DEFAULT_DIRECTION':2,
                    'SPEED_FIELD':'',
                    'DEFAULT_SPEED':50,
                    'TOLERANCE':0, 
                    'START_POINT': f"{startlon},{startlat} [EPSG:4326]",
                    'END_POINT': f"{endlon},{endlat} [EPSG:4326]",
                    'OUTPUT':'C:/Users/.....'})
1
  • thank you very much
    – Ben Cyffka
    May 2, 2021 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.