Package: konvert
konvertKonvert implements universal conversion graphs for e.g. coordinate transformations.At the core, konvert represents types connected by conversions on a graph. Such conversion graphs can be expandend incrementally, because it only requires two connections to connect a new type to the graph, This type can then be freely converted to any other type in the graph, by automatically chaining conversions between existing types.The automatic chaining requires the conversions to be parameter-free. Conversions with additional parameters are called projections, which konvert also helps you manage.PointsThe moule konvert.points includes a conversion graph for coordinate transformations. As an example consider a set of points in two-dimensional Cartesian coordinates:fromkonvert.pointsimportCartesian2Dline=Cartesian2D(x=[0,1,2],y=[0,1,2])This points can be converted to other implented 2D point sets, likePolarandBipolar, by using theto()methodfromkonvert.pointsimportPolar,Bipolarlp=line.to(Polar)lb=line.to(Bipolar)It can also be lifted up to its 3D description,l3 = line.to(Cartesian3D). The conversion graph can automatically chain conversions, so in fact, any point in the plane can be hoisted up to any point in three-dimensional space. That means, that points in e.g. polar coordinates, can be hoisted up toCartesian3D.p0=Polar(theta=30*degrees,r=1.5).to(Cartesian3D)Points in 3D can be represented using the coordinate representationsCartesian3D,Cylindrical, andSpherical. All Cartesian types have some helper methods for easily manipulating points.p0=Cartesian3D(1,1,1)q0=Cartesian3D(1,1,0)# Create normalized versionp1=p0.normalized()# Shift p0 by q0p0.shift(q0)# Rotate around an axis thorugh q0.p0.rotate(theta=45*degrees,point=q0)HelpersBecause the points module work extensively with angles, konvert defines a conveniencedegreessymbol, which converts values and arrays in degrees to radians,fromkonvert.pointsimportdegreestheta=90*degrees# theta is now pi/2theta=numpy.array([45,60,90,180,360])*degrees# theta is now numpy.array([pi/4, pi/3, pi/2, pi, 2*pi])Map projectionsThe points module also contains a set of map projections:Azimuthal,AzimuthalEquidistant,Mercator,Orthographic,StereographicandEquirectangular. Points are represented on theSphere, in spherical coordinates, or inEquitorialcoordinates (lattitude and longitude).points=Equitorial([40.12,50.53]*degrees,[33.16,44.53]*degrees,r=1)p0=points.project(Mercator)p1=points.project(Stereographic)...The points can be easily plotted using matplotlib and the plot utility on Cartesian2D,p0.to(Cartesian2D).plot().Extending the conversion graphIt is fairly simple to extend an existing conversion graph and an existing projection collection. Let us extend the points graph with a Skew2D coordinate system. Note the use of numpy to efficiently represent and transform coordinates.importnumpyfromkonvert.pointsimportPointsclassSkew2D(Points):_sig=('x','y','theta')def__init__(self,x,y,theta):"""Skew 2D coordinates. Theta is a scalar denominating the skew angle."""self.x=numpy.array(x)self.y=numpy.array(y)self.theta=thetaThe Skew2D class can be connected to thekonvert.pointsconversion graph by using the following conversion.fromkonvert.pointsimportconverters,Conversion@converters.register()classSkew2DToCartesian2D(Conversion):src=Skew2Ddst=points.Cartesian2D@staticmethoddefconvert(skew):returnpoints.Cartesian2D(skew.x+numpy.cos(theta)*skew.y,numpy.sin(theta)*skew.y)The reverse conversion requires the additional theta parameter and must be implemented as a projection.fromkonvert.pointsimportprojectors,Projection@projectors.register()classCartesian2DToSkew2D(Projection):src=points.Cartesian2Ddst=Skew2D@staticmethoddefproject(cart,theta=np.pi/2):y=cart.y/numpy.sin(theta)x=cart.x-numpy.cos(theta)*yreturnSkew2D(x,y,theta)With these two additions we can convet between Skew2D points and any points type in the graph,p0=Skew2D(1,2,theta=45*degrees)p1=p0.to(Cylindrical)or the opposite way,p1=Cylindrical(theta=30*degrees,phi=45*degrees,r=10)p0=p1.project(OnPlane).project(Cartesian2DToSkew2D,theta=45*degrees)ExtensionsThe conversion graph has been implemented in theconversionsmodule. It is possible to create additional conversion graphs and register existing or new conversions in those graphs. As an example we may create a colors module, which converts between values in the RGB and HSL color space. For this is example we will not bother with vectorizing the entities, and instead just work with single color entries and use thecolorsysmodule in the python standard library.importcolorsysfromkonvert.conversionsimportConversion,ConversionGraphconverters=ConversionGraph()classColor(metaclass=ABCMeta):defto(self,type):returnconvertes.convert(self,type)classRGB(Color):def__init__(self,r,g,b):self.r=rself.g=gself.b=bclassHSL(Color):def__init__(self,h,s,l):self.h=hself.s=sself.l=lconverters.register()classRGBToHSL(Conversion):src=RGBdst=HSL@staticmethoddefconvert(rgb):hsl=colorsys.rgb_to_hsl(rgb.r,rgb.g,rgb.b)returnHSL(*hsl)converters.register()classHSLToRGB(Conversion):src=RGBdst=HSL@staticmethoddefconvert(hsl):rgb=colorsys.hsl_to_rgb(hsl.h,hsl.s,hsl.l)returnRGB(*rgb)We can now writeRGB(0.1, 0.3, 0.3).to(HSL)and get the correct result.If we wanted, we could merge this graph into the points conversion graph, or include part of the points graph in the color conversion graph...