Spaces:
Sleeping
Sleeping
File size: 94,616 Bytes
9060118 6de9ffb 9060118 6de9ffb 9060118 d6337b9 14f0b4c d6337b9 14f0b4c d6337b9 14f0b4c d6337b9 c7d486d d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 f58a96b d6337b9 f58a96b d6337b9 f58a96b d6337b9 f58a96b d6337b9 f58a96b 9060118 d6337b9 f58a96b 9060118 d6337b9 9060118 d6337b9 f58a96b d6337b9 f58a96b d6337b9 f58a96b d6337b9 f58a96b d6337b9 f58a96b d6337b9 f58a96b d6337b9 9060118 d6337b9 9060118 f58a96b d6337b9 f58a96b d6337b9 f58a96b d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 f58a96b 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 f58a96b 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 f58a96b 9060118 d6337b9 f58a96b d6337b9 9060118 f58a96b 9060118 f58a96b 9060118 d6337b9 9060118 f58a96b 9060118 d6337b9 9060118 f58a96b 9060118 d6337b9 f58a96b 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 f58a96b 9060118 f58a96b 9060118 f58a96b d6337b9 f58a96b 9060118 f58a96b 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 d6337b9 9060118 f58a96b 9060118 f58a96b 9060118 d6337b9 9060118 d6337b9 6de9ffb 9060118 d6337b9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 |
import io
import os
import tempfile
import warnings
import zipfile
import folium
import geopandas as gpd
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import requests
import streamlit as st
from plotly.subplots import make_subplots
from scipy.spatial.distance import cdist
from shapely.geometry import LineString, Point
from streamlit_folium import st_folium
warnings.filterwarnings("ignore")
# === FIXED ENVIRONMENT SETUP FOR HUGGING FACE SPACES ===
# Set up writable directories for Hugging Face Spaces
# Set up writable directories for Hugging Face Spaces
temp_dir = tempfile.gettempdir()
# Environment variables that must be set BEFORE importing streamlit
os.environ["STREAMLIT_CONFIG_DIR"] = os.path.join(temp_dir, ".streamlit")
os.environ["HOME"] = temp_dir
os.environ["MPLCONFIGDIR"] = os.path.join(temp_dir, ".matplotlib")
os.environ["XDG_CONFIG_HOME"] = os.path.join(temp_dir, ".config")
os.environ["XDG_CACHE_HOME"] = os.path.join(temp_dir, ".cache")
# Create all necessary directories
directories = [
os.environ["STREAMLIT_CONFIG_DIR"],
os.environ["MPLCONFIGDIR"],
os.environ["XDG_CONFIG_HOME"],
os.environ["XDG_CACHE_HOME"],
os.path.join(temp_dir, ".local"),
os.path.join(temp_dir, ".cache", "matplotlib"),
os.path.join(temp_dir, ".config", "matplotlib"),
]
for dir_path in directories:
try:
os.makedirs(dir_path, mode=0o777, exist_ok=True)
except (OSError, PermissionError):
pass # Ignore errors
# Suppress warnings
warnings.filterwarnings("ignore")
# Set matplotlib backend to Agg (non-interactive) for server environment
try:
import matplotlib
matplotlib.use("Agg")
except Exception as e:
st.warning(f"Matplotlib configuration warning: {e}")
# === STREAMLIT CONFIGURATION ===
st.set_page_config(
page_title="Analisis Jaringan Listrik DIY",
page_icon="โก",
layout="wide",
initial_sidebar_state="expanded",
)
# === CSS STYLING ===
st.markdown(
"""
<style>
.main-header {
font-size: 2.5rem;
font-weight: bold;
color: #1e3a8a;
text-align: center;
margin-bottom: 2rem;
}
.sub-header {
font-size: 1.5rem;
font-weight: bold;
color: #3b82f6;
margin-top: 2rem;
margin-bottom: 1rem;
}
.metric-card {
background-color: #f8fafc;
padding: 1rem;
border-radius: 0.5rem;
border-left: 4px solid #3b82f6;
}
.stAlert > div {
padding: 0.5rem;
}
</style>
""",
unsafe_allow_html=True,
)
# === UTILITY FUNCTIONS ===
@st.cache_data
def safe_file_processing(uploaded_file):
"""Safely process uploaded file with error handling"""
try:
# Create temporary directory for file processing
with tempfile.TemporaryDirectory() as temp_dir:
# Save uploaded file
temp_file_path = os.path.join(temp_dir, uploaded_file.name)
with open(temp_file_path, "wb") as f:
f.write(uploaded_file.getvalue())
# Extract and process ZIP
with zipfile.ZipFile(temp_file_path) as zip_file:
zip_file.extractall(temp_dir)
# Find shapefile
shp_files = [f for f in os.listdir(temp_dir) if f.endswith(".shp")]
if not shp_files:
raise ValueError("File shapefile tidak ditemukan dalam ZIP")
# Read shapefile
shp_path = os.path.join(temp_dir, shp_files[0])
gdf = gpd.read_file(shp_path)
return gdf
except Exception as e:
st.error(f"Error memproses file: {str(e)}")
return None
@st.cache_data
def safe_url_download(data_url):
"""Safely download data from URL with timeout and error handling"""
try:
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}
with st.spinner("Mengunduh data dari server..."):
response = requests.get(data_url, timeout=60, headers=headers)
response.raise_for_status()
with tempfile.TemporaryDirectory() as temp_dir:
with zipfile.ZipFile(io.BytesIO(response.content)) as zip_file:
zip_file.extractall(temp_dir)
shp_files = [f for f in os.listdir(temp_dir) if f.endswith(".shp")]
if not shp_files:
raise ValueError(
"File shapefile tidak ditemukan dalam download"
)
shp_path = os.path.join(temp_dir, shp_files[0])
gdf = gpd.read_file(shp_path)
return gdf
except requests.exceptions.Timeout:
st.error("โฑ๏ธ Timeout: Server terlalu lama merespons")
return None
except requests.exceptions.ConnectionError:
st.error("๐ Error: Tidak dapat terhubung ke server")
return None
except Exception as e:
st.error(f"Error mengunduh data: {str(e)}")
return None
def create_network_graph(gdf):
"""Membuat graf jaringan dari data geografis dengan penanganan yang lebih baik"""
try:
G = nx.Graph()
# Data sudah dalam UTM Zone 49S (EPSG:32749)
gdf_utm = gdf.copy()
if gdf.crs != "EPSG:32749":
gdf_utm = gdf.to_crs("EPSG:32749")
# Dictionary untuk menyimpan koordinat ke node ID
coord_to_node = {}
node_counter = 0
edges = []
line_segments = []
# Tolerance untuk menggabungkan koordinat yang sangat dekat (dalam meter)
tolerance = 100.0 # 100 meter tolerance
def get_or_create_node(coord):
"""Dapatkan node ID untuk koordinat, atau buat baru jika belum ada"""
nonlocal node_counter
# Cari node yang sudah ada dalam tolerance
for existing_coord, node_id in coord_to_node.items():
if (
abs(existing_coord[0] - coord[0]) < tolerance
and abs(existing_coord[1] - coord[1]) < tolerance
):
return node_id
# Buat node baru
coord_to_node[coord] = node_counter
node_counter += 1
return node_counter - 1
for idx, row in gdf_utm.iterrows():
geom = row.geometry
line_name = row.get("nama", f"Line_{idx}")
line_id = row.get("id", idx)
# Handle MultiLineString dan LineString
if geom.geom_type == "MultiLineString":
# Pecah MultiLineString menjadi LineString individual
for i, line in enumerate(geom.geoms):
coords = list(line.coords)
if len(coords) >= 2:
# Untuk setiap segmen dalam line, buat edges berturut-turut
for j in range(len(coords) - 1):
start_point = coords[j]
end_point = coords[j + 1]
# Dapatkan atau buat node
start_idx = get_or_create_node(start_point)
end_idx = get_or_create_node(end_point)
# Hitung panjang segmen
segment_length = (
(end_point[0] - start_point[0]) ** 2
+ (end_point[1] - start_point[1]) ** 2
) ** 0.5
edge_data = {
"weight": segment_length,
"line_id": f"{line_id}_{i}_{j}",
"nama": f"{line_name}_segment_{i}_{j}",
"length_m": segment_length,
"length_km": segment_length / 1000,
}
edges.append((start_idx, end_idx, edge_data))
# Buat geometri LineString untuk segmen ini
segment_geom = LineString([start_point, end_point])
line_segments.append(
{
"geometry": segment_geom,
"start_node": start_idx,
"end_node": end_idx,
"nama": f"{line_name}_segment_{i}_{j}",
"length_m": segment_length,
"length_km": segment_length / 1000,
}
)
elif geom.geom_type == "LineString":
coords = list(geom.coords)
if len(coords) >= 2:
# Untuk LineString, buat edges berturut-turut untuk setiap segmen
for j in range(len(coords) - 1):
start_point = coords[j]
end_point = coords[j + 1]
# Dapatkan atau buat node
start_idx = get_or_create_node(start_point)
end_idx = get_or_create_node(end_point)
# Hitung panjang segmen
segment_length = (
(end_point[0] - start_point[0]) ** 2
+ (end_point[1] - start_point[1]) ** 2
) ** 0.5
edge_data = {
"weight": segment_length,
"line_id": f"{line_id}_{j}",
"nama": f"{line_name}_segment_{j}",
"length_m": segment_length,
"length_km": segment_length / 1000,
}
edges.append((start_idx, end_idx, edge_data))
# Buat geometri LineString untuk segmen ini
segment_geom = LineString([start_point, end_point])
line_segments.append(
{
"geometry": segment_geom,
"start_node": start_idx,
"end_node": end_idx,
"nama": f"{line_name}_segment_{j}",
"length_m": segment_length,
"length_km": segment_length / 1000,
}
)
# Tambahkan nodes ke graf dengan informasi posisi
for coord, node_id in coord_to_node.items():
node_data = {
"pos": coord,
"type": "junction",
"x": coord[0], # UTM Easting
"y": coord[1], # UTM Northing
"lat": None, # Will be calculated when needed
"lon": None, # Will be calculated when needed
}
G.add_node(node_id, **node_data)
# Tambahkan edges ke graf
G.add_edges_from(edges)
# Konversi coord_to_node menjadi list nodes untuk kompatibilitas
nodes = [None] * len(coord_to_node)
for coord, node_id in coord_to_node.items():
nodes[node_id] = coord
return G, nodes, gdf_utm, line_segments
except Exception as e:
st.error(f"Error creating network graph: {str(e)}")
return nx.Graph(), [], gdf, []
def calculate_centrality_measures(G):
"""Hitung berbagai ukuran sentralitas dengan error handling"""
centrality_measures = {}
try:
if G.number_of_nodes() == 0:
return {"degree": {}, "betweenness": {}, "closeness": {}, "eigenvector": {}}
centrality_measures["degree"] = nx.degree_centrality(G)
centrality_measures["betweenness"] = nx.betweenness_centrality(G)
centrality_measures["closeness"] = nx.closeness_centrality(G)
try:
centrality_measures["eigenvector"] = nx.eigenvector_centrality(
G, max_iter=1000
)
except:
# Jika eigenvector centrality gagal, gunakan nilai default
centrality_measures["eigenvector"] = {node: 0.0 for node in G.nodes()}
except Exception as e:
st.warning(f"Error calculating centrality measures: {str(e)}")
# Jika ada masalah dalam perhitungan, gunakan nilai default
num_nodes = G.number_of_nodes()
for measure in ["degree", "betweenness", "closeness", "eigenvector"]:
centrality_measures[measure] = {i: 0.0 for i in range(num_nodes)}
return centrality_measures
def find_minimum_spanning_tree(G):
"""Cari Minimum Spanning Tree menggunakan algoritma Kruskal dengan error handling"""
try:
if G.number_of_nodes() == 0:
return nx.Graph()
mst = nx.minimum_spanning_tree(G, weight="weight", algorithm="kruskal")
return mst
except Exception as e:
st.warning(f"Error finding MST: {str(e)}")
return nx.Graph()
def analyze_network_connectivity(G, line_segments=None):
"""Analisis konektivitas jaringan dengan detail tambahan dan error handling"""
analysis = {}
try:
analysis["num_nodes"] = G.number_of_nodes()
analysis["num_edges"] = G.number_of_edges()
analysis["is_connected"] = (
nx.is_connected(G) if G.number_of_nodes() > 0 else False
)
analysis["num_components"] = nx.number_connected_components(G)
if G.number_of_nodes() > 0:
analysis["density"] = nx.density(G)
if nx.is_connected(G):
try:
analysis["diameter"] = nx.diameter(G)
analysis["average_path_length"] = nx.average_shortest_path_length(G)
except:
analysis["diameter"] = "N/A (Error computing)"
analysis["average_path_length"] = "N/A (Error computing)"
else:
analysis["diameter"] = "N/A (Graf tidak terhubung)"
analysis["average_path_length"] = "N/A (Graf tidak terhubung)"
# Degree statistics
degrees = [d for n, d in G.degree()]
analysis["avg_degree"] = np.mean(degrees) if degrees else 0
analysis["max_degree"] = max(degrees) if degrees else 0
analysis["min_degree"] = min(degrees) if degrees else 0
# Network length statistics (dari line_segments)
if line_segments:
total_length_m = sum(seg["length_m"] for seg in line_segments)
total_length_km = total_length_m / 1000
avg_segment_length = (
total_length_m / len(line_segments) if line_segments else 0
)
analysis["total_network_length_m"] = total_length_m
analysis["total_network_length_km"] = total_length_km
analysis["avg_segment_length_m"] = avg_segment_length
analysis["avg_segment_length_km"] = avg_segment_length / 1000
analysis["longest_segment_km"] = (
max(seg["length_km"] for seg in line_segments)
if line_segments
else 0
)
analysis["shortest_segment_km"] = (
min(seg["length_km"] for seg in line_segments)
if line_segments
else 0
)
else:
# Default values for empty graph
for key in ["density", "avg_degree", "max_degree", "min_degree"]:
analysis[key] = 0
analysis["diameter"] = "N/A"
analysis["average_path_length"] = "N/A"
except Exception as e:
st.error(f"Error analyzing network connectivity: {str(e)}")
# Return minimal analysis
analysis = {
"num_nodes": 0,
"num_edges": 0,
"is_connected": False,
"num_components": 0,
"density": 0,
"diameter": "N/A",
"average_path_length": "N/A",
"avg_degree": 0,
"max_degree": 0,
"min_degree": 0,
}
return analysis
def create_network_visualization(
G,
nodes,
centrality_measures,
show_labels=False,
show_edge_details=False,
label_size=10,
label_color="white",
edge_offset=0.02,
show_edge_colors=True,
):
"""Buat visualisasi jaringan menggunakan Plotly dengan error handling"""
try:
if G.number_of_nodes() == 0:
fig = go.Figure()
fig.add_annotation(
x=0.5,
y=0.5,
text="Tidak ada data untuk divisualisasikan",
showarrow=False,
font=dict(size=16),
)
return fig
# Gunakan posisi asli dari koordinat UTM, kemudian normalisasi untuk visualisasi
pos = {}
node_coords = [(G.nodes[node]["x"], G.nodes[node]["y"]) for node in G.nodes()]
if node_coords:
# Normalisasi koordinat untuk visualisasi yang lebih baik
min_x = min(coord[0] for coord in node_coords)
max_x = max(coord[0] for coord in node_coords)
min_y = min(coord[1] for coord in node_coords)
max_y = max(coord[1] for coord in node_coords)
# Avoid division by zero
range_x = max_x - min_x if max_x != min_x else 1
range_y = max_y - min_y if max_y != min_y else 1
for node in G.nodes():
x_norm = (G.nodes[node]["x"] - min_x) / range_x
y_norm = (G.nodes[node]["y"] - min_y) / range_y
pos[node] = (x_norm, y_norm)
else:
# Fallback ke spring layout jika tidak ada koordinat
pos = nx.spring_layout(G, k=1, iterations=50)
# Siapkan data untuk edges dengan multiple edges terpisah
edge_traces = [] # List untuk menyimpan multiple traces
# Hitung statistik edge untuk normalisasi
edge_weights = [data.get("weight", 0) for _, _, data in G.edges(data=True)]
max_weight = max(edge_weights) if edge_weights else 1
min_weight = min(edge_weights) if edge_weights else 0
# Group edges berdasarkan pasangan node untuk mendeteksi multiple edges
edge_groups = {}
for edge in G.edges(data=True):
node_pair = tuple(sorted([edge[0], edge[1]]))
if node_pair not in edge_groups:
edge_groups[node_pair] = []
edge_groups[node_pair].append(edge)
# Fungsi untuk membuat offset untuk multiple edges
def calculate_edge_offset(
x0, y0, x1, y1, offset_distance, edge_index, total_edges
):
"""Hitung offset untuk edge paralel"""
if total_edges == 1:
return x0, y0, x1, y1
# Hitung vektor perpendicular
dx = x1 - x0
dy = y1 - y0
length = (dx**2 + dy**2) ** 0.5
if length == 0:
return x0, y0, x1, y1
# Vektor unit perpendicular
perp_x = -dy / length
perp_y = dx / length
# Hitung offset untuk edge ini
if total_edges % 2 == 1:
# Odd number: center edge at 0, others at ยฑoffset
center_index = total_edges // 2
offset = (edge_index - center_index) * offset_distance
else:
# Even number: no center edge
offset = (edge_index - (total_edges - 1) / 2) * offset_distance
# Apply offset
offset_x0 = x0 + perp_x * offset
offset_y0 = y0 + perp_y * offset
offset_x1 = x1 + perp_x * offset
offset_y1 = y1 + perp_y * offset
return offset_x0, offset_y0, offset_x1, offset_y1
# Proses setiap group edge
for node_pair, edges in edge_groups.items():
if edges[0][0] not in pos or edges[0][1] not in pos:
continue
x0, y0 = pos[edges[0][0]]
x1, y1 = pos[edges[0][1]]
total_edges = len(edges)
offset_distance = edge_offset # Gunakan parameter yang dapat diatur
for edge_index, edge in enumerate(edges):
# Hitung posisi dengan offset
offset_x0, offset_y0, offset_x1, offset_y1 = calculate_edge_offset(
x0, y0, x1, y1, offset_distance, edge_index, total_edges
)
weight = edge[2].get("weight", 0)
line_name = edge[2].get("nama", f"Edge_{edge[0]}_{edge[1]}")
line_id = edge[2].get("line_id", f"ID_{edge[0]}_{edge[1]}")
# Info detail untuk hover
if show_edge_details:
edge_info = (
f"Edge: {edge[0]} โ {edge[1]}<br>"
f"Nama: {line_name}<br>"
f"ID: {line_id}<br>"
f"Panjang: {weight:.2f}m ({weight/1000:.3f}km)<br>"
f"Saluran {edge_index + 1} dari {total_edges}"
)
else:
edge_info = f"Edge: {edge[0]} โ {edge[1]}<br>Panjang: {weight:.2f}m"
# Warna berdasarkan jumlah edge paralel
if total_edges > 1 and show_edge_colors:
# Multiple edges: gunakan warna berbeda jika diaktifkan
colors = ["red", "blue", "green", "orange", "purple", "brown"]
color = colors[edge_index % len(colors)]
edge_color = color
edge_width = 2.0 # Lebih tebal untuk multiple edges
elif total_edges > 1:
# Multiple edges tanpa warna berbeda
edge_color = "rgba(255,100,100,0.8)" # Merah muda untuk multiple
edge_width = 2.0
else:
# Single edge: warna berdasarkan panjang
if max_weight > min_weight:
normalized_weight = (weight - min_weight) / (
max_weight - min_weight
)
red_component = int(255 * (1 - normalized_weight))
blue_component = int(255 * normalized_weight)
edge_color = (
f"rgba({red_component}, 100, {blue_component}, 0.7)"
)
else:
edge_color = "rgba(125,125,125,0.8)"
edge_width = 1.2
# Buat trace untuk edge ini
edge_trace = go.Scatter(
x=[offset_x0, offset_x1, None],
y=[offset_y0, offset_y1, None],
line=dict(width=edge_width, color=edge_color),
hoverinfo="text" if show_edge_details else "none",
hovertext=edge_info if show_edge_details else None,
mode="lines",
name=(
f"Saluran {edge_index + 1}"
if total_edges > 1
else "Saluran Listrik"
),
showlegend=False,
)
edge_traces.append(edge_trace)
# Siapkan data untuk nodes
node_x = []
node_y = []
node_text = []
node_color = []
node_size = []
node_ids = [] # Pindahkan ke sini untuk sinkronisasi
# Gunakan degree centrality untuk pewarnaan dan ukuran
degree_cent = centrality_measures.get("degree", {})
for node in G.nodes():
if node in pos:
x, y = pos[node]
node_x.append(x)
node_y.append(y)
node_ids.append(str(node)) # Tambahkan ID node sesuai urutan
# Informasi node dengan detail koneksi
adjacencies = list(G.neighbors(node))
node_degree = G.degree(node)
# Hitung total edge secara manual untuk verifikasi
total_edges_manual = 0
connection_details = []
for neighbor in adjacencies:
# Hitung berapa banyak edge antara node ini dan neighbor
edge_count = G.number_of_edges(node, neighbor)
total_edges_manual += edge_count
if edge_count > 1:
connection_details.append(
f"โ Node {neighbor} ({edge_count} saluran)"
)
else:
connection_details.append(f"โ Node {neighbor}")
node_info = f"๐ต Node: {node}<br>"
node_info += f"๐ Degree (NetworkX): {node_degree}<br>"
node_info += f"๐ข Total Edge Manual: {total_edges_manual}<br>"
node_info += f"๐ฅ Tetangga: {len(adjacencies)}<br>"
# Tampilkan peringatan jika ada ketidaksesuaian
if node_degree != total_edges_manual:
node_info += f"โ ๏ธ INCONSISTENCY DETECTED!<br>"
if show_edge_details and connection_details:
node_info += f"๐ Detail Koneksi:<br>"
node_info += "<br>".join(
connection_details[:5]
) # Batasi 5 koneksi pertama
if len(connection_details) > 5:
node_info += (
f"<br>... dan {len(connection_details) - 5} lainnya"
)
node_info += "<br><br>"
node_info += f"๐ Sentralitas:<br>"
node_info += f"โข Degree: {degree_cent.get(node, 0):.4f}<br>"
node_info += f'โข Betweenness: {centrality_measures.get("betweenness", {}).get(node, 0):.4f}<br>'
node_info += f'โข Closeness: {centrality_measures.get("closeness", {}).get(node, 0):.4f}<br>'
node_info += f'โข Eigenvector: {centrality_measures.get("eigenvector", {}).get(node, 0):.4f}'
node_text.append(node_info)
node_color.append(degree_cent.get(node, 0))
# Ukuran node berdasarkan degree centrality
base_size = 8
size_multiplier = 20
node_size.append(base_size + degree_cent.get(node, 0) * size_multiplier)
# node_ids sudah dibuat di loop sebelumnya, tidak perlu duplikasi
# Trace untuk nodes dengan styling yang lebih menarik
node_trace = go.Scatter(
x=node_x,
y=node_y,
mode="markers+text" if show_labels else "markers",
hoverinfo="text",
text=node_ids if show_labels else [],
textposition="middle center",
textfont=dict(size=label_size, color=label_color, family="Arial Black"),
hovertext=node_text,
marker=dict(
showscale=True,
colorscale="Viridis",
reversescale=True,
color=node_color,
size=node_size,
colorbar=dict(
thickness=15,
len=0.7,
x=1.02,
title=dict(text="Degree Centrality", font=dict(size=12)),
tickfont=dict(size=10),
),
line=dict(width=1, color="white"),
opacity=0.9,
),
name="Node/Junction",
)
# Buat figure dengan multiple edge traces
all_traces = edge_traces + [node_trace]
fig = go.Figure(
data=all_traces,
layout=go.Layout(
title=dict(
text="Visualisasi Graf Jaringan Listrik DIY",
font=dict(size=16),
x=0.5,
),
showlegend=False,
hovermode="closest",
margin=dict(b=40, l=40, r=60, t=80),
annotations=[
dict(
text="Node berukuran dan berwarna berdasarkan Degree Centrality.<br>Saluran paralel ditampilkan dengan garis terpisah dan warna berbeda.<br>Node yang lebih besar dan gelap = lebih penting dalam jaringan",
showarrow=False,
xref="paper",
yref="paper",
x=0.02,
y=0.02,
xanchor="left",
yanchor="bottom",
font=dict(color="#666", size=10),
bgcolor="rgba(255,255,255,0.8)",
bordercolor="#ccc",
borderwidth=1,
)
],
xaxis=dict(
showgrid=True,
zeroline=False,
showticklabels=False,
gridcolor="rgba(128,128,128,0.2)",
),
yaxis=dict(
showgrid=True,
zeroline=False,
showticklabels=False,
gridcolor="rgba(128,128,128,0.2)",
),
plot_bgcolor="rgba(240,240,240,0.1)",
height=700,
),
)
return fig
except Exception as e:
st.error(f"Error creating network visualization: {str(e)}")
fig = go.Figure()
fig.add_annotation(
x=0.5,
y=0.5,
text=f"Error dalam visualisasi: {str(e)}",
showarrow=False,
font=dict(size=14),
)
return fig
def create_centrality_comparison(centrality_measures):
"""Buat perbandingan ukuran sentralitas dengan error handling"""
try:
if not centrality_measures or not centrality_measures.get("degree"):
fig = go.Figure()
fig.add_annotation(
x=0.5,
y=0.5,
text="Tidak ada data sentralitas untuk dibandingkan",
showarrow=False,
font=dict(size=16),
)
return fig
nodes = list(centrality_measures["degree"].keys())
fig = make_subplots(
rows=2,
cols=2,
subplot_titles=(
"Degree Centrality",
"Betweenness Centrality",
"Closeness Centrality",
"Eigenvector Centrality",
),
vertical_spacing=0.1,
)
measures = ["degree", "betweenness", "closeness", "eigenvector"]
positions = [(1, 1), (1, 2), (2, 1), (2, 2)]
for measure, (row, col) in zip(measures, positions):
values = [centrality_measures[measure].get(node, 0) for node in nodes]
fig.add_trace(
go.Bar(x=nodes, y=values, name=measure.title()), row=row, col=col
)
fig.update_layout(
height=600,
showlegend=False,
title=dict(text="Perbandingan Ukuran Sentralitas", font=dict(size=16)),
)
return fig
except Exception as e:
st.error(f"Error creating centrality comparison: {str(e)}")
return go.Figure()
def create_centrality_matrix(centrality_measures):
"""Buat matriks sentralitas untuk semua node dengan error handling"""
try:
if not centrality_measures or not centrality_measures.get("degree"):
return pd.DataFrame()
# Ambil semua node
nodes = list(centrality_measures["degree"].keys())
# Buat DataFrame dengan semua ukuran sentralitas
centrality_data = {
"Node": nodes,
"Degree Centrality": [
centrality_measures["degree"].get(node, 0) for node in nodes
],
"Closeness Centrality": [
centrality_measures["closeness"].get(node, 0) for node in nodes
],
"Betweenness Centrality": [
centrality_measures["betweenness"].get(node, 0) for node in nodes
],
"Eigenvector Centrality": [
centrality_measures["eigenvector"].get(node, 0) for node in nodes
],
}
df = pd.DataFrame(centrality_data)
# Urutkan berdasarkan Degree Centrality (descending)
df = df.sort_values("Degree Centrality", ascending=False).reset_index(drop=True)
return df
except Exception as e:
st.error(f"Error creating centrality matrix: {str(e)}")
return pd.DataFrame()
def create_node_connection_details(G, top_n=20):
"""Buat tabel detail koneksi untuk node-node teratas"""
try:
if G.number_of_nodes() == 0:
return pd.DataFrame()
# Ambil node dengan degree tertinggi
node_degrees = dict(G.degree())
top_nodes = sorted(node_degrees.items(), key=lambda x: x[1], reverse=True)[
:top_n
]
connection_data = []
# Deteksi self-loops dalam graf
self_loops = list(nx.selfloop_edges(G))
has_self_loops = len(self_loops) > 0
for node, degree in top_nodes:
neighbors = list(G.neighbors(node))
actual_neighbors = [n for n in neighbors if n != node] # Exclude self-loop
# Hitung detail koneksi
connection_details = []
total_edges = 0
# Hitung edges ke tetangga sebenarnya
for neighbor in actual_neighbors:
edge_count = G.number_of_edges(node, neighbor)
total_edges += edge_count
if edge_count > 1:
connection_details.append(f"Node {neighbor} ({edge_count}x)")
else:
connection_details.append(f"Node {neighbor}")
# Tambahkan self-loop jika ada
if G.has_edge(node, node):
self_edge_count = G.number_of_edges(node, node)
total_edges += self_edge_count
connection_details.append(
f"Node {node} (SELF-LOOP: {self_edge_count}x)"
)
# Batasi tampilan koneksi
if len(connection_details) > 8:
display_connections = (
", ".join(connection_details[:8])
+ f", ... (+{len(connection_details)-8})"
)
else:
display_connections = ", ".join(connection_details)
# Bandingkan degree NetworkX dengan perhitungan manual
degree_nx = G.degree(node)
# Cek self-loop untuk node ini
has_self_loop = G.has_edge(node, node)
self_loop_count = (
1 if has_self_loop else 0
) # Self-loop dihitung 1x sesuai teori graf
# Total edges termasuk self-loop
total_edges_with_self = total_edges + self_loop_count
is_consistent = degree_nx == total_edges_with_self
# Status dengan informasi self-loop
if is_consistent:
status = "โ
OK" + (" (with self-loop)" if has_self_loop else "")
elif has_self_loop:
status = f"โ ๏ธ SELF-LOOP (+{self_loop_count})"
else:
status = "โ ๏ธ INCONSISTENT"
connection_data.append(
{
"Node": node,
"Degree (NetworkX)": degree_nx,
"Total Edges (Manual)": total_edges,
"Self-Loop": "Yes" if has_self_loop else "No",
"Jumlah Tetangga": len(actual_neighbors),
"Detail Koneksi": display_connections,
"Rasio Edge/Tetangga": (
f"{total_edges/len(neighbors):.2f}" if neighbors else "0"
),
"Status": status,
}
)
return pd.DataFrame(connection_data)
except Exception as e:
st.error(f"Error creating connection details: {str(e)}")
return pd.DataFrame()
def create_map_visualization(gdf_original):
"""Buat visualisasi peta menggunakan Folium dengan error handling"""
try:
if gdf_original is None or gdf_original.empty:
return None
# Konversi ke WGS84 untuk visualisasi
gdf_wgs84 = gdf_original.to_crs("EPSG:4326")
# Hitung centroid untuk center map
bounds = gdf_wgs84.total_bounds
center_lat = (bounds[1] + bounds[3]) / 2
center_lon = (bounds[0] + bounds[2]) / 2
# Buat peta
m = folium.Map(
location=[center_lat, center_lon], zoom_start=12, tiles="OpenStreetMap"
)
# Tambahkan layer jaringan listrik
for idx, row in gdf_wgs84.iterrows():
geom = row.geometry
line_name = row.get("nama", f"Line_{idx}")
line_id = row.get("id", idx)
# Handle MultiLineString dan LineString
if geom.geom_type == "MultiLineString":
for i, line in enumerate(geom.geoms):
coords = [[lat, lon] for lon, lat in line.coords]
# Hitung panjang untuk popup
line_length_m = line.length * 111000 # Approximate conversion
line_length_km = line_length_m / 1000
popup_text = f"""
<b>{line_name} - Segment {i+1}</b><br>
ID: {line_id}<br>
Panjang: {line_length_km:.3f} km<br>
Tipe: MultiLineString
"""
folium.PolyLine(
locations=coords,
color="red",
weight=2,
opacity=0.8,
popup=folium.Popup(popup_text, max_width=300),
).add_to(m)
elif geom.geom_type == "LineString":
coords = [[lat, lon] for lon, lat in geom.coords]
# Hitung panjang untuk popup
line_length_m = geom.length * 111000 # Approximate conversion
line_length_km = line_length_m / 1000
popup_text = f"""
<b>{line_name}</b><br>
ID: {line_id}<br>
Panjang: {line_length_km:.3f} km<br>
Tipe: LineString
"""
folium.PolyLine(
locations=coords,
color="blue",
weight=2,
opacity=0.8,
popup=folium.Popup(popup_text, max_width=300),
).add_to(m)
# Tambahkan legend
legend_html = """
<div style="position: fixed;
bottom: 50px; left: 50px; width: 120px; height: 60px;
background-color: white; border:2px solid grey; z-index:9999;
font-size:14px; padding: 10px;">
<p><b>Legenda</b></p>
<p><i class="fa fa-minus" style="color:red"></i> MultiLineString</p>
<p><i class="fa fa-minus" style="color:blue"></i> LineString</p>
</div>
"""
m.get_root().html.add_child(folium.Element(legend_html))
return m
except Exception as e:
st.error(f"Error creating map visualization: {str(e)}")
return None
# === MAIN APPLICATION ===
def main():
st.markdown(
'<h1 class="main-header">โก Analisis Keterhubungan Jaringan Listrik DIY</h1>',
unsafe_allow_html=True,
)
st.markdown(
"""
**Aplikasi ini menganalisis jaringan listrik di Daerah Istimewa Yogyakarta menggunakan pendekatan Teori Graf**
Berdasarkan proposal penelitian: *"Analisis Keterhubungan Jaringan Listrik Menggunakan Pendekatan Teori Graf dalam Mendukung Ekonomi Digital di Daerah Istimewa Yogyakarta"*
"""
)
# Sidebar
st.sidebar.title("๐ง Konfigurasi Analisis")
# Add environment info for debugging
with st.sidebar.expander("๐ Debug Info"):
st.write("Environment Variables:")
st.write(f"HOME: {os.environ.get('HOME', 'Not set')}")
st.write(f"MPLCONFIGDIR: {os.environ.get('MPLCONFIGDIR', 'Not set')}")
st.write(
f"STREAMLIT_CONFIG_DIR: {os.environ.get('STREAMLIT_CONFIG_DIR', 'Not set')}"
)
# Pilihan sumber data
data_source = st.sidebar.radio(
"Pilih Sumber Data:",
["๐ Upload File ZIP", "๐ Download dari URL", "๐พ File Lokal"],
)
gdf = None
if data_source == "๐ Upload File ZIP":
uploaded_file = st.sidebar.file_uploader(
"Upload file ZIP berisi data shapefile:",
type=["zip"],
help="Upload file ZIP yang berisi data jaringan listrik dalam format shapefile",
)
if uploaded_file is not None:
with st.spinner("Memproses file yang diupload..."):
gdf = safe_file_processing(uploaded_file)
if gdf is not None:
st.sidebar.success(
f"โ
File berhasil diproses: {len(gdf)} features"
)
elif data_source == "๐พ File Lokal":
zip_path = st.sidebar.text_input(
"Path ke file ZIP lokal:",
placeholder="contoh: /path/to/data.zip",
help="Masukkan path lengkap ke file ZIP di sistem lokal",
)
if zip_path and st.sidebar.button("๐ Load File Lokal"):
if os.path.exists(zip_path):
with st.spinner("Memuat file lokal..."):
try:
with zipfile.ZipFile(zip_path, "r") as zip_file:
with tempfile.TemporaryDirectory() as temp_dir:
zip_file.extractall(temp_dir)
shp_files = [
f
for f in os.listdir(temp_dir)
if f.endswith(".shp")
]
if shp_files:
shp_path = os.path.join(temp_dir, shp_files[0])
gdf = gpd.read_file(shp_path)
st.sidebar.success(
f"โ
File lokal berhasil dimuat: {len(gdf)} features"
)
else:
st.sidebar.error(
"File shapefile tidak ditemukan dalam ZIP"
)
except Exception as e:
st.sidebar.error(f"Error memuat file lokal: {str(e)}")
else:
st.sidebar.error("File tidak ditemukan!")
else: # Download dari URL
data_url = st.sidebar.text_input(
"URL Data GeoServer:",
value="http://geoportal.jogjakota.go.id/geoserver/wms?service=WFS&version=1.0.0&request=GetFeature&typeName=Dispertaru:jaringan_listrik_347120201026134638&outputFormat=shape-zip",
)
if st.sidebar.button("๐ Download dari URL"):
gdf = safe_url_download(data_url)
if gdf is not None:
st.sidebar.success(f"โ
Data berhasil diunduh: {len(gdf)} features")
# Konfigurasi visualisasi
st.sidebar.markdown("### ๐จ Pengaturan Visualisasi")
show_labels = st.sidebar.checkbox(
"Tampilkan Label Node",
value=True,
help="Menampilkan ID node pada visualisasi graf",
)
show_edge_details = st.sidebar.checkbox(
"Tampilkan Detail Edge",
value=False,
help="Menampilkan informasi detail tentang saluran listrik",
)
# Pengaturan tampilan label
with st.sidebar.expander("๐ท๏ธ Pengaturan Label Node"):
label_size = st.slider(
"Ukuran Label", 6, 16, 10, help="Ukuran font untuk label node"
)
label_color = st.selectbox(
"Warna Label",
["white", "black", "red", "blue", "green"],
index=0,
help="Warna teks label node",
)
# Pengaturan edge paralel
with st.sidebar.expander("๐ Pengaturan Saluran Paralel"):
edge_offset = st.slider(
"Jarak Antar Saluran Paralel",
0.01,
0.05,
0.02,
0.005,
help="Mengatur jarak visual antar saluran paralel",
)
show_edge_colors = st.checkbox(
"Warna Berbeda untuk Saluran Paralel",
value=True,
help="Memberikan warna berbeda untuk setiap saluran paralel",
)
# Add performance settings
with st.sidebar.expander("โ๏ธ Pengaturan Performa"):
max_nodes_viz = st.slider("Max nodes untuk visualisasi", 50, 1000, 500)
use_cache = st.checkbox("Gunakan cache untuk analisis", value=True)
# Proses data jika sudah dimuat
if gdf is not None and not gdf.empty:
if (
st.sidebar.button("๐ Analisis Data", type="primary")
or "gdf" not in st.session_state
):
with st.spinner("Memproses analisis graf..."):
try:
st.session_state["gdf"] = gdf
# Buat graf jaringan
G, nodes, gdf_utm, line_segments = create_network_graph(gdf)
# Limit nodes for visualization if too many
if G.number_of_nodes() > max_nodes_viz:
st.warning(
f"โ ๏ธ Graf memiliki {G.number_of_nodes()} nodes. Visualisasi dibatasi pada {max_nodes_viz} nodes untuk performa."
)
# Create subgraph with highest degree nodes for visualization
degrees = dict(G.degree())
top_nodes = sorted(
degrees.keys(), key=lambda x: degrees[x], reverse=True
)[:max_nodes_viz]
G_viz = G.subgraph(top_nodes).copy()
st.session_state["G_viz"] = G_viz
else:
st.session_state["G_viz"] = G
st.session_state["G"] = G
st.session_state["nodes"] = nodes
st.session_state["gdf_utm"] = gdf_utm
st.session_state["line_segments"] = line_segments
# Hitung ukuran sentralitas
centrality_measures = calculate_centrality_measures(G)
st.session_state["centrality"] = centrality_measures
# Analisis konektivitas
connectivity_analysis = analyze_network_connectivity(
G, line_segments
)
st.session_state["connectivity"] = connectivity_analysis
# MST
mst = find_minimum_spanning_tree(G)
st.session_state["mst"] = mst
st.success("โ
Data berhasil diproses!")
except Exception as e:
st.error(f"โ Error dalam analisis: {str(e)}")
st.info(
"Silakan coba dengan file data yang berbeda atau periksa format data."
)
# Tampilkan hasil jika data sudah dimuat
if "gdf" in st.session_state:
gdf = st.session_state["gdf"]
G = st.session_state["G"]
G_viz = st.session_state.get("G_viz", G)
centrality_measures = st.session_state["centrality"]
connectivity_analysis = st.session_state["connectivity"]
mst = st.session_state["mst"]
# Tab layout
tab1, tab2, tab3, tab4, tab5 = st.tabs(
[
"๐ Overview",
"๐บ๏ธ Peta Jaringan",
"๐ Analisis Graf",
"๐ฏ Sentralitas",
"๐ณ MST Analysis",
]
)
with tab1:
st.markdown(
'<h2 class="sub-header">Overview Data dan Statistik Jaringan</h2>',
unsafe_allow_html=True,
)
# Informasi dasar dataset
st.markdown("### ๐ Informasi Dataset")
col1, col2, col3 = st.columns(3)
with col1:
st.info(f"**CRS:** {gdf.crs}")
with col2:
st.info(
f"**Geometri:** {gdf.geometry.geom_type.iloc[0] if not gdf.empty else 'N/A'}"
)
with col3:
unique_names = gdf["nama"].nunique() if "nama" in gdf.columns else 1
st.info(f"**Jenis Jaringan:** {unique_names}")
# Metrics utama
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Jumlah Fitur", len(gdf))
with col2:
st.metric("Jumlah Node", connectivity_analysis["num_nodes"])
with col3:
st.metric("Jumlah Edge", connectivity_analysis["num_edges"])
with col4:
st.metric("Komponen Terhubung", connectivity_analysis["num_components"])
# Metrics jaringan fisik
if "total_network_length_km" in connectivity_analysis:
st.markdown("### ๐ Statistik Panjang Jaringan")
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric(
"Total Panjang",
f"{connectivity_analysis['total_network_length_km']:.2f} km",
)
with col2:
st.metric(
"Rata-rata Segmen",
f"{connectivity_analysis['avg_segment_length_km']:.3f} km",
)
with col3:
st.metric(
"Segmen Terpanjang",
f"{connectivity_analysis['longest_segment_km']:.3f} km",
)
with col4:
st.metric(
"Segmen Terpendek",
f"{connectivity_analysis['shortest_segment_km']:.3f} km",
)
# Statistik detail
st.markdown("### ๐ Statistik Detail Jaringan")
col1, col2 = st.columns(2)
with col1:
st.markdown("**Konektivitas:**")
st.write(
f"- Jaringan Terhubung: {'โ
Ya' if connectivity_analysis['is_connected'] else 'โ Tidak'}"
)
st.write(f"- Densitas Graf: {connectivity_analysis['density']:.4f}")
st.write(f"- Diameter: {connectivity_analysis['diameter']}")
st.write(
f"- Rata-rata Panjang Jalur: {connectivity_analysis['average_path_length']}"
)
with col2:
st.markdown("**Statistik Degree:**")
st.write(
f"- Rata-rata Degree: {connectivity_analysis['avg_degree']:.2f}"
)
st.write(f"- Maximum Degree: {connectivity_analysis['max_degree']}")
st.write(f"- Minimum Degree: {connectivity_analysis['min_degree']}")
# Analisis komponen terpisah jika ada
if connectivity_analysis["num_components"] > 1:
st.markdown("### โ ๏ธ Analisis Komponen Terpisah")
components = list(nx.connected_components(G))
component_sizes = [len(comp) for comp in components]
col1, col2 = st.columns(2)
with col1:
st.write(f"- Komponen Terbesar: {max(component_sizes)} nodes")
st.write(f"- Komponen Terkecil: {min(component_sizes)} nodes")
with col2:
st.write(
f"- Rata-rata Ukuran: {np.mean(component_sizes):.1f} nodes"
)
st.write(f"- Komponen Singleton: {component_sizes.count(1)}")
# Rekomendasi untuk menghubungkan komponen
st.markdown("**๐ก Rekomendasi:**")
st.write("- Periksa gap fisik antar segmen jaringan")
st.write("- Pertimbangkan menambah saluran penghubung")
st.write(
f"- {len([s for s in component_sizes if s == 1])} node terisolasi perlu perhatian"
)
# Tampilkan sample data
st.markdown("### ๐ Sample Data")
# Tampilkan kolom yang relevan
display_cols = (
["id", "nama"]
if all(col in gdf.columns for col in ["id", "nama"])
else gdf.columns.tolist()
)
st.dataframe(gdf[display_cols].head(10))
with tab2:
st.markdown(
'<h2 class="sub-header">Peta Jaringan Listrik</h2>',
unsafe_allow_html=True,
)
# Buat peta
try:
map_viz = create_map_visualization(gdf)
if map_viz:
st_folium(map_viz, width=700, height=500)
else:
st.error("Tidak dapat membuat visualisasi peta")
except Exception as e:
st.error(f"Error creating map: {str(e)}")
with tab3:
st.markdown(
'<h2 class="sub-header">Visualisasi Graf Jaringan</h2>',
unsafe_allow_html=True,
)
# Performance warning
if G.number_of_nodes() > max_nodes_viz:
st.info(
f"โน๏ธ Menampilkan {max_nodes_viz} node dengan degree tertinggi dari total {G.number_of_nodes()} nodes"
)
# Kontrol visualisasi tambahan
col1, col2, col3 = st.columns(3)
with col1:
if show_edge_details:
st.info(
"โน๏ธ Mode Detail Edge: Hover pada garis untuk melihat detail saluran"
)
with col2:
if show_labels:
st.info("โน๏ธ Mode Label: ID node ditampilkan pada graf")
with col3:
# Fitur pencarian node
search_node = st.text_input(
"๐ Cari Node:",
placeholder="Masukkan ID node (contoh: 13, 83, 154)",
help="Masukkan ID node untuk mencari informasi detail",
)
if search_node:
try:
node_id = int(search_node)
if node_id in G.nodes():
neighbors = list(G.neighbors(node_id))
degree_nx = G.degree(node_id)
# Hitung manual untuk debugging (exclude self-loop dari neighbors)
total_edges_manual = 0
edge_details = []
actual_neighbors = [
n for n in neighbors if n != node_id
] # Exclude self
for neighbor in actual_neighbors:
edge_count = G.number_of_edges(node_id, neighbor)
total_edges_manual += edge_count
edge_details.append(f"โ {neighbor} ({edge_count} edge)")
# Tambahkan self-loop secara terpisah jika ada
if G.has_edge(node_id, node_id):
self_edge_count = G.number_of_edges(node_id, node_id)
edge_details.append(
f"โ {node_id} (SELF-LOOP: {self_edge_count} edge)"
)
total_edges_manual += self_edge_count
st.success(f"โ
Node {node_id} ditemukan!")
st.write(f"โข **Degree (NetworkX)**: {degree_nx}")
st.write(
f"โข **Total Edges (Manual)**: {total_edges_manual}"
)
st.write(
f"โข **Jumlah Tetangga Sebenarnya**: {len(actual_neighbors)}"
)
st.write(
f"โข **Neighbors dari NetworkX**: {len(neighbors)} (mungkin termasuk self)"
)
# Debugging mendalam untuk edge
has_self_loop_search = G.has_edge(node_id, node_id)
self_loop_adjustment = 1 if has_self_loop_search else 0
expected_degree = total_edges_manual
# Debug: Lihat semua edge yang terhubung ke node ini
st.write("**๐ Debug - Semua Edge yang Terhubung:**")
all_edges = []
# Metode 1: Dari G.edges()
for edge in G.edges(node_id, data=True):
all_edges.append(
f"Edge: {edge[0]} โ {edge[1]} (data: {edge[2]})"
)
# Metode 2: Cek degree calculation NetworkX
degree_dict = dict(G.degree([node_id]))
st.write(f"โข NetworkX degree calculation: {degree_dict}")
# Metode 3: Manual count semua edges
manual_degree = 0
for neighbor in G.neighbors(node_id):
edge_count = G.number_of_edges(node_id, neighbor)
manual_degree += edge_count
st.write(f"โข To {neighbor}: {edge_count} edge(s)")
st.write(f"โข **Manual degree total**: {manual_degree}")
st.write(f"โข **NetworkX degree**: {degree_nx}")
st.write(f"โข **Difference**: {degree_nx - manual_degree}")
if all_edges:
st.write("**All edges from G.edges():**")
for edge in all_edges:
st.write(f" {edge}")
if degree_nx != manual_degree:
st.error("โ ๏ธ **NETWORKX BUG DETECTED!**")
st.write("**Analysis:**")
st.write(f"- Manual count (CORRECT): {manual_degree}")
st.write(f"- NetworkX degree (WRONG): {degree_nx}")
st.write(f"- Difference: +{degree_nx - manual_degree}")
st.write(
f"- Self-loop present: {'Yes' if has_self_loop_search else 'No'}"
)
st.write("**Root Cause:**")
st.write(
"- NetworkX internal bug with self-loop counting"
)
st.write("- Graf construction issue")
st.write("- Use manual count as the correct value")
st.success(
f"โ
**CORRECTED**: Node {node_id} has {manual_degree} connections"
)
elif has_self_loop_search:
st.info(
"โน๏ธ **Self-loop detected** (counted as +1 degree)"
)
else:
st.success("โ
**All calculations consistent!**")
st.write("**Detail Koneksi:**")
for detail in edge_details[:8]:
st.write(f" {detail}")
if len(edge_details) > 8:
st.write(f" ... dan {len(edge_details) - 8} lainnya")
else:
st.warning(f"โ Node {node_id} tidak ditemukan dalam graf")
# Tampilkan beberapa node yang tersedia untuk referensi
available_nodes = sorted(list(G.nodes()))[:10]
st.info(f"๐ก Contoh node yang tersedia: {available_nodes}")
except ValueError:
st.warning("โ ๏ธ Masukkan angka yang valid")
# Info tambahan tentang node
st.markdown("### ๐ Informasi Node")
col1, col2, col3 = st.columns(3)
with col1:
total_nodes = G.number_of_nodes()
st.metric("Total Node", total_nodes)
with col2:
if total_nodes > 0:
min_node = min(G.nodes())
max_node = max(G.nodes())
st.metric("Range Node ID", f"{min_node} - {max_node}")
with col3:
# Tampilkan beberapa node dengan degree tertinggi
if G.number_of_nodes() > 0:
top_degree_nodes = sorted(
G.degree(), key=lambda x: x[1], reverse=True
)[:3]
top_nodes_str = ", ".join(
[str(node) for node, _ in top_degree_nodes]
)
st.metric("Top 3 Node (Degree)", top_nodes_str)
# Visualisasi graf
try:
network_fig = create_network_visualization(
G_viz,
st.session_state["nodes"],
centrality_measures,
show_labels,
show_edge_details,
label_size,
label_color,
edge_offset,
show_edge_colors,
)
st.plotly_chart(network_fig, use_container_width=True)
except Exception as e:
st.error(f"Error creating network visualization: {str(e)}")
# Detail Koneksi Node
st.markdown("### ๐ Detail Koneksi Node")
try:
connection_df = create_node_connection_details(G_viz, top_n=20)
if not connection_df.empty:
st.markdown(
"""
**Penjelasan Kolom:**
- **Total Edges**: Jumlah total saluran yang terhubung ke node
- **Jumlah Tetangga**: Jumlah node lain yang terhubung langsung
- **Rasio Edge/Tetangga**: Rata-rata saluran per tetangga (>1 = ada saluran paralel)
"""
)
# Highlight nodes dengan multiple edges
def highlight_multiple_edges(df):
def color_ratio(val):
try:
ratio = float(val)
if ratio > 1.5:
return "background-color: #ffcccc; font-weight: bold" # Merah muda untuk rasio tinggi
elif ratio > 1.0:
return "background-color: #fff2cc" # Kuning untuk rasio sedang
else:
return ""
except:
return ""
return df.style.applymap(
color_ratio, subset=["Rasio Edge/Tetangga"]
)
st.dataframe(
highlight_multiple_edges(connection_df),
use_container_width=True,
height=400,
)
# Analisis tambahan
high_ratio_nodes = connection_df[
connection_df["Rasio Edge/Tetangga"].astype(float) > 1.0
]
if not high_ratio_nodes.empty:
st.markdown("### ๐ Analisis Saluran Paralel")
st.info(
f"Ditemukan {len(high_ratio_nodes)} node dengan saluran paralel (rasio > 1.0)"
)
for _, row in high_ratio_nodes.head(5).iterrows():
st.write(
f"โข **Node {row['Node']}**: {row['Total Edges']} saluran ke {row['Jumlah Tetangga']} tetangga (rasio: {row['Rasio Edge/Tetangga']})"
)
else:
st.warning("Tidak ada data koneksi untuk ditampilkan")
except Exception as e:
st.error(f"Error creating connection details: {str(e)}")
# Informasi graf
st.markdown("### ๐ Interpretasi Graf")
st.markdown(
"""
- **Node (Simpul)**: Merepresentasikan titik-titik penting dalam jaringan (gardu, pembangkit, junction)
- **Edge (Sisi)**: Merepresentasikan saluran listrik yang menghubungkan antar titik
- **Warna Node**: Intensitas warna menunjukkan tingkat kepentingan berdasarkan Degree Centrality
- **Node dengan warna lebih gelap**: Memiliki lebih banyak koneksi (lebih kritis)
- **Saluran Paralel**: Node dengan rasio Edge/Tetangga > 1 memiliki multiple saluran ke tetangga yang sama
"""
)
with tab4:
st.markdown(
'<h2 class="sub-header">Analisis Sentralitas</h2>',
unsafe_allow_html=True,
)
# Perbandingan sentralitas
try:
centrality_fig = create_centrality_comparison(centrality_measures)
st.plotly_chart(centrality_fig, use_container_width=True)
except Exception as e:
st.error(f"Error creating centrality comparison: {str(e)}")
# Matriks Sentralitas
st.markdown("### ๐ Matriks Nilai Sentralitas")
try:
centrality_df = create_centrality_matrix(centrality_measures)
if not centrality_df.empty:
# Tampilkan statistik ringkas
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Total Node", len(centrality_df))
with col2:
st.metric(
"Max Degree Centrality",
f"{centrality_df['Degree Centrality'].max():.4f}",
)
with col3:
st.metric(
"Max Betweenness",
f"{centrality_df['Betweenness Centrality'].max():.4f}",
)
with col4:
st.metric(
"Max Closeness",
f"{centrality_df['Closeness Centrality'].max():.4f}",
)
# Opsi untuk menampilkan semua data atau hanya top N
display_option = st.radio(
"Pilih tampilan data:",
["Top 20 Node", "Top 50 Node", "Semua Node"],
horizontal=True,
)
if display_option == "Top 20 Node":
display_df = centrality_df.head(20)
elif display_option == "Top 50 Node":
display_df = centrality_df.head(50)
else:
display_df = centrality_df
# Tampilkan tabel dengan styling dan color coding
def highlight_values(df):
"""Apply color coding to centrality values"""
styled_df = df.style
# Color coding untuk setiap kolom centrality
centrality_cols = [
"Degree Centrality",
"Closeness Centrality",
"Betweenness Centrality",
"Eigenvector Centrality",
]
# Color mapping untuk setiap kolom dengan warna berbeda
color_maps = {
"Degree Centrality": "Reds", # Merah
"Closeness Centrality": "Blues", # Biru
"Betweenness Centrality": "Greens", # Hijau
"Eigenvector Centrality": "Purples", # Ungu
}
for col in centrality_cols:
if col in df.columns:
# Gradient color berbeda untuk setiap kolom
styled_df = styled_df.background_gradient(
subset=[col],
cmap=color_maps[col],
vmin=0,
vmax=df[col].max(),
)
# Format angka dengan 6 desimal
format_dict = {}
for col in centrality_cols:
if col in df.columns:
format_dict[col] = "{:.6f}"
styled_df = styled_df.format(format_dict)
# Styling tambahan
styled_df = styled_df.set_properties(
**{"font-weight": "bold", "text-align": "center"},
subset=["Node"],
)
# Highlight top 5 nodes dengan border tebal
top_5_indices = df.head(5).index
styled_df = styled_df.set_properties(
**{"border": "3px solid #ff6b6b", "font-weight": "bold"},
subset=pd.IndexSlice[top_5_indices, :],
)
return styled_df
# Tampilkan legend untuk color coding
st.markdown(
"""
**๐ Keterangan Visualisasi (Warna per Kolom):**
- ๐ฅ **Degree Centrality**: Gradasi Merah (putih โ merah gelap)
- ๐ฆ **Closeness Centrality**: Gradasi Biru (putih โ biru gelap)
- ๐ฉ **Betweenness Centrality**: Gradasi Hijau (putih โ hijau gelap)
- ๐ช **Eigenvector Centrality**: Gradasi Ungu (putih โ ungu gelap)
- ๐ด **Border Merah Tebal**: Top 5 node paling penting
*Semakin gelap warna = semakin tinggi nilai sentralitas*
"""
)
# Tampilkan tabel dengan styling
st.dataframe(
highlight_values(display_df),
use_container_width=True,
height=400,
)
# Informasi tambahan tentang interpretasi
with st.expander("โน๏ธ Cara Membaca Matriks Sentralitas"):
st.markdown(
"""
**Interpretasi Nilai Sentralitas:**
1. **Degree Centrality (0-1)**:
- Mengukur jumlah koneksi langsung
- Nilai tinggi = node dengan banyak koneksi
2. **Closeness Centrality (0-1)**:
- Mengukur kedekatan ke semua node lain
- Nilai tinggi = node yang mudah dijangkau dari mana saja
3. **Betweenness Centrality (0-1)**:
- Mengukur seberapa sering node berada di jalur terpendek
- Nilai tinggi = node yang berperan sebagai jembatan penting
4. **Eigenvector Centrality (0-1)**:
- Mengukur pengaruh berdasarkan kualitas koneksi
- Nilai tinggi = node yang terhubung ke node-node penting lainnya
**Tips Analisis:**
- Node dengan nilai tinggi di semua kategori = **Super Critical**
- Node dengan Betweenness tinggi = **Bottleneck** potensial
- Node dengan Degree tinggi tapi Eigenvector rendah = **Hub** lokal
"""
)
# Tombol download CSV
csv = centrality_df.to_csv(index=False)
st.download_button(
label="๐ฅ Download Matriks Sentralitas (CSV)",
data=csv,
file_name="centrality_matrix.csv",
mime="text/csv",
)
else:
st.warning("Tidak ada data sentralitas untuk ditampilkan")
except Exception as e:
st.error(f"Error creating centrality matrix: {str(e)}")
# Identifikasi node kritis
st.markdown("### ๐ฏ Identifikasi Node Kritis")
if centrality_measures.get("degree"):
# Top nodes berdasarkan degree centrality
degree_sorted = sorted(
centrality_measures["degree"].items(),
key=lambda x: x[1],
reverse=True,
)
top_nodes = degree_sorted[:5]
st.markdown("**Top 5 Node Berdasarkan Degree Centrality:**")
for i, (node, centrality) in enumerate(top_nodes, 1):
st.write(f"{i}. Node {node}: {centrality:.4f}")
# Rekomendasi berdasarkan analisis statistik
st.markdown("### ๐ก Rekomendasi Berbasis Data")
if top_nodes and centrality_measures.get("degree"):
# Analisis statistik degree centrality
degree_values = list(centrality_measures["degree"].values())
mean_degree = np.mean(degree_values)
std_degree = np.std(degree_values)
q75 = np.percentile(degree_values, 75)
q90 = np.percentile(degree_values, 90)
q95 = np.percentile(degree_values, 95)
# Threshold berdasarkan statistik
critical_threshold = q90 # Top 10%
high_priority_threshold = q75 # Top 25%
# Hitung jumlah node per kategori
critical_nodes = [
node
for node, cent in centrality_measures["degree"].items()
if cent >= critical_threshold
]
high_priority_nodes = [
node
for node, cent in centrality_measures["degree"].items()
if cent >= high_priority_threshold and cent < critical_threshold
]
st.markdown("#### ๐ Analisis Statistik Degree Centrality")
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Mean", f"{mean_degree:.4f}")
with col2:
st.metric("Std Dev", f"{std_degree:.4f}")
with col3:
st.metric("75th Percentile", f"{q75:.4f}")
with col4:
st.metric("90th Percentile", f"{q90:.4f}")
st.markdown(
f"""
**Node Paling Kritis:** Node {top_nodes[0][0]} (Degree Centrality: {top_nodes[0][1]:.4f})
**Rekomendasi Kebijakan Berbasis Data:**
1. **๐ด Monitoring Kritis** (โฅ {critical_threshold:.4f} - Top 10%):
- **{len(critical_nodes)} node** memerlukan monitoring 24/7
- Sistem backup dan redundansi wajib
- Maintenance preventif bulanan
2. **๐ก Monitoring Prioritas** ({high_priority_threshold:.4f} - {critical_threshold:.4f} - Top 25%):
- **{len(high_priority_nodes)} node** monitoring reguler
- Maintenance preventif triwulanan
- Rencana contingency tersedia
3. **๐ข Monitoring Standar** (< {high_priority_threshold:.4f}):
- Monitoring rutin sesuai jadwal normal
- Maintenance tahunan
**Basis Ilmiah:**
- Threshold berdasarkan distribusi statistik data aktual
- Top 10% (90th percentile) untuk monitoring kritis
- Top 25% (75th percentile) untuk monitoring prioritas
- Menggunakan analisis risiko berbasis data, bukan nilai arbitrary
"""
)
with tab5:
st.markdown(
'<h2 class="sub-header">Minimum Spanning Tree Analysis</h2>',
unsafe_allow_html=True,
)
if mst.number_of_nodes() > 0:
# Perhitungan dasar
total_weight_mst = sum(
[data["weight"] for _, _, data in mst.edges(data=True)]
)
original_weight = sum(
[data["weight"] for _, _, data in G.edges(data=True)]
)
savings = original_weight - total_weight_mst
efficiency = (
(savings / original_weight * 100) if original_weight > 0 else 0
)
# Tampilkan metrics
col1, col2 = st.columns(2)
with col1:
st.metric("Total Bobot MST", f"{total_weight_mst:.2f}m")
st.metric("Jumlah Edge MST", mst.number_of_edges())
with col2:
st.metric("Efisiensi", f"{efficiency:.2f}%")
st.metric("Penghematan", f"{savings:.2f}m")
# Proses Perhitungan Detail
st.markdown("### ๐งฎ Proses Perhitungan Efisiensi dan Penghematan")
with st.expander("๐ Detail Perhitungan Langkah demi Langkah"):
st.markdown("#### 1๏ธโฃ **Perhitungan Total Bobot Jaringan Asli**")
st.code(
f"""
# Formula: ฮฃ(weight_i) untuk semua edge dalam graf asli
original_edges = {G.number_of_edges()} edge
original_weight = ฮฃ(weight_i) = {original_weight:.2f} meter
"""
)
st.markdown("#### 2๏ธโฃ **Perhitungan Total Bobot MST**")
st.code(
f"""
# Formula: ฮฃ(weight_i) untuk edge dalam MST
mst_edges = {mst.number_of_edges()} edge
mst_weight = ฮฃ(weight_i) = {total_weight_mst:.2f} meter
"""
)
st.markdown("#### 3๏ธโฃ **Perhitungan Penghematan Absolut**")
st.code(
f"""
# Formula: Penghematan = Total_Asli - Total_MST
savings = {original_weight:.2f} - {total_weight_mst:.2f}
savings = {savings:.2f} meter
"""
)
st.markdown("#### 4๏ธโฃ **Perhitungan Efisiensi Relatif**")
st.code(
f"""
# Formula: Efisiensi = (Penghematan / Total_Asli) ร 100%
efficiency = ({savings:.2f} / {original_weight:.2f}) ร 100%
efficiency = {efficiency:.2f}%
"""
)
st.markdown("#### 5๏ธโฃ **Interpretasi Hasil**")
if efficiency > 50:
interpretation = (
"๐ด **Sangat Tinggi** - Jaringan asli sangat tidak efisien"
)
recommendation = "Pertimbangkan restrukturisasi besar-besaran"
elif efficiency > 30:
interpretation = (
"๐ก **Tinggi** - Ada potensi optimasi signifikan"
)
recommendation = "Evaluasi edge redundan untuk penghematan"
elif efficiency > 10:
interpretation = "๐ข **Sedang** - Jaringan cukup efisien"
recommendation = "Optimasi minor pada area tertentu"
else:
interpretation = "โ
**Rendah** - Jaringan sudah sangat efisien"
recommendation = "Pertahankan struktur existing"
st.markdown(
f"""
**Tingkat Efisiensi:** {interpretation}
**Rekomendasi:** {recommendation}
**Penjelasan:**
- **Efisiensi {efficiency:.2f}%** berarti MST dapat menghemat {efficiency:.2f}% dari total panjang kabel
- **Penghematan {savings:.2f}m** setara dengan {savings/1000:.3f} km kabel
- **Edge yang dihilangkan:** {G.number_of_edges() - mst.number_of_edges()} edge (redundan)
"""
)
# Analisis Biaya (opsional)
st.markdown("### ๐ฐ Analisis Biaya (Estimasi)")
col1, col2 = st.columns(2)
with col1:
cost_per_meter = st.number_input(
"Biaya per meter (Rp)",
min_value=0,
value=500000,
step=50000,
help="Estimasi biaya instalasi kabel per meter",
)
with col2:
if cost_per_meter > 0:
total_cost_original = original_weight * cost_per_meter
total_cost_mst = total_weight_mst * cost_per_meter
cost_savings = total_cost_original - total_cost_mst
st.metric(
"Biaya Jaringan Asli", f"Rp {total_cost_original:,.0f}"
)
st.metric("Biaya MST", f"Rp {total_cost_mst:,.0f}")
st.metric("Penghematan Biaya", f"Rp {cost_savings:,.0f}")
st.success(
f"๐ก **Insight**: Dengan MST, dapat menghemat **Rp {cost_savings:,.0f}** ({efficiency:.1f}%) dari biaya konstruksi!"
)
# Visualisasi MST
try:
mst_centrality = calculate_centrality_measures(mst)
mst_fig = create_network_visualization(
mst, st.session_state["nodes"], mst_centrality, show_labels
)
mst_fig.update_layout(
title=dict(
text="Minimum Spanning Tree - Jaringan Optimal",
font=dict(size=16),
)
)
st.plotly_chart(mst_fig, use_container_width=True)
except Exception as e:
st.error(f"Error creating MST visualization: {str(e)}")
st.markdown("### ๐ง Interpretasi MST dan Analisis Redundansi")
# Analisis Edge Redundan
st.markdown("#### ๐ Analisis Edge Redundan")
# Identifikasi edge redundan
mst_edges = set(mst.edges())
original_edges = set(G.edges())
redundant_edges = []
for edge in original_edges:
# Cek kedua arah karena edge tidak berarah
if edge not in mst_edges and (edge[1], edge[0]) not in mst_edges:
edge_data = G.get_edge_data(edge[0], edge[1])
if edge_data:
redundant_edges.append(
(edge[0], edge[1], edge_data["weight"])
)
# Hitung total bobot edge redundan
total_redundant_weight = sum(
[weight for _, _, weight in redundant_edges]
)
redundant_percentage = (
(total_redundant_weight / original_weight * 100)
if original_weight > 0
else 0
)
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Edge Redundan", f"{len(redundant_edges)}")
with col2:
st.metric("Bobot Redundan", f"{total_redundant_weight:.2f}m")
with col3:
st.metric("% Redundansi", f"{redundant_percentage:.2f}%")
# Detail perhitungan redundansi
with st.expander("๐งฎ Perhitungan Analisis Redundansi"):
st.markdown("#### 1๏ธโฃ **Identifikasi Edge Redundan**")
st.code(
f"""
# Edge dalam jaringan asli: {len(original_edges)}
# Edge dalam MST: {len(mst_edges)}
# Edge redundan = Edge_asli - Edge_MST
redundant_edges = {len(redundant_edges)}
"""
)
st.markdown("#### 2๏ธโฃ **Perhitungan Bobot Redundan**")
st.code(
f"""
# Formula: ฮฃ(weight_i) untuk edge yang tidak ada dalam MST
total_redundant_weight = ฮฃ(weight_redundant_i)
total_redundant_weight = {total_redundant_weight:.2f} meter
"""
)
st.markdown("#### 3๏ธโฃ **Persentase Redundansi**")
st.code(
f"""
# Formula: (Bobot_Redundan / Bobot_Total_Asli) ร 100%
redundancy_percentage = ({total_redundant_weight:.2f} / {original_weight:.2f}) ร 100%
redundancy_percentage = {redundant_percentage:.2f}%
"""
)
st.markdown("#### 4๏ธโฃ **Verifikasi Konsistensi**")
st.code(
f"""
# Verifikasi: MST_weight + Redundant_weight = Original_weight
{total_weight_mst:.2f} + {total_redundant_weight:.2f} = {total_weight_mst + total_redundant_weight:.2f}
Original weight: {original_weight:.2f}
Difference: {abs(original_weight - (total_weight_mst + total_redundant_weight)):.2f}m
"""
)
# Tampilkan beberapa edge redundan terbesar
if redundant_edges:
st.markdown("#### 5๏ธโฃ **Top 10 Edge Redundan Terpanjang**")
redundant_sorted = sorted(
redundant_edges, key=lambda x: x[2], reverse=True
)[:10]
redundant_df = pd.DataFrame(
redundant_sorted,
columns=["Node A", "Node B", "Panjang (m)"],
)
redundant_df["Panjang (km)"] = (
redundant_df["Panjang (m)"] / 1000
)
st.dataframe(redundant_df, use_container_width=True)
# Interpretasi berdasarkan tingkat redundansi
st.markdown("#### ๐ Interpretasi Tingkat Redundansi")
if redundant_percentage > 40:
redundancy_level = "๐ด **Sangat Tinggi**"
redundancy_meaning = "Jaringan memiliki banyak jalur alternatif"
redundancy_action = "Pertimbangkan untuk mengurangi edge redundan pada fase konstruksi baru"
elif redundant_percentage > 25:
redundancy_level = "๐ก **Tinggi**"
redundancy_meaning = (
"Jaringan memiliki redundansi yang baik untuk keandalan"
)
redundancy_action = (
"Evaluasi cost-benefit antara redundansi dan efisiensi"
)
elif redundant_percentage > 10:
redundancy_level = "๐ข **Sedang**"
redundancy_meaning = "Tingkat redundansi optimal untuk keseimbangan efisiensi-keandalan"
redundancy_action = "Pertahankan tingkat redundansi saat ini"
else:
redundancy_level = "โ ๏ธ **Rendah**"
redundancy_meaning = (
"Jaringan mendekati struktur minimal (seperti MST)"
)
redundancy_action = (
"Pertimbangkan menambah redundansi untuk meningkatkan keandalan"
)
st.markdown(
f"""
**Tingkat Redundansi:** {redundancy_level} ({redundant_percentage:.1f}%)
**Makna:** {redundancy_meaning}
**Rekomendasi:** {redundancy_action}
**Analisis Teknis:**
- **Edge redundan:** {len(redundant_edges)} dari {len(original_edges)} total edge
- **Bobot redundan:** {total_redundant_weight:.2f}m ({total_redundant_weight/1000:.3f} km)
- **Fungsi redundansi:** Menyediakan jalur alternatif jika terjadi gangguan
- **Trade-off:** Redundansi โ = Keandalan โ, Efisiensi โ
"""
)
# Kesimpulan MST
st.markdown("#### ๐ฏ Kesimpulan MST Analysis")
st.markdown(
f"""
**Ringkasan Analisis:**
1. **Efisiensi Jaringan:** {efficiency:.1f}% - MST dapat menghemat {efficiency:.1f}% dari total panjang kabel
2. **Redundansi Jaringan:** {redundant_percentage:.1f}% - {redundant_percentage:.1f}% dari jaringan bersifat redundan
3. **Optimasi Potensial:** {len(redundant_edges)} edge dapat dievaluasi untuk penghematan
4. **Keseimbangan:** Pertimbangkan trade-off antara efisiensi (MST) dan keandalan (redundansi)
**Aplikasi Praktis:**
- **Perencanaan Baru:** Gunakan MST sebagai baseline minimum
- **Optimasi Existing:** Evaluasi edge redundan untuk cost reduction
- **Maintenance:** Prioritaskan edge MST untuk pemeliharaan kritis
- **Expansion:** Tambahkan edge di luar MST untuk meningkatkan redundansi
"""
)
else:
st.warning(
"Tidak dapat membuat MST - graf mungkin tidak terhubung atau kosong"
)
else:
st.info("๐ Pilih sumber data dan klik tombol untuk memulai analisis")
# Tampilkan informasi tentang format data yang didukung
st.markdown("## ๐ Format Data yang Didukung")
col1, col2, col3 = st.columns(3)
with col1:
st.markdown(
"""
**๐ Upload File ZIP**
- Format: ZIP berisi shapefile
- Komponen: .shp, .shx, .dbf, .prj
- Ukuran maksimal: 200MB
- Paling mudah dan cepat
"""
)
with col2:
st.markdown(
"""
**๐พ File Lokal**
- Format: ZIP di sistem lokal
- Path: Absolut atau relatif
- Cocok untuk development
- Akses file sistem langsung
"""
)
with col3:
st.markdown(
"""
**๐ Download URL**
- Format: WFS GeoServer
- Output: shape-zip
- Real-time data
- Memerlukan koneksi internet
"""
)
# Tampilkan informasi metodologi
st.markdown("## ๐ Metodologi")
st.markdown(
"""
### Pendekatan Teori Graf untuk Analisis Jaringan Listrik:
1. **Pemodelan Graf**:
- Node = Gardu listrik, pembangkit, junction
- Edge = Saluran transmisi/distribusi listrik
- Weight = Panjang saluran atau impedansi
2. **Analisis Sentralitas**:
- **Degree Centrality**: Jumlah koneksi langsung
- **Betweenness Centrality**: Peran sebagai jembatan antar node
- **Closeness Centrality**: Kedekatan rata-rata ke semua node lain
- **Eigenvector Centrality**: Pengaruh berdasarkan kualitas koneksi
3. **Minimum Spanning Tree (MST)**:
- Menggunakan algoritma Kruskal
- Mencari jaringan dengan bobot minimum yang tetap terhubung
- Optimasi biaya konstruksi/operasional
4. **Analisis Konektivitas**:
- Identifikasi komponen terhubung
- Perhitungan diameter dan average path length
- Analisis keandalan jaringan
"""
)
# Troubleshooting section
st.markdown("## ๐ง Troubleshooting")
st.markdown(
"""
**Jika mengalami masalah:**
1. **File tidak terbaca**: Pastikan file ZIP berisi shapefile lengkap (.shp, .shx, .dbf, .prj)
2. **Error permission**: Aplikasi otomatis menangani permission di server
3. **Timeout download**: Coba gunakan upload file jika download dari URL gagal
4. **Visualisasi lambat**: Gunakan pengaturan performa untuk membatasi jumlah node
5. **Memory error**: Coba dengan dataset yang lebih kecil
"""
)
if __name__ == "__main__":
try:
main()
except Exception as e:
st.error(f"๐ฅ Application Error: {str(e)}")
st.info(
"Silakan refresh halaman atau hubungi administrator jika masalah berlanjut."
)
|