Skip to main content
added 100 characters in body
Source Link
MrXsquared
  • 36.3k
  • 22
  • 79
  • 128

If you are open to a PyQGIS solution you can use this script. It takes about two seconds on my test layer with 1000 features.

layer = iface.activeLayer() # get the layer layer.dataProvider().addAttributes([QgsField('nearest_older_fid', QVariant.Int)]) # add new field layer.updateFields() # update fields so we can work with the new one idx = QgsSpatialIndex(layer.getFeatures()) # create a spatial index nfeats = layer.featureCount() # count total features of the layer with edit(layer): # edit the layer for feat in layer.getFeatures(): # iterate over the layers features for near in idx.nearestNeighbor(feat.geometry(), nfeats): # for each feature iterate over the nearest ones if layer.getFeature(near)['year'] < feat['year']: # if the current nearest ones is smaller than the current feature ones feat['nearest_older_fid'] = layer.getFeature(near)['id'] # get the nearest featureid and fill the current feature with its value # Alternatively you could also use: # feat['nearest_older_fid'] = near # this will not return the attribute "id" but directly the featureid of the nearest feature. layer.updateFeature(feat) # update the feature break # break the for loop of near features and continue with the next feat 

You may need to adjust fieldnames.

The key elements are:

  • usage of a spatial index to find the nearest features
  • itereate over the nearest features
  • break this iteration as soon as a nearest-features year is smaller than the current feature ones year

Demo:

enter image description here

If you are open to a PyQGIS solution you can use this script. It takes about two seconds on my test layer with 1000 features.

layer = iface.activeLayer() # get the layer layer.dataProvider().addAttributes([QgsField('nearest_older_fid', QVariant.Int)]) # add new field layer.updateFields() # update fields so we can work with the new one idx = QgsSpatialIndex(layer.getFeatures()) # create a spatial index nfeats = layer.featureCount() # count total features of the layer with edit(layer): # edit the layer for feat in layer.getFeatures(): # iterate over the layers features for near in idx.nearestNeighbor(feat.geometry(), nfeats): # for each feature iterate over the nearest ones if layer.getFeature(near)['year'] < feat['year']: # if the current nearest ones is smaller than the current feature ones feat['nearest_older_fid'] = layer.getFeature(near)['id'] # get the nearest featureid and fill the current feature with its value # Alternatively you could also use: # feat['nearest_older_fid'] = near # this will not return the attribute "id" but directly the featureid of the nearest feature. layer.updateFeature(feat) # update the feature break # break the for loop of near features and continue with the next feat 

You may need to adjust fieldnames.

The key elements are:

  • usage of a spatial index to find the nearest features
  • itereate over the nearest features
  • break this iteration as soon as a nearest-features year is smaller than the current feature ones year

If you are open to a PyQGIS solution you can use this script. It takes about two seconds on my test layer with 1000 features.

layer = iface.activeLayer() # get the layer layer.dataProvider().addAttributes([QgsField('nearest_older_fid', QVariant.Int)]) # add new field layer.updateFields() # update fields so we can work with the new one idx = QgsSpatialIndex(layer.getFeatures()) # create a spatial index nfeats = layer.featureCount() # count total features of the layer with edit(layer): # edit the layer for feat in layer.getFeatures(): # iterate over the layers features for near in idx.nearestNeighbor(feat.geometry(), nfeats): # for each feature iterate over the nearest ones if layer.getFeature(near)['year'] < feat['year']: # if the current nearest ones is smaller than the current feature ones feat['nearest_older_fid'] = layer.getFeature(near)['id'] # get the nearest featureid and fill the current feature with its value # Alternatively you could also use: # feat['nearest_older_fid'] = near # this will not return the attribute "id" but directly the featureid of the nearest feature. layer.updateFeature(feat) # update the feature break # break the for loop of near features and continue with the next feat 

You may need to adjust fieldnames.

The key elements are:

  • usage of a spatial index to find the nearest features
  • itereate over the nearest features
  • break this iteration as soon as a nearest-features year is smaller than the current feature ones year

Demo:

enter image description here

added 202 characters in body
Source Link
MrXsquared
  • 36.3k
  • 22
  • 79
  • 128

If you are open to a PyQGIS solution you can use this script. It takes about two seconds on my test layer with 1000 features.

