def transform_path(self, path): def path_interpolation(path, n_steps): path_verts, path_codes = zip(*list(path.iter_segments(curves=False))) path_verts = np.array(path_verts) path_codes = np.array(path_codes) verts_split_inds = np.where(path_codes == Path.MOVETO)[0] verts_split = np.split(path_verts, verts_split_inds, 0) codes_split = np.split(path_codes, verts_split_inds, 0) v_collection = [] c_collection = [] for path_verts, path_codes in zip(verts_split, codes_split): if len(path_verts) == 0: continue import matplotlib.cbook # print path_verts.shape verts = matplotlib.cbook.simple_linear_interpolation(path_verts, n_steps) v_collection.append(verts) # print verts.shape codes = np.ones(verts.shape[0]) * Path.LINETO codes[0] = Path.MOVETO c_collection.append(codes) return Path(np.concatenate(v_collection), np.concatenate(c_collection)) # print 'transform path:', path # print 'p shape: ', path.vertices, path.vertices.shape if path.vertices.shape == (1, 2): return Path(self.transform_no_mod(path.vertices)) # print '\n'.join(['%s %s' % (point, code) for point, code in path.iter_segments()]) # p2 = polygon.Polygon(((-180, -90), (180, -90), (180, 90), (-180, 90))) p2 = self.projection.edge p2 = path_interpolation(p2, 30) p2 = shape_convert.path_to_geos(p2) p2 = p2[0] paths = [] for shp in shape_convert.path_to_geos(path): shps = [] for lon in range(-360, 720, 360): # for lon in range(0, 360, 360): if isinstance(shp, polygon.Polygon): c_shp = polygon.Polygon(np.array(shp.exterior) - [lon, 0], [np.array(ring) - [lon, 0] for ring in shp.interiors]) elif isinstance(shp, linestring.LineString): c_shp = linestring.LineString(np.array(shp) - [lon, 0]) elif isinstance(shp, MultiLineString): c_shp = MultiLineString([linestring.LineString(np.array(s) - [lon, 0]) for s in shp.geoms]) else: raise ValueError('Unknown shapely object (%s).' % (type(shp))) shps.append(c_shp) # Turn the list of shapes into a Geos type if isinstance(shp, polygon.Polygon): shps = MultiPolygon(shps) elif isinstance(shp, linestring.LineString): shps = MultiLineString(shps) else: ValueError('Unknown shape type') # join the shapes back together again if they have wrapped the entire 360 range. shps = cascaded_union(shps) # Do one more intersection of the overall union (this seems to be necessary) intersection_shp = shps.intersection(p2) pths = shape_convert.geos_to_path(intersection_shp) # pths = shape_convert.geos_to_path(shps) # Now interpolate the paths interp_resolution = 15 # interp_resolution = 5 # for pth in pths: # p9 = path_interpolation(pth, 9) ## print '\n------' * 30 ## print 'interp 9:', p9 ## print '\n------' * 5 ## print 'interp 5:', path_interpolation(pth, 5) ## print '\n------' * 30 # paths.append(path_interpolation(pth, 9)) # paths.extend([path_interpolation(pth, interp_resolution) for pth in pths]) if len(paths) == 1: path = paths[0] elif len(paths) == 0: return Path(np.empty([0,2])) else: points = [] codes = [] for path in paths: path_points, path_codes = zip(*(path.iter_segments(curves=False, simplify=False))) points.append(path_points) codes.append(path_codes) points = [np.array(pts) for pts in points] path = Path(np.concatenate(points, 0), np.concatenate(codes)) # print '------------' # print '\n'.join(['%s %s' % (point, code) for point, code in path.iter_segments()]) # print 'll post: ', path.vertices # print path.codes # path = path.interpolated(40) # path = path.interpolated(4) # print path.vertices path.vertices = self.transform_no_mod(path.vertices) return path
def transform_path(self, path): def path_interpolation(path, n_steps): path_verts, path_codes = zip(*list(path.iter_segments(curves=False))) path_verts = np.array(path_verts) path_codes = np.array(path_codes) verts_split_inds = np.where(path_codes == Path.MOVETO)[0] verts_split = np.split(path_verts, verts_split_inds, 0) codes_split = np.split(path_codes, verts_split_inds, 0) v_collection = [] c_collection = [] for path_verts, path_codes in zip(verts_split, codes_split): if len(path_verts) == 0: continue import matplotlib.cbook # print path_verts.shape verts = matplotlib.cbook.simple_linear_interpolation(path_verts, n_steps) v_collection.append(verts) # print verts.shape codes = np.ones(verts.shape[0]) * Path.LINETO codes[0] = Path.MOVETO c_collection.append(codes) return Path(np.concatenate(v_collection), np.concatenate(c_collection)) # print 'transform path:', path # print 'p shape: ', path.vertices, path.vertices.shape if path.vertices.shape == (1, 2): return Path(self.transform_no_mod(path.vertices)) # print '\n'.join(['%s %s' % (point, code) for point, code in path.iter_segments()]) p2 = polygon.Polygon(((-180, -90), (180, -90), (180, 90), (-180, 90))) paths = [] shps = [] for shp in shape_convert.path_to_geos(path): shps = [] for lon in range(-360, 720, 360): # for lon in range(0, 360, 360): if isinstance(shp, polygon.Polygon): c_shp = polygon.Polygon(np.array(shp.exterior) - [lon, 0], [np.array(ring) - [lon, 0] for ring in shp.interiors]) elif isinstance(shp, linestring.LineString): c_shp = linestring.LineString(np.array(shp) - [lon, 0]) elif isinstance(shp, MultiLineString): c_shp = MultiLineString([linestring.LineString(np.array(s) - [lon, 0]) for s in shp.geoms]) else: raise ValueError('Unknown shapely object (%s).' % (type(shp))) shps.append(c_shp) if isinstance(shp, polygon.Polygon): shps = MultiPolygon(shps) elif isinstance(shp, linestring.LineString): shps = MultiLineString(shps) else: ValueError('Unknown shape type') # join the shapes back together again if they have wrapped the entire 360 range. from shapely.ops import cascaded_union shps = cascaded_union(shps) interp_resolution = 40 # try: intersection_shp = shps.intersection(p2) pths = shape_convert.geos_to_path(intersection_shp) # print pths paths.extend([path_interpolation(pth, interp_resolution) for pth in pths]) # print paths # except shapely.geos.TopologicalError: # print 'failed with: ', shps # print 'orig path: ', path # print 'path: ', shape_convert.geos_to_path(shps) # import matplotlib.pyplot as plt # from matplotlib.collections import PatchCollection # import matplotlib.patches as mpatches # import matplotlib.cm # # plt.close() # pth = shape_convert.geos_to_path(shps)[0] # poly = mpatches.PathPatch(pth) # collection = PatchCollection([poly], cmap=matplotlib.cm.jet, alpha=0.4) # plt.gca().add_collection(collection) # plt.show() if len(paths) == 1: path = paths[0] elif len(paths) == 0: return Path(np.empty([0,2])) else: points = [] codes = [] for path in paths: path_points, path_codes = zip(*(path.iter_segments())) points.append(path_points) codes.append(path_codes) points = [np.array(pts) for pts in points] path = Path(np.concatenate(points, 0), np.concatenate(codes)) # print '------------' # print '\n'.join(['%s %s' % (point, code) for point, code in path.iter_segments()]) # print 'll post: ', path.vertices # print path.codes # path = path.interpolated(40) # path = path.interpolated(4) # print path.vertices # print ']]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' path.vertices = self.transform_no_mod(path.vertices) return path