Search code examples
openlayersgeoserver

openlayer point symbol from wkt string


I'd like to represent a series of points each associated with their own wkt string representing the shape of that point at a certain zoom level.

With geoserver, I can do that pretty easily. Something like this for example:

* {
   mark: symbol("wkt://MULTILINESTRING((-0.25 -0.25, -0.125 -0.25), (0.125 -0.25, 0.25 -0.25), (-0.25 0.25, -0.125 0.25), (0.125 0.25, 0.25 0.25))");
}

This symbol is relative to the actual coordinate of a point and can be sized up or down with another property.

Is there anything similar with openlayer ? How can I represent a point with a dynamic polygon shape ?

I'd be interesting to know how to do this with webGL in Openlayer, if it's possible as the data I'm working with contains thousands of points on the map.


Solution

  • OpenLayers styles have a geometry property (defaults to the feature's geometry but can be overridden) so if you had a WKT geometry string as a property of a point feature you could parse that, translate it relative to the feature's position and use it to style the point:

    var features = [
      new Feature({
        geometry: new Point([50, 50]),
        mark: "MULTILINESTRING((-0.25 -0.25, -0.125 -0.25), (0.125 -0.25, 0.25 -0.25), (-0.25 0.25, -0.125 0.25), (0.125 0.25, 0.25 0.25))"
      })
    ];
    
    var parser = new WKT();
    
    var style = new Style({
      stroke = new Stroke({
        width: 2,
        color: 'black'
    });
    
    var layer = new VectorLayer({
      source : new VectorSource({
        features: features,
      }),
      style: function(feature) {
        var point = feature.getGeometry().getCoordinates();
        var mark = parser.readGeometry(feature.get('mark'));
        mark.translate(point[0], [point[1]);
        style.setGeometry(mark);
        return style;
      }
    });