-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_mapColors.py
50 lines (41 loc) · 1.26 KB
/
split_mapColors.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import shapefile
import csv
mapColor_index = 30 #mapcolor7 in natural earth shapefile
#ifile = open('city_classifier.csv', "rb")
#reader = csv.reader(ifile)
#rownum = 0
#cityDictionary = {}
#for row in reader:
# cityDictionary[row[0]] = row[1]
#ifile.close()
def cropMapColor(sfOrigin, color_index):
sf = shapefile.Editor(sfOrigin)
def deleteShape(n):
del sf._shapes[n]
del sf.records[n]
return
# for r in sf.records:
# for i,f in enumerate(r):
# if isinstance(f, str):
# r[i] = unicode(f,'cp1252')
print 'deleteing ',
for i,r in reversed(list(enumerate(sf.records))):
print r[2],
if r[2] != color_index:
print i,
print r[2],
#print color_index,
#print cityDictionary[r[1]] == color_index,
deleteShape(i)
#for r in sf.records:
# for i,f in enumerate(r):
# if isinstance(f, unicode):
# r[i] = f.encode('cp1252')
foutname = '%s_%i'%(sfOrigin, color_index)
if len(sf.records) == 0:
print "This file %s doesn't have any data and is not rendered"%foutname
else:
print 'Outputting: ',foutname
sf.save(foutname)