[prev in list] [next in list] [prev in thread] [next in thread] 

List:       postgis-users
Subject:    Re: [postgis-users] Postgis spatial join display error
From:       "Tsering W. Shawa" <shawatw () princeton ! edu>
Date:       2020-04-28 17:18:42
Message-ID: BL0PR04MB648483766DD8BEE5B46E0189B9AC0 () BL0PR04MB6484 ! namprd04 ! prod ! outlook ! com
[Download RAW message or body]

Thanks Regina, Simon and Alexandre for a quick help.

I used both the suggestions in the QGIS DB Manager. Simon's SQL worked but gave me an \
error when I try to add the SQL layer on a map however, Regina's SQL worked without \
any problem.

I appreciate your help and suggestions.

Thanks,
-Tsering



________________________________
From: postgis-users <postgis-users-bounces@lists.osgeo.org> on behalf of Regina Obe \
                <lr@pcorp.us>
Sent: Tuesday, April 28, 2020 1:03 PM
To: 'PostGIS Users Discussion' <postgis-users@lists.osgeo.org>
Subject: Re: [postgis-users] Postgis spatial join display error


Oh one more thing – don’t put a ; after your query.



Sorry that was my mistake.  I ran into an issue which I discovered was because I put \
in a ; at the end



QGIS when it tried to add the row_number() just wrapped the query around – so the ; \
made the sub query invalid.



From: postgis-users [mailto:postgis-users-bounces@lists.osgeo.org] On Behalf Of \
                Alexandre Neto
Sent: Tuesday, April 28, 2020 12:59 PM
To: PostGIS Users Discussion <postgis-users@lists.osgeo.org>
Subject: Re: [postgis-users] Postgis spatial join display error



Hi,



Yes, QGIS dislike columns with the same name, so you need to use alias to change \
their names.



Regarding the unique id, if you are using the dbmanager sql editor, you no longer \
need a unique id column, qgis will create one. Nevertheless, if you use the query in \
a view, not only you should have a unique id column, it's suggested that it's the \
first column, so that QGIS pick it up automatically.



Alexandre Neto



A terça, 28/04/2020, 17:51, Simon Norris \
<snorris@hillcrestgeo.ca<mailto:snorris@hillcrestgeo.ca>> escreveu:

Also, the DB manager insists on a unique key per feature.

If there is more than one station per neighbourhood you may also have to add one - I \
generally wrap the query something like this:



SELECT row_number() over() as id, *

FROM (

 SELECT

   s.*, n.name<http://n.name> AS neighborhood

  FROM nyc_neighborhoods AS n

 JOIN nyc_subway_stations AS s

 ON ST_Contains(n.geom, s.geom)

) as q;



On Apr 28, 2020, at 9:45 AM, Regina Obe <lr@pcorp.us<mailto:lr@pcorp.us>> wrote:



Try doing



SELECT s.*, n.name<http://n.name> AS neighborhood FROM nyc_neighborhoods AS n JOIN \
nyc_subway_stations AS s ON ST_Contains(n.geom, s.geom);



When you do *, it picks up all columns from both tables and I don’t think QGIS likes \
duplicated columns





From: postgis-users [mailto:postgis-users-bounces@lists.osgeo.org] On Behalf Of \
                Tsering W. Shawa
Sent: Tuesday, April 28, 2020 12:32 PM
To: postgis-users@lists.osgeo.org<mailto:postgis-users@lists.osgeo.org>
Subject: [postgis-users] Postgis spatial join display error



I am new to PostGIS and trying to learn how everything works. I am using Introduction \
to PostGIS site data to test different GIS function including spatial join. The \
spatial join SQL statement seems to work [SELECT * FROM nyc_neighborhoods AS n JOIN \
nyc_subway_stations AS s ON ST_Contains(n.geom, s.geom); ] but when I try to add the \
result to the QGIS I get an error message saying "there was an error creating the SQL \
layer". When I tried the simpler the SQL function by selecting only row with a \
particular name [SELECT * FROM nyc_neighborhoods WHERE name = 'Soho';]. The SQL \
statement worked as well as I was able to add the SQL layer on a map. I am using \
QGIS's DB Manager to write SQL statements.



I was wondering what I am missing here.



Any suggestions or help will be appreciated. FYI. I have installed PostgreSQL 12 with \
PostGIS 3.



Many thanks,

-Tsering





_______________________________________________
postgis-users mailing list
postgis-users@lists.osgeo.org<mailto:postgis-users@lists.osgeo.org>
https://lists.osgeo.org/mailman/listinfo/postgis-users



_______________________________________________
postgis-users mailing list
postgis-users@lists.osgeo.org<mailto:postgis-users@lists.osgeo.org>
https://lists.osgeo.org/mailman/listinfo/postgis-users


