David98761324
David98761324

Reputation: 43

Can't interpret arcs in TopoJSON

I'm having a lot of problems trying to interpret the arcs member of the TopoJSON as opposed to the coordinates member of the GeoJSON.

Using this reference https://observablehq.com/@syaleni/topojson-vs-geojson

I'm told via the previously lnked reference that this GeoJSON:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "Africa"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [-6, 36],
            [33, 30],
            [43, 11],
            [51, 12],
            [29, -33],
            [18, -35],
            [7, 5],
            [-17, 14],
            [-6, 36]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "Australia"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [143, -11],
            [153, -28],
            [144, -38],
            [131, -31],
            [116, -35],
            [114, -22],
            [136, -12],
            [140, -17],
            [143, -11]
          ]
        ]
      }
    }
  ]
}

is the same as this TopoJSON:

{
  "type":"Topology",
  "objects": {
    "0": {
      "type":"Polygon",
      "arcs": [
        [
          0
        ]
      ],
      "properties": {
        "name":"Africa"
      }
    },
    "1": {
      "type":"Polygon",
      "arcs": [
        [
          1
        ]
      ],
      "properties": {
        "name":"Australia"
      }
      }
    },
    "arcs": [
      [
        [0,2],[1,0],[0,-1],[0,-1],[-1,0],[0,1],[0,1]
      ],
      [
        [2,1],[0,-1],[0,1]
      ]
    ],
    "bbox": [
      -17,
      -38,
      153,
      36
    ],
    "transform": {
      "scale": [
        85,
        37
      ],
      "translate": [
        -17,
        -38
      ]
    }
}

From what "I'm understanding" the arcs are encoded coordinates, the way to get the original cordinates is done with this function:

function decodeArc(topology, arc) {
  var x = 0, y = 0;
  return arc.map(function(position) {
    position = position.slice();
    position[0] = (x += position[0]) * topology.transform.scale[0] + topology.transform.translate[0];
    position[1] = (y += position[1]) * topology.transform.scale[1] + topology.transform.translate[1];
    return position;
  });
}

Let's say I send the first arc, the first array of points which is {0,2}, if I use the function:

position[0] = (x += position[0]) * topology.transform.scale[0] + topology.transform.translate[0];
position[1] = (y += position[1]) * topology.transform.scale[1] + topology.transform.translate[1];

position{0} = (0 += 0) * 85 + (-17)
position{1} = (0 += 2) * 37 + (-38)

This ends up giving me [-17, 36] which is different of [-6, 36] of the first coordinate on the GeoJSON object.

Tried reading different documentations, even with the official API one on Github and I just can't find the right interpretation for those arcs. ¿Am I doing it wrong? Would appreciate much some advice on this as I've been stuck a day and a half trying to understand these concepts. Thanks in advance.

Upvotes: 1

Views: 187

Answers (0)

Related Questions