2

I created a polygon using Iso-Area as polygons algorithm in QGIS. I have a point's coordinates and added it as a vector layer into QGIS. It is shown on the map that the point lies within the polygon. Then I wanted to create a dummy variable that indicates that the point lies inside the polygon. Using what I found in the following link I created a Virtual Layer by applying this query ('A' is the point layer and 'B' is the polygon layer):

SELECT A.*, st_within(A.geometry, B.geometry) AS dummy
FROM A, B

But the value of the created dummy variable is 0, while the point is inside the polygon. What is the solution?

Generating a dummy variable if point lies inside a polygon using QGIS?

3
  • 2
    Do both layers use the same CRS? Are you 100 % sure the point is within the polygon? Sometimes points close to the border look like they are inside at a certain scale. Can you provide an image showing the point and polygon in question? – Erik Feb 10 at 14:54
  • 1
    @Erik Both layers did not have the same CRS. After resolving this, the problem solved – Elham Feb 10 at 16:05
  • As far as I remember SQL cares about CRS way more than QGIS does ;-) – Erik Feb 10 at 16:10
4

Open the field calculator for your point layer and add a new virtual field, set the field type to whole number, and add the following expression (swap out 'polygons' for the name of your polygon layer in single quote marks):

intersects($geometry, aggregate('polygons', 'collect', $geometry))

This expression aggregates all features of the polygon layer into a single multipart geometry and then checks if the point intersects that multipart polygon.

A result of 1 means it does intersect.

1
  • 3
    The problem was because of not having both layers in the same CRS. After figuring out this, your solution as well as the solution that I mentioned in my question worked. – Elham Feb 10 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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