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:

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

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