5

I'm using QGIS and the points on this map represent different periods of the day in which coyotes were observed. I am trying to figure out if coyotes are active at different times of the day based on their geographical distance from the center of the downtown area.

Is there a way to calculate the average distance of all the points in a shapefile from a central point?

enter image description here

1

3 Answers 3

5

You might also use the processing tools to create a distance matrix:

Centroid and data in separate layers

It is a bit easier if you have the data in separate layers, say called data and centroid

  • Select Vector -> Analysis tools -> Distance Matrix to open the tool.

  • Set the Input point layer to data and the Target point layer to centroid

  • if you have a name column in data set this to be the Input unique ID field

  • Run the algorithm - this adds the distance matrix (table) to your layers with columns InputID (containing the names of the points if you specified them), TargetID and Distance. In this case the matrix only contains the distances between your data points and the centroid.

Centroid and data in the same layer

If you have the centroid in the same layer as the other observations then you need to note down an identifier - for instance the fid (using old fashioned tech such as pen & paper). lets call it fid = FID_centroid

  • Select Vector -> Analysis tools -> Distance Matrix to open the tool.

  • Set your data as both - the input point layer and target point layer

  • Set the unique ID field of Input and Target to the name of the field of the identifier you noted down earlier

  • Run the algorithm - this adds the distance matrix (table) to your layers with columns InputID (containing the names of the points if you specified them), TargetID and Distance. BUT! The table contains the distance from every point to every other point in the data set, so you will have to select the ones with the inputID of the centroid

  • from the Distance Matrix and select for the InputID = FID_centroid. You can then copy those rows out or save them as a csv file.

Hope this helps.

5

Let's guess you have one point layer 'downtown', consisting of one central point (red star). Then you have a second layer 'coyotes' with many observation points. Now you want to measure the distance from each point of 'coyotes' to 'downtown' and then find the average.

To do this, you can use an expression - change the names of the layer accordingly:

aggregate(
    @layer, 
    'mean', 
    distance( 
        $geometry, 
        geometry(
            get_feature_by_id('downtown', 1)
        )
    )
)

See the screenshot, where I already calculated a field distance_to_downtown and now add another field average_distance_to_downtown - this one will be the same for each feature, of course:

enter image description here

4

You can also use python:

import matplotlib.pyplot as plt

#Change these three lines to match your layer names and field:
obs = QgsProject.instance().mapLayersByName('data_104')[0]
centre = QgsProject.instance().mapLayersByName('New scratch layer')[0]
datefield = 'eventDate'

cgeom = [f.geometry() for f in centre.getFeatures()][0] #Fetch the one geometry in centre layer

data = [[f[datefield].toPyDate(), f.geometry().distance(cgeom)] for f in obs.getFeatures()]
#>>>data[0]
#[datetime.date(2015, 5, 2), 4434.566255219079] #A list of lists of dates and distances

dates, distances = zip(*data) #Unzip/unpack the list into two lists, one with dates and one with distances
plt.plot(dates, distances, 'o')
plt.show()

enter image description here

4
  • 1
    thank you so much for your idea. Can you tell me please what this statement zip(*data) actually does? Why do I need to use a * in it?
    – Taras
    Commented Dec 22, 2020 at 9:45
  • 1
    It's unzipping/unpacking the lists. From a list of lists like [datetime.date(2015, 5, 2), 4434.566255219079] to two list, one with all dates and one with all distances
    – Bera
    Commented Dec 22, 2020 at 9:53
  • 1
    gis.stackexchange.com/questions/247480/…
    – Bera
    Commented Dec 22, 2020 at 10:06
  • 1
    Thank you for your detailed and explicit explanations.
    – Taras
    Commented Dec 22, 2020 at 10:08

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.