2828import java .net .http .HttpResponse ;
2929import java .net .http .HttpResponse .BodyHandlers ;
3030import java .nio .charset .StandardCharsets ;
31+ import java .util .Arrays ;
3132import java .util .HashMap ;
3233import java .util .List ;
3334import java .util .Map ;
3435import javax .script .Invocable ;
3536import javax .script .ScriptEngine ;
3637import javax .script .ScriptEngineManager ;
3738import javax .script .ScriptException ;
39+
40+ import com .geomatys .crsservice .service .DefaultCrsOperationService ;
3841import org .apache .sis .metadata .iso .citation .DefaultCitation ;
3942import org .apache .sis .referencing .operation .AbstractCoordinateOperation ;
4043import org .apache .sis .referencing .operation .transform .AbstractMathTransform ;
5053import org .opengis .referencing .operation .OperationNotFoundException ;
5154import org .opengis .referencing .operation .TransformException ;
5255import org .opengis .util .FactoryException ;
56+ import org .python .core .Py ;
57+ import org .python .jsr223 .PyScriptEngine ;
5358
5459/**
5560 * CoordinateOperationFactory backed by a distance server.
@@ -70,11 +75,14 @@ public CoordinateOperation createOperation(CoordinateReferenceSystem crs1, Coord
7075
7176 final String crs1Txt = URLEncoder .encode (crs1 .toWKT (), StandardCharsets .UTF_8 );
7277 final String crs2Txt = URLEncoder .encode (crs2 .toWKT (), StandardCharsets .UTF_8 );
78+ // final String query = serviceURL.toString() + "?source=" + crs1Txt + "&target=" + crs2Txt + "&format=text/x-python";
7379 final String query = serviceURL .toString () + "?source=" + crs1Txt + "&target=" + crs2Txt + "&format=text/javascript" ;
7480
7581 try {
7682 final String code = getText (query );
7783 final ScriptEngineManager manager = new ScriptEngineManager ();
84+ // final ScriptEngine engine = manager.getEngineByName("python");
85+ // Object res = engine.eval(code);
7886 final ScriptEngine engine = manager .getEngineByName ("js" );
7987 Object res = engine .eval ("const operationClass = (" +code +");\n operation = new operationClass();" );
8088 final JSMathTransform trs = new JSMathTransform (crs1 .getCoordinateSystem ().getDimension (), crs2 .getCoordinateSystem ().getDimension (), (Invocable ) engine );
@@ -87,6 +95,36 @@ public CoordinateOperation createOperation(CoordinateReferenceSystem crs1, Coord
8795 }
8896 }
8997
98+ public CoordinateOperation createOperation (CoordinateReferenceSystem crs1 , CoordinateReferenceSystem crs2 , final String format ) throws OperationNotFoundException , FactoryException {
99+
100+ final String crs1Txt = URLEncoder .encode (crs1 .toWKT (), StandardCharsets .UTF_8 );
101+ final String crs2Txt = URLEncoder .encode (crs2 .toWKT (), StandardCharsets .UTF_8 );
102+ final String query = serviceURL .toString () + "?source=" + crs1Txt + "&target=" + crs2Txt + "&format=" + format ;
103+
104+ try {
105+ final String code = getText (query );
106+ final ScriptEngineManager manager = new ScriptEngineManager ();
107+ final ScriptEngine engine ;
108+ Object res ;
109+ if (DefaultCrsOperationService .FORMAT_JAVASCRIPT .equals (format )) {
110+ engine = manager .getEngineByName ("js" );
111+ res = engine .eval ("const operationClass = (" +code +");\n operation = new operationClass();" );
112+ } else if (DefaultCrsOperationService .FORMAT_PYTHON .equals (format )) {
113+ engine = manager .getEngineByName ("python" );
114+ res = engine .eval (code );
115+ } else {
116+ throw new FactoryException ("Format not supported yet : " + format );
117+ }
118+ final JSMathTransform trs = new JSMathTransform (crs1 .getCoordinateSystem ().getDimension (), crs2 .getCoordinateSystem ().getDimension (), (Invocable ) engine );
119+
120+ final Map properties = new HashMap ();
121+ properties .put ("name" , "javascript operation" );
122+ return new AbstractCoordinateOperation (properties , crs1 , crs2 , null , trs );
123+ } catch (Exception ex ) {
124+ throw new FactoryException (ex .getMessage (), ex );
125+ }
126+ }
127+
90128 @ Override
91129 public CoordinateOperation createOperation (CoordinateReferenceSystem crs , CoordinateReferenceSystem crs1 , OperationMethod om ) throws OperationNotFoundException , FactoryException {
92130 throw new UnsupportedOperationException ("Not supported." );
@@ -150,9 +188,13 @@ public Matrix transform(double[] src, int so, double[] dst, int doffset, boolean
150188 for (int i = 0 ; i < sourceDim ; i ++) {
151189 array [i ] = src [so + i ];
152190 }
153-
154- final Object jsOperation = ((ScriptEngine ) engine ).eval ("operation" );
155- final List result = (List ) engine .invokeMethod (jsOperation , "transform" , ProxyArray .fromArray (array ));
191+ final List result ;
192+ if (engine instanceof PyScriptEngine ) {
193+ result = (List ) ((PyScriptEngine ) engine ).eval ("Operation().transform(" + Arrays .toString (array ) + ")" );
194+ } else {
195+ final Object jsOperation = ((ScriptEngine ) engine ).eval ("operation" );
196+ result = (List ) engine .invokeMethod (jsOperation , "transform" , ProxyArray .fromArray (array ));
197+ }
156198 for (int i = 0 ; i < targetDim ; i ++) {
157199 dst [doffset + i ] = ((Number )result .get (i )).doubleValue ();
158200 }
0 commit comments