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

by Matteo Ruzzante   Last Updated November 27, 2017 14:22 PM

I have a set of polygons in json or geojson format and a set of points in csv (both containing geographic coordinates). I am able to join these two datasets in QGIS and display them.

I want to generate a dummy variable equal to 1 if a point lies inside a polygon and to 0 if it does not.

The "Points in polygon" function in QGIS does not help me with regard to this.



Answers 1


Solution has been provided by DPSSpatial already, but let me try to demonstrate a Virtual Layer approach (hence sqlite, as commented by DPSSpatial ):

Example ---------- Please consider some elephants were observed in and out of the Singapore zoo. I would like to mark elephants "inside green fence".

enter image description here

  • Elephants: point layer -- loaded as csv through Delimited text layer.
  • zoo: polygon layer (green) -- loaded as geojson file.

Virtual Layer

(1) Layer | Add Layer | Add/Edit Virtual Layer

(2) Import Elephants and zoo layers

(3) Give a query like below:

SELECT Elephants.*, st_within(Elephants.geometry, zoo.geometry) AS dummy
FROM Elephants, zoo
WHERE st_within(Elephants.geometry, zoo.geometry)

We will get something like:

enter image description here

Oh, sorry you wanted complete list of [1, 0] in the dummy column... Then remove WHERE (filter) from the above query, i.e.:

SELECT Elephants.*, st_within(Elephants.geometry, zoo.geometry) AS dummy
FROM Elephants, zoo

enter image description here

You will obtain full listing in dummy column.

Kazuhito
Kazuhito
November 27, 2017 14:09 PM

Related Questions


Polygons Centroids Alignment in QGIS

Updated June 18, 2017 13:22 PM


Boundary from Points

Updated July 29, 2015 13:09 PM