USING QGIS 2.18.19 with python console (planning to upgrade to the newest QGIS after this piece of programming)

I am struggling with finding the correct pyQGIS coding for this.

I have a shapefile containing points marking shelteredHousing and another shapefile containing groceryStores (points).

I wish to identify the nearest grocery store to each fox den point and calculate the distance between them.

This is as far as I have managed so far (and it's taken a long time)

import qgis.utils
layers = iface.mapCanvas().layers()
for layer in layers:
    layerName = layer.name()
    if layerName == "grocerystores":
        groceryStoresLayer = layer
    if layerName == "shelteredhousing":
        shelteredHousingLayer = layer
if storesData==0:
    DestinationLayer=iface.addVectorLayer("C:\\destinationpathway\\grocerystores.shp", "groceryStoresLayer", "ogr")
if housingData==0:
    DestinationLayer=iface.addVectorLayer("C:\\destinationpathway"\\shelteredhousing.shp, "shelteredHousingLayer", "ogr")
res = shelteredHousingLayer.dataProvider()

I think I need to have in the statement "from math import sqrt" and then perform a square route on the different layers, but I really wanted to see if someone could give a little help or nudge in the right direction.

Effectively I'm pretty sure I want to do the distance matrix on the data, but I don't know how to plug that in.

  • Welcome to GIS:SE @peterWeb! I think there's an available tool for this: Distance to nearest hub. If you want to see the code, see its script on GitHub – Joseph Jun 26 '18 at 12:47

Your Answer

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

Browse other questions tagged or ask your own question.