[Attachment #3 (text/html)]

<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 style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; \
color: rgb(0, 0, 0);"> Thanks Regina, Simon and Alexandre for a quick help.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; \
color: rgb(0, 0, 0);"> <br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; \
color: rgb(0, 0, 0);"> I used both the suggestions in the QGIS DB Manager. Simon's \
SQL worked but gave me an error when I try to add the SQL layer on a map however, \
Regina's SQL worked without any problem.</div> <div style="font-family: Calibri, \
Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"> <br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; \
color: rgb(0, 0, 0);"> I appreciate your help and suggestions.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; \
color: rgb(0, 0, 0);"> <br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; \
color: rgb(0, 0, 0);"> Thanks,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; \
                color: rgb(0, 0, 0);">
-Tsering</div>
<div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; \
color: rgb(0, 0, 0);"> <br>
</div>
<div id="Signature">
<div></div>
<p><br>
</p>
</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> postgis-users \
&lt;postgis-users-bounces@lists.osgeo.org&gt; on behalf of Regina Obe \
&lt;lr@pcorp.us&gt;<br> <b>Sent:</b> Tuesday, April 28, 2020 1:03 PM<br>
<b>To:</b> 'PostGIS Users Discussion' &lt;postgis-users@lists.osgeo.org&gt;<br>
<b>Subject:</b> Re: [postgis-users] Postgis spatial join display error</font>
<div>&nbsp;</div>
</div>
<style>
<!--
@font-face
	{font-family:Helvetica}
@font-face
	{font-family:"Cambria Math"}
@font-face
	{font-family:Calibri}
p.x_MsoNormal, li.x_MsoNormal, div.x_MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman",serif}
a:link, span.x_MsoHyperlink
	{color:blue;
	text-decoration:underline}
a:visited, span.x_MsoHyperlinkFollowed
	{color:purple;
	text-decoration:underline}
span.x_EmailStyle17
	{font-family:"Calibri",sans-serif;
	color:#1F497D}
.x_MsoChpDefault
	{font-family:"Calibri",sans-serif}
@page WordSection1
	{margin:1.0in 1.0in 1.0in 1.0in}
div.x_WordSection1
	{}
-->
</style>
<div lang="EN-US" link="blue" vlink="purple">
<div class="x_WordSection1">
<p class="x_MsoNormal"><span style="font-size:11.0pt; \
font-family:&quot;Calibri&quot;,sans-serif; color:#1F497D">Oh one more thing – don’t \
put a ; after your query.</span></p> <p class="x_MsoNormal"><span \
style="font-size:11.0pt; font-family:&quot;Calibri&quot;,sans-serif; \
color:#1F497D">&nbsp;</span></p> <p class="x_MsoNormal"><span \
style="font-size:11.0pt; font-family:&quot;Calibri&quot;,sans-serif; \
color:#1F497D">Sorry that was my mistake.&nbsp; I ran into an issue which I \
discovered was because I put in a ; at the end</span></p> <p \
class="x_MsoNormal"><span style="font-size:11.0pt; \
font-family:&quot;Calibri&quot;,sans-serif; color:#1F497D">&nbsp;</span></p> <p \
class="x_MsoNormal"><span style="font-size:11.0pt; \
font-family:&quot;Calibri&quot;,sans-serif; color:#1F497D">QGIS when it tried to add \
the row_number() just wrapped the query around – so the ; made the sub query \
invalid.</span></p> <p class="x_MsoNormal"><span style="font-size:11.0pt; \
font-family:&quot;Calibri&quot;,sans-serif; color:#1F497D">&nbsp;</span></p> <div \
style="border:none; border-left:solid blue 1.5pt; padding:0in 0in 0in 4.0pt"> <div>
<div style="border:none; border-top:solid #E1E1E1 1.0pt; padding:3.0pt 0in 0in 0in">
<p class="x_MsoNormal"><b><span style="font-size:11.0pt; \
font-family:&quot;Calibri&quot;,sans-serif">From:</span></b><span \
style="font-size:11.0pt; font-family:&quot;Calibri&quot;,sans-serif"> postgis-users \
[mailto:postgis-users-bounces@lists.osgeo.org] <b>On Behalf Of </b>Alexandre Neto<br>
<b>Sent:</b> Tuesday, April 28, 2020 12:59 PM<br>
<b>To:</b> PostGIS Users Discussion &lt;postgis-users@lists.osgeo.org&gt;<br>
<b>Subject:</b> Re: [postgis-users] Postgis spatial join display error</span></p>
</div>
</div>
<p class="x_MsoNormal">&nbsp;</p>
<div>
<p class="x_MsoNormal">Hi,</p>
<div>
<p class="x_MsoNormal">&nbsp;</p>
</div>
<div>
<p class="x_MsoNormal">Yes, QGIS dislike columns with the same name, so you need to \
use alias to change their names.</p> </div>
<div>
<p class="x_MsoNormal">&nbsp;</p>
</div>
<div>
<div>
<p class="x_MsoNormal">Regarding the unique id, if you are using the dbmanager sql \
editor, you no longer need a unique id column, qgis will create one. Nevertheless, if \
you use the query in a view, not only you should have a unique id column, it's \
suggested  that it's the first column, so that QGIS pick it up automatically.</p>
</div>
<div>
<p class="x_MsoNormal">&nbsp;</p>
</div>
<div>
<p class="x_MsoNormal">Alexandre Neto</p>
</div>
</div>
</div>
<p class="x_MsoNormal">&nbsp;</p>
<div>
<div>
<p class="x_MsoNormal">A terça, 28/04/2020, 17:51, Simon Norris &lt;<a \
href="mailto:snorris@hillcrestgeo.ca">snorris@hillcrestgeo.ca</a>&gt; escreveu:</p> \
</div> <blockquote style="border:none; border-left:solid #CCCCCC 1.0pt; padding:0in \
0in 0in 6.0pt; margin-left:4.8pt; margin-right:0in"> <div>
<p class="x_MsoNormal">Also, the DB manager insists on a unique key per feature.</p>
<div>
<p class="x_MsoNormal">If there is more than one station per neighbourhood you may \
also have to add one - I generally wrap the query something like this:</p> </div>
<div>
<p class="x_MsoNormal">&nbsp;</p>
</div>
<div>
<p class="x_MsoNormal">SELECT row_number() over() as id, *&nbsp;</p>
</div>
<div>
<p class="x_MsoNormal">FROM (</p>
</div>
<div>
<p class="x_MsoNormal">&nbsp;SELECT&nbsp;</p>
</div>
<div>
<p class="x_MsoNormal">&nbsp; &nbsp;s.*, <a href="http://n.name" \
target="_blank">n.name</a> AS neighborhood&nbsp;</p> </div>
<div>
<p class="x_MsoNormal">&nbsp; FROM nyc_neighborhoods AS n&nbsp;</p>
</div>
<div>
<p class="x_MsoNormal">&nbsp;JOIN&nbsp;nyc_subway_stations AS s&nbsp;</p>
</div>
<div>
<p class="x_MsoNormal">&nbsp;ON ST_Contains(n.geom, s.geom)</p>
</div>
<div>
<p class="x_MsoNormal">) as q;</p>
<div>
<p class="x_MsoNormal"><br>
<br>
</p>
<blockquote style="margin-top:5.0pt; margin-bottom:5.0pt">
<div>
<p class="x_MsoNormal">On Apr 28, 2020, at 9:45 AM, Regina Obe &lt;<a \
href="mailto:lr@pcorp.us" target="_blank">lr@pcorp.us</a>&gt; wrote:</p> </div>
<p class="x_MsoNormal">&nbsp;</p>
<div>
<div>
<div>
<p class="x_MsoNormal"><span style="font-size:11.0pt; \
font-family:&quot;Calibri&quot;,sans-serif; color:#1F497D">Try doing</span></p> \
</div> <div>
<p class="x_MsoNormal"><span style="font-size:11.0pt; \
font-family:&quot;Calibri&quot;,sans-serif; color:#1F497D">&nbsp;</span></p> </div>
<div>
<p class="x_MsoNormal"><span style="font-family:&quot;Calibri&quot;,sans-serif; \
color:#C82613">SELECT s.*, <a href="http://n.name" target="_blank">n.name</a> AS \
neighborhood FROM nyc_neighborhoods AS n JOIN nyc_subway_stations AS s ON \
ST_Contains(n.geom, s.geom);</span></p> </div>
<div>
<p class="x_MsoNormal"><span style="font-family:&quot;Calibri&quot;,sans-serif; \
color:#C82613">&nbsp;</span></p> </div>
<div>
<p class="x_MsoNormal"><span style="font-family:&quot;Calibri&quot;,sans-serif">When \
you do *, it picks up all columns from both tables and I don’t think QGIS likes \
duplicated columns</span></p> </div>
<div>
<p class="x_MsoNormal"><span \
style="font-family:&quot;Calibri&quot;,sans-serif">&nbsp;</span></p> </div>
<div>
<p class="x_MsoNormal"><span style="font-size:11.0pt; \
font-family:&quot;Calibri&quot;,sans-serif; color:#1F497D">&nbsp;</span></p> </div>
<div style="border:none; border-left:solid blue 1.5pt; padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none; border-top:solid #E1E1E1 1.0pt; padding:3.0pt 0in 0in 0in">
<div>
<p class="x_MsoNormal"><b><span style="font-size:11.0pt; \
font-family:&quot;Calibri&quot;,sans-serif">From:</span></b><span \
style="font-size:11.0pt; \
font-family:&quot;Calibri&quot;,sans-serif">&nbsp;postgis-users [<a \
href="mailto:postgis-users-bounces@lists.osgeo.org" \
target="_blank">mailto:postgis-users-bounces@lists.osgeo.org</a>]&nbsp;<b>On  Behalf \
Of&nbsp;</b>Tsering W. Shawa<br> <b>Sent:</b>&nbsp;Tuesday, April 28, 2020 12:32 \
PM<br> <b>To:</b>&nbsp;<a href="mailto:postgis-users@lists.osgeo.org" \
target="_blank">postgis-users@lists.osgeo.org</a><br> \
<b>Subject:</b>&nbsp;[postgis-users] Postgis spatial join display error</span></p> \
</div> </div>
</div>
<div>
<p class="x_MsoNormal">&nbsp;</p>
</div>
<div>
<div>
<div>
<p class="x_MsoNormal" style="background:white"><span \
style="font-family:&quot;Calibri&quot;,sans-serif">I am new to PostGIS and trying to \
learn how everything works. I am using Introduction to PostGIS site data to test \
different GIS function including spatial join.  The spatial join SQL statement seems \
to work [<span style="color:#C82613">SELECT * FROM nyc_neighborhoods AS n JOIN \
nyc_subway_stations AS s ON ST_Contains(n.geom, s.geom);</span>&nbsp;] but when I try \
to add the result to the QGIS I get an error message saying  &quot;there was an error \
creating the SQL layer&quot;. When I tried the simpler the SQL function by selecting \
only row with a particular name [<span style="color:#C82613">SELECT * FROM \
nyc_neighborhoods WHERE name = 'Soho';</span>]. The SQL statement worked as well  as \
I was able to add the SQL layer on a map. I am using QGIS's DB Manager to write SQL \
statements.</span></p> </div>
</div>
<div>
<div>
<p class="x_MsoNormal" style="background:white"><span \
style="font-family:&quot;Calibri&quot;,sans-serif">&nbsp;</span></p> </div>
</div>
<div>
<div>
<p class="x_MsoNormal" style="background:white"><span \
style="font-family:&quot;Calibri&quot;,sans-serif">I was wondering what I am missing \
here.&nbsp;</span></p> </div>
</div>
<div>
<div>
<p class="x_MsoNormal" style="background:white"><span \
style="font-family:&quot;Calibri&quot;,sans-serif">&nbsp;</span></p> </div>
</div>
<div>
<div>
<p class="x_MsoNormal" style="background:white"><span \
style="font-family:&quot;Calibri&quot;,sans-serif">Any suggestions or help will be \
appreciated. FYI. I have installed PostgreSQL 12 with PostGIS 3.</span></p> </div>
</div>
<div>
<div>
<p class="x_MsoNormal" style="background:white"><span \
style="font-family:&quot;Calibri&quot;,sans-serif">&nbsp;</span></p> </div>
</div>
<div>
<div>
<p class="x_MsoNormal" style="background:white"><span \
style="font-family:&quot;Calibri&quot;,sans-serif">Many thanks,</span></p> </div>
</div>
<div>
<div>
<p class="x_MsoNormal" style="background:white"><span \
style="font-family:&quot;Calibri&quot;,sans-serif">-Tsering</span></p> </div>
</div>
<div>
<p class="x_MsoNormal"><span \
style="font-family:&quot;Calibri&quot;,sans-serif">&nbsp;</span></p> </div>
</div>
<div>
<div>
<div>
<p class="x_MsoNormal"><span \
style="font-family:&quot;Calibri&quot;,sans-serif">&nbsp;</span></p> </div>
</div>
</div>
</div>
</div>
<p class="x_MsoNormal"><span style="font-size:9.0pt; \
font-family:&quot;Helvetica&quot;,sans-serif">_______________________________________________<br>
 postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org" \
target="_blank">postgis-users@lists.osgeo.org</a><br> <a \
href="https://lists.osgeo.org/mailman/listinfo/postgis-users" \
target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-users</a></span></p> \
</div> </blockquote>
</div>
<p class="x_MsoNormal">&nbsp;</p>
</div>
</div>
<p class="x_MsoNormal">_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org" \
target="_blank">postgis-users@lists.osgeo.org</a><br> <a \
href="https://lists.osgeo.org/mailman/listinfo/postgis-users" \
target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-users</a></p> \
</blockquote> </div>
</div>
</div>
</div>
</body>
</html>


[Attachment #4 (unknown)]

_______________________________________________
postgis-users mailing list
postgis-users@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/postgis-users

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic