<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi John,</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thanks for getting back to me.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
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.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I have not tried using the station_no column as a primary key, which may make a difference, though it shouldn't.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i>DB=tan2413.spdb</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i><br>
</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i># create unique constraint on station_no</i></div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i>echo "drop index uniq_stn;" | spatialite $DB</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i>echo "create unique index uniq_stn on station(station_no);" | spatialite $DB</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i><br>
</i></div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i># remove existing view & metadata before creating</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i>echo "drop view v_stat;" | spatialite $DB</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i>echo "delete from views_geometry_columns where view_name = 'v_stat';" | spatialite $DB</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i><br>
</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i># create view and register</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i>echo "create view v_stat as select station_no, startp from station;" | spatialite $DB</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i><br>
</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i>echo "insert into views_geometry_columns</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i>        (view_name, view_geometry, view_rowid, f_table_name, f_geometry_column, read_only)</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i>      values</i></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i>        ('v_stat', 'startp', 'station_no', 'station', 'startp', 1);" | spatialite $DB</i></div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
So I still can't add a view in QGIS.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div id="Signature" class="elementToProof" style="color: inherit;">
<div style="margin-right: 0px; margin-left: 0px; font-family: Calibri, Arial, Helvetica, sans-serif;">
Brent Wood<br>
<br>
Principal Technician, Fisheries<br>
NIWA<br>
DDI:  +64 (4) 3860529</div>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> John Stevenson - BGS <jostev@bgs.ac.uk><br>
<b>Sent:</b> Saturday, December 7, 2024 06:51<br>
<b>To:</b> Brent Wood <Brent.Wood@niwa.co.nz>; 'Qgis-user@lists.osgeo.org' <qgis-user@lists.osgeo.org><br>
<b>Subject:</b> RE: Help opening a Spatialite view in QGIS</font>
<div> </div>
</div>
<style>
<!--
@font-face
        {font-family:"Cambria Math"}
@font-face
        {font-family:Calibri}
@font-face
        {font-family:Aptos}
p.x_MsoNormal, li.x_MsoNormal, div.x_MsoNormal
        {margin:0cm;
        font-size:12.0pt;
        font-family:"Aptos",sans-serif}
a:link, span.x_MsoHyperlink
        {color:blue;
        text-decoration:underline}
span.x_EmailStyle19
        {font-family:"Arial",sans-serif;
        color:windowtext}
.x_MsoChpDefault
        {font-size:10.0pt}
@page WordSection1
        {margin:72.0pt 72.0pt 72.0pt 72.0pt}
div.x_WordSection1
        {}
