views.py 50 KB
Newer Older
Muhamad's avatar
Muhamad committed
1 2 3
from django.http import response
from API.views import jalan
from django.contrib.auth.models import User, Group
Manggar Mahardhika's avatar
Manggar Mahardhika committed
4 5 6
from django.shortcuts import render, redirect
# from django.views.generic import ListView, DetailView,  DeleteView, UpdateView, CreateView
from django.views import View, generic
Muhamad's avatar
Muhamad committed
7 8
from django.contrib.auth.decorators import login_required, user_passes_test
from django.utils.decorators import method_decorator
Manggar Mahardhika's avatar
Manggar Mahardhika committed
9
from django.db import connection as conn
Muhamad's avatar
Muhamad committed
10
# from edit import *
Manggar Mahardhika's avatar
Manggar Mahardhika committed
11 12 13

class Dashboard(generic.TemplateView):
    template_name = "layout/layout.html"
Muhamad's avatar
Muhamad committed
14
    # @method_decorator(login_required(login_url='/login/'))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
15
    def get(self, request):
Muhamad's avatar
Muhamad committed
16
        # print(Group.objects.get(user=request.user))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
17 18 19 20
        with conn.cursor() as all_:
            all_.execute("SELECT * from global_oku")
            all = all_.fetchall()
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
21
        with conn.cursor() as kel:
Muhamad's avatar
Muhamad committed
22
            kel.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (kode_desa, desa, kecamatan, kab_kota, provinsi, jumlah_pen, jumlah_kk, luas_desa, gambar),'penduduk', (pria, wanita, belum_kawin, kawin, cerai_hidup, cerai_mati, wajib_ktp, islam, kristen, khatolik, hindu, budha, konghucu, kepercayaan_lain, u0, u5, u10, u15, u20, u25, u30, u35, u40, u45, u50, u55, u60, u65, u70, u75),'pekerjaan_pendidikan', (tidak_sekolah, belum_tamat, tamat_sd, sltp, slta, diploma_i, diploma_ii, diploma_iv, strata_ii, strata_iii, tidak_bekerja, aparatur_pemerintah, tenaga_pendidik, wiraswasta, pertanian, tenaga_kesehatan, pensiunan, pegawai, tentara, kepolisian, pedagang, petani, peternak, nelayan, karyawan, buruh, pembantu, tukang, pendeta, pastor, ustadz, dosen, guru, pilot, pengacara, notaris, arsitek, akuntan, konsultan, dokter, bidan, perawat, psikiater, sopir, lainnya), 'poi',(jml_puskesma, jml_sekolah, jml_sarib),'geometry', ST_AsGeoJSON(geom :: geometry) :: json) from geo_data_kelurahan")
Manggar Mahardhika's avatar
Manggar Mahardhika committed
23 24 25
            kel_res_ = kel.fetchall()
        
        with conn.cursor() as kec:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
26
            kec.execute("SELECT kecamatan, json_build_object('type', 'Feature', 'administrasi', (id, kecamatan),'geometry', ST_AsGeoJSON(geom :: geometry) :: json) from geo_data_kecamatan")
Manggar Mahardhika's avatar
Manggar Mahardhika committed
27
            kec_res_ = kec.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
28
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
29 30 31
        # with conn.cursor() as knt:
        #     knt.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark),'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_kantor_administrasi a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
        #     knt_res = knt.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
32
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
33 34 35 36 37 38 39
        kec_res = []
        for i in kec_res_:
            kec_res.append(i[1])
        
        kel_res = []
        for i in kel_res_:
            kel_res.append(i[0])
Muhamad's avatar
Muhamad committed
40
            
Manggar Mahardhika's avatar
Manggar Mahardhika committed
41
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
42 43 44
        # kntl = []
        # for i in knt_res:
        #     kntl.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
45 46
        
        with conn.cursor() as kantor_administrasi:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
            kantor_administrasi.execute("""SELECT
                                            json_build_object (
                                                'type',
                                                'Feature',
                                                'administrasi',
                                                (
                                                    A.namobj,
                                                    A.remark,
                                                    A.alamat,
                                                    A.luas_bangunan,
                                                    A.luas_tanah,
                                                    A.satuan_luas_bangunan,
                                                    A.satuan_luas_tanah,
                                                    A.gambar,
                                                    A.tinggi_bangunan,
                                                    A.perangkat,
                                                    A.tipe_gambar,
                                                    b.kabupaten,
                                                    c.kecamatan,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
66 67
                                                    d.desa,
                                                    a.id
Manggar Mahardhika's avatar
Manggar Mahardhika committed
68 69 70 71 72 73 74 75
                                                ),
                                                'geometry',
                                                ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                            ) 
                                        FROM
                                            geo_kantor_administrasi A 
                                            JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
                                            JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
Muhamad's avatar
Muhamad committed
76 77
                                            join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                        WHERE a.is_deleted = 0""")
