[Proj] proj JNI wrapping

Chapman, Martin MChapman at sanz.com
Wed Oct 13 13:09:10 EDT 2004


See my answers next to your responses...

-----Original Message-----
From: Andrea Antonello [mailto:moovida at katamail.com] 
Sent: Wednesday, October 13, 2004 9:02 AM
To: Proj Mailinglist
Subject: Re: [Proj] proj JNI wrapping

Hy Martin, thanks for your reply.

> I've done this with proj and gdal.  JNI works really well.

The first question, which I guess you expect, is: is the work you have
done available? How come it is not officially known?

 -- It's inside a commercial product.

> The only
> concern you will have is thread safety, but that is easily solved on
> java side by synchronizing your jni wrappers.  

Why that? I would use it for certain queries, transformations and
which will way for the data to come back. Could you please explain me?

 -- GDAL and Proj both use the C runtime library extensively and the C
runtime is not threadsafe, but java is thread safe and therefore if you
have multiple java threads trying to use your JNI functions you will get
strange/random behaviors (including crashing) of your app.  The way to
solve this is easy, you simply mark your java functions as syncronized
using the syncronized key word.  It will be slower under multiple user
conditions, but at least it will never crash from threading issues.

> I wouldn't try to do a
> one for one mapping of functions and classes between java and
> Instead I would build a couple of jni classes that call a c++ class
> does a specific operation with gdal/proj.  If you try to use the

Yeah, that is a good idea, I'm in a study phase, do you have any

 -- Yes, Before you try to do anything with java, create a class in c++
that does exactly wht you want to do with gdal/proj.  Once you know that
works, you can attempt to expose that class to java with jni.  I say
this because a lot of programmers try to do everything at once and then
they end up having a hard time debugging their code because it's mixed
across java and c++.  It is possible to step from java to c++ code in
debug mode, but this requires you are an expert at using both your java
and c++ compilers.  Is this what you were looking for?

> gdal/proj c/c++ functions/objects directly in your java code it will 
> get ugly because all of the state management that needs to occur.  In 
> other words, use the JNI bridge sparingly.

Yeah, I know, I've done some JNI, and got only lots of troubles... even
if it 
workes well...

 -- yeah, take baby steps with jni, otherwise you may out smart
yourself.  Start with one function call and try to only pass native data
types (not class structures).  You may need to pass pointers to arrays
of doubles though, in that case, pass a double[] from java and then get
the memory address of the array in your jni (c++) function something
like this...

// in your jni function

// get your double array that was made in java and passed to jni
jbyteArray arr = env->NewDoubleArray(arraySize);
double *points = (double *)env->GetDoubleArrayElements(arr,NULL); 

// Use c++ to do something with your points...like a projection
transform with proj/ogr.

// release the c++ array...this will copy your changes back to the java

 -- Let me know if you have more questions. 

Let me know,

> Martin
> -----Original Message-----
> From: Andrea Antonello [mailto:moovida at katamail.com]
> Sent: Tuesday, October 12, 2004 11:12 PM
> To: Proj Mailinglist
> Subject: [Proj] proj JNI wrapping
> Dear all,
> the need of proj and gdal inside the JGrass project (which is 
> basically a java written portable interface for GRASS with some 
> standalone capabilities) makes
> me think of the possibility to use the JNI java/C bindings to create a
> set of
> java functions to use the proj library.
> Proj and Gdal would that way be awailable to a lot of java based 
> projects.
> Any comment?
> Did anybody already try? Or even finish? :)
> Is anybody willing to cooperate?
> To test if all this makes a sense I would start by doing this with 
> Proj, which is a smaller lib, and then decide what to do next.
> Please let me know what you think,
> Best regards
> Andrea Antonello

HydroloGIS - Environmental Open Source Solutions www.hydrologis.com

Andrea Antonello
Environmental Engineer
mobile:  +393288497722

"Let it be as much a great honour to take as to give learning, if you
want to be called wise." Skuggsja' - The King's mirror - 1240 Reykjavik

Proj mailing list
Proj at xserve.flids.com http://xserve.flids.com/mailman/listinfo/proj

More information about the Proj mailing list