Skip to main content
added 19 characters in body
Source Link
gene
  • 55.4k
  • 3
  • 113
  • 191

and the script, as you wrote is:

TheBut the second loop is not necessary here: you can do it all with one loop (reading the csv file and writing the shapefile without using the polyName and polyPart lists).

and the script is

The second loop is not necessary here: you can do it all with one loop (reading the csv file and writing the shapefile without using the polyName and polyPart lists).

and the script, as you wrote is:

But the second loop is not necessary here: you can do it all with one loop (reading the csv file and writing the shapefile without using the polyName and polyPart lists).

added 188 characters in body
Source Link
gene
  • 55.4k
  • 3
  • 113
  • 191

The second loop is not necessary here: you can do it all with one loop (reading the csv file and writing the shapefile without using the polyName and polyPart lists).

w = shapefile.Writer(shapefile.POLYGON)
w.field('name','C',50)
polyName, polyPart = [],[]
with open('your.csv', 'rb') as f:
    reader = csv.DictReader(f)
    for row in reader:
        bl  = [float(row['xl']),float(row['yb'])]
        tl  = [float(row['xl']),float(row['yt'])]
        br  = [float(row['xr']),float(row['yb'])]
        tr  = [float(row['xr']),float(row['yt'])]
        parr = [tl, tr, br, bl, tl]
        w.poly(parts=[parr])
        w.record(row['name'])

w.save("your.shp')

Result in QGIS:
polygon

enter image description here

The second loop is not necessary here: you can do it all with one loop (reading the csv file and writing the shapefile)

w = shapefile.Writer(shapefile.POLYGON)
w.field('name','C',50)
polyName, polyPart = [],[]
with open('your.csv', 'rb') as f:
    reader = csv.DictReader(f)
    for row in reader:
        bl  = [float(row['xl']),float(row['yb'])]
        tl  = [float(row['xl']),float(row['yt'])]
        br  = [float(row['xr']),float(row['yb'])]
        tr  = [float(row['xr']),float(row['yt'])]
        parr = [tl, tr, br, bl, tl]
        w.poly(parts=[parr])
        w.record(row['name'])

w.save("your.shp')

Result:
polygon

The second loop is not necessary here: you can do it all with one loop (reading the csv file and writing the shapefile without using the polyName and polyPart lists).

w = shapefile.Writer(shapefile.POLYGON)
w.field('name','C',50)
with open('your.csv', 'rb') as f:
    reader = csv.DictReader(f)
    for row in reader:
        bl  = [float(row['xl']),float(row['yb'])]
        tl  = [float(row['xl']),float(row['yt'])]
        br  = [float(row['xr']),float(row['yb'])]
        tr  = [float(row['xr']),float(row['yt'])]
        parr = [tl, tr, br, bl, tl]
        w.poly(parts=[parr])
        w.record(row['name'])

w.save("your.shp')

Result in QGIS:

enter image description here

added 188 characters in body
Source Link
gene
  • 55.4k
  • 3
  • 113
  • 191

Your approach is good, but you could make things clearer using dictionaries instead of list and the csv module allows it.
Moreover, your script use two loops while it is possible to simplify using only one (the second loop is redundant, one line of the csv file = one record of the shapefile).

1) With dictionaries:

Reading the csv file:

Writing the polygon shapefile

Now for writing the shapefile, ifIf you look at PyShpDocs you can see that:

But there are two loops while it is possible to simplify using only one (the2) Final solution using only one loop

The second loop is redundantnot necessary here,: you can do it all with one line ofloop (reading the csv file = one record ofand writing the shapefile)

Your approach is good, but you could make things clearer using dictionaries instead of list and the csv module allows it.

Now for writing the shapefile, if you look at PyShpDocs you can see that:

But there are two loops while it is possible to simplify using only one (the second loop is redundant here, one line of the csv file = one record of the shapefile)

Your approach is good, but you could make things clearer using dictionaries instead of list and the csv module allows it.
Moreover, your script use two loops while it is possible to simplify using only one (the second loop is redundant, one line of the csv file = one record of the shapefile).

1) With dictionaries:

Reading the csv file:

Writing the polygon shapefile

If you look at PyShpDocs you can see that:

2) Final solution using only one loop

The second loop is not necessary here: you can do it all with one loop (reading the csv file and writing the shapefile)

Source Link
gene
  • 55.4k
  • 3
  • 113
  • 191
Loading