Manggar Mahardhika's avatar
Manggar Mahardhika committed
78 79 80 81 82 83 84
            kantor_administrasi_res = kantor_administrasi.fetchall()
            
        kantor_administrasi_result = []
        for i in kantor_administrasi_res:
            kantor_administrasi_result.append(i[0])
            
        with conn.cursor() as layanan_kesehatan:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
            layanan_kesehatan.execute("""SELECT
                                            json_build_object (
                                                'type',
                                                'Feature',
                                                'administrasi',
                                                (
                                                    A.namobj,
                                                    A.remark,
                                                    A.alamat,
                                                    A.luas_bangunan,
                                                    A.luas_tanah,
                                                    A.satuan_luas_bangunan,
                                                    A.satuan_luas_tanah,
                                                    A.gambar,
                                                    A.tinggi_bangunan,
                                                    A.perangkat,
                                                    A.tipe_gambar,
                                                    b.kabupaten,
                                                    c.kecamatan,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
104 105
                                                    d.desa,
                                                    a.id
Manggar Mahardhika's avatar
Manggar Mahardhika committed
106 107 108 109 110 111 112 113 114 115
                                                ),
                                                'geometry',
                                                ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                            ) 
                                        FROM
                                            geo_layanan_kesehatan A 
                                            JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
                                            JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
                                            join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                            """)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
116 117 118 119 120 121
            layanan_kesehatan_res = layanan_kesehatan.fetchall()
        
        layanan_kesehatan_result = []
        for i in layanan_kesehatan_res:
            layanan_kesehatan_result.append(i[0])
            
Manggar Mahardhika's avatar
Manggar Mahardhika committed
122
            
Manggar Mahardhika's avatar
Manggar Mahardhika committed
123
        with conn.cursor() as puskesmas:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            puskesmas.execute("""SELECT
                                    json_build_object (
                                        'type',
                                        'Feature',
                                        'administrasi',
                                        (
                                            A.namobj,
                                            A.remark,
                                            A.alamat,
                                            A.luas_bangunan,
                                            A.luas_tanah,
                                            A.satuan_luas_bangunan,
                                            A.satuan_luas_tanah,
                                            A.gambar,
                                            A.tinggi_bangunan,
                                            A.perangkat,
                                            A.tipe_gambar,
                                            b.kabupaten,
                                            c.kecamatan,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
143 144
                                            d.desa,
                                            a.id
Manggar Mahardhika's avatar
Manggar Mahardhika committed
145 146 147 148 149 150 151 152 153 154
                                        ),
                                        'geometry',
                                        ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                    ) 
                                FROM
                                    geo_puskesmas A 
                                    JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
                                    JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
                                    join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                    """)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
155 156 157 158 159 160 161
            puskesmas_res = puskesmas.fetchall()
        
        puskesmas_result = []
        for i in puskesmas_res:
            puskesmas_result.append(i[0])
            
        with conn.cursor() as rumah_sakit:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
            rumah_sakit.execute("""SELECT
                                        json_build_object (
                                            'type',
                                            'Feature',
                                            'administrasi',
                                            (
                                                A.namobj,
                                                A.remark,
                                                A.alamat,
                                                A.luas_bangunan,
                                                A.luas_tanah,
                                                A.satuan_luas_bangunan,
                                                A.satuan_luas_tanah,
                                                A.gambar,
                                                A.tinggi_bangunan,
                                                A.perangkat,
                                                A.tipe_gambar,
                                                b.kabupaten,
                                                c.kecamatan,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
181 182
                                                d.desa,
                                                a.id
Manggar Mahardhika's avatar
Manggar Mahardhika committed
183 184 185 186 187 188 189 190 191 192
                                            ),
                                            'geometry',
                                            ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                        ) 
                                    FROM
                                        geo_rumah_sakit A 
                                        JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
                                        JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
                                        join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                        """)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
193 194 195 196 197 198 199
            rumah_sakit_res = rumah_sakit.fetchall()
        
        rumah_sakit_result = []
        for i in rumah_sakit_res:
            rumah_sakit_result.append(i[0])
            
        with conn.cursor() as sarana_ibadah:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
            sarana_ibadah.execute("""SELECT
                                        json_build_object (
                                            'type',
                                            'Feature',
                                            'administrasi',
                                            (
                                                A.namobj,
                                                A.remark,
                                                A.alamat,
                                                A.luas_bangunan,
                                                A.luas_tanah,
                                                A.satuan_luas_bangunan,
                                                A.satuan_luas_tanah,
                                                A.gambar,
                                                A.tinggi_bangunan,
                                                A.perangkat,
                                                A.tipe_gambar,
                                                b.kabupaten,
                                                c.kecamatan,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
219 220
                                                d.desa,
                                                a.id
Manggar Mahardhika's avatar
Manggar Mahardhika committed
221 222 223 224 225 226 227 228 229 230
                                            ),
                                            'geometry',
                                            ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                        ) 
                                    FROM
                                        geo_sarana_ibadah A 
                                        JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
                                        JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
                                        join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                        """)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
231 232 233 234 235 236 237 238 239
            sarana_ibadah_res = sarana_ibadah.fetchall()
        
        sarana_ibadah_result = []
        for i in sarana_ibadah_res:
            if i[0]["administrasi"]['f1'] == None:
                i[0]["administrasi"]['f1'] = "None"
            sarana_ibadah_result.append(i[0])
            
        with conn.cursor() as sarana_pendidikan:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
            sarana_pendidikan.execute("""SELECT
                                            json_build_object (
                                                'type',
                                                'Feature',
                                                'administrasi',
                                                (
                                                    A.namobj,
                                                    A.remark,
                                                    A.alamat,
                                                    A.luas_bangunan,
                                                    A.luas_tanah,
                                                    A.satuan_luas_bangunan,
                                                    A.satuan_luas_tanah,
                                                    A.gambar,
                                                    A.tinggi_bangunan,
                                                    A.perangkat,
                                                    A.tipe_gambar,
                                                    b.kabupaten,
                                                    c.kecamatan,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
259 260
                                                    d.desa,
                                                    a.id
Manggar Mahardhika's avatar
Manggar Mahardhika committed
261 262 263 264 265 266 267 268 269 270
                                                ),
                                                'geometry',
                                                ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                            ) 
                                        FROM
                                            geo_sarana_pendidikan A 
                                            JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
                                            JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
                                            join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                            """)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
