TopoJSON:计算比例和偏移量

时间:2013-08-21 01:08:56

标签: python math geometry geo

问题:

给定绝对坐标,如何为topojson拓扑变换计算scaletranslate的理想值?

我想离开这个:

absolute = [
     [-69.9009900990099, 12.451814888520133],
     [-69.9009900990099, 12.417091709170947],
     [-69.97299729972997, 12.43445329884554],
     [-70.00900090009, 12.486538067869319],
     [-70.08100810081008, 12.538622836893097],
     [-70.08100810081008, 12.590707605916876],
     [-70.04500450045005, 12.608069195591469],
     [-70.00900090009, 12.55598442656769],
     [-69.93699369936994, 12.469176478194726],
     [-69.9009900990099, 12.451814888520133],
]

对此:

scale = [0.036003600360036005, 0.017361589674592462]
translate = [-180, -89.99892578124998]

relative = [[3058, 5901], [0, -2], [-2, 1], [-1, 3], [-2, 3], [0, 3], [1, 1], 
     [1, -3], [2, -5], [1, -1]]

我得到了相对于绝对转换的工作:

def arc_to_coordinates(topology, arc):
    scale = topology['transform']['scale']
    translate = topology['transform']['translate']

    x = 0
    y = 0
    coordinates = []

    for point in arc:
        x += point[0]
        y += point[1]
        coordinates.append([
            x * scale[0] + translate[0],
            y * scale[1] + translate[1] 
        ])

    return coordinates

现在我需要编写绝对到相对转换。为此,有必要获取scaletranslate的值,我被卡住了。

关于topojson

topojson是一种用于存储地理特征的格式,与geojson非常相似。

为了节省空间(以及其他技巧),格式使用前一点的相对整数偏移量。例如,这是Aruba的topojson文件:

{
  "type": "Topology",
  "transform": {
    "scale": [0.036003600360036005, 0.017361589674592462],
    "translate": [-180, -89.99892578124998]
  },
  "objects": {
    "aruba": {
      "type": "Polygon",
      "arcs": [[0]],
      "id": 533
    }
  },
  "arcs": [
    [[3058, 5901], [0, -2], [-2, 1], [-1, 3], [-2, 3], [0, 3], [1, 1], 
     [1, -3], [2, -5], [1, -1]]
  ]
}

与GeoJSON功能相同的信息如下所示:

{
    "type" : "Feature",
    "id" : 533,
    "properties" : {},
    "geometry" : {
        "type" : "Polygon",
        "coordinates" : [[
                 [-69.9009900990099, 12.451814888520133], 
                 [-69.9009900990099, 12.417091709170947], 
                 [-69.97299729972997, 12.43445329884554], 
                 [-70.00900090009, 12.486538067869319], 
                 [-70.08100810081008, 12.538622836893097], 
                 [-70.08100810081008, 12.590707605916876], 
                 [-70.04500450045005, 12.608069195591469], 
                 [-70.00900090009, 12.55598442656769], 
                 [-69.93699369936994, 12.469176478194726], 
                 [-69.9009900990099, 12.451814888520133]
            ]]
    }
}

很容易看出前者比后者更紧凑。我的绝对相对功能正在发挥作用:

>>> arc_to_coordinates(topology, topology['arcs'][0])
[[-69.9009900990099, 12.451814888520133],
 [-69.9009900990099, 12.417091709170947],
 [-69.97299729972997, 12.43445329884554],
 [-70.00900090009, 12.486538067869319],
 [-70.08100810081008, 12.538622836893097],
 [-70.08100810081008, 12.590707605916876],
 [-70.04500450045005, 12.608069195591469],
 [-70.00900090009, 12.55598442656769],
 [-69.93699369936994, 12.469176478194726],
 [-69.9009900990099, 12.451814888520133]]

[更新]

我相信算法在某个地方in this file,但我很难将JS翻译成Python。我喜欢Mike的工作,但有时他的js看起来已经缩小了......: - )

0 个答案:

没有答案