How to integrate Python function into your Java code?

When I review my past projects, I found one that might benefit you when you want to invoke Python functions from your Java code directly.python-doc-icon

The project was for a local public agency. They hired people to collect air pollution data on streets and intersections using handheld device. The device stores longitude, latitude and other relevant data into attached MS Access database. Since client uses ESRI ArcGIS which mainly uses UTM projection. So we need to convert longitude and latitude data into UTM basis in order to generate the map.

One of the biggest challenges was the budget of this project was quite limited. It only allowed us spend 2 weeks time on achieving the data migration from MS Access to Oracle database, also creating the Java application to incorporate ESRI Spatial API to generate the map. Obviously it didn’t give us time to research how to do the projection conversion.

The good news was we found a Python function LatLongUTMconversion which does the conversion from longitude and latitude to UTM. So now we need to invoke the Python function in our Java code dynamically and get according UTM data. How did we do it?

IĀ built a PythonBridge class utilizing Jython API which offers the interface between Python and Java. Let’s dive into the code. Please pay attention to following highlighted rows:

//import jython lib from http://www.jython.org/
import org.python.core.*;
import org.python.util.*;
public class PythonBridge {
  //Set logger as current class logger
  static Logger logger = Logger.getLogger( PythonBridge.class.getName () ) ;
  org.python.util.PythonInterpreter interp = null;
  String sLatLongUTMconversion;

  public PythonBridge() {
    logger.debug(Resource.getResource("LogSeparator"));
    try {
      //Load in Python interpreter
      interp = new org.python.util.PythonInterpreter();
      //Resource bundle for the path of Python scripts
      sLatLongUTMconversion = Resource.getResource("LatLongUTMconversion.Path");
      //Log path loaded in
      logger.info("Python Script=" + sLatLongUTMconversion);
      //Execute Python script using interpreter
      interp.execfile(sLatLongUTMconversion);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

Below code provides the conversion from latitude and longitude data into UTM:

  public String convertToUTM (double dLatitude, double dLongitude)
  {
    String sUTM = "";
    PyObject pyUTM = null;

    try {
      //Load in latitude and longitude parameters
      interp.set("lati", new PyFloat(dLatitude));
      interp.set("logi", new PyFloat(dLongitude));
      //Execute Python function LLtoUTM with parameters
      interp.exec("x = LLtoUTM(lati, logi)");
      //Get result UTM stringpyUTM = interp.get("x");
      pyUTM = interp.get("x");
    }
      catch (Exception e) {
      e.printStackTrace();
    }

    if (pyUTM != null) {
    //Log UTM string
    //logger.debug("UTM String="+pyUTM.toString());
    return pyUTM.toString();
  }
    else {
    return "";
    }
  }

Here’s the complete PythonBridge class available for downloading. Have fun!