271 272 273 274 275 276 277 278 279
            sarana_pendidikan_res = sarana_pendidikan.fetchall()
        
        sarana_pendidikan_result = []
        for i in sarana_pendidikan_res:
            if i[0]["administrasi"]['f1'] == None:
                i[0]["administrasi"]['f1'] = "None"
            sarana_pendidikan_result.append(i[0])
            
        with conn.cursor() as spbu:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
            spbu.execute("""SELECT
                                json_build_object (
                                    'type',
                                    'Feature',
                                    'administrasi',
                                    (
                                        A.namobj,
                                        A.remark,
                                        A.alamat,
                                        A.luas_bangunan,
                                        A.luas_tanah,
                                        A.satuan_luas_bangunan,
                                        A.satuan_luas_tanah,
                                        A.gambar,
                                        A.tinggi_bangunan,
                                        A.perangkat,
                                        A.tipe_gambar,
                                        b.kabupaten,
                                        c.kecamatan,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
299 300
                                        d.desa,
                                        a.id
Manggar Mahardhika's avatar
Manggar Mahardhika committed
301 302 303 304 305 306 307 308 309 310
                                    ),
                                    'geometry',
                                    ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                ) 
                            FROM
                                geo_spbu A 
                                JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
                                JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
                                join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                """)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
311 312 313 314 315 316 317
            spbu_res = spbu.fetchall()
        
        spbu_result = []
        for i in spbu_res:
            spbu_result.append(i[0])
            
        with conn.cursor() as stasiun:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
            stasiun.execute("""SELECT
                                    json_build_object (
                                        'type',
                                        'Feature',
                                        'administrasi',
                                        (
                                            A.namobj,
                                            A.remark,
                                            A.alamat,
                                            A.luas_bangunan,
                                            A.luas_tanah,
                                            A.satuan_luas_bangunan,
                                            A.satuan_luas_tanah,
                                            A.gambar,
                                            A.tinggi_bangunan,
                                            A.perangkat,
                                            A.tipe_gambar,
                                            b.kabupaten,
                                            c.kecamatan,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
337 338
                                            d.desa,
                                            a.id
Manggar Mahardhika's avatar
Manggar Mahardhika committed
339 340 341 342 343 344 345 346 347 348
                                        ),
                                        'geometry',
                                        ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                    ) 
                                FROM
                                    geo_stasiun A 
                                    JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
                                    JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
                                    join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                    """)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
349 350 351 352 353 354 355
            stasiun_res = stasiun.fetchall()
        
        stasiun_result = []
        for i in stasiun_res:
            stasiun_result.append(i[0])
            
        with conn.cursor() as terminal:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
            terminal.execute("""SELECT
                                    json_build_object (
                                        'type',
                                        'Feature',
                                        'administrasi',
                                        (
                                            A.namobj,
                                            A.remark,
                                            A.alamat,
                                            A.luas_bangunan,
                                            A.luas_tanah,
                                            A.satuan_luas_bangunan,
                                            A.satuan_luas_tanah,
                                            A.gambar,
                                            A.tinggi_bangunan,
                                            A.perangkat,
                                            A.tipe_gambar,
                                            b.kabupaten,
                                            c.kecamatan,
                                            d.desa,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
376 377
                                            a.luas,
                                            a.id
Manggar Mahardhika's avatar
Manggar Mahardhika committed
378 379 380 381 382 383 384 385 386 387
                                        ),
                                        'geometry',
                                        ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                    ) 
                                FROM
                                    geo_terminal_bus A 
                                    JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
                                    JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
                                    join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                    """)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
388 389 390 391 392 393 394
            terminal_res = terminal.fetchall()
        
        terminal_result = []
        for i in terminal_res:
            terminal_result.append(i[0])
            
        with conn.cursor() as cagar_budaya:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
            cagar_budaya.execute("""SELECT
                                        json_build_object (
                                            'type',
                                            'Feature',
                                            'administrasi',
                                            (
                                                A.namobj,
                                                A.remark,
                                                A.alamat,
                                                A.gambar,
                                                A.perangkat,
                                                A.tipe_gambar,
                                                b.kabupaten,
                                                c.kecamatan,
                                                d.desa,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
410 411
                                                a.luas,
                                                a.id
Manggar Mahardhika's avatar
Manggar Mahardhika committed
412 413 414 415 416 417 418 419 420 421
                                            ),
                                            'geometry',
                                            ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                        ) 
                                    FROM
                                        geo_cagar_budaya A 
                                        JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
                                        JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
                                        join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                        """)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
