2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

h3 でジオデータの vertex count

Last updated at Posted at 2018-06-29

h3 は密度集計のバケツとして気軽に使える気がします。ジオデータの構成点数の密度集計をしてみました。

結果

地球地図日本の海岸線データの構成点数の密度集計をしてみました。画像をクリックすると GitHub のサイトに移動します。
スクリーンショット 2018-06-28 21.20.18.png

ソースコード

const shapefile = require('shapefile')
const h3 = require('h3-js')

let dict = {}
const add = (h3) => {
  if (dict[h3]) {
    dict[h3]++
  } else {
    dict[h3] = 1
  }
}

const push = f => {
  if (!f.geometry.type === 'LineString') {
    throw new Error(`${f.geometry.type} not supported.`)
  }
  for (let a of f.geometry.coordinates) {
    add(h3.geoToH3(a[1], a[0], 5))
  }
}

shapefile.open('coastl_jpn.shp')
  .then(source => source.read()
    .then(function process (result) {
      if (result.done) return
      push(result.value)
      return source.read().then(process)
    }))
  .then(() => {
    let geojson = {
      type: 'FeatureCollection',
      features: []
    }
    for (const k in dict) {
      let coords = h3.h3ToGeoBoundary(k)
      for (let i = 0; i < coords.length; i++) {
        let v = coords[i][0]
        coords[i][0] = coords[i][1]
        coords[i][1] = v
      }
      coords.push(coords[0])
      geojson.features.push({
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [coords]
        },
        properties: {
          'h3': k,
          'count': dict[k]
        }
      })
    }
    console.log(JSON.stringify(geojson))
  })
  .catch(error => console.error(error.stack))

感想

h3 の API の経緯度の順番は、GeoJSON の経緯度の順番と逆なので、そこがややぎこちなかったです。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?