-
Notifications
You must be signed in to change notification settings - Fork 0
/
safecastCommon.py
184 lines (154 loc) · 5.97 KB
/
safecastCommon.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright (C) 2011 Lionel Bergeret
#
# ----------------------------------------------------------------
# The contents of this file are distributed under the CC0 license.
# See http://creativecommons.org/publicdomain/zero/1.0/
# ----------------------------------------------------------------
# mathematical libraries
import numpy as np
from scipy import interpolate
# matplotlib
from matplotlib import cm, colors
import matplotlib.pyplot as plt
# Math
from math import pi,cos,sin,log,exp,atan
DEG_TO_RAD = pi/180
RAD_TO_DEG = 180/pi
# -----------------------------------------------------------------------------
# Discretize a colormap
# -----------------------------------------------------------------------------
def cmap_discretize(cmap, N, crop = 0):
"""Return a discrete colormap from the continuous colormap cmap"""
cdict = cmap._segmentdata.copy()
# N colors
colors_i = np.linspace(crop,1.,N)
# N+1 indices
indices = np.linspace(0,1.,N+1)
for key in ('red','green','blue'):
# Find the N colors
D = np.array(cdict[key])
I = interpolate.interp1d(D[:,0], D[:,1])
acolors = I(colors_i)
# Place these colors at the correct indices.
A = np.zeros((N+1,3), float)
A[:,0] = indices
A[1:,1] = acolors
A[:-1,2] = acolors
# Create a tuple for the dictionary.
L = []
for l in A:
L.append(tuple(l))
cdict[key] = tuple(L)
# Add dark gray at the end
for key in ('red','green','blue'):
L = list(cdict[key])
L[len(L)-1] = (1.0,0.3,0.3) # gray
cdict[key] = tuple(L)
return colors.LinearSegmentedColormap('colormap',cdict,1024)
# -----------------------------------------------------------------------------
# Perform a google projection
# -----------------------------------------------------------------------------
# from http://svn.openstreetmap.org/applications/rendering/mapnik/generate_tiles.py
#
class GoogleProjection:
def __init__(self,levels=18):
self.Bc = []
self.Cc = []
self.zc = []
self.Ac = []
c = 256
for d in range(0,levels):
e = c/2;
self.Bc.append(c/360.0)
self.Cc.append(c/(2 * pi))
self.zc.append((e,e))
self.Ac.append(c)
c *= 2
def minmax (self,a,b,c):
a = max(a,b)
a = min(a,c)
return a
def fromLLtoPixel(self,ll,zoom):
d = self.zc[zoom]
e = round(d[0] + ll[0] * self.Bc[zoom])
f = self.minmax(sin(DEG_TO_RAD * ll[1]),-0.9999,0.9999)
g = round(d[1] + 0.5*log((1+f)/(1-f))*-self.Cc[zoom])
return (e,g)
def fromPixelToLL(self,px,zoom):
e = self.zc[zoom]
f = (px[0] - e[0])/self.Bc[zoom]
g = (px[1] - e[1])/-self.Cc[zoom]
h = RAD_TO_DEG * ( 2 * atan(exp(g)) - 0.5 * pi)
return (f,h)
def convert(self, gx, gy, zoom):
# Calculate pixel positions of bottom-left & top-right
p0 = (gx * 256, (gy + 1) * 256)
p1 = ((gx + 1) * 256, gy * 256)
# Convert to LatLong (EPSG:4326)
l0 = self.fromPixelToLL(p0, zoom);
l1 = self.fromPixelToLL(p1, zoom);
# Get tile width and height in degrees
lonWidth = (l1[0]-l0[0])
latHeight = (l1[1]-l0[1])
# Convert main tile position to LatLong (EPSG:4326)
latlon = self.fromPixelToLL(((gx)*256, (gy+1)*256), zoom); # top-left
return (latlon[0], lonWidth, latlon[1], latHeight)
# -----------------------------------------------------------------------------
# Mask outside polygons
# -----------------------------------------------------------------------------
# Original from http://stackoverflow.com/questions/3320311/fill-outside-of-polygon-mask-array-where-indicies-are-beyond-a-circular-boundar
# Modified to add multiple polygons support (Lionel)
#
def mask_outside_polygons(polygons, pcolor, ax=None):
"""
Plots a mask on the specified axis ("ax", defaults to plt.gca()) such that
all areas outside of the polygon specified by "poly_verts" are masked.
"poly_verts" must be a list of tuples of the verticies in the polygon in
counter-clockwise order.
Returns the matplotlib.patches.PathPatch instance plotted on the figure.
"""
import matplotlib.patches as mpatches
import matplotlib.path as mpath
if ax is None:
ax = plt.gca()
# Get current plot limits
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# Verticies of the plot boundaries in clockwise order
bound_verts = [(xlim[0], ylim[0]), (xlim[0], ylim[1]),
(xlim[1], ylim[1]), (xlim[1], ylim[0]),
(xlim[0], ylim[0])]
# A series of codes (1 and 2) to tell matplotlib whether to draw a line or
# move the "pen" (So that there's no connecting line)
bound_codes = [mpath.Path.MOVETO] + (len(bound_verts) - 1) * [mpath.Path.LINETO]
poly_codes = []
poly_verts = []
for poly in polygons:
poly_codes += [mpath.Path.MOVETO] + (len(poly) - 1) * [mpath.Path.LINETO]
poly_verts += poly
# Plot the masking patch
path = mpath.Path(bound_verts + poly_verts, bound_codes + poly_codes)
patch = mpatches.PathPatch(path, facecolor=pcolor, edgecolor='none')
patch = ax.add_patch(patch)
# Reset the plot limits to their original extents
ax.set_xlim(xlim)
ax.set_ylim(ylim)
return patch
# -----------------------------------------------------------------------------
# Make a color transparent from an image
# -----------------------------------------------------------------------------
def makeColorTransparent(image, color):
"""
Replace the color from the input image by transparent
"""
img = image
img = img.convert("RGBA")
pixdata = img.load()
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y] == color:
pixdata[x, y] = (255, 255, 255, 0)
return img