layer = iface.activeLayer() # get the layer layer.dataProvider().addAttributes([QgsField('nearest_older_fid', QVariant.Int)]) # add new field layer.updateFields() # update fields so we can work with the new one idx = QgsSpatialIndex(layer.getFeatures()) # create a spatial index nfeats = layer.featureCount() # count total features of the layer with edit(layer): # edit the layer for feat in layer.getFeatures(): # iterate over the layers features for near in idx.nearestNeighbor(feat.geometry(), nfeats): # for each feature iterate over the nearest ones if layer.getFeature(near)['year'] < feat['year']: # if the current nearest ones is smaller than the current feature ones feat['nearest_older_fid'] = layer.getFeature(near)['id'] # get the nearest featureid and fill the current feature with its value # Alternatively you could also use: # feat['nearest_older_fid'] = near # this will not return the attribute "id" but directly the featureid of the nearest feature. layer.updateFeature(feat) # update the feature break # break the for loop of near features and continue with the next feat 

You may need to adjust fieldnames.

The key elements are:

  • usage of a spatial index to find the nearest features
  • itereate over the nearest features
  • break this iteration as soon as a nearest-features year is smaller than the current feature ones year

If you are open to a PyQGIS solution you can use this script. It takes about two seconds on my test layer with 1000 features.

layer = iface.activeLayer() # get the layer layer.dataProvider().addAttributes([QgsField('nearest_older_fid', QVariant.Int)]) # add new field layer.updateFields() # update fields so we can work with the new one idx = QgsSpatialIndex(layer.getFeatures()) # create a spatial index nfeats = layer.featureCount() # count total features of the layer with edit(layer): # edit the layer for feat in layer.getFeatures(): # iterate over the layers features for near in idx.nearestNeighbor(feat.geometry(), nfeats): # for each feature iterate over the nearest ones if layer.getFeature(near)['year'] < feat['year']: # if the current nearest ones is smaller than the current feature ones feat['nearest_older_fid'] = layer.getFeature(near)['id'] # get the nearest featureid and fill the current feature with its value layer.updateFeature(feat) # update the feature break # break the for loop of near features and continue with the next feat 

You may need to adjust fieldnames.

The key elements are:

  • usage of a spatial index to find the nearest features
  • itereate over the nearest features
  • break this iteration as soon as a nearest-features year is smaller than the current feature ones year

If you are open to a PyQGIS solution you can use this script. It takes about two seconds on my test layer with 1000 features.

layer = iface.activeLayer() # get the layer layer.dataProvider().addAttributes([QgsField('nearest_older_fid', QVariant.Int)]) # add new field layer.updateFields() # update fields so we can work with the new one idx = QgsSpatialIndex(layer.getFeatures()) # create a spatial index nfeats = layer.featureCount() # count total features of the layer with edit(layer): # edit the layer for feat in layer.getFeatures(): # iterate over the layers features for near in idx.nearestNeighbor(feat.geometry(), nfeats): # for each feature iterate over the nearest ones if layer.getFeature(near)['year'] < feat['year']: # if the current nearest ones is smaller than the current feature ones feat['nearest_older_fid'] = layer.getFeature(near)['id'] # get the nearest featureid and fill the current feature with its value # Alternatively you could also use: # feat['nearest_older_fid'] = near # this will not return the attribute "id" but directly the featureid of the nearest feature. layer.updateFeature(feat) # update the feature break # break the for loop of near features and continue with the next feat 

You may need to adjust fieldnames.

The key elements are:

  • usage of a spatial index to find the nearest features
  • itereate over the nearest features
  • break this iteration as soon as a nearest-features year is smaller than the current feature ones year
Source Link
MrXsquared
  • 36.3k
  • 22
  • 79
  • 128

If you are open to a PyQGIS solution you can use this script. It takes about two seconds on my test layer with 1000 features.

layer = iface.activeLayer() # get the layer layer.dataProvider().addAttributes([QgsField('nearest_older_fid', QVariant.Int)]) # add new field layer.updateFields() # update fields so we can work with the new one idx = QgsSpatialIndex(layer.getFeatures()) # create a spatial index nfeats = layer.featureCount() # count total features of the layer with edit(layer): # edit the layer for feat in layer.getFeatures(): # iterate over the layers features for near in idx.nearestNeighbor(feat.geometry(), nfeats): # for each feature iterate over the nearest ones if layer.getFeature(near)['year'] < feat['year']: # if the current nearest ones is smaller than the current feature ones feat['nearest_older_fid'] = layer.getFeature(near)['id'] # get the nearest featureid and fill the current feature with its value layer.updateFeature(feat) # update the feature break # break the for loop of near features and continue with the next feat 

You may need to adjust fieldnames.

The key elements are:

  • usage of a spatial index to find the nearest features
  • itereate over the nearest features
  • break this iteration as soon as a nearest-features year is smaller than the current feature ones year