422 423 424 425 426 427 428
            cagar_budaya_res = cagar_budaya.fetchall()
        
        cagar_budaya_result = []
        for i in cagar_budaya_res:
            cagar_budaya_result.append(i[0])
            
        with conn.cursor() as gardu_listrik:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
            gardu_listrik.execute("""SELECT
                                        json_build_object (
                                            'type',
                                            'Feature',
                                            'administrasi',
                                            (
                                                A.namobj,
                                                A.remark,
                                                A.alamat,
                                                A.luas_bangunan,
                                                A.luas_tanah,
                                                A.satuan_luas_bangunan,
                                                A.satuan_luas_tanah,
                                                A.gambar,
                                                A.tinggi_bangunan,
                                                A.perangkat,
                                                A.tipe_gambar,
                                                b.kabupaten,
                                                c.kecamatan,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
448 449
                                                d.desa,
                                                a.id
Manggar Mahardhika's avatar
Manggar Mahardhika committed
450 451 452 453 454 455 456 457 458 459
                                            ),
                                            'geometry',
                                            ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                        ) 
                                    FROM
                                        geo_gardu_listrik A 
                                        JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
                                        JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
                                        join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                        """)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
460 461 462 463 464
            gardu_listrik_res = gardu_listrik.fetchall()
        
        gardu_listrik_result = []
        for i in gardu_listrik_res:
            gardu_listrik_result.append(i[0])
Muhamad's avatar
Muhamad committed
465
            
Manggar Mahardhika's avatar
Manggar Mahardhika committed
466
        context = {
Muhamad's avatar
Muhamad committed
467
        'title':'Dashboard',
Manggar Mahardhika's avatar
Manggar Mahardhika committed
468 469
        'kelurahan':kel_res,
        'kecamatan':kec_res,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
470 471 472 473 474 475 476 477 478 479 480
        'kantor_administrasi':kantor_administrasi_result,
        'layanan_kesehatan':layanan_kesehatan_result,
        'puskesmas':puskesmas_result,
        'rumah_sakit':rumah_sakit_result,
        'sarana_ibadah':sarana_ibadah_result,
        'sarana_pendidikan':sarana_pendidikan_result,
        'spbu':spbu_result,
        'stasiun':stasiun_result,
        'terminal':terminal_result,
        'cagar_budaya':cagar_budaya_result,
        'gardu_listrik':gardu_listrik_result,
Muhamad's avatar
Muhamad committed
481
        'global':all[0],
Manggar Mahardhika's avatar
Manggar Mahardhika committed
482
        # 'kntl':kntl
Manggar Mahardhika's avatar
Manggar Mahardhika committed
483
        }
Manggar Mahardhika's avatar
Manggar Mahardhika committed
484 485
        return render (request, self.template_name, context)
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
486

Muhamad's avatar
Muhamad committed
487 488 489 490
from django import template
from django.contrib.auth.models import Group
from rest_framework.decorators import api_view, renderer_classes
from rest_framework.response import Response
Manggar Mahardhika's avatar
Manggar Mahardhika committed
491 492


Muhamad's avatar
Muhamad committed
493
register = template.Library()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
494

Muhamad's avatar
Muhamad committed
495 496 497 498
@register.filter(name='has_group')
def has_group(user, group_name):
    group = Group.objects.get(name=group_name)
    return True if group in user.groups.all() else False
Manggar Mahardhika's avatar
Manggar Mahardhika committed
499

Muhamad's avatar
Muhamad committed
500
@api_view(('GET',))
Muhamad's avatar
Muhamad committed
501
def perbaikanrenovasi(request):
Muhamad's avatar
Muhamad committed
502 503 504 505 506 507 508 509 510 511 512 513
    with conn.cursor() as kec:
        kec.execute("SELECT kecamatan from geo_data_kecamatan order by kecamatan asc")
        kec_res_ = kec.fetchall()
        
        # with conn.cursor() as knt:
        #     knt.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark),'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_kantor_administrasi a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
        #     knt_res = knt.fetchall()
        
    kec_res = []
    for i in kec_res_:
        kec_res.append(i[0])
    
Muhamad's avatar
Muhamad committed
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
    with conn.cursor() as data_per:
        data_per.execute("""
                         SELECT
                            gl.NAME,
                            tp.id_object,
                            tp.NAME,
                            tp.TYPE,
                            tp.tgl_perbaikan,
                            tp.anggaran,
                            tp.tgl_selesai
                        FROM
                            ( SELECT tbl_perbaikan.*, ROW_NUMBER ( ) OVER ( PARTITION BY id_object ORDER BY tgl_selesai DESC ) AS rn FROM tbl_perbaikan ) tp
                            JOIN geo_label gl ON gl.ID = tp.type_id 
                        WHERE
                            rn = 1
                         """)
        res_perbaikan = data_per.fetchall()
    
    res_all = []
    for j in res_perbaikan:
        data_all = {
            'name_perbaikan':j[0],
            'id_object':j[1],
            'name':j[2],
            'type':j[3],
            'tgl_perbaikan':j[4],
            'anggaran':j[5],
            'tgl_selesai':j[6],
        }
        res_all.append(data_all)
            
Muhamad's avatar
Muhamad committed
545 546 547
    context = {'res_all':res_all,'kec_res':kec_res}
    
    return Response(context)
Muhamad's avatar
Muhamad committed
548 549 550 551

@api_view(('POST',))
def loadDetail(request):
    id_object = request.POST.get('id')
