Search code examples
mapboxgeojsonmapbox-gl-jsturfjs

How to place squares along the line? (Geojson+mapbox)


I have a rectangular and need to fill it with squares. I find the center line and want to place the squares along that line. But is there any simple method to draw a square in mapboxgl with any other libs like turfjs? Like set the center of the square and a side length and get a coordinates of the square? Any ideas? As placing circles with geojson is not a problem, but looks like a problem for me with squares, as I have to calculate 4 coordinates.

enter image description here


Solution

  • With turf you got the right tool at hand to solve this task.

    One way of doing it would be something like this:

    1. Get the line distance

    2. Divide the line distance with the amount of rectangles you want

    3. Use turf.along() to get a point along your line

    4. Use turf.buffer, turf.bbox and turf.bboxPolygon to get a rectangle on the point location

    5. Draw everything onto the map via mapbox-gl-js

    Here is a code example. You can hit "run code snippet" to show the result.

    https://jsfiddle.net/andi_lo/3rc6vca3/

    mapboxgl.accessToken = 'pk.eyJ1IjoiZmFyYWRheTIiLCJhIjoiTUVHbDl5OCJ9.buFaqIdaIM3iXr1BOYKpsQ';
    
    var map = new mapboxgl.Map({
      container: 'map',
      style: 'mapbox://styles/mapbox/streets-v9',
      center: [-74.10931699675322, 4.61336863727521],
      zoom: 11,
    });
    
    map.on('load', () => {
      map.addSource('line', {
        'type': 'geojson',
        'data': {
          'type': 'FeatureCollection',
          'features': [],
        },
      });
    
      map.addSource('rect', {
        'type': 'geojson',
        'data': {
          'type': 'FeatureCollection',
          'features': [],
        },
      });
    
      map.addLayer({
        id: 'line_layer',
        source: 'line',
        type: 'line',
        paint: {
          'line-width': 3,
          'line-color': '#333',
        },
      });
    
      map.addLayer({
        id: 'rect_layer',
        source: 'rect',
        type: 'fill',
        paint: {
          'fill-color': '#00b7bf',
        },
      });
    
      const line = turf.lineString([[-74.07, 4.66], [-74.17, 4.56]]);
      const lineCollection = turf.featureCollection([line]);
      const lineDistance = turf.lineDistance(line);
      const rectCollection = turf.featureCollection([]);
    
      const RECT_COUNT = 7;
      const RECT_SIZE = 0.6;
      const BUFFER_STEPS = 32;
      const SEGMENTS = lineDistance / RECT_COUNT;
      const UNITS = 'kilometers';
      for(let i = 0; i <= RECT_COUNT; i++) {
        const point = turf.along(line, (SEGMENTS * i), UNITS);
        const buffered = turf.buffer(point, RECT_SIZE, UNITS, BUFFER_STEPS);
        const bbox = turf.bbox(buffered);
        const bboxPolygon = turf.bboxPolygon(bbox);
        rectCollection.features.push(bboxPolygon);
      }
    
      map.getSource('line').setData(lineCollection);
      map.getSource('rect').setData(rectCollection);
    });
    #map {
      height: 500px;
    }
    <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v0.38.0/mapbox-gl.css" rel="stylesheet"/>
    <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v0.38.0/mapbox-gl.js"></script>
    <script src="https://npmcdn.com/@turf/[email protected]/turf.min.js"></script>
    
    <div id="map"></div>