javascriptgeojsonturfjs

Turf js drawing perfect square with center point and radius


I am trying to generate p square polygon out of center point and radius. Like below.

bboxPolygon(square(bbox(circle(_circle.center, 0.5, { steps: 64 }))))

All the function is from the turf.js

I believe that should generate perfect square or at least close to square. However, it returns rectangular.

I am not sure this is turf library problem or I am using it wrong.

circle geojson

{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [
          -73.93524199999999,
          40.734656941636764
        ],
        [
          -73.93791238162646,
          40.73411472349626
        ],
        [
          -73.93986713367875,
          40.73263337851494
        ],
        [
          -73.94058248193825,
          40.730609876934174
        ],
        [
          -73.93986685239045,
          40.72858643688632
        ],
        [
          -73.93791210033818,
          40.72710521497083
        ],
        [
          -73.93524199999999,
          40.72656305836324
        ],
        [
          -73.93257189966181,
          40.72710521497083
        ],
        [
          -73.93061714760952,
          40.72858643688632
        ],
        [
          -73.92990151806174,
          40.730609876934174
        ],
        [
          -73.93061686632124,
          40.73263337851494
        ],
        [
          -73.93257161837353,
          40.73411472349626
        ],
        [
          -73.93524199999999,
          40.734656941636764
        ]
      ]
    ]
  }
}

bboxPolygon result

{
  "type": "Feature",
  "bbox": [
    -73.93928894163675,
    40.72656305836324,
    -73.93119505836323,
    40.734656941636764
  ],
  "properties": {},
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [
          -73.93928894163675,
          40.72656305836324
        ],
        [
          -73.93119505836323,
          40.72656305836324
        ],
        [
          -73.93119505836323,
          40.734656941636764
        ],
        [
          -73.93928894163675,
          40.734656941636764
        ],
        [
          -73.93928894163675,
          40.72656305836324
        ]
      ]
    ]
  }
}

Solution

  • I think the problem is with the turf.square function. It is not doing what I would expect.

    You should get something quite close to a square if you replace bboxPolygon(square(bbox(circle(_circle.center, 0.5, { steps: 64 }))))

    with

    bboxPolygon(bbox(circle(_circle.center, 0.5, { steps: 64 })))

    Here is an example of what the outputs looks like. red is the circle, green is the output you get from using square and blue is the output of not using square.

    enter image description here

    If you look into the source code of square you will notice that it actually does not create a bbox which has equal distances on each side. However it creates a bbox which has equal change in degree. Since we are working in longitude and latitude, that will usually NOT be a polygon which is square in distance.

    I am unsure why anyone would ever need this square function since i would find it way more usefull if it was square in distance, however I do not know if this is the intended behaviour.

    TLDR Try bboxPolygon(bbox(circle(_circle.center, 0.5, { steps: 64 }))) instead