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.

153 lines
5.5 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. type StreetLightCount = {
  9. zone: string;
  10. ward: string;
  11. road_name: string;
  12. roadside: number;
  13. centreMedian: number;
  14. parks: number;
  15. buildings: number;
  16. burialGround: number;
  17. playground: number;
  18. highmastJunction: number;
  19. highmastCentreMedian: number;
  20. highmastRoadside: number;
  21. };
  22. const RoadWiseStreetLightCount: React.FC = () => {
  23. const [counts, setCounts] = useState<StreetLightCount[]>([]);
  24. const [zone, setZone] = useState("");
  25. const [loading, setLoading] = useState(false);
  26. const [error, setError] = useState("");
  27. const fetchCountData = async () => {
  28. try {
  29. setLoading(true);
  30. setError("");
  31. const streetLightLayer = new FeatureLayer({ url: streetLightLayerUrl });
  32. let whereClause = "1=1";
  33. if (zone) whereClause = `zone='${zone}'`;
  34. const query = new Query();
  35. query.where = whereClause;
  36. query.returnGeometry = false;
  37. query.groupByFieldsForStatistics = ["zone", "ward", "road_name", "position_of_post", "highmast_position"];
  38. query.outStatistics = [
  39. new StatisticDefinition({
  40. statisticType: "count",
  41. onStatisticField: "position_of_post",
  42. outStatisticFieldName: "post_count",
  43. }),
  44. new StatisticDefinition({
  45. statisticType: "count",
  46. onStatisticField: "highmast_position",
  47. outStatisticFieldName: "highmast_count",
  48. }),
  49. ];
  50. query.outFields = ["zone", "ward", "road_name", "position_of_post", "highmast_position"];
  51. query.orderByFields = ["zone ASC", "ward ASC", "road_name ASC"];
  52. const result = await streetLightLayer.queryFeatures(query);
  53. console.log("Query Result:", result.features);
  54. const summary: Record<string, StreetLightCount> = {};
  55. result.features.forEach((feature) => {
  56. const { zone, ward, road_name, position_of_post, highmast_position } = feature.attributes;
  57. const postCount = feature.attributes.post_count || 0;
  58. const highmastCount = feature.attributes.highmast_count || 0;
  59. const key = `${zone}_${ward}_${road_name}`;
  60. if (!summary[key]) {
  61. summary[key] = { zone, ward, road_name, roadside: 0, centreMedian: 0, parks: 0, buildings: 0, burialGround: 0, playground: 0, highmastJunction: 0, highmastCentreMedian: 0, highmastRoadside: 0 };
  62. }
  63. if (position_of_post === 1) summary[key].centreMedian += postCount;
  64. else if (position_of_post === 2) summary[key].roadside += postCount;
  65. else if (position_of_post === 3) summary[key].parks += postCount;
  66. else if (position_of_post === 4) summary[key].buildings += postCount;
  67. else if (position_of_post === 5) summary[key].burialGround += postCount;
  68. else if (position_of_post === 6) summary[key].playground += postCount;
  69. if (highmast_position === 1) summary[key].highmastJunction += highmastCount;
  70. else if (highmast_position === 2) summary[key].highmastCentreMedian += highmastCount;
  71. else if (highmast_position === 3) summary[key].highmastRoadside += highmastCount;
  72. });
  73. setCounts(Object.values(summary));
  74. } catch (error) {
  75. console.error("Error fetching streetlight category count:", error);
  76. setError("Failed to fetch data. Please try again later.");
  77. setCounts([]);
  78. } finally {
  79. setLoading(false);
  80. }
  81. };
  82. useEffect(() => {
  83. fetchCountData();
  84. }, [zone]);
  85. const exportToCSV = () => {
  86. const csvContent = [
  87. ["Zone", "Ward", "Road Name", "Roadside", "Centre Median", "Parks", "Buildings", "Burial Ground", "Playground", "Highmast Junction", "Highmast Centre Median", "Highmast Roadside"].join(","),
  88. ...counts.map(row => Object.values(row).join(","))
  89. ].join("\n");
  90. const blob = new Blob([csvContent], { type: "text/csv" });
  91. const url = URL.createObjectURL(blob);
  92. const a = document.createElement("a");
  93. a.href = url;
  94. a.download = "Streetlight_Counts.csv";
  95. a.click();
  96. };
  97. return (
  98. <div className="road-wise-streetlight-container">
  99. <h3>Road Wise - Street Light Count</h3>
  100. <label>
  101. Select Zone:
  102. <select value={zone} onChange={(e) => setZone(e.target.value)} className="zone-select">
  103. <option value="">-- Select Zone --</option>
  104. {[...Array(15)].map((_, i) => (
  105. <option key={i} value={String(i + 1).padStart(2, "0")}>
  106. {String(i + 1).padStart(2, "0")}
  107. </option>
  108. ))}
  109. </select>
  110. </label>
  111. <button onClick={exportToCSV} className="export-button">Export to CSV</button>
  112. {error && <p className="error-message">{error}</p>}
  113. {loading ? (
  114. <p>Loading...</p>
  115. ) : (
  116. <table className="road-wise-table">
  117. <thead>
  118. <tr>
  119. {Object.keys(counts[0] || {}).map((key, i) => <th key={i}>{key.replace(/_/g, " ")}</th>)}
  120. </tr>
  121. </thead>
  122. <tbody>
  123. {counts.map((row, index) => (
  124. <tr key={index}>
  125. {Object.values(row).map((value, i) => <td key={i}>{String(value)}</td>)}
  126. </tr>
  127. ))}
  128. </tbody>
  129. </table>
  130. )}
  131. </div>
  132. );
  133. };
  134. export default RoadWiseStreetLightCount;