<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hi Marcin,</p>
    <p>You have to iterate over the features, with:</p>
    <p>for f in layer.getFeatures():</p>
    <p>But since you will have just one polygon for each four features
      in the input, if the input is small, you can create a auxiliary
      Python dict to store the polygon's points.</p>
    <p>Example:</p>
    <p>uri =
<a class="moz-txt-link-rfc2396E" href="file:///home/jgr/tmp/plikiCSVtoSHPviaPythonpokrycieCSV.csv?type=csv&delimiter=%5Ct%7C&quote='&skipEmptyFields=Yes&detectTypes=yes&xField=X&yField=Y&crs=EPSG:4326&spatialIndex=no&subsetIndex=no&watchFile=no">"file:///home/jgr/tmp/plikiCSVtoSHPviaPythonpokrycieCSV.csv?type=csv&delimiter=%5Ct%7C&quote='&skipEmptyFields=Yes&detectTypes=yes&xField=X&yField=Y&crs=EPSG:4326&spatialIndex=no&subsetIndex=no&watchFile=no"</a><br>
      layer=QgsVectorLayer(uri,"Tourism Sites","delimitedtext")<br>
      QgsProject.instance().addMapLayers([layer])<br>
      <br>
      pointdict = {}<br>
      for f in layer.getFeatures():<br>
          if f["LP"] not in pointdict.keys():<br>
              pointdict[f["LP"]] = [ None, None, None, None, None ]<br>
          pointdict[f["LP"]][f["CORNER"]-1] = f.geometry().asPoint()<br>
          if f["CORNER"] == 1:<br>
              pointdict[f["LP"]][4] = f.geometry().asPoint()<br>
      <br>
      vectorLyr = QgsVectorLayer('Polygon?crs=EPSG:4326', 'polygon' ,
      'memory')<br>
      vpr = vectorLyr.dataProvider()<br>
      for pointlist in pointdict:<br>
          poly = QgsGeometry.fromPolygonXY( [ pointdict[pointlist] ] )<br>
          new = QgsFeature()<br>
          new.setGeometry(poly)<br>
          vpr.addFeatures([ new ])<br>
      <br>
      QgsProject.instance().addMapLayer(vectorLyr)<br>
    </p>
    <p>Regards and #StayAtHome</p>
    <p>Jorge<br>
    </p>
    <div class="moz-cite-prefix">On 21/03/20 22:39, Marcin wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:ekthdebygvomaypnfzsj@bjxn">
      <pre class="moz-quote-pre" wrap="">
Hi folks,

QGIS 3.10

I would like to create polygons (see 4 corners everyone) with Python form .csv like that:

LP      CORNER  Y       X
1       1       51.558868       21.696985
1       2       51.596337       21.745589
1       3       51.603166       21.727687
1       4       51.565424       21.682416
2       1       51.590928       21.760442
2       2       51.553125       21.709847
2       3       51.559536       21.6959
2       4       51.598023       21.742078
3       1       51.548047       21.724327
3       2       51.586238       21.773843
3       3       51.593172       21.756185
3       4       51.554643       21.709932

According to QGIS Python Programming Cookbook 

I know how to load csv file:

uri = "D:/PODYPLOM/plikiCSVtoSHPviaPythonpokrycieCSV.csv?"
uri += "type=csv&"
uri += "xField=X&yField=Y&"
uri += "spatialIndex=no&"
uri += "subsetIndex=no&"
uri += "watchFile=no&"
uri += "crs=epsg:4326"
layer=QgsVectorLayer(uri,"Tourism Sites","delimitedtext")
QgsProject.instance().addMapLayers([layer])

and I know how to create polygon layer from points added by finger:

vectorLyr = QgsVectorLayer('D:/qgis_data/polygon/polygon.shp', 'Polygon', "ogr")
vpr = vectorLyr.dataProvider()
points = []
points.append(QgsPointXY(-123.26,49.06))
points.append(QgsPointXY(-127.19,43.07))
points.append(QgsPointXY(-120.70,35.21))
points.append(QgsPointXY(-115.89,40.02))
points.append(QgsPointXY(-123.26,49.06))
poly = QgsGeometry.fromPolygonXY([points])
f = QgsFeature()
f.setGeometry(poly)
vpr.addFeatures([f])
vectorLyr.updateExtents()

