B4J Question JavaObject arrays

Green1

Member
Licensed User
Longtime User
I am new to JavaObject and finding it useful but having trouble working with JavaObject arrays
Using org.locationtech.jts, I am able to construct linestrings, points and coordinates.
If I construct a coordinate I can access the ordinates via RunMethod or GetField but when I return an array of coordinates from a linestring, although I can apparently retrieve a coordinate from the array, I am unable to access the ordinates or construct a line from 2 coordinates. The array appears to be a valid array and the coordinates appear to be valid coordinates but they look to not be valid JavaObjects, but perhaps jts objects? so I must be missing something. Any assistance would be appreciated

My test code - sorry about the length but I tried a few options

Thanks

B4X:
Sub testJOarray()
    ' Dim line and point text
    Dim wkt As String = "LINESTRING M(116.016387 -31.908495 0, 116.016275 -31.908571 0.013985, 116.016134 -31.908641 0.03)"
    Dim pt As String = "POINT(116.016 -31.908)"
    Dim jo As JavaObject
    'Construct coordinate and return ordinates
    Dim coordA As JavaObject = jo.InitializeNewInstance("org.locationtech.jts.geom.CoordinateXYM", Array(116.016, -31.908, 2.15))
    Dim coordB As JavaObject = jo.InitializeNewInstance("org.locationtech.jts.geom.CoordinateXYM", Array(116.017, -31.909, 3.15))
    Dim x As Double = coordA.GetField("x") '116.017
    x = coordB.RunMethod("getX", Null) '116.017
    Dim y As Double = coordA.GetField("y") '-31.909
    Dim m As Double = coordA.RunMethod("getM", Null) '3.15
    'Construct line segment from coordinates
    Dim lineSegment As JavaObject = jo.InitializeNewInstance("org.locationtech.jts.geom.LineSegment", Array(coordA, coordB))
    'Construct linestring from well known text
    Dim rdr As JavaObject = jo.InitializeNewInstance("org.locationtech.jts.io.WKTReader",  Array(jo.InitializeNewInstance("org.locationtech.jts.geom.GeometryFactory", Array(jo.InitializeNewInstance("org.locationtech.jts.geom.PrecisionModel", Null), 4236))))
    Dim line As JavaObject = rdr.RunMethodJO("read", Array(wkt))
    'Return array of coordinates from linestring (2 methods)
    Dim coords0() As JavaObject = line.RunMethodJO("getCoordinates", Null)
    Dim coords1() As JavaObject = jo.InitializeArray("org.locationtech.jts.geom.Coordinate", line.RunMethodJO("getCoordinates", Null))
    'Return coordinate objects from arrays (1 from first array 2 from second for testing)
    Dim Coord0 As JavaObject = coords0(0)
    Dim Coord1 As JavaObject = coords1(1)
    Dim Coord2 As JavaObject = coords1(2)
    'several methods to return x ordinate - all fail
    x = coords0(0).RunMethod("getX", Null) 'java.lang.RuntimeException: Method: RunMethod not found in: org.locationtech.jts.geom.CoordinateXYM
    x = coords0(0).GetField("x") 'java.lang.RuntimeException: Method: GetField not found in: org.locationtech.jts.geom.CoordinateXYM
    x = Coord0.RunMethod("getX", Null) 'java.lang.RuntimeException: Method: RunMethod not found in: org.locationtech.jts.geom.CoordinateXYM
    x = Coord0.GetField("x") 'java.lang.RuntimeException: Method: GetField not found in: org.locationtech.jts.geom.CoordinateXYM
    'Construct line segment from array coordinates - fails
    'java.lang.RuntimeException: Method: getObject not found in: org.locationtech.jts.geom.CoordinateXYM
    Dim lineSegment1 As JavaObject = jo.InitializeNewInstance("org.locationtech.jts.geom.LineSegment", Array(Coord0, Coord1))

End Sub
 

stevel05

Expert
Licensed User
Longtime User
Assigning an array directly to JavaObject does not cast each element of the array to a Javaobject, but the whole array to a single JavaObject. Sounds complicated but if you think about it, it can only work like that.

You can cast to an array of object, and as you are already casting the elements you want to JavaObject as you want to use them, the modifications required are quite simple.

This works without error (I cant test further as I don't know what else is expected) I only downloaded the jts.core jar.

I only changed lines 19 and 20 and commented 27 and 28.

B4X:
Sub testJOarray()
    ' Dim line and point text
    Dim wkt As String = "LINESTRING M(116.016387 -31.908495 0, 116.016275 -31.908571 0.013985, 116.016134 -31.908641 0.03)"
    Dim pt As String = "POINT(116.016 -31.908)"
    Dim jo As JavaObject
    'Construct coordinate and return ordinates
    Dim coordA As JavaObject = jo.InitializeNewInstance("org.locationtech.jts.geom.CoordinateXYM", Array(116.016, -31.908, 2.15))
    Dim coordB As JavaObject = jo.InitializeNewInstance("org.locationtech.jts.geom.CoordinateXYM", Array(116.017, -31.909, 3.15))
    Dim x As Double = coordA.GetField("x") '116.017
    x = coordB.RunMethod("getX", Null) '116.017
    Dim y As Double = coordA.GetField("y") '-31.909
    Dim m As Double = coordA.RunMethod("getM", Null) '3.15
    'Construct line segment from coordinates
    Dim lineSegment As JavaObject = jo.InitializeNewInstance("org.locationtech.jts.geom.LineSegment", Array(coordA, coordB))
    'Construct linestring from well known text
    Dim rdr As JavaObject = jo.InitializeNewInstance("org.locationtech.jts.io.WKTReader",  Array(jo.InitializeNewInstance("org.locationtech.jts.geom.GeometryFactory", Array(jo.InitializeNewInstance("org.locationtech.jts.geom.PrecisionModel", Null), 4236))))
    Dim line As JavaObject = rdr.RunMethodJO("read", Array(wkt))
    'Return array of coordinates from linestring (2 methods)
    Dim coords0() As Object = line.RunMethodJO("getCoordinates", Null)
    Dim coords1() As Object = jo.InitializeArray("org.locationtech.jts.geom.Coordinate", line.RunMethodJO("getCoordinates", Null))
    'Return coordinate objects from arrays (1 from first array 2 from second for testing)
'    Dim Coord0 As JavaObject = coords0(0)
    Dim Coord0 As JavaObject = coords0(0)
    Dim Coord1 As JavaObject = coords1(1)
    Dim Coord2 As JavaObject = coords1(2)
    'several methods to return x ordinate - all fail
'    x = coords0(0).RunMethod("getX", Null) 'java.lang.RuntimeException: Method: RunMethod not found in: org.locationtech.jts.geom.CoordinateXYM
'    x = coords0(0).GetField("x") 'java.lang.RuntimeException: Method: GetField not found in: org.locationtech.jts.geom.CoordinateXYM

    x = Coord0.RunMethod("getX", Null) 'java.lang.RuntimeException: Method: RunMethod not found in: org.locationtech.jts.geom.CoordinateXYM
    x = Coord0.GetField("x") 'java.lang.RuntimeException: Method: GetField not found in: org.locationtech.jts.geom.CoordinateXYM
    'Construct line segment from array coordinates - fails
    'java.lang.RuntimeException: Method: getObject not found in: org.locationtech.jts.geom.CoordinateXYM
    Dim lineSegment1 As JavaObject = jo.InitializeNewInstance("org.locationtech.jts.geom.LineSegment", Array(Coord0, Coord1))

End Sub

Hope this helps.
 
Last edited:
Upvote 0
Top