Muhamad's avatar
Muhamad committed
552 553 554 555 556 557 558 559 560
    with conn.cursor() as data_per:
        data_per.execute("""
                         SELECT
                            gl.NAME,
                            tp.name,
                            gj.remark,
                            st_length ( gj.geom :: geography ),
                            tp.tgl_pembuatan,
                            tp.tgl_perbaikan,
Muhamad's avatar
Muhamad committed
561
                            tp.tgl_selesai,
Muhamad's avatar
Muhamad committed
562 563 564 565 566 567 568
                            tp.anggaran,
                            tp.pj,
                            tp.pj_lapangan,
                            tp.kontraktor,
                            ST_AsGeoJSON ( tp.geom :: geometry ) :: json,
                            tp.pjg_perbaikan,
                            tp.lbr_perbaikan,
Muhamad's avatar
Muhamad committed
569 570 571 572
                            tp.id_object,
                            tp.id,
                            tp.proposal,
                            tp.bukti_selesai
Muhamad's avatar
Muhamad committed
573 574 575 576
                        FROM
                            tbl_perbaikan tp
                            JOIN geo_label gl ON gl.ID = tp.type_id
                            JOIN geo_jalan gj ON gj.ID = tp.id_object
Muhamad's avatar
Muhamad committed
577
                        where tp.id_object = %s"""% id_object)
Muhamad's avatar
Muhamad committed
578
        res_perbaikan = data_per.fetchall()
Muhamad's avatar
Muhamad committed
579 580
        
    res_all = []
Muhamad's avatar
Muhamad committed
581
    for j in res_perbaikan:
Muhamad's avatar
Muhamad committed
582
        data_all = {
Muhamad's avatar
Muhamad committed
583 584 585 586 587
                'name':j[1],
                'type':j[2],
                'panjang':j[3],
                'tgl_pembuatan':j[4],
                'tgl_perbaikan':j[5],
Muhamad's avatar
Muhamad committed
588 589
                'tgl_selesai':j[6],
                'anggaran':j[7],
Muhamad's avatar
Muhamad committed
590 591 592 593 594 595
                'pj':j[8],
                'pj_lapangan':j[9],
                'kontraktor':j[10],
                'geometry':j[11],
                'pjg_jln_reno':j[12],
                'lbr_jln_reno':j[13],
Muhamad's avatar
Muhamad committed
596 597 598 599
                'id_object':j[14],
                'id':j[15],
                'proposal':j[16],
                'bukti_selesai':j[17]
Muhamad's avatar
Muhamad committed
600
            }
Muhamad's avatar
Muhamad committed
601 602 603 604
        res_all.append(data_all)

    respon =res_all
    return Response(respon)
Muhamad's avatar
Muhamad committed
605

Muhamad's avatar
Muhamad committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
@api_view(('GET',))
def inpuperbaikan(request):
    id = request.GET.get('id_jalan')
    with conn.cursor() as data_per:
        data_per.execute("""
                         SELECT
                            namrjl,
                            remark,
                            st_length ( geom :: geography ),
                            json_build_object (
                                        'type', 'Feature',
                                        'geometry', ST_AsGeoJSON ( geom :: geometry ) :: json ) ,
                            id,
                            type_id
                        FROM
                            geo_jalan
                        where id = %s
                         """% id)
        res_data = data_per.fetchall()
    
    respon = res_data[0]
    return Response(respon)

Muhamad's avatar
Muhamad committed
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
@api_view(('GET',))
def inputperbaikanbangunan(request):
    id = request.GET.get('id_bangunan')
    with conn.cursor() as data_per:
        data_per.execute("""
                         SELECT
                            name,
                            type_id,
                            json_build_object ( 'type', 'Feature', 'geometry', ST_AsGeoJSON ( geom :: geometry ) :: json ),
                            ID,
                            remark,
                            address_1,
                            address_2
                        FROM
                            geo_bangunan 
                        WHERE
                            ID = %s
                         """% id)
        res_data = data_per.fetchall()
    
    respon = res_data[0]
    return Response(respon)

Muhamad's avatar
Muhamad committed
652 653 654 655
from django.core.files.storage import FileSystemStorage
import random
import re
import json
Muhamad's avatar
Muhamad committed
656
@api_view(('POST',))
Muhamad's avatar
Muhamad committed
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
def saveperbaikan(request):
    type_id         = request.POST.get("type_id")
    id_object       = request.POST.get("object_id")
    name            = request.POST.get("name")
    type            = request.POST.get("type")
    tgl_pembuatan   = request.POST.get("tgl_pembuatan")
    tgl_perbaikan   = request.POST.get("tgl_perbaikan")
    tgl_disetujui   = request.POST.get("tgl_disetujui")
    anggaran        = request.POST.get("anggaran")
    
    
    pj              = request.POST.get("pj")
    pj_lapangan     = request.POST.get("pj_lapangan")
    kontraktor      = request.POST.get("kontraktor")
    pjg_perbaikan   = request.POST.get("pjg_perbaikan")
    lbr_perbaikan   = request.POST.get("lbr_perbaikan")
    jenis_perbaikan = request.POST.get("jenis_perbaikan")
    luas            = request.POST.get("luas")
    coor            = request.POST.get("coord")
    tgl_selesai     = request.POST.get("tgl_selesai")
    
    proposal        = request.FILES.get("proposal")
    bukti_selesai   = request.FILES.get("bukti_selesai")
    print(coor, proposal)
    
    
    
Muhamad's avatar
Muhamad committed
684 685
    fss_p = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/media/proposal/')
    fss_b = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/media/bukti_penyelesaian/')