but I can't load points from csv to line: points.append(QgsPointXY())

Anybody can help me how to do this ?

Cheers, Marcin Pietruszewski
_______________________________________________
Qgis-user mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Qgis-user@lists.osgeo.org">Qgis-user@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a></pre>
    </blockquote>
    <div class="moz-signature">-- <br>
      <title>Email Signature</title>
      <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
      <table style="width: 525px; font-size: 11pt; font-family: Arial,
        sans-serif;" cellspacing="0" cellpadding="0">
        <tbody>
          <tr>
            <td style="text-align:center; font-size: 10pt; font-family:
              Arial, sans-serif; border-right: 1px solid;
              border-right-color: #fb6303; width: 125px; padding-right:
              10px; vertical-align: top;" rowspan="6" width="125"
              valign="top"> <a href="https://www.geomaster.pt"
                target="_blank"><img alt="Logo" style="width:105px;
                  height:auto; border:0;"
src="https://geomaster.pt/wp-content/uploads/2017/02/cropped-geomaster300x300-1.png"
                  width="85" border="0"></a> </td>
            <td style="padding-left:10px">
              <table cellspacing="0" cellpadding="0">
                <tbody>
                  <tr>
                    <td style="font-size: 10pt; color:#0079ac;
                      font-family: Arial, sans-serif; width: 400px;
                      padding-bottom: 5px; padding-left: 10px;
                      vertical-align: top;" valign="top"> <strong><span
                          style="font-size: 14pt; font-family: Arial,
                          sans-serif; color:#fb6303;">Geomaster</span></strong><br>
                      <strong><span style="font-size: 11pt; font-family:
                          Arial, sans-serif; color:black;">Jorge Gustavo
                          Rocha</span></strong> <span
                        style="font-family: Arial, sans-serif;
                        font-size:11pt; color:#000000;"><span
                          style="padding-right: 5px; padding-left: 5px;">
                          | </span>Software Engineer</span> </td>
                  </tr>
                  <tr>
                    <td style="font-size: 10pt; color:#444444;
                      font-family: Arial, sans-serif; padding-bottom:
                      5px; padding-top: 5px; padding-left: 10px;
                      vertical-align: top; line-height:17px;"
                      valign="top"> <span><span style="color: #fb6303;"><strong>e:</strong></span><span
                          style="font-size: 10pt; font-family: Arial,
                          sans-serif; color:#000000;"> <a class="moz-txt-link-abbreviated" href="mailto:jgr@geomaster.pt">jgr@geomaster.pt</a></span></span>
                      <span><span style="padding-left:
                          5px;padding-right: 5px;"> | </span><span
                          style="color: #fb6303;"><strong>m:</strong></span><span
                          style="font-size: 10pt; font-family: Arial,
                          sans-serif; color:#000000;"> +351 910 333 888<br>
                        </span></span> <span><span style="color:
                          #fb6303;"><strong>g:</strong></span><span
                          style="font-size: 10pt; font-family: Arial,
                          sans-serif; color:#000000;"> 41.54094,-8.40490</span></span>
                      <span><span style="padding-left:
                          7px;padding-right: 5px;"> | </span><span
                          style="color: #fb6303;"><strong>v: </strong></span><span
                          style="font-size: 10pt; font-family: Arial,
                          sans-serif; color:#000000; padding-left: 7px;">
                          510 906 109<br>
                        </span></span> <span> <span style="color:
                          #fb6303;"><strong>a: </strong></span> </span>
                      <span> <span style="font-size: 10pt; font-family:
                          Arial, sans-serif; color: #000000;"><span> </span>Rua
                          António Cândido Pinto, 67, 4715-400 Braga<br>
                        </span></span> </td>
                  </tr>
                </tbody>
              </table>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </body>
</html>