Skip to main content
deleted 120 characters in body; edited tags; edited title
Source Link
Taras
  • 34.1k
  • 4
  • 73
  • 148

Remove Removing points outside of polygon but keepkeeping points with the same feature/ID ID using PyQGIS

I am new to using python in QGIS. I have basic coding and QGIS knowledge, but do not know how to combine it yet.

I have a polygon layer and a point layer. The point layer has specficspecific features in its table such as an ID called "fwg_type""fwg_type". There can be several points with the same "fw_type""fw_type".

I would likewant to remove all the points whichthat lie outside the polygon except the points whichthose that share the same ID ofas points, which lie inside the layer boundaries.

So if points with ID 1 lie inside and outside the layer, thethey would not be removed.

Here is my script. It does not work yet.. AnyAny ideas?

# Replace 'point_layer' and 'polygon_layer' with the actual names of your layers
point_layer = QgsProject.instance().mapLayersByName('point_layer')[0]
polygon_layer = QgsProject.instance().mapLayersByName('polygon_layer')[0]

# Create a set to store the IDs of points within the polygon
ids_within_polygon = set()

# Iterate through the features in the polygon layer and store their IDs
for feature in polygon_layer.getFeatures():
    ids_within_polygon.add(feature['ID'])  # Replace 'ID' with the actual field name

# Create a new memory layer to store the filtered features
new_layer = QgsVectorLayer("Point?crs=" + point_layer.crs().authid(), "Filtered Points", "memory")
new_layer.startEditing()

# Define the attributes and fields for the new layer
new_layer.dataProvider().addAttributes(point_layer.fields())
new_layer.updateFields()

# Create a list to store the features to keep
features_to_keep = []

# Iterate through the features in the point layer
for feature in point_layer.getFeatures():
    if feature['ID'] in ids_within_polygon or feature['ID'] not in ids_within_polygon:
        features_to_keep.append(feature)

# Add the features to keep to the new layer
new_layer.addFeatures(features_to_keep)
new_layer.commitChanges()

# Add the new layer to the QGIS map
QgsProject.instance().addMapLayer(new_layer)
# Replace 'point_layer' and 'polygon_layer' with the actual names of your layers
point_layer = QgsProject.instance().mapLayersByName('point_layer')[0]
polygon_layer = QgsProject.instance().mapLayersByName('polygon_layer')[0]

# Create a set to store the IDs of points within the polygon
ids_within_polygon = set()

# Iterate through the features in the polygon layer and store their IDs
for feature in polygon_layer.getFeatures():
    ids_within_polygon.add(feature['ID'])  # Replace 'ID' with the actual field name

# Create a new memory layer to store the filtered features
new_layer = QgsVectorLayer("Point?crs=" + point_layer.crs().authid(), "Filtered Points", "memory")
new_layer.startEditing()

# Define the attributes and fields for the new layer
new_layer.dataProvider().addAttributes(point_layer.fields())
new_layer.updateFields()

# Create a list to store the features to keep
features_to_keep = []

# Iterate through the features in the point layer
for feature in point_layer.getFeatures():
    if feature['ID'] in ids_within_polygon or feature['ID'] not in ids_within_polygon:
        features_to_keep.append(feature)

# Add the features to keep to the new layer
new_layer.addFeatures(features_to_keep)
new_layer.commitChanges()

# Add the new layer to the QGIS map
QgsProject.instance().addMapLayer(new_layer)

Remove points outside of polygon but keep points with the same feature/ID

I am new to using python in QGIS. I have basic coding and QGIS knowledge, but do not know how to combine it yet.

I have a polygon layer and a point layer. The point layer has specfic features in its table such as an ID called "fwg_type". There can be several points with the same "fw_type".

I would like to remove all the points which lie outside the polygon except the points which share the same ID of points, which lie inside the layer boundaries.

So if points with ID 1 lie inside and outside the layer, the would not be removed.

Here is my script. It does not work yet.. Any ideas?

# Replace 'point_layer' and 'polygon_layer' with the actual names of your layers
point_layer = QgsProject.instance().mapLayersByName('point_layer')[0]
polygon_layer = QgsProject.instance().mapLayersByName('polygon_layer')[0]

# Create a set to store the IDs of points within the polygon
ids_within_polygon = set()

# Iterate through the features in the polygon layer and store their IDs
for feature in polygon_layer.getFeatures():
    ids_within_polygon.add(feature['ID'])  # Replace 'ID' with the actual field name

# Create a new memory layer to store the filtered features
new_layer = QgsVectorLayer("Point?crs=" + point_layer.crs().authid(), "Filtered Points", "memory")
new_layer.startEditing()

# Define the attributes and fields for the new layer
new_layer.dataProvider().addAttributes(point_layer.fields())
new_layer.updateFields()