Muhamad's avatar
Muhamad committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
    
    coord = json.loads(str(coor))
    
    pjg_perbaikan = re.sub('[^0-9.]+', '', str(pjg_perbaikan))
    lbr_perbaikan = re.sub('[^0-9.]+', '', str(lbr_perbaikan))
    luas = re.sub('[^0-9.]+', '', str(luas))
    
    if coord['features'][0]['geometry']['type'] == 'LineString':
        print("oke")
        for co in coord['features']:
            ll = str(co["geometry"]['coordinates'])
            ll += ll
        ll = ll.replace(", "," ")
        ll = ll.replace("]][[","),(")
        ll = ll.replace("] [",", ")
        ll = ll.replace("]]",")")
        ll = ll.replace("[[","(")

        coordinat = f'MULTILINESTRING({ll})'
Muhamad's avatar
Muhamad committed
705
        print(ll)
Muhamad's avatar
Muhamad committed
706 707 708 709 710 711 712 713 714 715 716
    if coord['features'][0]['geometry']['type'] == 'Polygon':
        print('tidakoke')
        
    try:
        format = (proposal.name).split(".")
        num = random.randrange(1, 10000000000000)
        nproposal = f"{name}_{num}.{format[-1:][0]}"
        nproposal = nproposal.replace(" ","-")
        filename_p = fss_p.save(nproposal, proposal)
        url_p = fss_p.url(filename_p)

Muhamad's avatar
Muhamad committed
717 718
        format_ = (bukti_selesai.name).split(".")
        nbukti = f"{name}_{num}.{format_[-1:][0]}"
Muhamad's avatar
Muhamad committed
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
        nbukti = nbukti.replace(" ","-")
        filename_b = fss_b.save(nbukti, bukti_selesai)
        url_b = fss_b.url(filename_b)
        
        with conn.cursor() as data_per:
            data_per.execute("""
                        INSERT INTO tbl_perbaikan (
                            type_id,
                            id_object,
                            name,
                            type,
                            tgl_selesai,
                            tgl_perbaikan,
                            tgl_disetujui,
                            anggaran,
                            proposal,
                            bukti_selesai,
                            pj,
                            pj_lapangan,
                            kontraktor,
                            pjg_perbaikan,
                            lbr_perbaikan,
                            jenis_perbaikan,
                            geom
                        )
                        VALUES
                            (
                                %s,
                                %s,
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                %s,
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                %s,
                                %s,
                                '%s',
                                ST_GeomFromText('%s')
                            )
                            """% (
                                int(type_id),
                                int(id_object),
                                name,
                                type,
                                tgl_selesai,
                                tgl_perbaikan,
                                tgl_disetujui,
                                int(anggaran),
                                nproposal,
                                nbukti,
                                pj,
                                pj_lapangan,
                                kontraktor,
                                float(pjg_perbaikan),
                                float(lbr_perbaikan),
                                jenis_perbaikan,
                                coordinat))
        conn.commit()
Muhamad's avatar
Muhamad committed
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
        data = "Sukses"
        info = "Data Telah Berhasil di Perbaharui"
        code = 0

    except:
        data = "Pastikan Semua Data Terisi Termasuk Proposal dan Bukti Selesai"
        info = "Oops"
        code = 1
    respon = {"data":data,"info":info,"code":code}
    return Response(respon)

