0

I am dealing with a vector layer containing ~3400 points. All of them have an individual identifying code (Name) and another one (Network) they share with others of the dataset since they belong a larger network. Thus the attribute table looks somewhat like this:

 Network  Name   AdditionalInformation
 a        Name1  Some
 a        Name2  Information
 a        Name3  Is
 a        Name4  Written
 b        Name5  In 
 b        Name6  This
 b        Name7  Field

What needs to be done is calculating the maximum distance between several points belonging to the same network (these are more than 500). However, my knowledge of calculation tools of this kind is limited to QGIS' Distance Matrix tool and I do not see a suitable solution for this entire dataset.

What might be the best way to proceed here?

Is there any way, for instance, to use the Python console in order to calculate distance matrices for each and every network based on the common denominator Network and save all maximum values to a CSV file afterwards?


I split the layer into smaller ones and saved them accordingly. The script, copied from Joseph, is now:

import glob, os, processing

path = "G:\ZIM\Analyse_Foerderprogramme\_ZIM-SOLO und ZIM-KOOP 2014\GIS\Adressen_TOMTOM\Split"
output_path = "G:\ZIM\Analyse_Foerderprogramme\_ZIM-SOLO und ZIM-KOOP 2014\GIS\Adressen_TOMTOM\Split\Ergebnisse"
field_1 = "NAME"
field_2 = "GEOCODE"

for layer in glob.glob(path + "*.shp"):
    processing.runalg("qgis:distancematrix", layer, field_1, layer, field_2, 2, 0, output_path + os.path.basename(layer) + ".csv")

Yet nothing is happening so far after typing it into the console. I am but a beginner in the usage of the Python console so surely I made a mistake. What next?


The earlier mistake was the use of \ instead of the proper /. Now, the script (with more readable paths) looks like:

import glob, os, processing

path = "H:/Split/"
output_path = "H:/Split/Ergebnisse/"
field_1 = "NAME"
field_2 = "GEOCODE"

for layer in glob.glob(path + "*.shp"):
    processing.runalg("qgis:distancematrix", layer, field_1, layer, field_2, 2, 0, output_path + os.path.basename(layer) + ".csv")

This results in the error [Errno 2] No such file or directory: 'H:/Split/Ergebnisse/Adressen_projiziert_Info_NAME__G533.shp.csv' See log for more details

Why does Python look for a CSV file at the very beginning instead of calculating first and writing the results in a file of this sort second?


Joseph's script works. However, the error message remains the same for the file I'm trying to work with. Can anyone tell me what the log message might point to?

2016-09-23T16:22:45 2   Uncaught error while executing algorithm
            Traceback (most recent call last):
              File "C:/PROGRA~1/QGIS2~1.14/apps/qgis-ltr/./python/plugins\processing\core\GeoAlgorithm.py", line 203, in execute
                self.processAlgorithm(progress)
              File "C:/PROGRA~1/QGIS2~1.14/apps/qgis-ltr/./python/plugins\processing\algs\qgis\PointDistance.py", line 89, in processAlgorithm
                self.writer = outputFile.getTableWriter([])
              File "C:/PROGRA~1/QGIS2~1.14/apps/qgis-ltr/./python/plugins\processing\core\outputs.py", line 245, in getTableWriter
                return TableWriter(self.value, self.encoding, fields)
              File "C:/PROGRA~1/QGIS2~1.14/apps/qgis-ltr/./python/plugins\processing\tools\vector.py", line 668, in __init__
                with open(self.fileName, 'wb') as csvFile:
            IOError: [Errno 2] No such file or directory: 'C:/Users/klinge/GIS/Split/Ergebnisse/Ausgangsdaten_Vorhaben__ZKN060503.csv'

2016-09-23T16:22:45 1   There were errors executing the algorithm.

The error message did not occur on another (Linux) computer.

9
  • Looks like you need to escape your backslash by either inserting it twice before your desired directory or use r before your path. I.e. path = "G:\ZIM\Analyse_Foerderprogramme\_ZIM-SOLO und ZIM-KOOP 2014\GIS\Adressen_TOMTOM\\Split"; or path = r'G:\ZIM\Analyse_Foerderprogramme\_ZIM-SOLO und ZIM-KOOP 2014\GIS\Adressen_TOMTOM\Split'
    – Joseph
    Commented Sep 23, 2016 at 11:23
  • The error was another one, the path now works. However, there is the next error message after running the script.
    – T. K.
    Commented Sep 23, 2016 at 11:54
  • I've edited my post, the output name was incorrect. Although I'm not quite sure if that will sove the problem as it was still working for me...
    – Joseph
    Commented Sep 23, 2016 at 12:06
  • You're right, it doesn't. The error message remains the same.
    – T. K.
    Commented Sep 23, 2016 at 12:26
  • Try moving the shapefiles and set the paths to your main C:/ drive. I'm guessing the H:/ drive is external?
    – Joseph
    Commented Sep 23, 2016 at 12:30

1 Answer 1

1

I would probably do the following:

  1. Load your point layer into QGIS and run the Split vector layer with Network as the Unique ID field. The results will be saved to a directory.

  2. Run the following code in the Python Console. It uses the same Distance matrix tool but only calculates the points for each Network and outputs the statistics including the maximum distance. Change the paths and field names accordingly:

    import glob, os, processing
    
    path = "C:/Users/You/Desktop/Folder/"
    output_path = "C:/Users/You/Desktop/Folder/Results/"
    field_1 = "Field_1"
    field_2 = "Field_2"
    
    for layer in glob.glob(path + "*.shp"):
        processing.runalg("qgis:distancematrix", layer, field_1, layer, field_2, 2, 0, output_path + os.path.splitext(os.path.basename(layer))[0] + ".csv")
    
2
  • Thank you for taking the time; unfortunately, I am stuck at the next step (see edit).
    – T. K.
    Commented Sep 23, 2016 at 11:19
  • @T.K. If you have another question then please ask it as a new question so that you can upvote and Accept Joseph's answer to this one.
    – PolyGeo
    Commented Feb 23, 2018 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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