forked from GeospatialPython/Learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2shp.py
32 lines (27 loc) · 925 Bytes
/
csv2shp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import csv
import shapefile
# Create a polygon shapefile writer
w = shapefile.Writer(shapefile.POLYGON)
# Add our fields
w.field("NAME", "C", "40")
w.field("AREA", "C", "40")
# Open the csv file and set up a reader
with open("sample.csv") as p:
reader = csv.DictReader(p)
for row in reader:
# Add records for each polygon for name and area
w.record(row["Name"], row["Area"])
# parse the coordinate string
wkt = row["geometry"][9:-2]
# break the coordinate string in to x,y values
coords = wkt.split(",")
# set up a list to contain the coordinates
part = []
# convert the x,y values to floats
for c in coords:
x,y = c.split(" ")
part.append([float(x),float(y)])
# create a polygon record with the list of coordinates.
w.poly(parts=[part])
# save the shapefile!
w.save("polys.shp")