# from django.core.files.storage import FileSystemStorage
# import random
# import re
# import json
@api_view(('POST',))
def saveperbaikanbangunan(request):
    type_id         = '1'
    id_object       = request.POST.get("object_idbang")
    name            = request.POST.get("namebang")
    type            = request.POST.get("typebang")
    # tgl_pembuatan   = request.POST.get("tgl_pembuatanbang")
    tgl_perbaikan   = request.POST.get("tgl_perbaikanbang")
    tgl_disetujui   = request.POST.get("tgl_disetujuibang")
    anggaran        = request.POST.get("anggaranbang")
    
    
    pj              = request.POST.get("pjbang")
    pj_lapangan     = request.POST.get("pj_lapanganbang")
    kontraktor      = request.POST.get("kontraktorbang")
    jenis_perbaikan = request.POST.get("jenis_perbaikanbang")
    coor            = request.POST.get("coordbang")
    
    # for co in coor['features']:
    # ll = coor['geometry']['coordinates']
    # ll += ll
    # ll = ll.replace(", "," ")
    # ll = ll.replace("]][[","),(")
    # ll = ll.replace("] [",", ")
    # ll = ll.replace("]]",")")
    # ll = ll.replace("[[","(")
    # corcoran = ll
    # p_create = coor.replace('"','')
    # po_create = p_create.replace(', ','],[')
    # cood_edit = json.loads(coor)
    # co_edit = cood_edit['geometry']['coordinates']
    
    yak = str(coor)
    suk = yak.replace("""{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":""","")
    sik = suk.replace("""}}""","")
    cor = sik.replace("""[[[""","((")
    coran = cor.replace("""]]]""","))")
    ll = coran.replace(","," ")
    ii = ll.replace("] [",",")
    
    corcoran = f'MULTIPOLYGON({ii})'
    print("-------------------")
    print(str(corcoran))
    print("-------------------")
    # print(yak)
    tgl_selesai     = request.POST.get("tgl_selesaibang")
    
    proposal        = request.FILES.get("proposal_bang")
    bukti_selesai   = request.FILES.get("bukti_selesai_bang")
    print((proposal.name).split("."))
    print((bukti_selesai.name).split("."))
    
    fss_p = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/media/proposal/')
    fss_b = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/media/bukti_penyelesaian/')

    try:
        format1 = (proposal.name).split(".")
        num = random.randrange(1, 10000000000000)
        nproposal = f"{name}_{num}.{format1[-1:][0]}"
        nproposal = nproposal.replace(" ","-")
        filename_p = fss_p.save(nproposal, proposal)
        url_p = fss_p.url(filename_p)

        format2 = (bukti_selesai.name).split(".")
        nbukti = f"{name}_{num}.{format2[-1:][0]}"
        nbukti = nbukti.replace(" ","-")
        filename_b = fss_b.save(nbukti, bukti_selesai)
        url_b = fss_b.url(filename_b)
        
        with conn.cursor() as data_per:
            data_per.execute("""
                            INSERT INTO tbl_perbaikan (
                                type_id,
                                id_object,
                                name,
                                type,
                                tgl_selesai,
                                tgl_perbaikan,
                                tgl_disetujui,
                                anggaran,
                                proposal,
                                bukti_selesai,
                                pj,
                                pj_lapangan,
                                kontraktor,
                                jenis_perbaikan,
                                geom
                            )
                            VALUES
                                (
                                    %s,
                                    %s,
                                    '%s',
                                    '%s',
                                    '%s',
                                    '%s',
                                    '%s',
                                    %s,
                                    '%s',
                                    '%s',
                                    '%s',
                                    '%s',
                                    '%s',
                                    '%s',
                                    ST_GeomFromText('%s')
                                )
                                """% (
                                    int(type_id),
                                    int(id_object),
                                    name,
                                    type,
                                    tgl_selesai,
                                    tgl_perbaikan,
                                    tgl_disetujui,
                                    int(anggaran),
                                    nproposal,
                                    nbukti,
                                    pj,
                                    pj_lapangan,
                                    kontraktor,
                                    jenis_perbaikan,
                                    corcoran))
        conn.commit()
        data = "Sukses"
Muhamad's avatar
Muhamad committed
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
        info = "Data Telah Berhasil di Perbaharui"
        code = 0

    except:
        data = "Pastikan Semua Data Terisi Termasuk Proposal dan Bukti Selesai"
        info = "Oops"
        code = 1
    respon = {"data":data,"info":info,"code":code}
    return Response(respon)

@api_view(('GET',))
def loadbyiddetail(request):
    id = request.GET.get('id')
    id_jl = request.GET.get('id_jl')
    with conn.cursor() as detail_maps:
        detail_maps.execute("""
                            SELECT
                                json_build_object (
                                        'type', 'Feature',
                                        'geometry', ST_AsGeoJSON ( a.geom :: geometry ) :: json ),
                                json_build_object (
                                        'type', 'Feature',
                                        'geometry', ST_AsGeoJSON ( b.geom :: geometry ) :: json )
                            from 
                                tbl_perbaikan a, geo_jalan b
                            where
                                a.id = %s and b.id = %s
                            """% (id, id_jl))
        detail_ = detail_maps.fetchone()
    print(detail_)
    respon=detail_
    return Response(respon)

@api_view(('GET',))
def getjalanperbaikan(request):
    id = request.GET.get('id')

    with conn.cursor() as detail_maps:
        detail_maps.execute("""
                            SELECT
                                json_build_object (
                                        'type', 'Feature',
                                        'geometry', ST_AsGeoJSON ( a.geom :: geometry ) :: json )
                            from 
                                tbl_perbaikan a
                            where
                                a.id = %s
                            """% id)
        detail_ = detail_maps.fetchone()
    print(detail_)
    respon=detail_
    return Response(respon)

@api_view(('GET',))
def getdetailperbaikan(request):
    id_object = request.GET.get('id')
Muhamad's avatar
Muhamad committed
978 979 980 981 982 983 984 985 986
    with conn.cursor() as data_per:
        data_per.execute("""
                         SELECT
                            gl.NAME,
                            tp.name,
                            gj.remark,
                            st_length ( gj.geom :: geography ),
                            tp.tgl_pembuatan,
                            tp.tgl_perbaikan,
Muhamad's avatar
Muhamad committed
987
                            tp.tgl_selesai,
Muhamad's avatar
Muhamad committed
988 989 990 991 992 993 994
                            tp.anggaran,
                            tp.pj,
                            tp.pj_lapangan,
                            tp.kontraktor,
                            ST_AsGeoJSON ( tp.geom :: geometry ) :: json,
                            tp.pjg_perbaikan,
                            tp.lbr_perbaikan,
Muhamad's avatar
Muhamad committed
995 996 997 998
                            tp.id_object,
                            tp.id,
                            tp.proposal,
                            tp.bukti_selesai
Muhamad's avatar
Muhamad committed
999 1000 1001 1002
                        FROM
                            tbl_perbaikan tp
                            JOIN geo_label gl ON gl.ID = tp.type_id
                            JOIN geo_jalan gj ON gj.ID = tp.id_object
Muhamad's avatar
Muhamad committed
1003
                        where tp.id = %s"""% id_object)
Muhamad's avatar
Muhamad committed
1004
        res_perbaikan = data_per.fetchall()
Muhamad's avatar
Muhamad committed
1005
                
