Skip to main content
deleted 24 characters in body
Source Link
GforGIS
  • 3.3k
  • 4
  • 22
  • 39

It does work! Thank you so much!works

I tried adding another point layer and keep its points if the first point layer has points within the polygon with the same value.

I added the following to the code but it does not seem to work. At least he just prints the remaining points of the first point layer.

from qgis.core import QgsProject, QgsVectorLayer, QgsGeometry, QgsCoordinateTransform

project = QgsProject.instance()

point_lyr = project.mapLayersByName('05334036_STR_FPG')[0]
point_lyr_2 = project.mapLayersByName('05334036_STR_WGF')[0]
polygon_lyr = project.mapLayersByName('L-den / dB(A)')[0]

output_lyr = QgsVectorLayer(f'Point?crs={point_lyr.crs().authid()}', 'result', 'memory')

output_lyr.dataProvider().addAttributes(point_lyr.fields())
output_lyr.dataProvider().addAttributes(point_lyr_2.fields())
output_lyr.updateFields()

poly_combined_geom = QgsGeometry.collectGeometry([ft.geometry() for ft in polygon_lyr.getFeatures()])

# transform combined polygon geometry to point layer crs if required
if point_lyr.crs() != polygon_lyr.crs():
    xform = QgsCoordinateTransform(polygon_lyr.crs(), point_lyr.crs(), project)
    poly_combined_geom.transform(xform)

geom_engine = QgsGeometry.createGeometryEngine(poly_combined_geom.constGet())

geom_engine.prepareGeometry()

pts_in = [ft for ft in point_lyr.getFeatures() if geom_engine.contains(ft.geometry().constGet())]
pts_in_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.contains(ft.geometry().constGet())]


ids_in = set(ft['GEB_ID'] for ft in pts_in)
ids_in_2 = set(ft['GEB_ID'] for ft in pts_in_2)

pts_out_to_keep = [ft for ft in point_lyr.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]
pts_out_to_keep_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]

all_fts = pts_in + pts_in + pts_out_to_keep + pts_out_to_keep_2

output_lyr.dataProvider().addFeatures(all_fts)

output_lyr.updateExtents()

project.addMapLayer(output_lyr)

It does work! Thank you so much!

I tried adding another point layer and keep its points if the first point layer has points within the polygon with the same value.

I added the following to the code but it does not seem to work. At least he just prints the remaining points of the first point layer.

from qgis.core import QgsProject, QgsVectorLayer, QgsGeometry, QgsCoordinateTransform

project = QgsProject.instance()

point_lyr = project.mapLayersByName('05334036_STR_FPG')[0]
point_lyr_2 = project.mapLayersByName('05334036_STR_WGF')[0]
polygon_lyr = project.mapLayersByName('L-den / dB(A)')[0]

output_lyr = QgsVectorLayer(f'Point?crs={point_lyr.crs().authid()}', 'result', 'memory')

output_lyr.dataProvider().addAttributes(point_lyr.fields())
output_lyr.dataProvider().addAttributes(point_lyr_2.fields())
output_lyr.updateFields()

poly_combined_geom = QgsGeometry.collectGeometry([ft.geometry() for ft in polygon_lyr.getFeatures()])

# transform combined polygon geometry to point layer crs if required
if point_lyr.crs() != polygon_lyr.crs():
    xform = QgsCoordinateTransform(polygon_lyr.crs(), point_lyr.crs(), project)
    poly_combined_geom.transform(xform)

geom_engine = QgsGeometry.createGeometryEngine(poly_combined_geom.constGet())

geom_engine.prepareGeometry()

pts_in = [ft for ft in point_lyr.getFeatures() if geom_engine.contains(ft.geometry().constGet())]
pts_in_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.contains(ft.geometry().constGet())]


ids_in = set(ft['GEB_ID'] for ft in pts_in)
ids_in_2 = set(ft['GEB_ID'] for ft in pts_in_2)

pts_out_to_keep = [ft for ft in point_lyr.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]
pts_out_to_keep_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]

all_fts = pts_in + pts_in + pts_out_to_keep + pts_out_to_keep_2

output_lyr.dataProvider().addFeatures(all_fts)

output_lyr.updateExtents()

project.addMapLayer(output_lyr)

It works

I tried adding another point layer and keep its points if the first point layer has points within the polygon with the same value.

I added the following to the code but it does not seem to work. At least he just prints the remaining points of the first point layer.

from qgis.core import QgsProject, QgsVectorLayer, QgsGeometry, QgsCoordinateTransform

project = QgsProject.instance()

point_lyr = project.mapLayersByName('05334036_STR_FPG')[0]
point_lyr_2 = project.mapLayersByName('05334036_STR_WGF')[0]
polygon_lyr = project.mapLayersByName('L-den / dB(A)')[0]

output_lyr = QgsVectorLayer(f'Point?crs={point_lyr.crs().authid()}', 'result', 'memory')

