[OpenLayers-Users] Styling a GeoJson vector layer with a stylemap by value

Eric Lemoine eric.lemoine at camptocamp.com
Wed Jan 18 13:14:40 EST 2012


On Wednesday, January 18, 2012, John Cole <jcole at solidearth.com> wrote:
> Hey guys,
>   I'm trying to use a stylemap on a GeoJson datasource, and want to
change the color of the symbol based off of a field in the data.
> My data looks like:
> {
> "type" : "GeometryCollection",
> "geometries" : [{
> "type" : "Point",
> "_id" : {
> "code" : "123",
> "version" : 0
> },
> "coordinates" : [-86.586502, 34.731189],
> "Status" : "W"
> }, {
> "type" : "Point",
> "_id" : {
> "code" : "234",
> "version" : 0
> },
> "coordinates" : [-86.57294326, 34.73563314],
> "Status" : "W"
> }, {
> "type" : "Point",
> "_id" : {
> "code" : "456",
> "version" : 0
> },
> "coordinates" : [-86.45717254, 34.82754903],
> "Status" : "A"
> }
> ]
> }

I don't think OpenLayers's GeoJSON format can produce
OpenLayers.Feature.Vector objects from such a JSON doc. To produce features
the GeoJSON format should receive a doc whose root object is of type
FeatureCollection.

>
> and my style stylemap:
>         var styleMap = new OpenLayers.StyleMap({
>             "default": new OpenLayers.Style({
>                 fillOpacity: 0.5,
>                 pointRadius: 5,
>                 fillColor: "#ee9900"
>             })
>         });
>
>         var lookup = {
>             "S": {fillColor: "red"},
>             "A": {fillColor: "green"},
>             "X": {fillColor: "blue"}
>         };
>         styleMap.addUniqueValueRules("default", "Status", lookup);
> However, when I apply the addUniqueValueRules, no features are drawn.
 Tracing through the draw cycle,
> line 117 of Comparison.js which is getting the value of "Status" from the
context returns undefined.  What do I need to do to get the "Status" into
the correct context?


For your rules to work "Status" must be a property of the "properties"
object in each "feature" object of the GeoJSON doc.

So make sure you have correctly formed feature objects in your layer. Each
feature object should a "Status" property in its "attributes" object.

Hope this helps.

-- 
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : eric.lemoine at camptocamp.com
http://www.camptocamp.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20120118/44bb1807/attachment.html


More information about the Users mailing list