var project={"_id":"5c7682c3ab233e8499c7b2d9","id":"17d459744b96d9a2ae0c72f8c038a06359cc62a7","name":"Public Contingency Planning Aid","user":"566ac9850286a8f9080cdd79","description":"","created":"2017-01-12T22:12:10.308Z","shared":false,"server":"micro","datafiles":[{"file":{"link":"https://v4p4sz5ijk.execute-api.us-east-1.amazonaws.com/anbdata/airspaces/zones/fir-list?api_key=7c3cb620-ed23-11e5-a5f9-adff4fb3e3e1&format=json","type":"URL/API","url":"https://v4p4sz5ijk.execute-api.us-east-1.amazonaws.com/anbdata/airspaces/zones/fir-list?api_key=7c3cb620-ed23-11e5-a5f9-adff4fb3e3e1&format=json","message":{"error":false,"text":""}},"varname":"FIRs","title":"FIRs","activated":true,"name":"datafile","linkedtofile":true,"optional":true,"content":"","sendtojs":true}],"packs":["jquery","D3","highstock"],"container":"mainpart","js":{"file":{"type":"","url":"","link":"","message":{"error":true,"text":"File not defined"},"depurl":"https://quipslibistars.s3.amazonaws.com/prod/17d459744b96d9a2ae0c72f8c038a06359cc62a7/script.js"},"title":"Javascript","activated":true,"name":"js","linkedtofile":false,"optional":true,"content":"//check if loaded\nvar thisuseremail=\"mmerens@icao.int\"\nif (typeof(getEmail)==\"function\") {\n getEmail(function(e){\n thisuseremail=e\n $(\"#email\").val(thisuseremail)\n })\n $(\"#emailbox\").hide()\n}\nif (getParameterByName(\"withprint\")) $(\"#nopdf\").show()\nif (getParameterByName(\"print\")) {\n $(\".printit\").hide()\n \n var options=JSON.parse(getParameterByName(\"options\"))\n $(options).each(function(i,v){\n if (v.value==\"checked\") {\n $(\"#\"+v.element).attr(\"checked\",true)\n }\n else if (v.value==\"unchecked\") {\n $(\"#\"+v.element).attr(\"checked\",false)\n }\n else {\n $(\"#\"+v.element).val(v.value)\n }\n })\n \n \n \n}\n\nvar map = L.map('map').setView([20, 10], 2);\nvar simmap = L.map('simmap').setView([20, 10], 2);\nL.control.scale().addTo(map); \n//length\nL.control.scale().addTo(simmap); \n\nvar currentfir,currentfirlayer,currentfirlayer2,currentfirnb,flightlayer,overlayer,inoutlayer,internallayer,\n fullfirlayer,statistics\nvar smallbuffer,bigbuffer\nvar markers={\n all:L.markerClusterGroup({iconCreateFunction: fc}),\n over:L.markerClusterGroup({iconCreateFunction: fc}),\n inout:L.markerClusterGroup({iconCreateFunction: fc})}\n//var t1=FIRs.filter(function(d){return d.properties.FIRname==\"FIR ISTANBUL\"})[0]\n//var t2=FIRs.filter(function(d){return d.properties.FIRname==\"FIR ANKARA\"})[0]\n//var t3=turf.union(t1,t2)\n//console.log(JSON.stringify(t3))\nvar flights,inters,simrec,simlayer,nextlayer,therec,smallrec,plusdist,nextfirs\nvar color={over:\"orange\",inout:\"green\",internal:\"red\",sim:\"green\"}\n\n/*\nL.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {\n attribution: '© OpenStreetMap contributors'\n}).addTo(map);\nL.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {\n attribution: '© OpenStreetMap contributors'\n}).addTo(simmap);\n*/\nL.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', {\n attribution: '© OpenStreetMap contributors'\n}).addTo(map);\nL.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', {\n attribution: '© OpenStreetMap contributors'\n}).addTo(simmap);\n\nsimlayer=L.layerGroup()\nsimlayer.addTo(simmap)\n\nflightlayer=L.layerGroup()\nflightlayer.addTo(map)\noverlayer=L.layerGroup()\noverlayer.addTo(map)\nnextlayer=L.layerGroup()\nnextlayer.addTo(simmap)\noverlayer2=L.layerGroup()\n//overlayer2.addTo(simmap)\ninoutlayer=L.layerGroup()\nfullfirlayer=L.layerGroup()\ninoutlayer.addTo(map)\ninternallayer=L.layerGroup()\ninternallayer.addTo(map)\nmap.addLayer(markers.all);\n\n\n $(FIRs).each(function(i,u){\n //if (currentfir.properties.ICAOCODE!=u.properties.ICAOCODE) {\n fullfirlayer.addLayer(L.geoJSON(u.geometry,\n {fillOpacity:0,opacity:0.5,code:u.properties.ICAOCODE})\n .bindPopup(u.properties.FIRname+\" (\"+u.properties.ICAOCODE+\")\"))\n //}\n })\n\n\n\ndraw()\n\n$(\"#editzones\").click(function(e){\n e.preventDefault()\n $(\"#customzone\").modal()\n})\n\n\nfunction fc(cluster) {\n var ids=[]\n var allmarkers = cluster.getAllChildMarkers();\n var childCount=0\n for (var i = 0; i < allmarkers.length; i++) {\n if(ids.indexOf(allmarkers[i].id)<0) {\n childCount++\n ids.push(allmarkers[i].id)\n }\n }\n var c = ' marker-cluster-';\n\t\tif (childCount < 10) {\n\t\t\tc += 'small';\n\t\t} else if (childCount < 100) {\n\t\t\tc += 'medium';\n\t\t} else {\n\t\t\tc += 'large';\n\t\t}\n\n\t\treturn new L.DivIcon({ html: '
\"+Math.round(10*statistics.avoidance_pc)/10+\"% of flights seem to avoid overflying the \"+$(\"#mainfirname\").text()+\n \", as their shortest route would normally make them overfly this zone.
\"\n return s\n}\nfunction buildflights(res){\n markers.all.clearLayers()\n markers.over.clearLayers()\n markers.inout.clearLayers()\n map.removeLayer(markers.over)\n map.removeLayer(markers.inout)\n map.removeLayer(markers.all)\n\n //console.log(res[0])\n $(res.filter(function(d){return d.flight_type!=\"internal\"&&d.flight_type!=\"avoidance\"})).each(function(i,v){\n v.id=i\n addcluster(v,v.flight_type)\n })\n \n \n if ($(\"#hotspots\").is(\":checked\")) {\n var val=$(\"#tftoggle .active input\").val()\n if (val!=\"internal\") {\n markers[val].addTo(map)\n }\n \n }\n\n}\n\nfunction addnextlayers() {\n nextlayer.clearLayers()\n nextfirs={}\n $(FIRs).each(function(i,v){\n if (v.properties.ICAOCODE!=currentfir.properties.ICAOCODE) {\n if (v.geometry.type==\"MultiPolygon\") {\n if (multisect(v.geometry,therec)) {\n nextfirs[v.properties.ICAOCODE]=v\n nextlayer.addLayer(L.geoJSON(v.geometry,\n {fillColor:\"yellow\",color:\"orange\",opacity:0.5,code:v.properties.ICAOCODE})\n .bindPopup(v.properties.FIRname+\" (\"+v.properties.ICAOCODE+\")\")\n )\n }\n\n }\n else {\n if (turf.intersect(v.geometry,therec)) {\n nextfirs[v.properties.ICAOCODE]=v\n nextlayer.addLayer(L.geoJSON(v.geometry,\n {fillColor:\"yellow\",color:\"orange\",opacity:0.5,code:v.properties.ICAOCODE})\n .bindPopup(v.properties.FIRname+\" (\"+v.properties.ICAOCODE+\")\")\n )\n }\n }\n }\n })\n}\nfunction multisect(polys,layer){\n var s=0\n $.each(polys.coordinates,function(i,poly){\n if (turf.intersect({type:\"Polygon\",coordinates:poly},layer)) {\n s++\n }\n })\n return (s>0)\n}\n\n\nfunction buildsimflights3(){\n var fir=$(\"#zone\").val()\n var theyear=$(\"#theyear\").val()\n simlayer.clearLayers()\n $.getJSON(\"https://api.anbdata.com/anb/airspaces/traffic/fir-closure-trajectory-list?callback=?\",\n //$.getJSON(\"http://test.anbdata.com:8000/anb/airspaces/traffic/fir-closure-trajectory-list?callback=?\",\n {api_key:\"7c3cb620-ed23-11e5-a5f9-adff4fb3e3e1\",year:theyear,fir:fir},\n function(simulatedtrajectories){\n $(simulatedtrajectories).each(function(i,v){\n \n \n /*\n var ll=L.geodesic([], {\n \t //weight: 7,\n \t opacity: 0.2,\n \t color: color.sim,\n \t steps: 50,\n \t wrap: false\n }).addTo(simlayer)\n ll.geoJson(v.newline)\n */\n simlayer.addLayer(L.geoJSON(v.newline,{color:color.sim,opacity:0.2}))\n })\n \n })\n \n $(\"#totdist\").text(Math.round(statistics.total_delta_distance_nm)+\" nm\")\n $(\"#avdist\").text(Math.round(statistics.av_delta_distance_nm)+\" nm\")\n}\n\nfunction drawnextfirs(){\n var c=[]\n var total=0\n $(statistics.closure_impact.filter(function(d){return d.delta!=0}))\n .each(function(i,v){\n c.push({code:v.FIRCode,\n name:FIRs.filter(function(q){return q.properties.ICAOCODE==v.FIRCode})[0].properties.FIRname,\n flights:v.flights,\n delta:Math.round(100*v.delta)/100})\n total+=Math.abs(v.flights)\n })\n /*\n for (var key in nextfirs) {\n if (nextfirs[key].properties.delta!=0) {\n c.push({code:nextfirs[key].properties.ICAOCODE,\n name:nextfirs[key].properties.FIRname,\n delta:nextfirs[key].properties.delta\n })\n }\n } \n */\n c.sort(function(a,b){return b.delta-a.delta})\n var min=c[c.length-1].delta\n var max=c[0].delta\n \n var colorScale = d3.scale.linear().domain([min,0,max]).range([\"blue\",\"white\",\"red\"]);\n\n chart2.xAxis.categories=[]\n chart2.series[0].data=[]\n chart2.series[1].data=[]\n $.each(c,function(i,v){\n chart2.xAxis.categories.push(v.name+\" (\"+v.code+\")\")\n chart2.series[0].data.push({y:v.delta,color:colorScale(v.delta)})\n chart2.series[1].data.push({y:Math.round(1000*v.flights/statistics.overflights)/10})\n \n })\n nextlayer.eachLayer(function (layer) {\n var col=c.filter(function(d){return d.code==layer.options.code})[0]\n layer.setStyle({fillColor :colorScale((col?col.delta:0))})\n });\n\n $(\"#chart2\").highcharts(chart2)\n $(\"#pcfir\").text(c[0].name+\" (\"+c[0].code+\")\")\n $(\"#pcincrease\").text(Math.round(10*c[0].delta)/10)\n \n c.sort(function(a,b){return b.flights-a.flights})\n $(\"#firstfir\").text(c[0].name+\" (\"+c[0].code+\")\")\n $(\"#firstfirpart\").text(Math.round(1000*c[0].flights/statistics.overflights)/10)\n $(\"#firstfirpc\").text(c[0].flights)\n //console.log(c[0])\n //console.log(chart2.series)\n \n}\nfunction addcluster(v,ty){\n var p=turf.intersect(currentfir.geometry,v.line)\n if (p&&p.geometry.type==\"LineString\") {\n var f=L.marker(L.GeoJSON.coordsToLatLng(p.geometry.coordinates[0]))\n var l=L.marker(L.GeoJSON.coordsToLatLng(p.geometry.coordinates[p.geometry.coordinates.length-1]))\n //l.bindPopup(\"Flight \"+v.ident+\"