# Create a list to store the features to keep
features_to_keep = []

# Iterate through the features in the point layer
for feature in point_layer.getFeatures():
    if feature['ID'] in ids_within_polygon or feature['ID'] not in ids_within_polygon:
        features_to_keep.append(feature)

# Add the features to keep to the new layer
new_layer.addFeatures(features_to_keep)
new_layer.commitChanges()

# Add the new layer to the QGIS map
QgsProject.instance().addMapLayer(new_layer)

Removing points outside of polygon but keeping points with the same feature ID using PyQGIS

I have a polygon layer and a point layer. The point layer has specific features in its table such as an ID called "fwg_type". There can be several points with the same "fw_type".

I want to remove all the points that lie outside the polygon except those that share the same ID as points, which lie inside the layer boundaries.

So if points with ID 1 lie inside and outside the layer, they would not be removed.

Here is my script. It does not work yet. Any ideas?

# Replace 'point_layer' and 'polygon_layer' with the actual names of your layers
point_layer = QgsProject.instance().mapLayersByName('point_layer')[0]
polygon_layer = QgsProject.instance().mapLayersByName('polygon_layer')[0]

# Create a set to store the IDs of points within the polygon
ids_within_polygon = set()

# Iterate through the features in the polygon layer and store their IDs
for feature in polygon_layer.getFeatures():
    ids_within_polygon.add(feature['ID'])  # Replace 'ID' with the actual field name

# Create a new memory layer to store the filtered features
new_layer = QgsVectorLayer("Point?crs=" + point_layer.crs().authid(), "Filtered Points", "memory")
new_layer.startEditing()

# Define the attributes and fields for the new layer
new_layer.dataProvider().addAttributes(point_layer.fields())
new_layer.updateFields()

# Create a list to store the features to keep
features_to_keep = []

# Iterate through the features in the point layer
for feature in point_layer.getFeatures():
    if feature['ID'] in ids_within_polygon or feature['ID'] not in ids_within_polygon:
        features_to_keep.append(feature)

# Add the features to keep to the new layer
new_layer.addFeatures(features_to_keep)
new_layer.commitChanges()

# Add the new layer to the QGIS map
QgsProject.instance().addMapLayer(new_layer)
edited tags
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

I am new to using python in QGIS. I have basic coding and QGIS knowledge, but do not know how to combine it yet.

I have a polygon layer and a point layer. The point layer has specfic features in its table such as an ID called "fwg_type". There can be several points with the same "fw_type".

I would like to remove all the points which lie outside the polygon except the points which share the same ID of points, which lie inside the layer boundaries.

So if points with ID 1 lie inside and outside the layer, the would not be removed.

Here is my script. It does not work yet.. Any ideas?

# Replace 'point_layer' and 'polygon_layer' with the actual names of your layers
point_layer = QgsProject.instance().mapLayersByName('point_layer')[0]
polygon_layer = QgsProject.instance().mapLayersByName('polygon_layer')[0]

# Create a set to store the IDs of points within the polygon
ids_within_polygon = set()

# Iterate through the features in the polygon layer and store their IDs
for feature in polygon_layer.getFeatures():
    ids_within_polygon.add(feature['ID'])  # Replace 'ID' with the actual field name

# Create a new memory layer to store the filtered features
new_layer = QgsVectorLayer("Point?crs=" + point_layer.crs().authid(), "Filtered Points", "memory")
new_layer.startEditing()

# Define the attributes and fields for the new layer
new_layer.dataProvider().addAttributes(point_layer.fields())
new_layer.updateFields()

# Create a list to store the features to keep
features_to_keep = []

# Iterate through the features in the point layer
for feature in point_layer.getFeatures():
    if feature['ID'] in ids_within_polygon or feature['ID'] not in ids_within_polygon:
        features_to_keep.append(feature)

# Add the features to keep to the new layer
new_layer.addFeatures(features_to_keep)
new_layer.commitChanges()

# Add the new layer to the QGIS map
QgsProject.instance().addMapLayer(new_layer)

Best regards

I am new to using python in QGIS. I have basic coding and QGIS knowledge, but do not know how to combine it yet.

I have a polygon layer and a point layer. The point layer has specfic features in its table such as an ID called "fwg_type". There can be several points with the same "fw_type".

I would like to remove all the points which lie outside the polygon except the points which share the same ID of points, which lie inside the layer boundaries.

So if points with ID 1 lie inside and outside the layer, the would not be removed.

Here is my script. It does not work yet.. Any ideas?

# Replace 'point_layer' and 'polygon_layer' with the actual names of your layers
point_layer = QgsProject.instance().mapLayersByName('point_layer')[0]
polygon_layer = QgsProject.instance().mapLayersByName('polygon_layer')[0]

