Web Developer Work - I
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. import React, { useEffect, useState } from "react";
  2. import FeatureLayer from "@arcgis/core/layers/FeatureLayer";
  3. import Query from "@arcgis/core/rest/support/Query";
  4. import StatisticDefinition from "@arcgis/core/rest/support/StatisticDefinition";
  5. import "../styles.css";
  6. const streetLightLayerUrl =
  7. "https://gisgcc.chennaicorporation.gov.in/server/rest/services/GCCDepts/streetlight_Enumeration/FeatureServer/0";
  8. const RoadWiseStreetLightCount: React.FC = () => {
  9. const [counts, setCounts] = useState<
  10. { zone: string; ward: string; road_name: string; RoadSide_Count: number; CentreMedianCount: number;ParksCount: number;BuildingsCount: number;BurialGround:number;PlayGround:number }[]
  11. >([]);
  12. const [zone, setZone] = useState<string>("");
  13. const [loading, setLoading] = useState<boolean>(false);
  14. const [error, setError] = useState<string>("");
  15. const fetchCountData = async () => {
  16. try {
  17. setLoading(true);
  18. setError("");
  19. const streetLightLayer = new FeatureLayer({ url: streetLightLayerUrl });
  20. let whereClause = "1=1";
  21. if (zone) whereClause = `zone='${zone}'`;
  22. const query = new Query();
  23. query.where = whereClause;
  24. query.returnGeometry = false;
  25. query.groupByFieldsForStatistics = ["zone", "ward", "road_name", "Position_of_post","highmast_position"];
  26. query.outStatistics = [
  27. new StatisticDefinition({
  28. statisticType: "count",
  29. onStatisticField: "Position_of_post",
  30. outStatisticFieldName: "count",
  31. }),
  32. ];
  33. query.outFields = ["zone", "ward", "road_name", "Position_of_post","highmast_position"];
  34. query.orderByFields = ["zone ASC", "ward ASC", "road_name ASC"];
  35. const result = await streetLightLayer.queryFeatures(query);
  36. const summary: { [key: string]: { RoadSide_Count: number; CentreMedianCount: number;ParksCount: number;BuildingsCount: number;BurialGround:number;PlayGround:number } } = {};
  37. result.features.forEach((feature) => {
  38. const { zone, ward, road_name, category } = feature.attributes;
  39. const count = feature.attributes.count as number || 0;
  40. const key = `${zone}_${ward}_${road_name}`;
  41. if (!summary[key]) {
  42. summary[key] = { RoadSide_Count: 0; CentreMedianCount: 0;ParksCount: 0;BuildingsCount: 0;BurialGround:0;PlayGround:0 };
  43. }
  44. if (Position_of_post === "Centre median") {
  45. summary[key].CentreMedianCount += count;
  46. } else if (Position_of_post === "Roadside") {
  47. summary[key].RoadSide_Count += count;
  48. }
  49. } else if (Position_of_post === "Parks") {
  50. summary[key].ParksCount += count;
  51. }
  52. } else if (Position_of_post === "Buildings") {
  53. summary[key].BuildingsCount += count;
  54. }
  55. } else if (Position_of_post === "Burial Ground") {
  56. summary[key].BurialGround += count;
  57. }
  58. } else if (Position_of_post === "Playground") {
  59. summary[key].PlayGround += count;
  60. }
  61. });
  62. const countsArray = Object.keys(summary).map((key) => {
  63. const [zone, ward, road_name] = key.split("_");
  64. return {
  65. zone,
  66. ward,
  67. road_name,
  68. CentreMedianCount: summary[key].CentreMedianCount,
  69. RoadSide_Count: summary[key].RoadSide_Count,
  70. ParksCount: summary[key].ParksCount,
  71. BuildingsCount: summary[key].BuildingsCount,
  72. BurialGround: summary[key].BurialGround,
  73. PlayGround: summary[key].PlayGround,
  74. };
  75. });
  76. setCounts(countsArray);
  77. } catch (error) {
  78. console.error("Error fetching streetlight category count:", error);
  79. setError("Failed to fetch data. Please try again later.");
  80. setCounts([]);
  81. } finally {
  82. setLoading(false);
  83. }
  84. };
  85. useEffect(() => {
  86. fetchCountData();
  87. }, [zone]);
  88. const exportToCSV = () => {
  89. const csvContent = [
  90. ["Zone", "Ward", "Road Name", "CentreMedianCount", "RoadSide_Count","ParksCount","BuildingsCount"].join(","),
  91. ...counts.map(({ zone, ward, road_name, streetLightCount, highmastCount }) =>
  92. [zone, ward, road_name, streetLightCount, highmastCount].join(",")
  93. ),
  94. ].join("\n");
  95. const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
  96. const link = document.createElement("a");
  97. link.href = URL.createObjectURL(blob);
  98. link.download = `StreetLight_Count_Zone_${zone || "All"}.csv`;
  99. link.click();
  100. };
  101. return (
  102. <div className="road-wise-streetlight-container">
  103. <h3>Road Wise - Street Light Count</h3>
  104. <label>
  105. Select Zone:
  106. <select value={zone} onChange={(e) => setZone(e.target.value)} className="zone-select">
  107. <option value="">-- Select Zone --</option>
  108. {[...Array(15)].map((_, i) => (
  109. <option key={i} value={String(i + 1).padStart(2, "0")}>
  110. {String(i + 1).padStart(2, "0")}
  111. </option>
  112. ))}
  113. </select>
  114. </label>
  115. <button onClick={exportToCSV} className="export-button">Export CSV</button>
  116. {error && <p className="error-message">{error}</p>}
  117. {loading ? <p>Loading...</p> : (
  118. <div className="table-container">
  119. <table className="road-wise-table">
  120. <thead>
  121. <tr>
  122. <th>Zone</th>
  123. <th>Ward</th>
  124. <th>Road Name</th>
  125. <th>Street Light Count</th>
  126. <th>Highmast Count</th>
  127. </tr>
  128. </thead>
  129. <tbody>
  130. {counts.map(({ zone, ward, road_name, streetLightCount, highmastCount }, index) => (
  131. <tr key={index}>
  132. <td>{zone}</td>
  133. <td>{ward}</td>
  134. <td>{road_name}</td>
  135. <td>{streetLightCount}</td>
  136. <td>{highmastCount}</td>
  137. </tr>
  138. ))}
  139. </tbody>
  140. </table>
  141. </div>
  142. )}
  143. </div>
  144. );
  145. };
  146. export default RoadWiseStreetLightCount;