-->
</style>
<div lang="EN-GB" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="x_WordSection1">
<p class="x_MsoNormal"><span style="font-size:11.0pt; font-family:"Arial",sans-serif">Hi Brent,</span></p>
<p class="x_MsoNormal"><span style="font-size:11.0pt; font-family:"Arial",sans-serif"> </span></p>
<p class="x_MsoNormal"><span style="font-size:11.0pt; font-family:"Arial",sans-serif">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.</span></p>
<p class="x_MsoNormal"><span style="font-size:11.0pt; font-family:"Arial",sans-serif"><br>
I put some notes on Spatialite views in this Stack Exchange answer a few years ago.<br>
<br>
</span></p>
<p class="x_MsoNormal"><span style="font-size:11.0pt; font-family:"Arial",sans-serif"><a href="https://gis.stackexchange.com/a/301444/64762" originalsrc="https://gis.stackexchange.com/a/301444/64762" shash="AWuS1WY1zr4DNoHQcBOwLrzPLuz5vGAS2y/DTnTQSRe/RETTZjmaAHgu+zgZhmtRAIK2HGbCPHlOyD0K8CikN957NwB+I2iLBG7YgdLo59SL9zMMFNtymXLjtlR8jOzDjJKAxsmaRutRJVmj8KWNpAURBpU/eHWL1GuRHR4cfUs=">https://gis.stackexchange.com/a/301444/64762</a></span></p>
<p class="x_MsoNormal"><span style="font-size:11.0pt; font-family:"Arial",sans-serif"> </span></p>
<p class="x_MsoNormal"><span style="font-size:11.0pt; font-family:"Arial",sans-serif">John</span></p>
<p class="x_MsoNormal"><span style="font-size:11.0pt; font-family:"Arial",sans-serif"> </span></p>
<div>
<div style="border:none; border-top:solid #E1E1E1 1.0pt; padding:3.0pt 0cm 0cm 0cm">
<p class="x_MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt; font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt; font-family:"Calibri",sans-serif"> QGIS-User <qgis-user-bounces@lists.osgeo.org>
<b>On Behalf Of </b>Brent Wood via QGIS-User<br>
<b>Sent:</b> 05 December 2024 21:53<br>
<b>To:</b> 'Qgis-user@lists.osgeo.org' <qgis-user@lists.osgeo.org><br>
<b>Subject:</b> [Qgis-user] Help opening a Spatialite view in QGIS</span></p>
</div>
</div>
<p class="x_MsoNormal"> </p>
<div>
<p class="x_MsoNormal"><span style="color:black">Hi,</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black">I have created a view in a spatialite database, but am unable to open it in QGIS.</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black">I can't find any documentation on how this works.</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black">I have a table "stations" which contains a station_no column (integer) and a point geometry column (startp, EPSG 4326).</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black">The view is created by:</span></p>
</div>
<div>
<p class="x_MsoNormal"><i><span style="color:black">create view v_stat as select station_no, startp from station;</span></i><span style="color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black">I add an entry to the Spatialite views_geometry_columns table to populate the metadata describing the view.</span></p>
</div>
<div>
<p class="x_MsoNormal"><i><span style="color:black">insert into views_geometry_columns</span></i><span style="color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal"><i><span style="color:black">        (view_name, view_geometry, view_rowid, f_table_name, f_geometry_column, read_only)</span></i><span style="color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal"><i><span style="color:black">      values</span></i><span style="color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal"><i><span style="color:black">        ('v_stat', 'startp', 'station_no', 'station', 'startp', 1)</span></i><span style="color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black">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.</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><i><span style="color:black"> dbname='/home/baw/tan2413/tan2413.spdb' table="v_stat" (startp) is an invalid layer - not loaded</span></i><span style="color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black">Can anyone tell me how to set up a Spatialite view so it can be successfully opened in QGIS?</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black">Thanks</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="color:black"> </span></p>
</div>
<div id="x_Signature">
<div>
<p class="x_MsoNormal"><span style="font-family:"Calibri",sans-serif">Brent Wood<br>
<br>
Principal Technician, Fisheries<br>
NIWA<br>
DDI:  +64 (4) 3860529</span></p>
</div>
</div>
<table class="x_MsoNormalTable" border="0" cellspacing="20" cellpadding="0" width="600" style="width:450.0pt">
<tbody>
<tr>
<td style="padding:.75pt .75pt .75pt .75pt">
<p class="x_MsoNormal"><a href="https://www.niwa.co.nz/"><span style="font-size:8.0pt; font-family:"Arial",sans-serif; border:solid windowtext 1.0pt; padding:0cm; text-decoration:none"><img border="0" width="100" height="100" id="x_Picture_x0020_1" alt="Image removed by sender." style="width:1.0416in; height:1.0416in" data-outlook-trace="F:1|T:1" src="cid:~WRD0000.jpg"></span></a><span style="font-size:8.0pt; font-family:"Arial",sans-serif"></span></p>
</td>
<td style="padding:.75pt .75pt .75pt .75pt">
<table class="x_MsoNormalTable" border="0" cellpadding="0" width="500" style="width:375.0pt">
<tbody>
<tr>
<td style="padding:.75pt .75pt .75pt .75pt">
<p class="x_MsoNormal">Brent Wood <br>
<span style="color:#1793D2">Principal Technician - GIS and Spatial Data Management</span><br>
<span style="color:#1793D2">Programme Leader - Environmental Information Delivery</span><br>
+64-4-386-0529<br>
<br>
National Institute of Water & Atmospheric Research Ltd (NIWA)<br>
301 Evans Bay Parade Hataitai Wellington New Zealand<br>
<b>Connect with NIWA:</b> <a href="https://www.niwa.co.nz/">niwa.co.nz</a> <a href="https://www.facebook.com/nzniwa" originalsrc="https://www.facebook.com/nzniwa" shash="utUk0thmsm40qxX3mH2qKpGleP2ykJ/7ZEtBSlpAhBL/+oLha/pReoJMbwUTmGnTA5jXROU1gasOJ31JC+qzyjztt0oCfe5fcqHzYJNniBn6tyFDAHLMUkGdHHyMzGuQ1FuTWudL35yZ83ZzAiGi8dGJE/ULu5TCkOT02YFKNDw=">
Facebook</a> <a href="https://www.linkedin.com/company/niwa" originalsrc="https://www.linkedin.com/company/niwa" shash="OLYC2slr/l7hOa1e924DmD8zabAtzSmDqAqaFe9dEOuwVBDbIGA6AD3oagzSf3xIWdEPmVMhgXW8z2dc7+ByoPnuQsRU6TL9p9ASLXkEYdjUCE2g9iGZqWpDzua72xR2H+xRqAl+12x1VfTVvr6WGcJ5opRL/oetaceywdYooEM=">
LinkedIn</a> <a href="https://twitter.com/niwa_nz" originalsrc="https://twitter.com/niwa_nz" shash="VvUwLV2bXDmebTsUVSe2XwMQdGJ/IXfgWGIqpqB1d/eWRe6toRVQ/AhZBERNGOwRsM9PIGWrWFiUONNUTmn9gLhWvkAEirAMp9drPIY7jz7W5g+Z0INLm7drdUmJhf6NwoTgYxwi1QQtGM2rN2AHv0klkXW3cLlzEWa/nnuN6uA=">
Twitter</a> <a href="https://www.instagram.com/niwa_science" originalsrc="https://www.instagram.com/niwa_science" shash="XYm1UgQ+IlR0sUQIui+dP1003QNKDpZcusMSY8hhOnTh7XsNpKGnsqPgHcHdRYQEEYYEJK0gme3WWLznp6kgg2TrdMvsTx36pJcuiBz4zi7EKEW5fKJIDSvL+QML5QDaDvT0FyRzpnxnb+FN03R55jYMVQ2rSGii7N4GQJIM40g=">
Instagram</a> <a href="https://www.youtube.com/channel/UCJ-j3MLMg1H59Ak2UaNLL3A" originalsrc="https://www.youtube.com/channel/UCJ-j3MLMg1H59Ak2UaNLL3A" shash="SkVQHocCCrePYXpy1k8YIP7zgDzZu06rn9n2QhiYmpB0bBzojzB+fbv6/TfR0GNYBIIzrVAOxGK3/EvP07QnuusST9KqAbTpNdVN/EaNgSReNym0wWSOrKZJfQ3qVIQop2fR4j9n/agb3myy6kMTw3Q7gdZri7nQEABEVDeaY1I=">
YouTube</a> </p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<p class="x_MsoNormal"><span style="font-size:7.5pt; font-family:"Arial",sans-serif">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.
<br>
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.
</span></p>
</div>
<br>
<br>
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.<br>
<br>
</div>
</body>
</html>