<div id="output" style="font-weight:bold; font-size:1.2em; margin: 1em 0;">
  Loading noise complaint data...
</div>

<script src="https://cdn.jsdelivr.net/npm/@turf/turf@6/turf.min.js"></script>

<script>
const cbdGeojson = {
"type": "FeatureCollection",
"name": "CBD",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::2263" } },
"features": [
{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -73.9592177, 40.7586545 ], [ -73.962899, 40.76024 ], [ -73.9627109, 40.760574 ], [ -73.9627582, 40.7606411 ], [ -73.9628166, 40.7607121 ], [ -73.9628738, 40.7608792 ], [ -73.9629361, 40.76094 ], [ -73.9676372, 40.7630327 ], [ -73.9724084, 40.765044 ], [ -73.9733357, 40.7654104 ], [ -73.9737672, 40.7649272 ], [ -73.9808602, 40.7679278 ], [ -73.980963, 40.7686397 ], [ -73.9924605, 40.7733774 ], [ -73.9932823, 40.7722103 ], [ -73.9938511, 40.7713617 ], [ -73.9941206, 40.769898 ], [ -73.9954558, 40.7678198 ], [ -73.9966746, 40.7659769 ], [ -73.9981835, 40.7639838 ], [ -73.9991778, 40.763033 ], [ -74.0007645, 40.7616972 ], [ -74.0018318, 40.7605226 ], [ -74.0027558, 40.7591771 ], [ -74.0037781, 40.7576249 ], [ -74.0046729, 40.7563681 ], [ -74.0059159, 40.7548219 ], [ -74.0066243, 40.7537403 ], [ -74.0074232, 40.75159 ], [ -74.0077323, 40.750352 ], [ -74.0072282, 40.7487739 ], [ -74.007489, 40.7462796 ], [ -74.0078666, 40.7443744 ], [ -74.0082923, 40.7422208 ], [ -74.008537, 40.7407296 ], [ -74.0093292, 40.7391995 ], [ -74.0093558, 40.7375952 ], [ -74.0098554, 40.7325933 ], [ -74.0100089, 40.7310901 ], [ -74.0100665, 40.7298182 ], [ -74.01033, 40.7275663 ], [ -74.0107424, 40.725111 ], [ -74.0112211, 40.7223279 ], [ -74.0119076, 40.7186505 ], [ -74.0128193, 40.7149813 ], [ -74.0134758, 40.7130675 ], [ -74.0139728, 40.7111899 ], [ -74.014148, 40.7102593 ], [ -74.0143983, 40.7093939 ], [ -74.0149532, 40.7077046 ], [ -74.0145764, 40.7072464 ], [ -74.0143834, 40.7069803 ], [ -74.0142825, 40.7065119 ], [ -74.01447, 40.706057 ], [ -74.014624, 40.705705 ], [ -74.014794, 40.705301 ], [ -74.0149415, 40.7046603 ], [ -74.014407, 40.704454 ], [ -74.01462, 40.703356 ], [ -74.014494, 40.70283 ], [ -74.013971, 40.702303 ], [ -74.0132, 40.7022574 ], [ -74.0129592, 40.7022462 ], [ -74.01286, 40.701722 ], [ -74.0125562, 40.7015574 ], [ -74.0111477, 40.7018365 ], [ -74.0096568, 40.702318 ], [ -74.008178, 40.7032417 ], [ -74.0064798, 40.7043884 ], [ -74.0034072, 40.70636 ], [ -74.0004726, 40.7080542 ], [ -74.0008051, 40.7086504 ], [ -74.001746, 40.7094405 ], [ -74.0027828, 40.7102375 ], [ -74.0033248, 40.7106612 ], [ -74.0035821, 40.7107851 ], [ -74.004088, 40.7108907 ], [ -74.0043095, 40.7109527 ], [ -74.00447, 40.7110589 ], [ -74.0045186, 40.7112163 ], [ -74.0045185, 40.711325 ], [ -74.00446, 40.711419 ], [ -74.004361, 40.711504 ], [ -74.004105, 40.711576 ], [ -74.003562, 40.7115648 ], [ -74.003413, 40.711748 ], [ -74.003238, 40.711899 ], [ -74.002995, 40.711932 ], [ -74.00284, 40.711914 ], [ -74.002667, 40.711825 ], [ -74.002345, 40.711284 ], [ -74.001872, 40.710758 ], [ -74.001281, 40.710215 ], [ -74.0008344, 40.7097934 ], [ -74.0003579, 40.7094133 ], [ -74.000025, 40.7091151 ], [ -73.9998366, 40.7090121 ], [ -73.9994029, 40.7086882 ], [ -73.9990686, 40.7086934 ], [ -73.9987284, 40.7088711 ], [ -73.9980176, 40.7091881 ], [ -73.9965684, 40.7094158 ], [ -73.9948602, 40.7096797 ], [ -73.993555, 40.7098639 ], [ -73.9905637, 40.7102949 ], [ -73.9888049, 40.7105039 ], [ -73.9870935, 40.7107136 ], [ -73.98473, 40.7109303 ], [ -73.9835571, 40.711014 ], [ -73.9818336, 40.7111312 ], [ -73.9795704, 40.7115565 ], [ -73.9786637, 40.7119992 ], [ -73.9780908, 40.7129085 ], [ -73.9769708, 40.714981 ], [ -73.9760738, 40.7167453 ], [ -73.9757009, 40.717774 ], [ -73.9754617, 40.7189241 ], [ -73.97503, 40.7216137 ], [ -73.9748494, 40.7223692 ], [ -73.974478, 40.7230132 ], [ -73.9737245, 40.7242354 ], [ -73.972791, 40.7255485 ], [ -73.9721949, 40.7270026 ], [ -73.9720612, 40.7280148 ], [ -73.9725413, 40.7295884 ], [ -73.9736504, 40.7304466 ], [ -73.9740008, 40.7309898 ], [ -73.9742118, 40.731718 ], [ -73.9743588, 40.7322374 ], [ -73.974632, 40.7333402 ], [ -73.975198, 40.7355147 ], [ -73.97527, 40.73568 ], [ -73.9753818, 40.7363415 ], [ -73.9751363, 40.7371913 ], [ -73.9743511, 40.738195 ], [ -73.9736623, 40.7391983 ], [ -73.9729701, 40.7424296 ], [ -73.9725173, 40.7432435 ], [ -73.9723624, 40.7434175 ], [ -73.9716105, 40.7447917 ], [ -73.9711545, 40.7453843 ], [ -73.9706289, 40.7459687 ], [ -73.9698745, 40.7467523 ], [ -73.9689683, 40.7476975 ], [ -73.9672742, 40.7499694 ], [ -73.9653882, 40.7519325 ], [ -73.964696, 40.7526127 ], [ -73.9632631, 40.7541473 ], [ -73.9628254, 40.7547841 ], [ -73.9616843, 40.755916 ], [ -73.9611687, 40.7565287 ], [ -73.9592177, 40.7586545 ] ] ] } },
{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -73.9586025, 40.7591455 ], [ -73.9625877, 40.7609042 ], [ -73.9626005, 40.7608501 ], [ -73.9625718, 40.7607887 ], [ -73.9625315, 40.7607222 ], [ -73.9624487, 40.7606173 ], [ -73.962315, 40.7605346 ], [ -73.9621527, 40.760457 ], [ -73.962054, 40.760424 ], [ -73.9616516, 40.7602641 ], [ -73.9609789, 40.7599537 ], [ -73.9602754, 40.7596698 ], [ -73.958705, 40.7589866 ], [ -73.9586025, 40.7591455 ] ] ] } },
{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -74.010502, 40.701627 ], [ -74.009247, 40.699793 ], [ -73.998293, 40.704978 ], [ -74.000959, 40.706967 ], [ -74.002459, 40.706257 ], [ -74.003818, 40.705434 ], [ -74.006554, 40.703649 ], [ -74.007356, 40.703001 ], [ -74.008266, 40.702451 ], [ -74.009328, 40.701919 ], [ -74.010502, 40.701627 ] ] ] } },
{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -73.973934, 40.734814 ], [ -73.973258, 40.734828 ], [ -73.972293, 40.734891 ], [ -73.972296, 40.735998 ], [ -73.97238, 40.737153 ], [ -73.972389, 40.739219 ], [ -73.972231, 40.739875 ], [ -73.971645, 40.742605 ], [ -73.970946, 40.743818 ], [ -73.971287, 40.743924 ], [ -73.972003, 40.742786 ], [ -73.972417, 40.741061 ], [ -73.972856, 40.739305 ], [ -73.972993, 40.738908 ], [ -73.97323, 40.738535 ], [ -73.973693, 40.737873 ], [ -73.973972, 40.737493 ], [ -73.974245, 40.737136 ], [ -73.974436, 40.736836 ], [ -73.974591, 40.736311 ], [ -73.974602, 40.735843 ], [ -73.974508, 40.735206 ], [ -73.974334, 40.734765 ], [ -73.973934, 40.734814 ] ] ] } },
{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -73.98907, 40.709899 ], [ -73.988882, 40.708923 ], [ -73.979548, 40.709801 ], [ -73.977148, 40.710723 ], [ -73.976025, 40.711678 ], [ -73.975428, 40.712701 ], [ -73.97325, 40.719166 ], [ -73.971652, 40.725814 ], [ -73.971986, 40.725893 ], [ -73.974141, 40.722382 ], [ -73.974335, 40.721265 ], [ -73.974308, 40.720143 ], [ -73.974416, 40.718521 ], [ -73.974995, 40.717069 ], [ -73.976904, 40.713434 ], [ -73.977504, 40.712248 ], [ -73.978111, 40.711466 ], [ -73.979243, 40.710949 ], [ -73.980208, 40.710711 ], [ -73.981244, 40.710615 ], [ -73.982155, 40.710559 ], [ -73.983403, 40.710413 ], [ -73.984485, 40.710381 ], [ -73.984805, 40.710413 ], [ -73.986841, 40.710162 ], [ -73.98907, 40.709899 ] ] ] } },
{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -74.0156618, 40.7069632 ], [ -74.0159292, 40.7061839 ], [ -74.0159856, 40.7061917 ], [ -74.0160827, 40.7059385 ], [ -74.0161813, 40.705654 ], [ -74.0163068, 40.7053736 ], [ -74.0164228, 40.705093 ], [ -74.0164282, 40.7048322 ], [ -74.016386, 40.70443 ], [ -74.01586, 40.704415 ], [ -74.0155973, 40.7050375 ], [ -74.0151333, 40.7062843 ], [ -74.0151098, 40.706596 ], [ -74.0152583, 40.7068494 ], [ -74.0154501, 40.7069762 ], [ -74.0156618, 40.7069632 ] ] ] } },
{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -74.0104378, 40.7392454 ], [ -74.0102143, 40.7398551 ], [ -74.0095688, 40.7409506 ], [ -74.0090691, 40.742826 ], [ -74.008184, 40.7485209 ], [ -74.0085789, 40.7494669 ], [ -74.0088006, 40.7504749 ], [ -74.0083731, 40.7517246 ], [ -74.0073049, 40.7543681 ], [ -74.0062368, 40.7558831 ], [ -74.0046452, 40.7581973 ], [ -74.0031747, 40.7602345 ], [ -74.0015753, 40.7622716 ], [ -73.9996253, 40.7636746 ], [ -73.9969428, 40.7673695 ], [ -73.9950928, 40.7698923 ], [ -73.9949212, 40.7715583 ], [ -73.9933245, 40.7737265 ], [ -73.994557, 40.774141 ], [ -73.996067, 40.774653 ], [ -74.006693, 40.760336 ], [ -74.007943, 40.758806 ], [ -74.005655, 40.757941 ], [ -74.008254, 40.755232 ], [ -74.009738, 40.753506 ], [ -74.011037, 40.751197 ], [ -74.011307, 40.749465 ], [ -74.019806, 40.70621 ], [ -74.019436, 40.704693 ], [ -74.018621, 40.703614 ], [ -74.017562, 40.703562 ], [ -74.017032, 40.704149 ], [ -74.0169526, 40.7045597 ], [ -74.0170295, 40.7048438 ], [ -74.0169485, 40.7051505 ], [ -74.016775, 40.7055891 ], [ -74.016664, 40.706257 ], [ -74.016502, 40.706712 ], [ -74.01614, 40.707716 ], [ -74.015539, 40.709514 ], [ -74.015142, 40.710888 ], [ -74.014562, 40.713149 ], [ -74.013887, 40.715174 ], [ -74.011671, 40.724938 ], [ -74.0114022, 40.7266972 ], [ -74.0110904, 40.7284646 ], [ -74.0109281, 40.732105 ], [ -74.0105411, 40.7357181 ], [ -74.0104378, 40.7392454 ] ] ] } }
]
}
;
const polygon = cbdGeojson.features[0];

