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]:
(array([0, 3, 6, 9]), array([0, 2, 4, 6, 8]))

In [48]:
src_mesh = plt.pcolormesh(X,Y, X+Y, cmap='Blues')
plt.colorbar()


Out[48]:
<matplotlib.colorbar.Colorbar instance at 0x5468440>

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]:
array([ 0.199794  ,  0.7907004 ,  0.38656598,  0.74146505,  1.        ,
        0.98945207,  0.74974587,  1.        ,  1.        ,  0.58111295,
        1.        ,  1.        ])

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]:
(array([ 0.56947804,  0.3483037 ,  0.        ,  0.05739693,  0.02482133,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ]),
 array([ 0.1545438 ,  0.        ,  0.58047618,  0.26498002,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ]))

In [ ]: