4

I have a shapefile of points and polygons layers with a common field attribute "Parcel_No". Each of the points share a common unique "Parcel_No" with the polygons, but the points are outside the polygons. I need an expression to move the points into the polygons base on the common field attribute (Parcel_No) using QGIS.

2 Answers 2

11

More details would be needed, but here's a prime example: through the use of QGIS expressions:

closest_point(
   geometry(get_feature('polygon','Parcel_No',
   attribute( $currentfeature, 'Parcel_No' ))),
   $geometry)

the expression plots, for each point and following the Parcel_No attribute, the point closest to the corresponding polygon.enter image description here

Or

with the points collected around a circumference

with_variable('geomPolygon', 
  geometry(
     get_feature('polygon',
                 'Parcel_No',
                  attribute( $currentfeature, 'Parcel_No' ))),
closest_point(buffer(centroid(
   @geomPolygon), bounds_height((@geomPolygon))/5),
   $geometry))

enter image description here

To move all points in the centroid of its polygon:

enter image description here

with_variable('geomPolygon', 
  geometry(
    get_feature('polygon',
                'Parcel_No',
                 attribute($currentfeature, 'Parcel_No'))),
  closest_point(
    centroid(@geomPolygon),
    $geometry))

Moves the points within the polygons and distributes them randomly around its centroid:

enter image description here

--> variabile
with_variable('geomPolygon', 
    geometry(
    get_feature('polygon',
            'Parcel_No',
             attribute( $currentfeature, 'Parcel_No' ))),
--> variabile
project(
point:=
    closest_point(
    geometry1:=centroid(@geomPolygon), -- polygon
    geometry2:=$geometry), -- point
distance:=
    distance(
    closest_point(
    boundary(@geomPolygon),centroid(@geomPolygon)),
    centroid(@geomPolygon))*randf(0,1),
azimuth:=
    radians (rand(0,360))
    )
)
4
  • 1
    Very clever! Perhaps a variation where the incoming points were randomly distributed throughout each polygon? Or to each centroid?
    – Stu Smith
    Mar 12, 2022 at 15:56
  • 1
    also added the case that moves all points in the centroid of its polygon
    – pigreco
    Mar 12, 2022 at 16:17
  • 1
    @pigreco. Wow,Wow Wow!!!!!!!!! the expressions really works. Thanks alot. am looking forward to learn more from you. Mar 13, 2022 at 9:46
  • I added the case where it shifts them inside the polygons randomly
    – pigreco
    Mar 13, 2022 at 16:19
3

Using Virtual Layers and SQL Queries:

SELECT pt.Parcel_No,closestpoint(st_centroid(pl.geometry),
                    pt.geometry) as geom /*:point:3857*/
FROM point pt, polygon pl
where pt.Parcel_No = pl.Parcel_No

enter image description here enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.