In [125]:
import shapely
import numpy as np
import shapely.geometry
import scipy.sparse
In [45]:
x = np.arange(0,12,3)
y = np.arange(0,10,2)
X, Y = np.meshgrid(x,y)
x,y
Out[45]:
In [48]:
src_mesh = plt.pcolormesh(X,Y, X+Y, cmap='Blues')
plt.colorbar()
Out[48]:
In [49]:
src_paths = src_mesh.get_paths()
In [51]:
src_polys = [shapely.geometry.Polygon(shell=path.vertices) for path in src_paths]
In [77]:
x = np.random.random(20)*10
y = np.random.random(20)*10
dst_tri = matplotlib.tri.Triangulation(x,y)
dst_mesh = matplotlib.tri.TriMesh(dst_tri, facecolor='black')
dst_paths = dst_mesh.convert_mesh_to_paths(dst_tri)
dst_polys = [shapely.geometry.Polygon(shell=path.vertices) for path in dst_paths]
In [78]:
fig, ax = plt.subplots()
ax.triplot(dst_tri, 'bo-')
#ax.add_collection(dst_mesh, autolim=False)
In [76]:
In [118]:
fig, ax = plt.subplots()
import matplotlib.patches
import matplotlib.collections
for i, poly in enumerate(dst_polys):
patch = matplotlib.patches.Polygon(array(poly.exterior), facecolor='black', edgecolor='white', linewidth=2, alpha=0.3)
ax.text(poly.centroid.x, poly.centroid.y, i, fontdict={'color': 'black','alpha': 0.5})
ax.add_patch(patch)
for i, poly in enumerate(src_polys):
patch = matplotlib.patches.Polygon(array(poly.exterior), facecolor='green', edgecolor='white', linewidth=2, alpha=0.3)
ax.text(poly.centroid.x, poly.centroid.y, i, fontdict={'color': 'green','alpha': 0.5})
ax.add_patch(patch)
ax.autoscale()
In [138]:
W = scipy.sparse.dok_matrix((len(src_polys), len(dst_polys)), dtype='double')
for i, src_poly in enumerate(src_polys):
for j, dst_poly in enumerate(dst_polys):
src_poly.area, dst_poly.area, src_poly.intersection(dst_poly).area
area = src_poly.intersection(dst_poly).area
if area > 0:
W[i,j] = area
src_areas = [poly.area for poly in src_polys]
dst_areas = [poly.area for poly in dst_polys]
In [184]:
fig, ax = plt.subplots()
src_weights = W.sum(1).A.squeeze()/src_areas
N = matplotlib.colors.Normalize(vmin=src_weights.min(), vmax=src_weights.max())
colors = matplotlib.cm.Blues(N(src_weights))
for i, (poly, color) in enumerate(zip(src_polys,colors)):
patch = matplotlib.patches.Polygon(array(poly.exterior), facecolor=color, edgecolor='white', linewidth=2, alpha=0.3)
ax.text(poly.centroid.x, poly.centroid.y, i, fontdict={'color': 'green','alpha': 0.5})
ax.add_patch(patch)
dst_weights = W.sum(0).A.squeeze()/dst_areas
N = matplotlib.colors.Normalize(vmin=dst_weights.min(), vmax=dst_weights.max())
colors = matplotlib.cm.Greens(N(dst_weights))
for i, (poly, color) in enumerate(zip(dst_polys,colors)):
patch = matplotlib.patches.Polygon(array(poly.exterior), facecolor=color, edgecolor='white', linewidth=2, alpha=0.3)
ax.text(poly.centroid.x, poly.centroid.y, i, fontdict={'color': 'black','alpha': 0.5})
ax.add_patch(patch)
ax.autoscale()
In [180]:
W.sum(1).A.squeeze()/src_areas
Out[180]:
In [265]:
dst2src = np.multiply(W.A, 1/W.sum(0).A)
src2dst = np.multiply(1/W.sum(1).A, W.A)
In [270]:
dst2src[:,0], src2dst[1,:]
Out[270]:
In [ ]: