pyhelpers: helping with list conversions
Disclaimer: I'm not used to JCC, so I may have missed something obvious :)
I use the following 2 functions when passing Python lists to Orekit functions expecting arrays or lists.
element_type
is optional, only required for empty lists. Only lists of objects are supported, not lists of primitive types.
def to_java_array(elements, element_type=None):
"""Convert a Python list of objects to a Java array."""
if elements is None:
return None
if not element_type:
if elements:
element_type = elements[0].__class__
else:
raise RuntimeError('to_java_array error: element_type is required if elements is empty')
return JArray('object')(elements, element_type)
def to_java_list(elements, element_type=None):
"""Convert a Python list of objects to a Java list."""
if elements is None:
return None
if not element_type:
if elements:
element_type = elements[0].__class__
else:
raise RuntimeError('to_java_list error: element_type is required if elements is empty')
ret = ArrayList().of_(element_type)
for element in elements:
ret.add(element)
return ret
# example:
s2p_list = [S2Point(...), S2Point(...), ...]
aoi = SphericalPolygonsSet(1.e-9, to_java_array(s2p_list))
# instead of:
aoi = SphericalPolygonsSet(1.e-9, JArray('object')(s2p_list, S2Point))
If this is the right way to do it, could this kind of helpers be added to pyhelpers.py? That would be easier.
Thanks