# Create a set to store the IDs of points within the polygon
ids_within_polygon = set()

# Iterate through the features in the polygon layer and store their IDs
for feature in polygon_layer.getFeatures():
    ids_within_polygon.add(feature['ID'])  # Replace 'ID' with the actual field name

# Create a new memory layer to store the filtered features
new_layer = QgsVectorLayer("Point?crs=" + point_layer.crs().authid(), "Filtered Points", "memory")
new_layer.startEditing()

# Define the attributes and fields for the new layer
new_layer.dataProvider().addAttributes(point_layer.fields())
new_layer.updateFields()

# Create a list to store the features to keep
features_to_keep = []

# Iterate through the features in the point layer
for feature in point_layer.getFeatures():
    if feature['ID'] in ids_within_polygon or feature['ID'] not in ids_within_polygon:
        features_to_keep.append(feature)

# Add the features to keep to the new layer
new_layer.addFeatures(features_to_keep)
new_layer.commitChanges()

# Add the new layer to the QGIS map
QgsProject.instance().addMapLayer(new_layer)

Best regards

I am new to using python in QGIS. I have basic coding and QGIS knowledge, but do not know how to combine it yet.

I have a polygon layer and a point layer. The point layer has specfic features in its table such as an ID called "fwg_type". There can be several points with the same "fw_type".

I would like to remove all the points which lie outside the polygon except the points which share the same ID of points, which lie inside the layer boundaries.

So if points with ID 1 lie inside and outside the layer, the would not be removed.

Here is my script. It does not work yet.. Any ideas?

# Replace 'point_layer' and 'polygon_layer' with the actual names of your layers
point_layer = QgsProject.instance().mapLayersByName('point_layer')[0]
polygon_layer = QgsProject.instance().mapLayersByName('polygon_layer')[0]

# Create a set to store the IDs of points within the polygon
ids_within_polygon = set()

# Iterate through the features in the polygon layer and store their IDs
for feature in polygon_layer.getFeatures():
    ids_within_polygon.add(feature['ID'])  # Replace 'ID' with the actual field name

# Create a new memory layer to store the filtered features
new_layer = QgsVectorLayer("Point?crs=" + point_layer.crs().authid(), "Filtered Points", "memory")
new_layer.startEditing()

# Define the attributes and fields for the new layer
new_layer.dataProvider().addAttributes(point_layer.fields())
new_layer.updateFields()

# Create a list to store the features to keep
features_to_keep = []

# Iterate through the features in the point layer
for feature in point_layer.getFeatures():
    if feature['ID'] in ids_within_polygon or feature['ID'] not in ids_within_polygon:
        features_to_keep.append(feature)

# Add the features to keep to the new layer
new_layer.addFeatures(features_to_keep)
new_layer.commitChanges()

# Add the new layer to the QGIS map
QgsProject.instance().addMapLayer(new_layer)
Source Link

Remove points outside of polygon but keep points with the same feature/ID

I am new to using python in QGIS. I have basic coding and QGIS knowledge, but do not know how to combine it yet.

I have a polygon layer and a point layer. The point layer has specfic features in its table such as an ID called "fwg_type". There can be several points with the same "fw_type".

I would like to remove all the points which lie outside the polygon except the points which share the same ID of points, which lie inside the layer boundaries.

So if points with ID 1 lie inside and outside the layer, the would not be removed.

Here is my script. It does not work yet.. Any ideas?

# Replace 'point_layer' and 'polygon_layer' with the actual names of your layers
point_layer = QgsProject.instance().mapLayersByName('point_layer')[0]
polygon_layer = QgsProject.instance().mapLayersByName('polygon_layer')[0]

# Create a set to store the IDs of points within the polygon
ids_within_polygon = set()

# Iterate through the features in the polygon layer and store their IDs
for feature in polygon_layer.getFeatures():
    ids_within_polygon.add(feature['ID'])  # Replace 'ID' with the actual field name

# Create a new memory layer to store the filtered features
new_layer = QgsVectorLayer("Point?crs=" + point_layer.crs().authid(), "Filtered Points", "memory")
new_layer.startEditing()

# Define the attributes and fields for the new layer
new_layer.dataProvider().addAttributes(point_layer.fields())
new_layer.updateFields()

# Create a list to store the features to keep
features_to_keep = []

# Iterate through the features in the point layer
for feature in point_layer.getFeatures():
    if feature['ID'] in ids_within_polygon or feature['ID'] not in ids_within_polygon:
        features_to_keep.append(feature)

# Add the features to keep to the new layer
new_layer.addFeatures(features_to_keep)
new_layer.commitChanges()

# Add the new layer to the QGIS map
QgsProject.instance().addMapLayer(new_layer)

Best regards