Thanks Andrea !

I found what I did wrong. I used two Strings instead of a String and a Double. Your example helped me to find this rather trivial mistake.

Here is the working code, for those who are interested.

Attachment: NumberFormatFunction.java
Description: Binary data





Le 3 mars 09 à 18:10, Andrea Aime a écrit :

Benoît Thiébault wrote:
OK for the text symbolizer, I have replaced the code as you said.
Regarding the evaluate method, I have evaluated the two parameters against the object as you said (see below).
I have built the formatter from the string result.
But then, I don't know what to return. I have tried (T) formatter.format(propertyString), but it doesn't seem to work.


Here is a working example (you need to register the function
in SPI for it to work):

package org.aaa;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.geotools.data.DataUtilities;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.filter.FunctionExpressionImpl;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.FilterFactory;
import org.opengis.filter.expression.Function;


public class FormatterFunction extends FunctionExpressionImpl {

   public FormatterFunction() {
       super("formatter");
   }

   @Override
   public int getArgCount() {
       return 2;
   }

   @Override
   public Object evaluate(Object object) {

Double number = getExpression(0).evaluate(object, Double.class); String pattern = getExpression(1).evaluate(object, String.class);

       NumberFormat format = new DecimalFormat(pattern);
       return format.format(number);
   }

   public static void main(String[] args) throws Exception {
SimpleFeatureType ft = DataUtilities.createType("testType", "value:double,msg:String"); SimpleFeature f = DataUtilities.parse(ft, "testType.1", new String[] {"1.56789", "hello"});

       FilterFactory ff = CommonFactoryFinder.getFilterFactory(null);
Function formatter = ff.function("formatter", ff.property("value"), ff.literal("#.##"));
       System.out.println(formatter.evaluate(f));
   }
}


Cheers
Andrea




------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to