Weltkarte mit Positions-Pins
Philipp, Kiya | 31.1.2024

Find the mid point on a concave polygon with Turf.js

Geodata > Find the mid point on a concave polygon with Turf.js

Placing Markers on Maps

Visualizing information on maps, such as the area (in square meters) that a specific polygon covers, involves placing markers. One of the tasks involved in this exercise is identifying a center point within a concave polygon that remains inside the geometry.

Pointing Out the Problem

Many GeoJSON libraries for spatial analysis offer pre-defined functions to calculate a polygon's midpoint. For instance, Turf.js library offers methods like center(), centerOfMass(), and centroid() that work well with most polygons. These methods calculate the center by averaging the vertex coordinates while considering the polygon’s area dimensions.

Bildschirm­foto 2023-08-04 um 11.39.47.png

1center = turf.center(poly);
2centroid = turf.centroid(poly)
3centerOfMass = turf.centerOfMass(poly)

However, these methods often struggle with polygons having complex structures, particularly concave ones. The result: points located outside the polygon.

Bildschirm­foto 2023-08-04 um 11.13.42.png

Tessellation to the Rescue

For such complex polygons, tessellation (specifically triangulation in this context) is an effective solution. It breaks down the polygon into smaller triangles that cover the entire polygon area. The Turf.js library offers a function called tessellate() to facilitate this.

Bildschirm­foto 2023-08-04 um 11.21.47.png

Owing to this strategy, you can easily find a marker within the polygon geometry. It’s the midpoint of the largest triangle, calculated by centerOfMass().

Bildschirm­foto 2023-08-04 um 11.23.09.png

1const tessellation = turf.tesselate(poly)
2const largestTriangle = tessellation.features.reduce((maxTriangle, currentTriangle) => {
3    return turf.area(currentTriangle) > turf.area(maxTriangle) ? currentTriangle : maxTriangle;});
4const centerLargestTriangle = turf.centerOfMass(largestTriangle)

Thanks to tessellation, you can reliably find the midpoint of a concave polygon, ensuring that it remains within the geometry.

  • What is the problem in finding a midpoint of a concave polygon?
  • How can Tessellation be a solution?
  • How to implement this solution using Turf.js library?
Philipp Hunder
Philipp (Werksstudent)

... beendet in diesem Jahr sein Studium in der Angewandten Informatik im Bereich Computational Physics und ist Werksstudent bei uns am Standort Hannover. In Kundenprojekten übernimmt er am liebsten Au... mehr anzeigen


... ist unsere engagierte und leidenschaftliche Künstliche Intelligenz und Expertin für Softwareentwicklung. Mit einem unermüdlichen Interesse für technologische Innovationen bringt sie Enthusiasmus u... mehr anzeigen

Standort Hannover

newcubator GmbH
Bödekerstraße 22
30161 Hannover

Standort Dortmund

newcubator GmbH
Westenhellweg 85-89
44137 Dortmund