{"id":4164,"date":"2015-03-24T15:52:07","date_gmt":"2015-03-24T06:52:07","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=4164"},"modified":"2015-03-24T15:53:54","modified_gmt":"2015-03-24T06:53:54","slug":"post-4164","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/leaflet-js\/4164","title":{"rendered":"[Turf.js]\u30d8\u30c3\u30af\u30b9(\u516d\u89d2\u5f62)\u3067\u7d71\u8a08\u60c5\u5831\u3092\u5730\u56f3\u4e0a\u306b\u8996\u899a\u5316\u3059\u308b"},"content":{"rendered":"<p><a href=\"http:\/\/shimz.me\/example\/turf\/hex\/\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2015\/03\/turfhexgrid150324.jpg\" alt=\"turf hexgrid\" width=\"690\" height=\"574\" class=\"aligncenter size-full wp-image-4165 border\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2015\/03\/turfhexgrid150324.jpg 690w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2015\/03\/turfhexgrid150324-180x150.jpg 180w\" sizes=\"auto, (max-width: 690px) 100vw, 690px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/shimz.me\/example\/turf\/hex\/index.html\">example<\/a><\/p>\n<p>\u8868\u793a\u3055\u308c\u3066\u3044\u308b\u7bc4\u56f2\u5185\u306b\u30d8\u30c3\u30af\u30b9(\u516d\u89d2\u5f62)\u3092\u6577\u304d\u8a70\u3081\u3001\u5404\u30bb\u30eb\u306b\u542b\u307e\u308c\u308b\u30de\u30fc\u30ab\u30fc\u306e\u6570\u3084\u30de\u30fc\u30ab\u30fc\u306b\u8a2d\u5b9a\u3055\u308c\u305fvalue\u5024\u306e\u5408\u8a08\u6570\u3067\u53ef\u8996\u5316\u3057\u3066\u3044\u307e\u3059\u3002\u30b5\u30f3\u30d7\u30eb\u3068\u3057\u3066\u30bb\u30eb\u306e\u30b5\u30a4\u30ba\u3092\u5927\u304d\u3081\u306b\u8a2d\u5b9a(1km)\u3057\u3066\u3044\u307e\u3059\u304c\u3001100m\u4f4d\u307e\u3067\u306a\u3089\u305d\u3053\u305d\u3053\u554f\u984c\u306a\u304f\u52d5\u304d\u307e\u3059\u3002<br \/>\n\u305d\u308c\u3088\u308a\u5c0f\u3055\u3044\u30b5\u30a4\u30ba\u306e\u30bb\u30eb\u3092\u8868\u793a\u3057\u305f\u3044\u5834\u5408\u306f\u3001node\u3092\u4f7f\u3063\u3066\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u3067\u8a08\u7b97\u3057\u3066\u304b\u3089\u51fa\u529b\u3057\u305f\u65b9\u304c\u3088\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n<h2>\u30b5\u30f3\u30d7\u30eb<\/h2>\n<pre class=\"lang:js decode:true \" >fetch('..\/_dataset\/sample.geojson')\r\n\t.then(function(response) {\r\n\t\treturn response.json()\r\n\t}).then(function(json) {\r\n\t\tdraw(json)\r\n\t});\r\n\t\r\nfunction draw(points){\r\n\t\/\/\u53d6\u5f97\u3059\u308b\u7d71\u8a08\u60c5\u5831(\u5408\u8a08\u5024\u3001\u500b\u6570)\r\n\tvar aggregations = [\r\n\t\t{\r\n\t\t\taggregation: 'sum',\r\n\t\t\tinField: 'value',\r\n\t\t\toutField: 'pt_sum'\r\n\t\t},\r\n\t\t{\r\n\t\t\taggregation: 'count',\r\n\t\t\tinField: '',\r\n\t\t\toutField: 'pt_count'\r\n\t\t}\t\t\r\n\t];\r\n\t\r\n\tmap = L.map('map').setView([36.3265281, 139.00918460000003], 14);\r\n\t\r\n\t\/\/\u5730\u7406\u9662\u5730\u56f3\u30ec\u30a4\u30e4\u30fc\u8ffd\u52a0\r\n\tL.tileLayer(\r\n\t\t'http:\/\/cyberjapandata.gsi.go.jp\/xyz\/pale\/{z}\/{x}\/{y}.png',\r\n\t\t{\r\n\t\t\tattribution: \"&lt;a href='http:\/\/www.gsi.go.jp\/kikakuchousei\/kikakuchousei40182.html' target='_blank'&gt;\u56fd\u571f\u5730\u7406\u9662&lt;\/a&gt;\"\r\n\t\t}\r\n\t).addTo(map);\r\n\t\r\n\t\/\/\u30de\u30fc\u30ab\u30fc\u30af\u30ea\u30c3\u30af\u6642\u306b\u30dd\u30c3\u30d7\u30a2\u30c3\u30d7\u3092\u8868\u793a\r\n\tvar onEachFeature = function(feature, layer) {\r\n\t\tvar popupContent = \"value:\";\r\n\t\tif (feature.properties &amp;&amp; feature.properties.value) {\r\n\t\t\tpopupContent += feature.properties.value;\r\n\t\t}\r\n\t\tlayer.bindPopup(popupContent);\r\n\t}\r\n\t\/\/\u30de\u30fc\u30ab\u30fc\u3092\u5730\u56f3\u306b\u8ffd\u52a0\r\n\tvar markerLayer = L.geoJson(points, { onEachFeature: onEachFeature }).addTo(map);\r\n \r\n\t\/\/\u30d8\u30c3\u30af\u30b9\u30b0\u30ea\u30c3\u30c9\u7528\u306e\u30ec\u30a4\u30e4\u30fc\u3092\u6e96\u5099\r\n\tvar hexLayer = L.geoJson().addTo(map);\r\n \r\n\t\/\/\u8868\u793a\u3055\u308c\u3066\u3044\u308b\u56db\u9685\u306e\u7def\u5ea6\u7d4c\u5ea6\u3092\u53d6\u5f97\r\n\tvar b = map.getBounds();\r\n\textend = [b.getSouthWest().lng , b.getSouthWest().lat , b.getNorthEast().lng, b.getNorthEast().lat]\t\r\n\t\r\n\t\/\/\u8868\u793a\u3055\u308c\u3066\u3044\u308b\u7bc4\u56f2\u3092\u30d8\u30c3\u30af\u30b9\u3067\u57cb\u3081\u308b\r\n\tvar grid = turf.hexGrid(extend,  1, \"kilometers\");\r\n\t\r\n\t\/\/\u30d8\u30c3\u30af\u30b9\u30c7\u30fc\u30bf\u306b\u7d71\u8a08\u60c5\u5831\u3092\u4ed8\u52a0(\u5404\u30bb\u30eb\u306b\u304a\u3051\u308b\u30de\u30fc\u30ab\u30fc\u306e\u6570\u3068value\u306e\u5408\u8a08\u5024)\r\n\tvar grid = turf.aggregate(grid, points, aggregations);\r\n\t\r\n\t\/\/\u5404\u30bb\u30eb\u306e\u30b9\u30bf\u30a4\u30eb\u3092\u8a2d\u5b9a\r\n\tgrid.features.forEach(setStyle);\r\n\t\r\n\t\/\/\u30d8\u30c3\u30af\u30b9\u30b0\u30ea\u30c3\u30c9\u3092\u5730\u56f3\u306b\u8ffd\u52a0\r\n\thexLayer.addData(grid);\r\n\t\r\n\t\/*\r\n\t * event lestener\r\n\t *\/\r\n\tdocument.querySelector(\"#non\").addEventListener(\"click\",function(){ \r\n\t\thexLayer.eachLayer(function(l) { \r\n\t\t\tl.setStyle(l.feature.properties.withNon); \/\/\u30d8\u30c3\u30af\u30b9\u306e\u307f\u8868\u793a\r\n\t\t});\r\n\t});\r\n\t\r\n\tdocument.querySelector(\"#sum\").addEventListener(\"click\",function(){ \r\n\t\thexLayer.eachLayer(function(l) { \r\n\t\t\tl.setStyle(l.feature.properties.withSum);\t\/\/\u5408\u8a08\u5024\u3092\u8996\u899a\u5316\r\n\t\t});\r\n\t});\r\n\tdocument.querySelector(\"#count\").addEventListener(\"click\",function(){ \r\n\t\tconsole.log(\"click\");\r\n\t\thexLayer.eachLayer(function(l) { \r\n\t\t\tl.setStyle(l.feature.properties.withCount);\t\/\/\u30de\u30fc\u30ab\u30fc\u306e\u6570\u3092\u8996\u899a\u5316\r\n\t\t});\r\n\t});\r\n\t\r\n\t\/\/\u521d\u671f\u30b9\u30bf\u30a4\u30eb\u3092\u5b9f\u884c\r\n\tdocument.querySelector(\"#non\").dispatchEvent(new Event('click'))\r\n\t\r\n}\r\n\r\n\/\/\u5404\u30bb\u30eb\u306e\u30b9\u30bf\u30a4\u30eb\u3092\u6307\u5b9a\r\nfunction setStyle(cell){\r\n\t\tcell.properties.withNon = {};\t\t\r\n\t\tcell.properties.withCount = {};\r\n\t\tcell.properties.withSum= {};\r\n\t\t\r\n\t\t\r\n\t\tvar _widthNon = {};\r\n\t\t_widthNon.color = '#0000FF';\r\n\t\t_widthNon.weight = 1;\r\n\t\t_widthNon.fill = '#0000FF';\r\n\t\t_widthNon.fillOpacity = 0.1;\r\n\t\tcell.properties.withNon =  _widthNon;\r\n\t\t\r\n\t\tvar pt_count = cell.properties.pt_count;\r\n\t\tvar pt_sum = cell.properties.pt_sum;\r\n\t\t\r\n\t\tvar _withCount = {};\r\n\t\t_withCount.color = '#00FF00';\r\n\t\t_withCount.weight = 0;\r\n\t\t_withCount.fill = '#00FF00';\r\n\t\t_withCount.fillOpacity = 0;\r\n\t\tif(pt_count &gt;= 1) {\r\n\t\t\t_withCount.fillOpacity = 0.1;\r\n\t\t} if(pt_count &gt;= 2) {\r\n\t\t\t_withCount.fillOpacity = 0.2;\r\n\t\t\t_withCount.weight = 1;\r\n\t\t} if(pt_count &gt;= 3) {\r\n\t\t\t_withCount.weight = 2;\r\n\t\t\t_withCount.fillOpacity = 0.35;\r\n\t\t} if(pt_count &gt;= 4) {\r\n\t\t\t_withCount.weight = 3;\r\n\t\t\t_withCount.fillOpacity = 0.55;\r\n\t\t}\r\n\t\tcell.properties.withCount = _withCount;\r\n\r\n\t\tvar _withSum = {};\r\n\t\t_withSum.color = '#FF0000';\r\n\t\t_withSum.weight = 0;\r\n\t\t_withSum.fill = '#FF0000';\r\n\t\t_withSum.fillOpacity = 0;\r\n\t\tif(pt_sum &gt;= 50) {\r\n\t\t\t_withSum.fillOpacity = 0.1;\r\n\t\t} if(pt_sum &gt;= 100) {\r\n\t\t\t_withSum.fillOpacity = 0.2;\r\n\t\t\t_withSum.weight = 1;\r\n\t\t} if(pt_sum &gt;= 150) {\r\n\t\t\t_withSum.weight = 2;\r\n\t\t\t_withSum.fillOpacity = 0.35;\r\n\t\t} if(pt_sum &gt;= 200) {\r\n\t\t\t_withSum.weight = 3;\r\n\t\t\t_withSum.fillOpacity = 0.55;\r\n\t\t}\r\n\t\tcell.properties.withSum = _withSum;\t\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>example \u8868\u793a\u3055\u308c\u3066\u3044\u308b\u7bc4\u56f2\u5185\u306b\u30d8\u30c3\u30af\u30b9(\u516d&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4165,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55,62],"tags":[],"class_list":["post-4164","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-leaflet-js","category-turf-js","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/4164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=4164"}],"version-history":[{"count":0,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/4164\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/4165"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=4164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=4164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=4164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}