function formatDate(date) {
  return date.toISOString().split('.')[0];
}

const today = new Date();
const currentYear = today.getFullYear();
const lastYear = currentYear - 1;

const startMonthDay = '01-05';

function buildRange(year) {
  const startDate = new Date(`${year}-${startMonthDay}T00:00:00`);
  let endDate = year === currentYear ? today : new Date(`${year}-${(today.getMonth()+1).toString().padStart(2,'0')}-${today.getDate().toString().padStart(2,'0')}T23:59:59`);
  if (endDate < startDate) endDate = startDate;
  return {
    start: formatDate(startDate),
    end: formatDate(endDate),
  };
}

const ranges = {
  [lastYear]: buildRange(lastYear),
  [currentYear]: buildRange(currentYear),
};

const baseUrl = 'https://data.cityofnewyork.us/resource/erm2-nwe9.json';
const limit = 5000;

async function fetchAllForRange(start, end) {
  let offset = 0;
  let allData = [];

  while (true) {
    const whereClause = `complaint_type='Noise - Vehicle' AND created_date between '${start}' and '${end}'`;
    const url = `${baseUrl}?$where=${encodeURIComponent(whereClause)}&$limit=${limit}&$offset=${offset}`;
    const res = await fetch(url);
    const data = await res.json();
    if (data.length === 0) break;
    allData = allData.concat(data);
    offset += limit;
    if (data.length < limit) break;
  }

  return allData;
}

