I am adding the following section to the function tutorial:
Functions are used to process their parameters and produce an answer:
public Object evaluate(Object feature) { Expression geomExpression =
parameters.get(0); Geometry geom = geomExpression.evaulate( feature,
Geometry.class ); return geom.centroid(); }
When a function is used as part of a Style users often want to calculate a
value based on the attributes of the Feature being drawn. The Expression
PropertyName is used in this fashion to extract values out of a Feature and
pass them into the function for evaluation
IMPORTANT: When using your function with a Style we try and be efficient. If
the style calls your function without any reference to PropertyName it will
only be called once.
The assumption is that the function will produce the same value each time it is
called, and thus will produce the same value for all features. By only calling
the function once (and remembering the result) the rendering engine is able to
perform much faster.
If this is not the functionality you are after please implement
VolatileFunction:
public class MagicFunction extends FunctionExpressionImpl implements
VolatileFunction { Random random; public MagicFunction() { super("magic");
random = new Random(); } public int getArgCount() { return 0; // no arguments!
} public Object evaluate(Object feature) { float r = rand.nextFloat(); float g
= rand.nextFloat(); float b = rand.nextFloat(); Color color = new Color(r, g,
b); return color; } }
--
Jody Garnett
On Monday, 25 June 2012 at 12:15 PM, Jody Garnett wrote:
> Sure. Do you have a code example or something you would like to see added?
>
> --
> Jody Garnett
>
>
> On 25/06/2012, at 10:00 AM, "[email protected]
> (mailto:[email protected])" <[email protected]
> (mailto:[email protected])> wrote:
>
> > Thank you thank you THANK YOU! That actually worked. I have been
> > playing around with this on and off for TWO YEARS trying to get this to
> > work. I was beginning to doubt my sanity. Thank you very much.
> >
> > I would be happy to work with [whoever] to update the tutorials and the
> > documentation to make Geotools a little more accessible to folks like me.
> > Please let me know if you are interested.
> >
> > Thanks again -
> >
> > Tom
> >
> >
> >
> > > ----- Original Message -----
> > > From: Andrea Aime (mailto:[email protected])
> > > To: [email protected] (mailto:[email protected])
> > > Cc: [email protected]
> > > (mailto:[email protected])
> > > Sent: Sunday, June 24, 2012 12:14 PM
> > > Subject: Re: [Geotools-gt2-users] Passing a feature to a function
> > >
> > > On Sun, Jun 24, 2012 at 7:07 PM, <[email protected]
> > > (mailto:[email protected])> wrote:
> > > > Unfortunately, that's not the behavior I'm seeing. I create my fill
> > > > function like so:
> > > >
> > > > Fill fill = styleFactory.createFill(
> > > > newColor, // function to choose
> > > > feature colour
> > > > filterFactory.literal(1.0f)); // opacity
> > > > where newColor is my external function. When I use
> > > > JMapFrame.showMap(map); to show the map, it makes exactly one call to
> > > > newColor.evaluate(Object), where the parameter is a SimpleFeatureImpl
> > > > object containing only the geometry. My evaluate function looks like
> > > > this:
> > > >
> > > > @Override
> > > > public Object evaluate(Object object) {
> > > > return Color.GREEN;
> > > > }
> > > > Unless the calling routine is "sensing" that I am returning a constant,
> > > > there is no reason that this should only be called once for a map with
> > > > 18,000 features.
> > >
> > > It is actually sensing that, since you have no parameters in your
> > > function the assumption is that the result will be
> > > the same for all features, and thus the evaluation is performed just once
> > > and your function turned into a literal.
> > > If you function has not parameter depending on feature attributes but it
> > > still returns varying output you should
> > > make it implement the "VolatileFunction" interface (which, as of today,
> > > only the random function implements)
> > >
> > > Cheers
> > > Andrea
> > >
> > >
> > >
> > > --
> > > Ing. Andrea Aime
> > > GeoSolutions S.A.S.
> > > Tech lead
> > >
> > > Via Poggio alle Viti 1187
> > > 55054 Massarosa (LU)
> > > Italy
> > >
> > > phone: +39 0584 962313
> > > fax: +39 0584 962313
> > > mob: +39 339 8844549
> > >
> > > http://www.geo-solutions.it
> > > http://geo-solutions.blogspot.com/
> > > http://www.youtube.com/user/GeoSolutionsIT
> > > http://www.linkedin.com/in/andreaaime
> > > http://twitter.com/geowolf
> > >
> > ------------------------------------------------------------------------------
> > Live Security Virtual Conference
> > Exclusive live event will cover all the ways today's security and
> > threat landscape has changed and how IT managers can respond. Discussions
> > will include endpoint security, mobile security and the latest in malware
> > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> > _______________________________________________
> > GeoTools-GT2-Users mailing list
> > [email protected]
> > (mailto:[email protected])
> > https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
GeoTools-GT2-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users