[Qgis-user] Polygon from 4 points

Richard Duivenvoorde rdmailings at duif.net
Sat Nov 28 04:25:55 PST 2020


As a variation on this...

You can create 'virtual layers' in QGIS, see:

https://docs.qgis.org/3.16/en/docs/user_manual/managing_data_source/create_layers.html?highlight=virtual#creating-virtual-layers

In attached zip file (a csv+a project file), 
I created a csv with 4 records with (just) 2 coordinates x1,y1,x2,y2.
Loaded it (as table) in QGIS (named it xyxy
Then created a Virtual Layer using one of the following 'Queries':

# a point:
SELECT make_point(x2, y2, 4326) as geometry, x1 as x1, y1 as y1, x2 as x2, y2 as y2 FROM xyxy

# a polygon, one of these:

select geom_from_wkt( 'POLYGON(('||x1||' '||y1||',  '||x2||' '||y1||',   '||x2||' '||y2||',  '||x1||' '||y2||',  '||x1||' '||y1||'))'  ) as geometry, x1 as x1, y1 as y1, x2 as x2, y2 as y2  from xyxy


select st_geomfromtext( 'POLYGON(('||x1||' '||y1||',  '||x2||' '||y1||',   '||x2||' '||y2||',  '||x1||' '||y2||',  '||x1||' '||y1||'))'  ) as geometry, x1 as x1, y1 as y1, x2 as x2, y2 as y2  from xyxy

(in the dialog I selected 
Geometry column 'geometry'
Type 'Polygon'
CRS 'EPSG:4326'

In this way, you can just load the QGIS project, and the virtual layer will do the magic for you to create a polygon layer from your csv.

You can probably even use the project file as an input for QGIS-Server to serve it as WMS to you clients. 
(but you can also just share the project file).

Regards,

Richard Duivenvoorde


PS it took me some puzzling to get the query/expression right. At first I used concatting with '+' but you have to use the '||' as in expressions....

PS2 you have to update the queries to reflect your 8 columns....




On 11/26/20 4:02 PM, Totò Fiandaca wrote:
> From Processing:
> 
> search for Geometry by Expression:
> 1. as input layer put your file with the coordinates
> 2. and write this expression:
> 
>   make_polygon (
>   make_line (
>   make_point ("x1", "y1"),
>   make_point ("x2", "y2"),
>   make_point ("x3", "y3"),
>   make_point ("x4", "y4")
>   ))
> 
> and launch the geoprocess.
> 
> I used QGIS 3.16 Hannover
> 
> Il giorno gio 26 nov 2020 alle ore 15:48 Ryan Peel <ryanpeel at gmail.com <mailto:ryanpeel at gmail.com>> ha scritto:
> 
>     I have a csv/table that has a single row that contains some metadata as well as four SEPARATE columns, x1, y1, x2, y2, x3, y3, x4, y4 of decimal degree coordinates.
> 
>     I want to import all of this data into QGIS and create a polygon from the four points as a vector layer.  
> 
>     What is the best way to do this?  I'm not a programmer or scripter really and fairly new to QGIS.  
> 
>     Thanks!
> 
> 
>     *Ryan Peel*
> 
>     _______________________________________________
>     Qgis-user mailing list
>     Qgis-user at lists.osgeo.org <mailto:Qgis-user at lists.osgeo.org>
>     List info: https://lists.osgeo.org/mailman/listinfo/qgis-user <https://lists.osgeo.org/mailman/listinfo/qgis-user>
>     Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user <https://lists.osgeo.org/mailman/listinfo/qgis-user>
> 
> 
> 
> -- 
> *Ing. Salvatore Fiandaca*
> *mobile*.:+39 327.493.8955 
> *m*: *pigrecoinfinito at gmail.com <mailto:pigrecoinfinito at gmail.com>*
> *C.F*.: FNDSVT71E29Z103G
> *P.IVA*: 06597870820
> *membro QGIS Italia - http://qgis.it/ <http://qgis.it/>*
> *socio GFOSS.it - *http://gfoss.it/ <http://gfoss.it/>
> *blog:** https://pigrecoinfinito.com/ <https://pigrecoinfinito.com/> FB: Co-admin - https://www.facebook.com/qgis.it/ <https://www.facebook.com/qgis.it/>**  <https://www.facebook.com/qgis.it/> *
> *TW:  <http://goog_95411464>**https://twitter.com/totofiandaca <https://twitter.com/totofiandaca>*
> 
> 43°51'0.54"N  10°34'27.62"E - EPSG:4326
> 
> “Se la conoscenza deve essere aperta a tutti,
> perchè mai limitarne l’accesso?” 
> R. Stallman
> 
> Questo documento, allegati inclusi, contiene informazioni di proprietà di FIANDACA SALVATORE e deve essere utilizzato esclusivamente dal destinatario in relazione alle finalità per le quali è stato ricevuto. E' vietata qualsiasi forma di riproduzione o divulgazione senza l'esplicito consenso di FIANDACA SALVATORE. Qualora fosse stato ricevuto per errore si prega di informare tempestivamente il mittente e distruggere la copia in proprio possesso.
> 
> 
> 
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: csv2polygon.zip
Type: application/zip
Size: 9366 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20201128/b9b5b989/attachment.zip>


More information about the Qgis-user mailing list