[Qgis-user] Help opening a Spatialite view in QGIS

Brent Wood Brent.Wood at niwa.co.nz
Fri Dec 6 14:10:19 PST 2024


Hi John,

Thanks for getting back to me.

The following commands are used to create the view (from a bash script), they include creating the unique index on station_no. QGIS is still unable to load the layer, despite listing it as a spatial table in the database.

I have not tried using the station_no column as a primary key, which may make a difference, though it shouldn't.

DB=tan2413.spdb

# create unique constraint on station_no
echo "drop index uniq_stn;" | spatialite $DB
echo "create unique index uniq_stn on station(station_no);" | spatialite $DB

# remove existing view & metadata before creating
echo "drop view v_stat;" | spatialite $DB
echo "delete from views_geometry_columns where view_name = 'v_stat';" | spatialite $DB

# create view and register
echo "create view v_stat as select station_no, startp from station;" | spatialite $DB

echo "insert into views_geometry_columns
        (view_name, view_geometry, view_rowid, f_table_name, f_geometry_column, read_only)
      values
        ('v_stat', 'startp', 'station_no', 'station', 'startp', 1);" | spatialite $DB


So I still can't add a view in QGIS.

Brent Wood

Principal Technician, Fisheries
NIWA
DDI:  +64 (4) 3860529
________________________________
From: John Stevenson - BGS <jostev at bgs.ac.uk>
Sent: Saturday, December 7, 2024 06:51
To: Brent Wood <Brent.Wood at niwa.co.nz>; 'Qgis-user at lists.osgeo.org' <qgis-user at lists.osgeo.org>
Subject: RE: Help opening a Spatialite view in QGIS


Hi Brent,



I’ve often found that the problem with views has come from the id column.  Does the integer “station_no” column have a unique constraint?  It may need one.

I put some notes on Spatialite views in this Stack Exchange answer a few years ago.


https://gis.stackexchange.com/a/301444/64762



John



From: QGIS-User <qgis-user-bounces at lists.osgeo.org> On Behalf Of Brent Wood via QGIS-User
Sent: 05 December 2024 21:53
To: 'Qgis-user at lists.osgeo.org' <qgis-user at lists.osgeo.org>
Subject: [Qgis-user] Help opening a Spatialite view in QGIS



Hi,



I have created a view in a spatialite database, but am unable to open it in QGIS.



I can't find any documentation on how this works.



I have a table "stations" which contains a station_no column (integer) and a point geometry column (startp, EPSG 4326).



The view is created by:

create view v_stat as select station_no, startp from station;





I add an entry to the Spatialite views_geometry_columns table to populate the metadata describing the view.

insert into views_geometry_columns

        (view_name, view_geometry, view_rowid, f_table_name, f_geometry_column, read_only)

      values

        ('v_stat', 'startp', 'station_no', 'station', 'startp', 1)



QGIS now lists the view correctly as a table with a geometry & lists startp as the geometry, but when I try to open it, I just get an "invalid layer" error message.



 dbname='/home/baw/tan2413/tan2413.spdb' table="v_stat" (startp) is an invalid layer - not loaded





Can anyone tell me how to set up a Spatialite view so it can be successfully opened in QGIS?





Thanks



Brent Wood

Principal Technician, Fisheries
NIWA
DDI:  +64 (4) 3860529

[Image removed by sender.]<https://www.niwa.co.nz/>

Brent Wood
Principal Technician - GIS and Spatial Data Management
Programme Leader - Environmental Information Delivery
+64-4-386-0529

National Institute of Water & Atmospheric Research Ltd (NIWA)
301 Evans Bay Parade Hataitai Wellington New Zealand
Connect with NIWA: niwa.co.nz<https://www.niwa.co.nz/> Facebook<https://www.facebook.com/nzniwa> LinkedIn<https://www.linkedin.com/company/niwa> Twitter<https://twitter.com/niwa_nz> Instagram<https://www.instagram.com/niwa_science> YouTube<https://www.youtube.com/channel/UCJ-j3MLMg1H59Ak2UaNLL3A>

To ensure compliance with legal requirements and to maintain cyber security standards, NIWA's IT systems are subject to ongoing monitoring, activity logging and auditing. This monitoring and auditing service may be provided by third parties. Such third parties can access information transmitted to, processed by and stored on NIWA's IT systems.
Note: This email is intended solely for the use of the addressee and may contain information that is confidential or subject to legal professional privilege. If you receive this email in error please immediately notify the sender and delete the email.


This email and any attachments are intended solely for the use of the named recipients. If you are not the intended recipient you must not use, disclose, copy or distribute this email or any of its attachments and should notify the sender immediately and delete this email from your system. UK Research and Innovation (UKRI) has taken every reasonable precaution to minimise risk of this email or any attachments containing viruses or malware but the recipient should carry out its own virus and malware checks before opening the attachments. UKRI does not accept any liability for any losses or damages which the recipient may sustain due to presence of any viruses.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20241206/c605832a/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ~WRD0000.jpg
Type: image/jpeg
Size: 823 bytes
Desc: ~WRD0000.jpg
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20241206/c605832a/attachment.jpg>


More information about the QGIS-User mailing list