Menu

[r8871]: / trunk / toolkits / basemap / utils / readboundaries.py  Maximize  Restore  History

Download this file

154 lines (141 with data), 5.7 kB

  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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import numpy, sys
lsd = 3
def quantize(data,least_significant_digit):
"""
quantize data to improve compression. data is quantized using
around(scale*data)/scale, where scale is 2**bits, and bits is determined
from the least_significant_digit. For example, if
least_significant_digit=1, bits will be 4.
This function is pure python.
"""
precision = pow(10.,-least_significant_digit)
exp = numpy.log10(precision)
if exp < 0:
exp = int(numpy.floor(exp))
else:
exp = int(numpy.ceil(exp))
bits = numpy.ceil(numpy.log2(pow(10.,-exp)))
scale = pow(2.,bits)
return numpy.around(scale*data)/scale
def get_coast_polygons(coastfile):
polymeta = []; polybounds = []
lats = []; lons = []
for line in open(coastfile):
if line.startswith('#'): continue
linesplit = line.split()
if line.startswith('P'):
area = float(linesplit[5])
west,east,south,north = float(linesplit[6]),float(linesplit[7]),float(linesplit[8]),float(linesplit[9])
typ = int(linesplit[3])
polymeta.append((typ,area,south,north))
if lons:
#lons.append(lons[0]); lats.append(lats[0])
b = numpy.empty((len(lons),2),numpy.float32)
b[:,0] = lons; b[:,1] = lats
if lsd is not None:
b = quantize(b,lsd)
polybounds.append(b)
lats = []; lons = []
continue
lon = float(line[1:10])
lat = float(line[10:20])
lons.append(lon); lats.append(lat)
#lons.append(lons[0]); lats.append(lats[0])
b = numpy.empty((len(lons),2),numpy.float32)
b[:,0] = lons; b[:,1] = lats
if lsd is not None:
b = quantize(b,lsd)
polybounds.append(b)
polymeta2 = []
for meta,bounds in zip(polymeta,polybounds):
npts = bounds.shape[0]
polymeta2.append(meta+(npts,))
return polybounds, polymeta2
def get_boundary_lines(bdatfile):
lons = []; lats = []; polybounds = []
for line in open(bdatfile):
if line.startswith('#'): continue
linesplit = line.split()
if line.startswith('>'):
if lons:
b = numpy.empty((len(lons),2),numpy.float32)
b[:,0] = lons; b[:,1] = lats
if lsd is not None:
b = quantize(b,lsd)
polybounds.append(b)
lons = []; lats = []
continue
lon, lat = [float(val) for val in linesplit]
lats.append(lat); lons.append(lon)
b = numpy.empty((len(lons),2),numpy.float32)
b[:,0] = lons; b[:,1] = lats
if lsd is not None:
b = quantize(b,lsd)
polybounds.append(b)
polymeta = []
polybounds2 = []
for bounds in polybounds:
npts = bounds.shape[0]
if npts == 2 and\
bounds[0,0] == bounds[1,0] and\
bounds[0,1] == bounds[1,1]: continue
polybounds2.append(bounds)
south = bounds[:,1].min()
north = bounds[:,1].max()
polymeta.append((south,north,npts))
return polybounds2, polymeta
# read in coastline data (only those polygons whose area > area_thresh).
for resolution in ['c','l','i','h','f']:
coastlons = []; coastlats = []; coastsegind = []; coastsegtype = []
coastfile = 'gshhs_'+resolution+'.txt'
countryfile = 'countries_'+resolution+'.txt'
statefile = 'states_'+resolution+'.txt'
riverfile = 'rivers_'+resolution+'.txt'
poly, polymeta = get_coast_polygons(coastfile)
f = open('../lib/matplotlib/toolkits/basemap/data/gshhs_'+resolution+'.dat','wb')
f2 = open('../lib/matplotlib/toolkits/basemap/data/gshhsmeta_'+resolution+'.dat','w')
offset = 0
for p,pm in zip(poly,polymeta):
bstring = p.tostring()
f.write(bstring)
typ = pm[0]; area = pm[1]; south = pm[2]; north = pm[3]; npts = pm[4]
f2.write('%s %s %s %9.5f %9.5f %s %s\n' % (typ, area, npts, south, north, offset, len(bstring)))
offset = offset + len(bstring)
f.close()
f2.close()
poly, polymeta = get_boundary_lines(countryfile)
f = open('../lib/matplotlib/toolkits/basemap/data/countries_'+resolution+'.dat','wb')
f2 = open('../lib/matplotlib/toolkits/basemap/data/countriesmeta_'+resolution+'.dat','w')
offset = 0
for p,pm in zip(poly,polymeta):
bstring = p.tostring()
f.write(bstring)
south,north,npts = pm[:]
f2.write('%s %s %s %9.5f %9.5f %s %s\n' % (-1,-1,npts, south, north, offset, len(bstring)))
offset = offset + len(bstring)
f.close()
f2.close()
poly, polymeta = get_boundary_lines(statefile)
f = open('../lib/matplotlib/toolkits/basemap/data/states_'+resolution+'.dat','wb')
f2 = open('../lib/matplotlib/toolkits/basemap/data/statesmeta_'+resolution+'.dat','w')
offset = 0
for p,pm in zip(poly,polymeta):
bstring = p.tostring()
f.write(bstring)
south,north,npts = pm[:]
f2.write('%s %s %s %9.5f %9.5f %s %s\n' % (-1,-1,npts, south, north, offset, len(bstring)))
offset = offset + len(bstring)
f.close()
f2.close()
poly, polymeta = get_boundary_lines(riverfile)
f = open('../lib/matplotlib/toolkits/basemap/data/rivers_'+resolution+'.dat','wb')
f2 = open('../lib/matplotlib/toolkits/basemap/data/riversmeta_'+resolution+'.dat','w')
offset = 0
for p,pm in zip(poly,polymeta):
bstring = p.tostring()
f.write(bstring)
south,north,npts = pm[:]
f2.write('%s %s %s %9.5f %9.5f %s %s\n' % (-1,-1,npts, south, north, offset, len(bstring)))
offset = offset + len(bstring)
f.close()
f2.close()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.