TransWikia.com

Generating dummy variable if point lies inside polygon using QGIS

Geographic Information Systems Asked by Matteo Ruzzante on May 24, 2021

I have a set of polygons in JSON or GeoJSON format and a set of points in a 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.

One Answer

The 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 a 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 a 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 a full listing in the dummy column.

Answered by Kazuhito on May 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP