forked from guziy/CMOS-python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiona_test.py
92 lines (75 loc) · 2.5 KB
/
fiona_test.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#Fix a bug!!!!!
import os
import numpy as np
import matplotlib.pyplot as plt
import fiona
from descartes import PolygonPatch
from matplotlib.collections import PatchCollection
from shapely.geometry import MultiPolygon, shape, Polygon
from mpl_toolkits.basemap import Basemap
from matplotlib import cm
def download_folder():
#download the shape files directory
from urllib2 import urlopen
import re
local_folder = "countries"
remote_folder = 'http://scaweb.sca.uqam.ca/~huziy/example_data/countries/'
if not os.path.isdir(local_folder):
urlpath = urlopen(remote_folder)
string = urlpath.read().decode('utf-8')
pattern = re.compile(r'cntry00\...."')
filelist = pattern.findall(string)
filelist = [s[:-1] for s in filelist if not s.endswith('zip"')]
os.mkdir(local_folder)
for fname in filelist:
f_path = os.path.join(local_folder, fname)
remote_f_path = os.path.join(remote_folder, fname)
#download selected files
download_link(remote_f_path, f_path)
def download_link(url, local_path):
import os
if os.path.isfile(local_path):
return
import urllib2
s = urllib2.urlopen(url)
with open(local_path, "wb") as local_file:
local_file.write(s.read())
def main():
"""
Main function
"""
download_folder()
#Read country polygons and associated population
polygons = []
populations = []
with fiona.open('countries/cntry00.shp', 'r') as inp:
for f in inp:
the_population = f["properties"]["POP_CNTRY"]
try:
sh = shape(f["geometry"])
assert sh.is_valid
if isinstance(sh, Polygon):
polygons.append(PolygonPatch(sh))
populations.append(the_population)
else:
mp = MultiPolygon(sh)
for pol in mp:
polygons.append(PolygonPatch(pol))
except TypeError, err:
print type(sh)
raise err
populations = np.array(populations)
cmap = cm.get_cmap("Dark2", 20)
print len(polygons)
fig = plt.figure()
ax = fig.add_subplot(111)
bworld = Basemap()
pcol = PatchCollection(polygons, cmap = cmap)
pcol.set_array(populations)
ax.add_collection(pcol)
bworld.colorbar(pcol)
bworld.drawcoastlines()
plt.show()
##Entry point
if __name__ == "__main__":
main()