Skip to main content
added 78 characters in body
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

This Python script works. But it can be used only in QGIS Python Editor. It is not a Processing script.

from PyQt5.QtCore import QVariant

# layers
P = QgsProject.instance().mapLayersByName("Posibility_Layer")[0]
S = QgsProject.instance().mapLayersByName("Stop_Layer")[0]

S.startEditing()

# add new fields
if S.fields().indexFromName('near_pl_id') == -1:
    # field for polygonid of the nearest point
    S.addAttribute(QgsField('near_pl_id', QVariant.Int))
if S.fields().indexFromName('near_ps_id') == -1:
    # field for possibilityid of the nearest point
    S.addAttribute(QgsField('near_ps_id', QVariant.Int))

for s in S.getFeatures():
    # get points which have different polygon-id than the stop-point itself has
    request = 'polygonid != ' + str(s["polygonid"])
    distances = {p: s.geometry().distance(p.geometry()) for p in P.getFeatures(request)}
    
    # get the feature which has the minimum distance value
    nearest_point = min(distances, key=distances.get)

    # you may need to change 'possibilit' as 'possibilityid', (character limit of shapefile)
    s['near_ps_id'] = nearest_point["possibilit"]
    s['near_pl_id'] = nearest_point["polygonid"]

    S.updateFeature(s)

S.commitChanges()

Result:

enter image description here

enter image description here

This Python script works.

from PyQt5.QtCore import QVariant

# layers
P = QgsProject.instance().mapLayersByName("Posibility_Layer")[0]
S = QgsProject.instance().mapLayersByName("Stop_Layer")[0]

S.startEditing()

# add new fields
if S.fields().indexFromName('near_pl_id') == -1:
    # field for polygonid of the nearest point
    S.addAttribute(QgsField('near_pl_id', QVariant.Int))
if S.fields().indexFromName('near_ps_id') == -1:
    # field for possibilityid of the nearest point
    S.addAttribute(QgsField('near_ps_id', QVariant.Int))

for s in S.getFeatures():
    # get points which have different polygon-id than the stop-point itself has
    request = 'polygonid != ' + str(s["polygonid"])
    distances = {p: s.geometry().distance(p.geometry()) for p in P.getFeatures(request)}
    
    # get the feature which has the minimum distance value
    nearest_point = min(distances, key=distances.get)

    # you may need to change 'possibilit' as 'possibilityid', (character limit of shapefile)
    s['near_ps_id'] = nearest_point["possibilit"]
    s['near_pl_id'] = nearest_point["polygonid"]

    S.updateFeature(s)

S.commitChanges()

Result:

enter image description here

enter image description here

This Python script works. But it can be used only in QGIS Python Editor. It is not a Processing script.

from PyQt5.QtCore import QVariant

# layers
P = QgsProject.instance().mapLayersByName("Posibility_Layer")[0]
S = QgsProject.instance().mapLayersByName("Stop_Layer")[0]

S.startEditing()

# add new fields
if S.fields().indexFromName('near_pl_id') == -1:
    # field for polygonid of the nearest point
    S.addAttribute(QgsField('near_pl_id', QVariant.Int))
if S.fields().indexFromName('near_ps_id') == -1:
    # field for possibilityid of the nearest point
    S.addAttribute(QgsField('near_ps_id', QVariant.Int))

for s in S.getFeatures():
    # get points which have different polygon-id than the stop-point itself has
    request = 'polygonid != ' + str(s["polygonid"])
    distances = {p: s.geometry().distance(p.geometry()) for p in P.getFeatures(request)}
    
    # get the feature which has the minimum distance value
    nearest_point = min(distances, key=distances.get)

    # you may need to change 'possibilit' as 'possibilityid', (character limit of shapefile)
    s['near_ps_id'] = nearest_point["possibilit"]
    s['near_pl_id'] = nearest_point["polygonid"]

    S.updateFeature(s)

S.commitChanges()

Result:

enter image description here

enter image description here

added 6 characters in body
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

This Python script works, if you prefer.

from PyQt5.QtCore import QVariant

# layers
P = QgsProject.instance().mapLayersByName("Posibility_Layer")[0]
S = QgsProject.instance().mapLayersByName("Stop_Layer")[0]

S.startEditing()

# add new fields
if S.fields().indexFromName('near_pl_id') == -1:
    # field for polygonid of the nearest point
    S.addAttribute(QgsField('near_pl_id', QVariant.Int))
