[Qgis-user] Creating Polygon Vector Layer from specific .csv file

Jorge Gustavo Rocha jgr at geomaster.pt
Sun Mar 22 05:41:14 PDT 2020


Hi Marcin,

You have to iterate over the features, with:

for f in layer.getFeatures():

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.

Example:

uri =
"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"
layer=QgsVectorLayer(uri,"Tourism Sites","delimitedtext")
QgsProject.instance().addMapLayers([layer])

pointdict = {}
for f in layer.getFeatures():
    if f["LP"] not in pointdict.keys():
        pointdict[f["LP"]] = [ None, None, None, None, None ]
    pointdict[f["LP"]][f["CORNER"]-1] = f.geometry().asPoint()
    if f["CORNER"] == 1:
        pointdict[f["LP"]][4] = f.geometry().asPoint()

vectorLyr = QgsVectorLayer('Polygon?crs=EPSG:4326', 'polygon' , 'memory')
vpr = vectorLyr.dataProvider()
for pointlist in pointdict:
    poly = QgsGeometry.fromPolygonXY( [ pointdict[pointlist] ] )
    new = QgsFeature()
    new.setGeometry(poly)
    vpr.addFeatures([ new ])

QgsProject.instance().addMapLayer(vectorLyr)

Regards and #StayAtHome

Jorge

On 21/03/20 22:39, Marcin wrote:
> 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
> 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
-- 
Email Signature
Logo <https://www.geomaster.pt> 	
*Geomaster*
*Jorge Gustavo Rocha* | Software Engineer
*e:*jgr at geomaster.pt | *m:*+351 910 333 888
*g:*41.54094,-8.40490 | *v: *510 906 109
*a: * Rua António Cândido Pinto, 67, 4715-400 Braga

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20200322/79a3782f/attachment.html>


More information about the Qgis-user mailing list