function countInPolygon(data) {
  return data.filter(item => {
    if (!item.latitude || !item.longitude) return false;
    const pt = turf.point([parseFloat(item.longitude), parseFloat(item.latitude)]);
    return turf.booleanPointInPolygon(pt, polygon);
  }).length;
}

async function runComparison() {
  try {
    const counts = {};
    for (const year of [lastYear, currentYear]) {
      const range = ranges[year];
      const data = await fetchAllForRange(range.start, range.end);
      counts[year] = countInPolygon(data);
    }

    const diff = counts[currentYear] - counts[lastYear];
    const pctChange = counts[lastYear] === 0 ? (diff === 0 ? 0 : 100) : (diff / counts[lastYear]) * 100;

    let message = '';
    if (pctChange > 0) {
      message = `Noise Complaints in the Congestion Zone - Up ${pctChange.toFixed(1)}%`;
    } else if (pctChange < 0) {
      message = `Noise Complaints in the Congestion Zone - Down ${Math.abs(pctChange).toFixed(1)}%`;
    } else {
      message = `Noise Complaints in the Congestion Zone - No Change`;
    }

    document.getElementById('output').textContent = message;

  } catch (e) {
    document.getElementById('output').textContent = 'Error: ' + e;
  }
}

runComparison();


</script>