Thursday 27 September 2018

qgis - Spatial query, embed in layer or project


I have 2 vector layers:


1) Polygons with parcel-boundaries


2) Polygon with project-boundaries


I want to query all the parcels that are within the project-area, using spatial query 'select parcels that intersect project-area'. Works fine, so far no problem.


The parcel-layer however is a layer that is often subject to changes. Instead of always renewing the spatial query, I want to embed the query in a layer or project. So that every time when I open the layer/project, I automatticaly get the current parcels within the project-area.


Can this be done in QGIS? In Mapinfo it was very easy but I'm walking over to Qgis, so it would be great not to lose this ability!



Answer



Using shapefiles, you can create a layer that is a 'memory spatial view' with the QGIS DB Manager.



If I have schools and neighborhoods, they show up as Virtual Layers in the QGIS DB Manager. I can create a query that only shows me the points within a particular neighborhood with this query:


enter image description here


Here's the SQL:


select 
sch.name
, n."NBHD_ID"
, sch.geometry
from 'schools' as sch
join 'neighborhoods'as n on ST_Intersects(sch.geometry, n.geometry)
where n."NBHD_ID" = '41'


Now at the bottom of the DB Manager is the 'load as new layer' option. To get your query to save as a virtual layer, enable these settings:


enter image description here


Now in my map, the virtual layer shows up with the points defined in the query:


enter image description here


Now save this QGIS document.


Note: you might need the Memory Layer Saver plugin enabled.


So I did a test where in a new QGIS document I created a new school point in the same NHood my spatial 'view' queries. Sure enough when I opened up the QGIS document with the spatial view layer, the new point showed up!


I think this would work just fine for what you're describing.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...