Alireza Shamakhy
Alireza Shamakhy

Reputation: 11

Geotools Sort By issue

I have a simple SQL query in postgis :

"Select * from table1 order by filed1 desc, ABS(filed2 - 10) asc"

in above query I order firstly based on the field1, and then absolute value of field2 minus 10, which works fine in postgis.

I am going to simulate this using geotools but I can not implement ABS function. Here is part of my code:

final SortBy sortByField1 = ff.sort("field1", SortOrder.DESCENDING);

final SortBy sortByField2 = ff.sort("ABS(field2 -10)", SortOrder.ASCENDING);

The seconds statement throws an exception since sort only recognizes field name not expression. I had a look at documentation and I found that we can use expression to handle this but I couldnt implement it :

   PropertyName name = sortByField2.getPropertyName();  
   name.evaluate(object)
    final SortBy newSort  = new SortByImpl(name, SortOrder.ASCENDING);

I know there are FilterFunction_abs and Add functions in Geotools but I dont know how I can use those to evaluate ABS and subtract function.

It would be great if someone can help me to find a solution.

Thanks,

Upvotes: 1

Views: 142

Answers (1)

Ian Turton
Ian Turton

Reputation: 10976

Abs should already exist (if you look in the GeoServer documentation) depending on the type of the column you will need abs_3 or abs_4. You'll also need to break out the field2 - 10 in to a sub function (that's a built in part of the standard).

If all else fails you can write a custom filter using this tutorial.

Upvotes: 0

Related Questions