Context: I'm building a WebApp with OpenLayers that allows users to represent on a map their commuting trips by selecting a bus stop, choosing a bus line and finally selecting where they get off the bus.

My data consists of a Shapefile with Points (bus stops) and another Shapefile with MultiLineStrings representing every allowed segment between two bus stops. They contain information about bus lines, starting and ending stops, direction, etc. All this data is served from a GeoServer and retrieved through WFS to OpenLayers.

I've made an algorithm to get all the MultiLineStrings needed to represent user's commute and it works fine. So, if user travels from stop 0 to stop 4, my trip data consists in 4 MultiLineString Features.

Problem: I'd like to merge the n MultiLineStrings of a trip into a new single one Feature that represents the whole trip from end to end (because later I need the user to rate it and stuff...)

I've looked for some feature merging method in OpenLayers' API but I didn't find anything that helps me...

So, in short, is it possible to merge two or more features in a new one?

1 Answer 1


It is not possible with OpenLayers alone but there are other options. If You want to do it in JavaScript you could use JSTS. With this you can make a union operation on two geometries.

var union = a.union(b)

There is also an example that uses OpenLayers and JSTS together.

You could also import your Shapefiles into PostGIS and than make use of ST_Union.

  • Thank you Fabian! I'll take a look at JSTS since I'd like to do it on client's side... May 17, 2017 at 10:48
  • 2
    Another alternative is TurfJS, I find it a bit easier to use than JSTS but both are solid options.
    – rbkb
    May 17, 2017 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.