output_lyr.dataProvider().addAttributes(point_lyr.fields())
output_lyr.dataProvider().addAttributes(point_lyr_2.fields())
output_lyr.updateFields()

poly_combined_geom = QgsGeometry.collectGeometry([ft.geometry() for ft in polygon_lyr.getFeatures()])

# transform combined polygon geometry to point layer crs if required
if point_lyr.crs() != polygon_lyr.crs():
    xform = QgsCoordinateTransform(polygon_lyr.crs(), point_lyr.crs(), project)
    poly_combined_geom.transform(xform)

geom_engine = QgsGeometry.createGeometryEngine(poly_combined_geom.constGet())

geom_engine.prepareGeometry()

pts_in = [ft for ft in point_lyr.getFeatures() if geom_engine.contains(ft.geometry().constGet())]
pts_in_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.contains(ft.geometry().constGet())]


ids_in = set(ft['GEB_ID'] for ft in pts_in)
ids_in_2 = set(ft['GEB_ID'] for ft in pts_in_2)

pts_out_to_keep = [ft for ft in point_lyr.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]
pts_out_to_keep_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]

all_fts = pts_in + pts_in + pts_out_to_keep + pts_out_to_keep_2

output_lyr.dataProvider().addFeatures(all_fts)

output_lyr.updateExtents()

project.addMapLayer(output_lyr)
deleted 32 characters in body
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

It does work! Thank you so much!

I tried adding another point layer and keep its points if the first point layer has points within the polygon with the same value.

I added the following to the code but it does not seem to work. At least he just prints the remaining points of the first point layer.

from qgis.core import QgsProject, QgsVectorLayer, QgsGeometry, QgsCoordinateTransform

project = QgsProject.instance()

point_lyr = project.mapLayersByName('05334036_STR_FPG')[0]
point_lyr_2 = project.mapLayersByName('05334036_STR_WGF')[0]
polygon_lyr = project.mapLayersByName('L-den / dB(A)')[0]

output_lyr = QgsVectorLayer(f'Point?crs={point_lyr.crs().authid()}', 'result', 'memory')

output_lyr.dataProvider().addAttributes(point_lyr.fields())
output_lyr.dataProvider().addAttributes(point_lyr_2.fields())
output_lyr.updateFields()

poly_combined_geom = QgsGeometry.collectGeometry([ft.geometry() for ft in polygon_lyr.getFeatures()])

# transform combined polygon geometry to point layer crs if required
if point_lyr.crs() != polygon_lyr.crs():
    xform = QgsCoordinateTransform(polygon_lyr.crs(), point_lyr.crs(), project)
    poly_combined_geom.transform(xform)

geom_engine = QgsGeometry.createGeometryEngine(poly_combined_geom.constGet())

geom_engine.prepareGeometry()

pts_in = [ft for ft in point_lyr.getFeatures() if geom_engine.contains(ft.geometry().constGet())]
pts_in_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.contains(ft.geometry().constGet())]


ids_in = set(ft['GEB_ID'] for ft in pts_in)
ids_in_2 = set(ft['GEB_ID'] for ft in pts_in_2)

pts_out_to_keep = [ft for ft in point_lyr.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]
pts_out_to_keep_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]

all_fts = pts_in + pts_in + pts_out_to_keep + pts_out_to_keep_2

output_lyr.dataProvider().addFeatures(all_fts)

output_lyr.updateExtents()

project.addMapLayer(output_lyr)

 

Sorry for my bad coding.

It does work! Thank you so much!

I tried adding another point layer and keep its points if the first point layer has points within the polygon with the same value.

I added the following to the code but it does not seem to work. At least he just prints the remaining points of the first point layer.

from qgis.core import QgsProject, QgsVectorLayer, QgsGeometry, QgsCoordinateTransform

project = QgsProject.instance()

point_lyr = project.mapLayersByName('05334036_STR_FPG')[0]
point_lyr_2 = project.mapLayersByName('05334036_STR_WGF')[0]
polygon_lyr = project.mapLayersByName('L-den / dB(A)')[0]

output_lyr = QgsVectorLayer(f'Point?crs={point_lyr.crs().authid()}', 'result', 'memory')

output_lyr.dataProvider().addAttributes(point_lyr.fields())
output_lyr.dataProvider().addAttributes(point_lyr_2.fields())
output_lyr.updateFields()

poly_combined_geom = QgsGeometry.collectGeometry([ft.geometry() for ft in polygon_lyr.getFeatures()])

# transform combined polygon geometry to point layer crs if required
if point_lyr.crs() != polygon_lyr.crs():
    xform = QgsCoordinateTransform(polygon_lyr.crs(), point_lyr.crs(), project)
    poly_combined_geom.transform(xform)

geom_engine = QgsGeometry.createGeometryEngine(poly_combined_geom.constGet())

geom_engine.prepareGeometry()