if S.fields().indexFromName('near_ps_id') == -1:
    # field for possibilityid of the nearest point
    S.addAttribute(QgsField('near_ps_id', QVariant.Int))

for s in S.getFeatures():
    # get points which has ahave different polygon-id than the stop-point itself has
    request = 'polygonid != ' + str(s["polygonid"])
    distances = {p: s.geometry().distance(p.geometry()) for p in P.getFeatures(request)}
    
    # get the feature which has the minimum distance value
    nearest_point = min(distances, key=distances.get)

    # you may need to change 'possibilit' as 'possibilityid', (character limit of shapefile)
    s['near_ps_id'] = nearest_point["possibilit"]
    s['near_pl_id'] = nearest_point["polygonid"]

    S.updateFeature(s)

S.commitChanges()

Result:

enter image description here

enter image description here

This Python script works, if you prefer.

from PyQt5.QtCore import QVariant

# layers
P = QgsProject.instance().mapLayersByName("Posibility_Layer")[0]
S = QgsProject.instance().mapLayersByName("Stop_Layer")[0]

S.startEditing()

# add new fields
if S.fields().indexFromName('near_pl_id') == -1:
    # polygonid of the nearest point
    S.addAttribute(QgsField('near_pl_id', QVariant.Int))
if S.fields().indexFromName('near_ps_id') == -1:
    # possibilityid of the nearest point
    S.addAttribute(QgsField('near_ps_id', QVariant.Int))

for s in S.getFeatures():
    # get points which has a different polygon-id than the stop-point itself has
    request = 'polygonid != ' + str(s["polygonid"])
    distances = {p: s.geometry().distance(p.geometry()) for p in P.getFeatures(request)}
    
    # get the feature which has the minimum distance value
    nearest_point = min(distances, key=distances.get)

    # you may need to change 'possibilit' as 'possibilityid', character limit of shapefile
    s['near_ps_id'] = nearest_point["possibilit"]
    s['near_pl_id'] = nearest_point["polygonid"]

    S.updateFeature(s)

S.commitChanges()

Result:

enter image description here

enter image description here

This Python script works.

from PyQt5.QtCore import QVariant

# layers
P = QgsProject.instance().mapLayersByName("Posibility_Layer")[0]
S = QgsProject.instance().mapLayersByName("Stop_Layer")[0]

S.startEditing()

# add new fields
if S.fields().indexFromName('near_pl_id') == -1:
    # field for polygonid of the nearest point
    S.addAttribute(QgsField('near_pl_id', QVariant.Int))
if S.fields().indexFromName('near_ps_id') == -1:
    # field for possibilityid of the nearest point
    S.addAttribute(QgsField('near_ps_id', QVariant.Int))

for s in S.getFeatures():
    # get points which have different polygon-id than the stop-point itself has
    request = 'polygonid != ' + str(s["polygonid"])
    distances = {p: s.geometry().distance(p.geometry()) for p in P.getFeatures(request)}
    
    # get the feature which has the minimum distance value
    nearest_point = min(distances, key=distances.get)

    # you may need to change 'possibilit' as 'possibilityid', (character limit of shapefile)
    s['near_ps_id'] = nearest_point["possibilit"]
    s['near_pl_id'] = nearest_point["polygonid"]

    S.updateFeature(s)

S.commitChanges()

Result:

enter image description here

enter image description here

Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

This Python script works, if you prefer.

from PyQt5.QtCore import QVariant

# layers
P = QgsProject.instance().mapLayersByName("Posibility_Layer")[0]
S = QgsProject.instance().mapLayersByName("Stop_Layer")[0]

S.startEditing()

# add new fields
if S.fields().indexFromName('near_pl_id') == -1:
    # polygonid of the nearest point
    S.addAttribute(QgsField('near_pl_id', QVariant.Int))
if S.fields().indexFromName('near_ps_id') == -1:
    # possibilityid of the nearest point
    S.addAttribute(QgsField('near_ps_id', QVariant.Int))

for s in S.getFeatures():
    # get points which has a different polygon-id than the stop-point itself has
    request = 'polygonid != ' + str(s["polygonid"])
    distances = {p: s.geometry().distance(p.geometry()) for p in P.getFeatures(request)}
    
    # get the feature which has the minimum distance value
    nearest_point = min(distances, key=distances.get)

    # you may need to change 'possibilit' as 'possibilityid', character limit of shapefile
    s['near_ps_id'] = nearest_point["possibilit"]
    s['near_pl_id'] = nearest_point["polygonid"]

    S.updateFeature(s)

S.commitChanges()

Result:

enter image description here

enter image description here