forked from nysenate/JGeocoder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvertShapefile.py
35 lines (28 loc) · 1.14 KB
/
convertShapefile.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
33
34
35
import shapefile
import csv
import os,sys
def main():
with open(sys.argv[2], "wb") as outputFile:
for path in os.listdir(os.path.abspath(sys.argv[1])):
path = os.path.join(sys.argv[1],path)
if os.path.isdir(path):
convertShapefile(path, outputFile)
def convertShapefile(folderPath, outputFile):
shp_path = os.path.join(folderPath, os.path.basename(folderPath)+'.shp')
sf = shapefile.Reader(shp_path)
shapes = sf.shapes()
fields=sf.fields[1:]
records=sf.records()
fullfields=records[:len(fields)]+["BBOX text","NUMPARTS","SHAPETYPE","LATLONGPAIRS"]
csv_writer = csv.writer(outputFile)
# csv_writer.writerow(fullfields)
for shape, record in zip(shapes, records):
bbox = ";".join(str(point) for point in shape.bbox)
num_parts = len(shape.parts)
shape_type = shape.shapeType
point_str = ":".join("({0};{1})".format(p[1],p[0]) for p in shape.points)
full_record = record+[bbox, num_parts,shape_type,point_str]
csv_writer.writerow(full_record)
print("Done with {0}".format(folderPath))
if __name__ == "__main__":
main()