## geotools-gt2-users

 [Geotools-gt2-users] ColorMap on polygons From: Enamul - 2010-05-20 23:17:48 ```I'd like to fill the polygons with multiple colors such that adjacent polygons don't have the same color. How can I do this ? -- View this message in context: http://osgeo-org.1803224.n2.nabble.com/ColorMap-on-polygons-tp5081998p5081998.html Sent from the geotools-gt2-users mailing list archive at Nabble.com. ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Michael Bedward - 2010-05-20 23:42:50 ```Hello, > I'd like to fill the polygons with multiple colors such that adjacent > polygons don't have the same color. How can I do this ? Easiest answer - if number of polygons is small make them all different colors :) General answer... Theoretically you only need four colors to achieve what you want with any map... http://en.wikipedia.org/wiki/Four_color_theorem But it's actually better to have five colors because there is a much faster algorithm for this... http://en.wikipedia.org/wiki/Five_color_theorem#Linear_time_five-coloring_algorithm I'd be interested to hear if anyone on the list has already got an implementation of this. Perhaps some clever bit of GeoServer ? Michael ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Enamul - 2010-05-21 00:30:37 ```I'm aware of the 4 color algorithm although I didn't really know about the 5 color version. And I'm dealing with 20k shapes ... so using different colors is obviously out of consideration. I have been assigning random colors from a pool of colors but was wondering if geotools has sth that I can take advantage of. -- View this message in context: http://osgeo-org.1803224.n2.nabble.com/ColorMap-on-polygons-tp5081998p5082153.html Sent from the geotools-gt2-users mailing list archive at Nabble.com. ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Andrea Aime - 2010-05-21 10:13:07 ```Michael Bedward ha scritto: > Hello, > >> I'd like to fill the polygons with multiple colors such that adjacent >> polygons don't have the same color. How can I do this ? > > Easiest answer - if number of polygons is small make them all > different colors :) > > General answer... > Theoretically you only need four colors to achieve what you want with any map... > > http://en.wikipedia.org/wiki/Four_color_theorem > > But it's actually better to have five colors because there is a much > faster algorithm for this... > > http://en.wikipedia.org/wiki/Five_color_theorem#Linear_time_five-coloring_algorithm > > I'd be interested to hear if anyone on the list has already got an > implementation of this. Perhaps some clever bit of GeoServer ? Nope, we don't have anything like that. As far as I can see this would have to be done in pre-processing anyways, first a full scan of the data and then assign the colors? A tool could be used to load the data, build a connectivity map, assign the colors, and generate a SLD using FID filters to assign the 5 colors to the different polygons. Cheers Andrea -- Andrea Aime OpenGeo - http://opengeo.org Expert service straight from the developers. ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Michael Bedward - 2010-05-21 11:44:21 ```On 21 May 2010 20:12, Andrea Aime wrote: > > A tool could be used to load the data, build a connectivity map, > assign the colors, and generate a SLD using FID filters to assign > the 5 colors to the different polygons. It would be cool to have something like that wouldn't it ? It sounds like the sort of thing that would make a nice SoC project or similar sometime. Michael ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Duane Zamrok - 2010-05-21 13:54:31 ```Assigning a color to each one would indicate qualitative/quantitative classification, but using random colors could result in two adjacent shapes receiving the same color. Perhaps if you instead started your coloring at a random shape in the set, and then progressed from there. Just throwing ideas in the air. -----Original Message----- From: Andrea Aime [mailto:aaime@...] Sent: Friday, May 21, 2010 9:51 AM To: Michael Bedward Cc: geotools-gt2-users@... Subject: Re: [Geotools-gt2-users] ColorMap on polygons Michael Bedward ha scritto: > On 21 May 2010 20:12, Andrea Aime wrote: >> A tool could be used to load the data, build a connectivity map, >> assign the colors, and generate a SLD using FID filters to assign >> the 5 colors to the different polygons. > > It would be cool to have something like that wouldn't it ? It sounds > like the sort of thing that would make a nice SoC project or similar > sometime. Yep, would be a nice tool. Could be useful for uDig too. There is one catch however. Not having a random color might induce people looking at the map to think there is some sort of qualitative/quantitative classification. I think for this case having a RandomColor filter function would be more appropriate. I guess it might just take no arguments, and/or maybe take a list of colors to choose from. Cheers Andrea -- Andrea Aime OpenGeo - http://opengeo.org Expert service straight from the developers. ------------------------------------------------------------------------------ _______________________________________________ Geotools-gt2-users mailing list Geotools-gt2-users@... https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Andrea Aime - 2010-05-21 14:00:58 ```Duane Zamrok ha scritto: > Assigning a color to each one would indicate qualitative/quantitative > classification, but using random colors could result in two adjacent > shapes receiving the same color. Perhaps if you instead started your > coloring at a random shape in the set, and then progressed from > there. Just throwing ideas in the air. Ah, this would be problematic. In server side applications the same filter function is used by multiple threads in parallel, so making it stateful is tricky (one can use thread locals, but then they also have to be cleaned up, failure to do that results in the web application leaking permanent generation memory on undeploy). If one really needs a guarantee that no two adjacent polygon get the same color it's better to do that deterministically, and in pre-processing. Cheers Andrea -- Andrea Aime OpenGeo - http://opengeo.org Expert service straight from the developers. ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Andrea Aime - 2010-05-21 13:51:38 ```Michael Bedward ha scritto: > On 21 May 2010 20:12, Andrea Aime wrote: >> A tool could be used to load the data, build a connectivity map, >> assign the colors, and generate a SLD using FID filters to assign >> the 5 colors to the different polygons. > > It would be cool to have something like that wouldn't it ? It sounds > like the sort of thing that would make a nice SoC project or similar > sometime. Yep, would be a nice tool. Could be useful for uDig too. There is one catch however. Not having a random color might induce people looking at the map to think there is some sort of qualitative/quantitative classification. I think for this case having a RandomColor filter function would be more appropriate. I guess it might just take no arguments, and/or maybe take a list of colors to choose from. Cheers Andrea -- Andrea Aime OpenGeo - http://opengeo.org Expert service straight from the developers. ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Ian Turton - 2010-05-21 15:23:57 ```On Fri, May 21, 2010 at 6:12 AM, Andrea Aime wrote: > Michael Bedward ha scritto: >> Hello, >> >>> I'd like to fill the polygons with multiple colors such that adjacent >>> polygons don't have the same color. How can I do this ? >> >> Easiest answer - if number of polygons is small make them all >> different colors :) >> >> General answer... >> Theoretically you only need four colors to achieve what you want with any map... >> >> http://en.wikipedia.org/wiki/Four_color_theorem >> >> But it's actually better to have five colors because there is a much >> faster algorithm for this... >> >> http://en.wikipedia.org/wiki/Five_color_theorem#Linear_time_five-coloring_algorithm >> >> I'd be interested to hear if anyone on the list has already got an >> implementation of this. Perhaps some clever bit of GeoServer ? > > Nope, we don't have anything like that. As far as I can see this would > have to be done in pre-processing anyways, first a full scan of the > data and then assign the colors? > > A tool could be used to load the data, build a connectivity map, > assign the colors, and generate a SLD using FID filters to assign > the 5 colors to the different polygons. > I have a paper on how to create the adjacency graph (http://www.citeulike.org/group/9502/article/6600150) that is on my to do list (but quite low down it :-) I can mail a copy of the PDF to anyone who's interested. Ian -- Ian Turton ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Michael Bedward - 2010-05-22 02:14:52 ```On 22 May 2010 01:23, Ian Turton wrote: > > I have a paper on how to create the adjacency graph > (http://www.citeulike.org/group/9502/article/6600150) that is on my to > do list (but quite low down it :-) I can mail a copy of the PDF to > anyone who's interested. > Thanks Ian - that's an interesting paper ! Michael ps. also reminds me to tell folks again about the GeoTools CiteULike group ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Enamul Haque - 2010-05-27 00:27:59 ```I tried this really simple example to color the map based on the hashcode of the geometry. But it seems to be drawing all the shapes with a single color on every refresh. What am I doing wrong here ? public ColorRampFunction(List fillColors) { super("colorramp"); fillColors_.addAll(fillColors); propertyName_ = filterFactory_.property("the_geom"); } public Object evaluate(Object feature) { Object id = propertyName_.evaluate(feature); logger_.debug("Hash code {}", id.hashCode()); int index = (int)(((long)Integer.MAX_VALUE + (long)id.hashCode()) % fillColors_.size()); logger_.debug("Index {}", index); return fillColors_.get(index); } On Fri, May 21, 2010 at 10:14 PM, Michael Bedward wrote: > On 22 May 2010 01:23, Ian Turton  wrote: >> >> I have a paper on how to create the adjacency graph >> (http://www.citeulike.org/group/9502/article/6600150) that is on my to >> do list (but quite low down it :-) I can mail a copy of the PDF to >> anyone who's interested. >> > > Thanks Ian - that's an interesting paper ! > > Michael > > ps. also reminds me to tell folks again about the GeoTools CiteULike group > > ------------------------------------------------------------------------------ > > _______________________________________________ > Geotools-gt2-users mailing list > Geotools-gt2-users@... > https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users > ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Michael Bedward - 2010-05-27 04:19:04 ```Hello Enamul, > I tried this really simple example to color the map based on the > hashcode of the geometry. But it seems to be drawing all the shapes > with a single color on every refresh. What am I doing wrong here ? It sounds like your function is only being called once to set the fill for all features, rather than being called for each feature. How are you using it in your Style ? Michael ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Enamul Haque - 2010-05-27 04:30:33 ```I'm just calling polygonSymbolizer.setFill(styleBuilder.createFill(new ColorRampFunction(colorList))). I have one featuretypestyle with one rule. On 2010-05-27, at 12:18 AM, Michael Bedward wrote: > Hello Enamul, > >> I tried this really simple example to color the map based on the >> hashcode of the geometry. But it seems to be drawing all the shapes >> with a single color on every refresh. What am I doing wrong here ? > > It sounds like your function is only being called once to set the fill > for all features, rather than being called for each feature. How are > you using it in your Style ? > > Michael ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Enamul - 2010-05-27 13:19:42 ```Any suggestions ? -- View this message in context: http://osgeo-org.1803224.n2.nabble.com/ColorMap-on-polygons-tp5081998p5108512.html Sent from the geotools-gt2-users mailing list archive at Nabble.com. ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Enamul Haque - 2010-05-27 20:20:52 ```Turns out that SLDStyleFactory will cache the style if the fill expression doesn't have at least one attribute set as argument. I was using a ColorRampFunction that takes zero arguments, which made SLDStyleFactory think that this is a static style. Doing this resolves it: propertyName_ = filterFactory_.property("the_geom"); super.setParameters(new ArrayList() {{ add(propertyName_); }}); On Thu, May 27, 2010 at 9:19 AM, Enamul wrote: > > Any suggestions ? > -- > View this message in context: http://osgeo-org.1803224.n2.nabble.com/ColorMap-on-polygons-tp5081998p5108512.html > Sent from the geotools-gt2-users mailing list archive at Nabble.com. > > ------------------------------------------------------------------------------ > > _______________________________________________ > Geotools-gt2-users mailing list > Geotools-gt2-users@... > https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users > ```
 Re: [Geotools-gt2-users] ColorMap on polygons From: Michael Bedward - 2010-05-28 10:01:40 ```Hi Enamul, > Turns out that SLDStyleFactory will cache the style if the fill > expression doesn't have at least one attribute set as argument. I was > using a ColorRampFunction that takes zero arguments, which made > SLDStyleFactory think that this is a static style.  Doing this > resolves it: > >        propertyName_ = filterFactory_.property("the_geom"); >        super.setParameters(new ArrayList() >        {{ >            add(propertyName_); >        }}); > Well done ! I couldn't see why your code wasn't working, but testing it here with a similar case shows that you are quite right about the behaviour of SLDStyleFactory. I'll add this to the material that we cover in our examples and put an item about it into the FAQ because it will surely trap others in the future :-) In case it's useful, here is a more GeoTools-ized version of the function... import java.awt.Color; import java.util.List; import org.geotools.filter.FunctionExpressionImpl; public class ColorPickerFunction extends FunctionExpressionImpl { public ColorPickerFunction() { super("colorfn"); } @Override public int getArgCount() { return 2; } @Override public Object evaluate(Object feature) { int value = getExpression(0).evaluate(feature).hashCode(); List colors = (List) getExpression(1).evaluate(feature); int index = value % colors.size(); return colors.get(index); } } And here is an example of use via the standard filter factory API to create a polygon style... StyleFactory sf = CommonFactoryFinder.getStyleFactory(null); FilterFactory2 ff2 = CommonFactoryFinder.getFilterFactory2(null); Function fn = ff2.function("ColorFn", ff2.property("the_geom"), ff2.literal(Arrays.asList(Color.BLUE, Color.CYAN, Color.GREEN, Color.RED, Color.YELLOW))); Fill fill = sf.createFill(fn); Stroke stroke = sf.createStroke(fn, ff2.literal(1.0f)); Symbolizer sym = sf.createPolygonSymbolizer(stroke, fill, "the_geom"); Style style = SLD.wrapSymbolizers(sym); ```