16
from abc import ABCMeta, abstractmethod
20
__geod = pyproj.Geod(ellps='WGS84', units='m')
22
__whitespaceRe = re.compile('\s+')
24
def __init__(self, x, y):
34
def __invgeod(self, other):
35
return Point.__geod.inv(self.x, self.y, other.x, other.y)
37
def distance(self, other):
38
return self.__invgeod(other)[2]
40
def azimuth(self, other):
41
return self.__invgeod(other)[0]
45
abstract base class for geometries
48
__metaclass__ = ABCMeta
52
def __init__(self, srid = DefaultSrid):
58
def setSrid(self, srid):
66
class Envelope(Geometry):
67
''' class for definition of coordinate envelopes '''
69
def __init__(self, xmin, xmax, ymin, ymax, srid=Geometry.DefaultSrid):
70
Geometry.__init__(self, srid)
92
return abs(self.ymax - self.ymin)
95
return abs(self.xmax - self.xmin)
97
def contains(self, point):
98
if ((point.getX() >= self.xmin) and
99
(point.getX() <= self.xmax) and
100
(point.getY() >= self.ymin) and
101
(point.getY() <= self.ymax)):
107
return shapely.geometry.Polygon(((self.xmin, self.ymin), (self.xmin, self.ymax), (self.xmax, self.ymax), (self.xmax, self.ymin)))
110
return 'longitude: %.2f .. %.2f, latitude: %.2f .. %.2f' % (self.xmin, self.xmax, self.ymin, self.ymax)
113
class Raster(Envelope):
114
' class for raster characteristics and data '
116
def __init__(self, xmin, xmax, ymin, ymax, xdiv, ydiv, srid=Geometry.DefaultSrid, nodata = 0):
117
Envelope.__init__(self, xmin, xmax, ymin, ymax, srid)
121
self.data = numpy.zeros((self.getYBinCount(), self.getXBinCount()), dtype=type(self.nodata))
129
def getXBinCount(self):
130
return int(math.ceil(1.0 * (self.xmax - self.xmin) / self.xdiv))
132
def getYBinCount(self):
133
return int(math.ceil(1.0 * (self.ymax - self.ymin) / self.ydiv))
135
def set(self, x, y, data):
136
self.data[y][x] = data
139
return self.data[y][x]
141
def getNodataValue(self):
145
result = 'NCOLS %d\n' % self.getXBinCount()
146
result += 'NROWS %d\n' % self.getYBinCount()
147
result += 'XLLCORNER %.4f\n' % self.getXMin()
148
result += 'YLLCORNER %.4f\n' % self.getYMin()
149
result += 'CELLSIZE %.4f\n' % self.getXDiv()
150
result += 'NODATA_VALUE %s\n' % str(self.getNodataValue())
152
for row in self.data[::-1]:
154
result += str(cell) + ' '
157
return result.strip()
164
for row in self.data[::-1]:
170
if maximum > len(chars):
171
divider = float(maximum) / (len(chars) - 1)
175
result = (self.getXBinCount() + 2) * '-' + '\n'
176
for row in self.data[::-1]:
179
index = int(math.floor((cell - 1) / divider + 1))
180
result += chars[index]
183
result += (self.getXBinCount() + 2) * '-' + '\n'
184
result += 'total count: %d, max per area: %d' %(total, maximum)