pts_in = [ft for ft in point_lyr.getFeatures() if geom_engine.contains(ft.geometry().constGet())]
pts_in_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.contains(ft.geometry().constGet())]


ids_in = set(ft['GEB_ID'] for ft in pts_in)
ids_in_2 = set(ft['GEB_ID'] for ft in pts_in_2)

pts_out_to_keep = [ft for ft in point_lyr.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]
pts_out_to_keep_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]

all_fts = pts_in + pts_in + pts_out_to_keep + pts_out_to_keep_2

output_lyr.dataProvider().addFeatures(all_fts)

output_lyr.updateExtents()

project.addMapLayer(output_lyr)

 

Sorry for my bad coding.

It does work! Thank you so much!

I tried adding another point layer and keep its points if the first point layer has points within the polygon with the same value.

I added the following to the code but it does not seem to work. At least he just prints the remaining points of the first point layer.

from qgis.core import QgsProject, QgsVectorLayer, QgsGeometry, QgsCoordinateTransform

project = QgsProject.instance()

point_lyr = project.mapLayersByName('05334036_STR_FPG')[0]
point_lyr_2 = project.mapLayersByName('05334036_STR_WGF')[0]
polygon_lyr = project.mapLayersByName('L-den / dB(A)')[0]

output_lyr = QgsVectorLayer(f'Point?crs={point_lyr.crs().authid()}', 'result', 'memory')

output_lyr.dataProvider().addAttributes(point_lyr.fields())
output_lyr.dataProvider().addAttributes(point_lyr_2.fields())
output_lyr.updateFields()

poly_combined_geom = QgsGeometry.collectGeometry([ft.geometry() for ft in polygon_lyr.getFeatures()])

# transform combined polygon geometry to point layer crs if required
if point_lyr.crs() != polygon_lyr.crs():
    xform = QgsCoordinateTransform(polygon_lyr.crs(), point_lyr.crs(), project)
    poly_combined_geom.transform(xform)

geom_engine = QgsGeometry.createGeometryEngine(poly_combined_geom.constGet())

geom_engine.prepareGeometry()

pts_in = [ft for ft in point_lyr.getFeatures() if geom_engine.contains(ft.geometry().constGet())]
pts_in_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.contains(ft.geometry().constGet())]


ids_in = set(ft['GEB_ID'] for ft in pts_in)
ids_in_2 = set(ft['GEB_ID'] for ft in pts_in_2)

pts_out_to_keep = [ft for ft in point_lyr.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]
pts_out_to_keep_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]

all_fts = pts_in + pts_in + pts_out_to_keep + pts_out_to_keep_2

output_lyr.dataProvider().addFeatures(all_fts)

output_lyr.updateExtents()

project.addMapLayer(output_lyr)
Source Link

It does work! Thank you so much!

I tried adding another point layer and keep its points if the first point layer has points within the polygon with the same value.

I added the following to the code but it does not seem to work. At least he just prints the remaining points of the first point layer.

from qgis.core import QgsProject, QgsVectorLayer, QgsGeometry, QgsCoordinateTransform

project = QgsProject.instance()

point_lyr = project.mapLayersByName('05334036_STR_FPG')[0]
point_lyr_2 = project.mapLayersByName('05334036_STR_WGF')[0]
polygon_lyr = project.mapLayersByName('L-den / dB(A)')[0]

output_lyr = QgsVectorLayer(f'Point?crs={point_lyr.crs().authid()}', 'result', 'memory')

output_lyr.dataProvider().addAttributes(point_lyr.fields())
output_lyr.dataProvider().addAttributes(point_lyr_2.fields())
output_lyr.updateFields()

poly_combined_geom = QgsGeometry.collectGeometry([ft.geometry() for ft in polygon_lyr.getFeatures()])

# transform combined polygon geometry to point layer crs if required
if point_lyr.crs() != polygon_lyr.crs():
    xform = QgsCoordinateTransform(polygon_lyr.crs(), point_lyr.crs(), project)
    poly_combined_geom.transform(xform)

geom_engine = QgsGeometry.createGeometryEngine(poly_combined_geom.constGet())

geom_engine.prepareGeometry()

pts_in = [ft for ft in point_lyr.getFeatures() if geom_engine.contains(ft.geometry().constGet())]
pts_in_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.contains(ft.geometry().constGet())]


ids_in = set(ft['GEB_ID'] for ft in pts_in)
ids_in_2 = set(ft['GEB_ID'] for ft in pts_in_2)

pts_out_to_keep = [ft for ft in point_lyr.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]
pts_out_to_keep_2 = [ft for ft in point_lyr_2.getFeatures() if geom_engine.disjoint(ft.geometry().constGet()) and ft['GEB_ID'] in ids_in]

all_fts = pts_in + pts_in + pts_out_to_keep + pts_out_to_keep_2

output_lyr.dataProvider().addFeatures(all_fts)

output_lyr.updateExtents()

project.addMapLayer(output_lyr)


Sorry for my bad coding.