Muhamad's avatar
Muhamad committed
1006 1007 1008 1009 1010 1011 1012 1013
    res_all = []
    for j in res_perbaikan:
        data_all = {
                'name':j[1],
                'type':j[2],
                'panjang':j[3],
                'tgl_pembuatan':j[4],
                'tgl_perbaikan':j[5],
Muhamad's avatar
Muhamad committed
1014 1015
                'tgl_selesai':j[6],
                'anggaran':j[7],
Muhamad's avatar
Muhamad committed
1016 1017 1018 1019 1020 1021
                'pj':j[8],
                'pj_lapangan':j[9],
                'kontraktor':j[10],
                'geometry':j[11],
                'pjg_jln_reno':j[12],
                'lbr_jln_reno':j[13],
Muhamad's avatar
Muhamad committed
1022 1023 1024 1025
                'id_object':j[14],
                'id':j[15],
                'proposal':j[16],
                'bukti_selesai':j[17]
Muhamad's avatar
Muhamad committed
1026 1027 1028 1029
            }
        res_all.append(data_all)

    respon =res_all
Muhamad's avatar
Muhamad committed
1030 1031
    return Response(respon)

Muhamad's avatar
Muhamad committed
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
@api_view(('GET',))
def getdetailperbaikanbangunan(request):
    id_object = request.GET.get('id')
    with conn.cursor() as data_per:
        data_per.execute("""
                         SELECT
                            gl.NAME,
                            tp.name,
                            gj.remark,
                            tp.tgl_pembuatan,
                            tp.tgl_perbaikan,
                            tp.tgl_selesai,
                            tp.anggaran,
                            tp.pj,
                            tp.pj_lapangan,
                            tp.kontraktor,
                            ST_AsGeoJSON ( tp.geom :: geometry ) :: json,
                            tp.id_object,
                            tp.id,
                            tp.proposal,
                            tp.bukti_selesai,
                            tp.jenis_perbaikan
                        FROM
                            tbl_perbaikan tp
                            JOIN geo_label gl ON gl.ID = tp.type_id
                            JOIN geo_bangunan gj ON gj.ID = tp.id_object
                        where tp.id_object = %s
                        order by tp.tgl_perbaikan desc"""% id_object)
        res_perbaikan = data_per.fetchall()
                
    res_all = []
    for j in res_perbaikan:
        data_all = {
                'name':j[1],
                'type':j[2],
                'tgl_pembuatan':j[3],
                'tgl_perbaikan':j[4],
                'tgl_selesai':j[5],
                'anggaran':j[6],
                'pj':j[7],
                'pj_lapangan':j[8],
                'kontraktor':j[9],
                'geometry':j[10],
                'id_object':j[11],
                'id':j[12],
                'proposal':j[13],
                'bukti_selesai':j[14],
                'jenis_perbaikan':j[15]
            }
        res_all.append(data_all)

    respon = res_all
    return Response(respon)

Muhamad's avatar
Muhamad committed
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
@api_view(('GET',))
def editkelurahan(request):
    kode_dagri = request.GET.get('kode_dagri')
    
    with conn.cursor() as edit_poly:
        edit_poly.execute("SELECT json_build_object('type', 'Feature', 'administrasi', json_build_array(kode_desa, desa, kecamatan, kab_kota, provinsi, jumlah_pen, jumlah_kk, luas_desa),'penduduk', json_build_array(pria, wanita, belum_kawin, kawin, cerai_hidup, cerai_mati, wajib_ktp, islam, kristen, khatolik, hindu, budha, konghucu, kepercayaan_lain, u0, u5, u10, u15, u20, u25, u30, u35, u40, u45, u50, u55, u60, u65, u70, u75),'pekerjaan_pendidikan', json_build_array(tidak_sekolah, belum_tamat, tamat_sd, sltp, slta, diploma_i, diploma_ii, diploma_iv, strata_ii, strata_iii, tidak_bekerja, aparatur_pemerintah, tenaga_pendidik, wiraswasta, pertanian, tenaga_kesehatan, pensiunan, pegawai, tentara, kepolisian, pedagang, petani, peternak, nelayan, karyawan, buruh, pembantu, tukang, pendeta, pastor, ustadz, dosen, guru, pilot, pengacara, notaris, arsitek, akuntan, konsultan, dokter, bidan, perawat, psikiater, sopir, lainnya), 'poi',json_build_array(jml_puskesma, jml_sekolah, jml_sarib),'geometry', ST_AsGeoJSON(geom :: geometry) :: json) from geo_data_kelurahan where kode_desa = '%s'"% kode_dagri)
        res_edit_poly = edit_poly.fetchall()
        
    print(res_edit_poly)
    
    la = []
    lo = []
    for i in res_edit_poly[0][0]['geometry']['coordinates'][0][0]:
        la.append(i[0])
        lo.append(i[1])
        
    avg_la = sum(la)/len(la)
    avg_lo = sum(lo)/len(lo)
    
    desa = res_edit_poly[0][0]['administrasi'][1]
    kec = res_edit_poly[0][0]['administrasi'][2]
    kab = res_edit_poly[0][0]['administrasi'][3]
    prov = res_edit_poly[0][0]['administrasi'][4]
    context = {'desa':desa, 'kec':kec, 'kab':kab, 'prov':prov,'result_poly': res_edit_poly[0][0], 'avg_la':avg_la, 'avg_lo':avg_lo}
    return Response(context)