views.py 170 KB
Newer Older
Muhamad's avatar
Muhamad committed
1 2
from django.http.response import Http404
from django.shortcuts import redirect, render
Manggar Mahardhika's avatar
Manggar Mahardhika committed
3 4 5
from rest_framework import status
from rest_framework.decorators import api_view, renderer_classes
from rest_framework.response import Response
Muhamad's avatar
Muhamad committed
6 7 8 9 10 11 12
from django.core.files.storage import FileSystemStorage
from django.core.files.base import ContentFile, File
import base64
import json
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
13
from django.db import connection as conn
Manggar Mahardhika's avatar
Manggar Mahardhika committed
14 15

# Create your views here.
Manggar Mahardhika's avatar
Manggar Mahardhika committed
16 17
from rest_framework.renderers import JSONRenderer, TemplateHTMLRenderer

18
#from django.utils import six
Muhamad's avatar
Muhamad committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32
import six
from django.core.exceptions import PermissionDenied
from django.contrib.auth.decorators import user_passes_test
from django.http import HttpResponse, HttpResponseNotFound


def group_required(group, login_url=None, raise_exception=False):
    def check_perms(user):

        if isinstance(group, six.string_types):
            groups = (group, )
        else:
            groups = group

33
        if user.groups.filter(name__in=groups).exists() or user.is_superuser:
Muhamad's avatar
Muhamad committed
34 35 36 37 38 39 40 41 42 43 44
            return True

        if raise_exception:
            print('raise')
            raise PermissionDenied("dilarang")

        return False

    return user_passes_test(check_perms, login_url=login_url)


Manggar Mahardhika's avatar
Manggar Mahardhika committed
45 46
import random

Manggar Mahardhika's avatar
Manggar Mahardhika committed
47 48 49
@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def search_auto(request):
Muhamad's avatar
Muhamad committed
50

Manggar Mahardhika's avatar
Manggar Mahardhika committed
51
    with conn.cursor() as desa:
Muhamad's avatar
Muhamad committed
52
        desa.execute(f"SELECT id, desa, kecamatan FROM geo_data_kelurahan order by desa ASC")
Manggar Mahardhika's avatar
Manggar Mahardhika committed
53 54 55
        all = desa.fetchall()    
        
    data = {'nama_des':all}
Manggar Mahardhika's avatar
Manggar Mahardhika committed
56 57 58 59
    return Response(data)

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
Muhamad's avatar
Muhamad committed
60

Manggar Mahardhika's avatar
Manggar Mahardhika committed
61 62
def tuplah(request):
    with conn.cursor() as desa:
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        desa.execute(f"""SELECT
                            json_build_object (
                                'type',
                                'Feature',
                                'legend',
                                ( A.ID, A.legenda, st_area ( A.geom :: geography ), A.gambar ),
                                'geometry',
                                ST_AsGeoJSON ( st_intersection ( A.geom, b.geom ) :: geometry ) :: json 
                            ) 
                        FROM
                            geo_tuplah A,
                            geo_prov_kab b 
                        WHERE
                            b.kabupaten = 'OGAN KOMERING ULU'""")
        all = desa.fetchall()
        ltup = []
        for i in all:
            i[0]['legend']['f3'] = '{0:,.2f}'.format(i[0]['legend']['f3'])
            ltup.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
82 83 84
    data = {'tuplah':all}
    return Response(data)

Muhamad's avatar
Muhamad committed
85

Manggar Mahardhika's avatar
Manggar Mahardhika committed
86 87 88 89 90 91 92 93 94 95 96 97 98
@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def building(request):
    if request.method == 'GET':
        wetan = request.GET.get("wetan")
        kulon = request.GET.get("kulon")
        lor = request.GET.get("lor")
        kidul = request.GET.get("kidul")
        # lat = request.GET.get("lat")
        # lng = request.GET.get("lng")
        
        # kulon lor, wetan lor, wetan kidul, kulon kidul, kulon lor
        with conn.cursor() as buldings:
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
            buldings.execute("""SELECT
                                json_build_object (
                                    'type',
                                    'Feature',
                                    'properties',
                                    ( gb.ID, gb.NAME, gb.information,
                                        (
                                            SELECT ARRAY_AGG
                                                ( '' || ID || ',' || tgl_perbaikan || ',' || jenis_perbaikan || '' ORDER BY tgl_perbaikan DESC ) 
                                            FROM
                                                tbl_perbaikan 
                                            WHERE
                                                id_object = gb.ID 
                                                AND type_id = 1 
                                        )
                                    ),
                                    'administrasi',
                                    (
                                        gb.address_1,
                                        gb.address_2,
                                        gb.address_3,
                                        gb.provinsi,
                                        gb.alamat,
                                        gb.luas_bangunan,
                                        gb.luas_tanah,
                                        gb.tinggi_bangunan,
                                        gb.gambar,
                                        gb.pemilik,
                                        gb.db_tipe,
                                        gb.remark,
                                        gb.point_id,
                                        gb.point_name 
                                    ),
                                    'geometry',
                                    ST_AsGeoJSON ( gb.geom :: geometry ) :: json 
                                ) 
                            FROM
                                geo_bangunan gb
                            WHERE
Muhamad's avatar
Muhamad committed
138
                                st_intersects ( st_setsrid(gb.geom, 4326), 'SRID=4326;POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))' )"""% (kulon, lor, wetan, lor, wetan, kidul, kulon, kidul, kulon, lor))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
            prov_res = buldings.fetchall()
            
            polbang = []        
            for i in prov_res:
                i[0]['properties']['f2'] = i[0]['properties']['f2'].replace("'"," ")
                polbang.append(i[0])
            
        data = {'gen':prov_res}
        return Response(data)
    
@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def jalan(request):
    if request.method == 'GET':
        wetan = request.GET.get("wetan")
        kulon = request.GET.get("kulon")
        lor = request.GET.get("lor")
        kidul = request.GET.get("kidul")
Muhamad's avatar
Muhamad committed
157
        print(kulon, lor, wetan, lor, wetan, kidul, kulon, kidul, kulon, lor, kulon, lor, wetan, lor, wetan, kidul, kulon, kidul, kulon, lor)
Muhamad's avatar
Muhamad committed
158

Manggar Mahardhika's avatar
Manggar Mahardhika committed
159
        with conn.cursor() as jalan:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
160
            jalan.execute("SELECT json_build_object ('type', 'Feature', 'properties', ( gj.ID, namrjl, st_length ( gj.geom :: geography ), gj.lebar_jalan, gj.remark, gj.gambar,(SELECT ARRAY_AGG ( '' || id || ',' || tgl_perbaikan || '' order by tgl_perbaikan DESC ) FROM tbl_perbaikan WHERE id_object = gj.ID and type_id = 2) ), 'geometry', ST_AsGeoJSON ( st_intersection(gj.geom, 'SRID=4326;POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))') :: geometry ) :: json ) FROM geo_jalan gj WHERE st_intersects(gj.geom,'SRID=4326;POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))') GROUP BY gj.id"% (kulon, lor, wetan, lor, wetan, kidul, kulon, kidul, kulon, lor, kulon, lor, wetan, lor, wetan, kidul, kulon, kidul, kulon, lor))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
161 162 163 164 165
            prov_res = jalan.fetchall()
                                
        data = {'gen':prov_res}
        return Response(data)
    
Muhamad's avatar
Muhamad committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    
@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))

def editjalan(request):
    if request.method == 'GET':
        idjalan = request.GET.get("idjalan")

        with conn.cursor() as jalan:
            jalan.execute("SELECT json_build_object ('geometry', ST_AsGeoJSON (geom :: geometry ) :: json ) FROM geo_jalan WHERE id = %s"% idjalan)
            prov_res = jalan.fetchall()
                                
        data = {'jalan':prov_res[0][0]}
        return Response(data)

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def deletejalan(request):

    idjalan = request.GET.get("idjalan")

    # try:
    with conn.cursor() as jalan:
        jalan.execute("DELETE from geo_jalan WHERE id = %s"% idjalan)
    
    conn.commit()
    
            
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    # except:
    #     data = 'Oops!!'
    #     info = 'Hubungi Developer'
    #     status = 1
    
    respon={'data':data, 'info':info, 'code':status}
    return Response(respon)

Muhamad's avatar
Muhamad committed
205 206 207 208 209 210 211 212 213 214 215 216 217
@api_view(('POST',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))

def editjalanvalid(request):
    file = request.FILES.get('file')
    idjalan = request.POST.get('idjalan')
    namajalan = request.POST.get('namajalan')
    panjangjalan = request.POST.get('panjangjalan')
    tipejalan = request.POST.get('tipejalan')
    lebarjalan = request.POST.get('lebarjalan')
    cordi = request.POST.get('cord')
    cord = json.loads(cordi)
    
218 219 220 221 222 223 224 225 226
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/image/jalan/')
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{idjalan}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        url = fss.url(filename)
    except:
        namafile = 'None'
Muhamad's avatar
Muhamad committed
227
    
228 229 230 231 232 233 234 235 236 237 238 239
    try:
        with conn.cursor() as desa:
            desa.execute("""UPDATE geo_jalan set geom = st_setsrid(st_geomfromgeojson('{"type":"MultiLineString", "coordinates":[%s]}'),4326), namrjl = '%s', panjang_jalan = %s, lebar_jalan = %s, remark = '%s', gambar = '%s' where id=%s"""%(cord['geometry']['coordinates'], namajalan, panjangjalan, lebarjalan, tipejalan, namafile, int(idjalan)))
        conn.commit()
            
        data = 'SUCCESS'
        info = 'data telah diperbaharui'
        status = 0
    except:
        data = 'NOT SUCCESS'
        info = 'Hubungi Orang Server'
        status = 1
Muhamad's avatar
Muhamad committed
240 241 242 243 244
    
    respon={'data':data, 'info':info, 'code':status}
    return Response(respon)


Manggar Mahardhika's avatar
Manggar Mahardhika committed
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
@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def sungai(request):
    if request.method == 'GET':
        wetan = request.GET.get("wetan")
        kulon = request.GET.get("kulon")
        lor = request.GET.get("lor")
        kidul = request.GET.get("kidul")
        # lat = request.GET.get("lat")
        # lng = request.GET.get("lng")
        
        # kulon lor, wetan lor, wetan kidul, kulon kidul, kulon lor
        with conn.cursor() as sungai:
            sungai.execute("SELECT json_build_object ('type', 'Feature', 'properties', ( a.id, a.namobj ), 'geometry', ST_AsGeoJSON ( st_intersection(a.geom, b.geom) :: geometry ) :: json ) FROM geo_sungai a, geo_prov_kab b")
            prov_res = sungai.fetchall()
                                
        data = {'gen':prov_res}
        return Response(data)
    
@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpoly(request):
    if request.method == 'GET':
        id_poly = request.GET.get("id_poly")                   
        with conn.cursor() as buldings:
270
            buldings.execute("SELECT json_build_object('type', 'Feature', 'properties', (id, name, information, 'osm'), 'administrasi',(address_1, address_2, address_3, provinsi, alamat, luas_bangunan, luas_tanah, tinggi_bangunan, gambar, pemilik, db_tipe, remark, point_id, tampak_atas, tampak_depan), 'geometry', ST_AsGeoJSON(geom :: geometry) :: json) FROM geo_bangunan WHERE id = %s"% int(id_poly))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
271 272 273 274
            build = buldings.fetchall()
        
        return Response(build[0][0])

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
@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpointsearch(request):
    if request.method == 'GET':
        id_point = request.GET.get("id_point")
        db_point = request.GET.get("db_point")
        if (db_point == "geo_cagar_budaya" or db_point == "geo_terminal_bus"):
            l = "a.luas"
        else:
            l = "a.luas_tanah"
            
        print(id_point, db_point, l)           
        with conn.cursor() as buldings:
            buldings.execute("""SELECT
                                    json_build_object (
                                        'type',
                                        'Feature',
                                        'administrasi',
                                        (
                                            a.id,
                                            A.namobj,
                                            A.remark,
                                            A.alamat,
                                            A.gambar,
                                            A.perangkat,
                                            A.tipe_gambar,
                                            b.kabupaten,
                                            c.kecamatan,
                                            d.desa,
                                            %s
                                        ),
                                        'geometry',
                                        ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                    ) 
                                FROM
                                    %s 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) 
                                WHERE a.is_deleted = 0 and a.id = %s"""% (l, db_point, int(id_point)))
            build = buldings.fetchall()
        
        return Response(build[0][0])

Manggar Mahardhika's avatar
Manggar Mahardhika committed
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
@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpointka(request):
    if request.method == 'GET':
        id_point = request.GET.get("id_point")                   
        with conn.cursor() as buldings:
            buldings.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,
                                            a.id
                                        ),
                                        '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)
                                    join geo_data_kelurahan d on st_intersects(A.geom, d.geom) 
355
                                WHERE a.is_deleted = 0 and a.id = %s"""% int(id_point))
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 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
            build = buldings.fetchall()
        
        return Response(build[0][0])

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpointlk(request):
    if request.method == 'GET':
        id_point = request.GET.get("id_point")                   
        with conn.cursor() as buldings:
            buldings.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,
                                            a.id
                                        ),
                                        '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)
                                WHERE a.id = %s"""% int(id_point))
            build = buldings.fetchall()
        
        return Response(build[0][0])

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpointpks(request):
    if request.method == 'GET':
        id_point = request.GET.get("id_point")                   
        with conn.cursor() as buldings:
            buldings.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,
                                            a.id
                                        ),
                                        '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)
                                WHERE a.id = %s"""% int(id_point))
            build = buldings.fetchall()
        
        return Response(build[0][0])

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpointrs(request):
    if request.method == 'GET':
        id_point = request.GET.get("id_point")                   
        with conn.cursor() as buldings:
            buldings.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,
                                            a.id
                                        ),
                                        '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)
                                WHERE a.id = %s"""% int(id_point))
            build = buldings.fetchall()
        
        return Response(build[0][0])

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpointsi(request):
    if request.method == 'GET':
        id_point = request.GET.get("id_point")                   
        with conn.cursor() as buldings:
            buldings.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,
                                            a.id
                                        ),
                                        '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)
                                WHERE a.id = %s"""% int(id_point))
            build = buldings.fetchall()
        
        return Response(build[0][0])

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpointsp(request):
    if request.method == 'GET':
        id_point = request.GET.get("id_point")                   
        with conn.cursor() as buldings:
            buldings.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,
                                            a.id
                                        ),
                                        '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)
                                WHERE a.id = %s"""% int(id_point))
            build = buldings.fetchall()
        
        return Response(build[0][0])

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpointspbu(request):
    if request.method == 'GET':
        id_point = request.GET.get("id_point")                   
        with conn.cursor() as buldings:
            buldings.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,
                                            a.id
                                        ),
                                        '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)
                                WHERE a.id = %s"""% int(id_point))
            build = buldings.fetchall()
        
        return Response(build[0][0])

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpointst(request):
    if request.method == 'GET':
        id_point = request.GET.get("id_point")                   
        with conn.cursor() as buldings:
            buldings.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,
                                            a.id
                                        ),
                                        '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)
                                WHERE a.id = %s"""% int(id_point))
            build = buldings.fetchall()
        
        return Response(build[0][0])

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpointgl(request):
    if request.method == 'GET':
        id_point = request.GET.get("id_point")                   
        with conn.cursor() as buldings:
            buldings.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,
                                            a.id
                                        ),
                                        '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)
                                WHERE a.id = %s"""% int(id_point))
            build = buldings.fetchall()
        
        return Response(build[0][0])

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpointtr(request):
    if request.method == 'GET':
        id_point = request.GET.get("id_point")                   
        with conn.cursor() as buldings:
            buldings.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,
                                            a.luas,
                                            a.id
                                        ),
                                        '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)
                                WHERE a.id = %s"""% int(id_point))
            build = buldings.fetchall()
        
        return Response(build[0][0])

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def editpointcb(request):
    if request.method == 'GET':
        id_point = request.GET.get("id_point")                   
        with conn.cursor() as buldings:
            buldings.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,
                                                a.luas,
                                                a.id
                                            ),
                                            '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)
                                WHERE a.id = %s"""% int(id_point))
            build = buldings.fetchall()
        
        return Response(build[0][0])

767 768
import json

Manggar Mahardhika's avatar
Manggar Mahardhika committed
769 770 771 772 773 774 775 776 777 778
@api_view(('POST',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def validatedit(request):
    if request.method == 'POST':
        name_b = request.POST.get("name_b")
        info_b = request.POST.get("info_b")
        desa_b = request.POST.get("desa_b")
        kec_b = request.POST.get("kec_b")
        kab_b = request.POST.get("kab_b")
        pro_b = request.POST.get("pro_b")
779
        gmb_b = request.POST.get("gambar")
Manggar Mahardhika's avatar
Manggar Mahardhika committed
780
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
781 782 783 784 785
        luast_b = request.POST.get("luastanah_b")
        luasb_b = request.POST.get("luasbangunan_b")
        tinggi_b = request.POST.get("tinggibangunan_b")
        pemilik_b = request.POST.get("pemilik_b")
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
786 787 788
        id_poly = request.POST.get("id_poly")
        polygon = request.POST.get("polygon")
        cood_edit = json.loads(polygon)
789 790 791 792 793 794
        
        tipedb = request.POST.get('tipedbbe')
        poin = request.POST.get('poinbe')
        remark = request.POST.get('remarkbe')
        namapoin = request.POST.get('napo')
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
795
        co_edit = cood_edit['geometry']['coordinates']
796 797
        print(co_edit)
        
Muhamad's avatar
Muhamad committed
798 799

        file = request.FILES.get('file')
800 801 802 803 804 805 806 807 808 809 810
        fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/image/bangunan/')
        
        if tipedb == "":
            tdb = "bangunan"
        else:
            tdb = tipedb

        if poin == "":
            pointopeto = 0
        else:
            pointopeto = poin
Muhamad's avatar
Muhamad committed
811

812 813 814 815 816
        if remark == "":
            rem = "bangunan"
        else:
            rem = remark
        
Muhamad's avatar
Muhamad committed
817 818 819 820 821 822 823 824
        try:
            try:
                format = (file.name).split(".")
                num = random.randrange(1, 10000000000000)
                namafile = f"{name_b}_{num}.{format[-1:][0]}"
                filename = fss.save(namafile, file)
                url = fss.url(filename)
            except:
825
                namafile = gmb_b
Muhamad's avatar
Muhamad committed
826
            print(co_edit,name_b, info_b, desa_b, kec_b, kab_b, pro_b, namafile, int(id_poly))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
827
            
Muhamad's avatar
Muhamad committed
828
            with conn.cursor() as buldings:
829
                buldings.execute("""UPDATE geo_bangunan set geom = st_geomfromgeojson('{"type":"MultiPolygon", "coordinates":[%s]}'),name='%s',information='%s',address_1='%s', address_2 = '%s', address_3 = '%s', provinsi='%s', gambar = '%s', is_deleted = 0, luas_bangunan=%s, luas_tanah=%s, tinggi_bangunan=%s, pemilik='%s', type_id = 1, point_id=%s, remark='%s', db_tipe='%s', point_name='%s' WHERE id = %s"""% (co_edit,name_b, info_b, desa_b, kec_b, kab_b, pro_b, namafile, luasb_b, luast_b, tinggi_b, pemilik_b, pointopeto, rem, tdb, namapoin, int(id_poly)))
Muhamad's avatar
Muhamad committed
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
            conn.commit()
            
            data = 'SUCCESS'
            info = 'data telah diperbaharui'
            status = 0
            
        except:
            data = 'SORRY'
            info = 'hubungi orang terdekat'
            status = 1
            
        respon = {
            'data':data, 
            'info':info, 
            'code':status,
        }
        print(respon)
            
        return Response(respon)
849

Manggar Mahardhika's avatar
Manggar Mahardhika committed
850 851 852 853
@api_view(('POST',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def hapus(request):
    if request.method == 'POST':
854 855
        try:
            id_poly = request.POST.get("id_poly")
Manggar Mahardhika's avatar
Manggar Mahardhika committed
856

857 858 859
            with conn.cursor() as buldings:
                buldings.execute("""DELETE from geo_bangunan where id = %s"""% int(id_poly))
            conn.commit()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
860
            
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
            data = 'SUCCESS'
            info = 'data telah diperbaharui'
            status = 0
            
        except:
            data = 'SORRY'
            info = 'hubungi orang terdekat'
            status = 1
            
        respon = {
            'data':data, 
            'info':info, 
            'code':status,
        }
        print(respon)
            
        return Response(respon)
Muhamad's avatar
Muhamad committed
878

Manggar Mahardhika's avatar
Manggar Mahardhika committed
879 880 881 882
@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def hasilsearch(request):
    if request.method == 'GET':
Muhamad's avatar
Muhamad committed
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
        nama_desa = request.GET.get("id_desa_search")
        
        nama_desa = nama_desa.split(",")[0]
        with conn.cursor() as bts:
            bts.execute("""
                        SELECT
                            st_asgeojson ( ST_ExteriorRing ( the_geom ) ) AS erings 
                        FROM
                            ( SELECT ( ST_Dump ( geom ) ).geom AS the_geom FROM geo_data_kelurahan WHERE desa = '%s' ) as foo
                        """% nama_desa)
            batas = bts.fetchall()
            # batas_desa = json.loads(batas)
            line_o = []
            for i in batas:
                o_pol1 = ''
                o_poly = []
                
                for x in json.loads(i[0])['coordinates']:
                    o_pol1 += (f'{x[0]},{x[1]}""')
                
                o_pol2 = o_pol1.replace('""1',',1')

                o_poly.append(o_pol2.replace('""',''))
                
                line_o.append(o_poly)
        
        with conn.cursor() as desa:
            desa.execute("""
                        SELECT
                            ST_AsGeoJson ( ST_centroid ( geom ) ) 
                        FROM
                            geo_data_kelurahan 
                        WHERE
                            desa = '%s'"""% nama_desa)
            lat_lng = desa.fetchall()
        
        with conn.cursor() as bang:
            bang.execute("""SELECT
                                json_build_object (
                                    'type',
                                    'Feature',
                                    'properties',
                                    ( buildings.ID, buildings.NAME, buildings.information ),
                                    'administrasi',
                                    (
                                        buildings.address_1,
                                        buildings.address_2,
                                        buildings.address_3,
                                        buildings.provinsi,
                                        buildings.alamat,
                                        buildings.luas_bangunan,
                                        buildings.luas_tanah,
                                        buildings.tinggi_bangunan,
                                        buildings.gambar,
937 938 939
                                        buildings.pemilik,
                                        buildings.tampak_atas,
                                        buildings.tampak_depan
Muhamad's avatar
Muhamad committed
940 941 942 943 944 945
                                    ),
                                    'geometry',
                                    ST_AsGeoJSON ( buildings.geom :: geometry ) :: json 
                                ) 
                            FROM
                                geo_bangunan buildings
Muhamad's avatar
Muhamad committed
946
                                JOIN geo_data_kelurahan desa ON st_intersects (  ST_SetSRID(desa.geom, 4326), ST_SetSRID(buildings.geom, 4326) ) 
Muhamad's avatar
Muhamad committed
947 948
                            WHERE
                                desa.desa = '%s'"""% nama_desa)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
949
            
Muhamad's avatar
Muhamad committed
950 951 952
            bangunan = bang.fetchall()
            
            polygon_o = []
953
            
Muhamad's avatar
Muhamad committed
954 955
            info_polygon = []
            tinggi_pol = []
956 957
            tampak_atas_pol = []
            tampak_depan_pol = []
Muhamad's avatar
Muhamad committed
958 959 960 961 962 963 964 965 966 967 968 969 970
            for i in bangunan:
                o_pol1 = ''
                o_poly = []
                for x in i[0]['geometry']['coordinates'][0][0]:
                    o_pol1 += (f'{x[0]},{x[1]}""')
                
                o_pol2 = o_pol1.replace('""1',',1')

                o_poly.append(o_pol2.replace('""',''))
                
                polygon_o.append(o_poly)
                tinggi_pol.append(i[0]['administrasi']['f8'])
                info_polygon.append(i[0]['properties']['f1'])
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
                tampak_atas_pol.append(i[0]['administrasi']['f11'])
                tampak_depan_pol.append(i[0]['administrasi']['f12'])
        
        with conn.cursor() as kantor_administrasi:
            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,
                                                    d.desa,
                                                    a.id
                                                ),
                                                '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)
                                            join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                        WHERE a.is_deleted = 0 and d.desa = '%s'"""% nama_desa)
            kantor_administrasi_res = kantor_administrasi.fetchall()
            
Muhamad's avatar
Muhamad committed
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
            lat_po = []
            long_po = []
            info_po = []
            rema_po = []
            
            for i in kantor_administrasi_res:
                lat_po.append(i[0]['geometry']['coordinates'][1])
                long_po.append(i[0]['geometry']['coordinates'][0])
                info_po.append(i[0]['administrasi']['f15'])
                rema_po.append(i[0]['administrasi']['f2'])
                print(rema_po)
            
        kantor_administrasi_result = []
        for i in kantor_administrasi_res:
            kantor_administrasi_result.append(i[0])
            
        with conn.cursor() as layanan_kesehatan:
            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,
                                                    d.desa,
                                                    a.id
                                                ),
                                                '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)
                                        WHERE d.desa = '%s'"""% nama_desa)
            layanan_kesehatan_res = layanan_kesehatan.fetchall()
Muhamad's avatar
Muhamad committed
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
            lat_po_lk = []
            long_po_lk = []
            info_po_lk = []
            rema_po_lk = []
            
            for i in kantor_administrasi_res:
                lat_po_lk.append(i[0]['geometry']['coordinates'][1])
                long_po_lk.append(i[0]['geometry']['coordinates'][0])
                info_po_lk.append(i[0]['administrasi']['f15'])
                rema_po_lk.append(i[0]['administrasi']['f2'])
        
        layanan_kesehatan_result = []
        for i in layanan_kesehatan_res:
            layanan_kesehatan_result.append(i[0])
            
            
        with conn.cursor() as puskesmas:
            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,
                                            d.desa,
                                            a.id
                                        ),
                                        '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)
                                WHERE d.desa = '%s'"""% nama_desa)
            puskesmas_res = puskesmas.fetchall()
            
            lat_po_pks = []
            long_po_pks = []
            info_po_pks = []
            rema_po_pks = []
            
            for i in puskesmas_res:
                lat_po_pks.append(i[0]['geometry']['coordinates'][1])
                long_po_pks.append(i[0]['geometry']['coordinates'][0])
                info_po_pks.append(i[0]['administrasi']['f15'])
                rema_po_pks.append(i[0]['administrasi']['f2'])
        
        puskesmas_result = []
        for i in puskesmas_res:
            puskesmas_result.append(i[0])
            
        with conn.cursor() as rumah_sakit:
            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,
                                                d.desa,
                                                a.id
                                            ),
                                            '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)
                                    WHERE d.desa = '%s'"""% nama_desa)
            rumah_sakit_res = rumah_sakit.fetchall()
            
            lat_po_rs = []
            long_po_rs = []
            info_po_rs = []
            rema_po_rs = []
            
            for i in rumah_sakit_res:
                lat_po_rs.append(i[0]['geometry']['coordinates'][1])
                long_po_rs.append(i[0]['geometry']['coordinates'][0])
                info_po_rs.append(i[0]['administrasi']['f15'])
                rema_po_rs.append(i[0]['administrasi']['f2'])
        
        rumah_sakit_result = []
        for i in rumah_sakit_res:
            rumah_sakit_result.append(i[0])
            
        with conn.cursor() as sarana_ibadah:
            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,
                                                d.desa,
                                                a.id
                                            ),
                                            '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)
                                    WHERE d.desa = '%s'"""% nama_desa)
            sarana_ibadah_res = sarana_ibadah.fetchall()
            
            lat_po_si = []
            long_po_si = []
            info_po_si = []
            rema_po_si = []
            
            for i in sarana_ibadah_res:
                lat_po_si.append(i[0]['geometry']['coordinates'][1])
                long_po_si.append(i[0]['geometry']['coordinates'][0])
                info_po_si.append(i[0]['administrasi']['f15'])
                rema_po_si.append(i[0]['administrasi']['f2'])
        
        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:
            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,
                                                    d.desa,
                                                    a.id
                                                ),
                                                '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)
                                        WHERE d.desa = '%s'"""% nama_desa)
            sarana_pendidikan_res = sarana_pendidikan.fetchall()
        
            lat_po_sp = []
            long_po_sp = []
            info_po_sp = []
            rema_po_sp = []
            
            for i in sarana_pendidikan_res:
                lat_po_sp.append(i[0]['geometry']['coordinates'][1])
                long_po_sp.append(i[0]['geometry']['coordinates'][0])
                info_po_sp.append(i[0]['administrasi']['f15'])
                rema_po_sp.append(i[0]['administrasi']['f2'])
        
        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:
            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,
                                        d.desa,
                                        a.id
                                    ),
                                    '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)
                            WHERE d.desa = '%s'"""% nama_desa)
            spbu_res = spbu.fetchall()
        
            lat_po_spbu = []
            long_po_spbu = []
            info_po_spbu = []
            rema_po_spbu = []
            
            for i in spbu_res:
                lat_po_spbu.append(i[0]['geometry']['coordinates'][1])
                long_po_spbu.append(i[0]['geometry']['coordinates'][0])
                info_po_spbu.append(i[0]['administrasi']['f15'])
                rema_po_spbu.append(i[0]['administrasi']['f2'])
        
        spbu_result = []
        for i in spbu_res:
            spbu_result.append(i[0])
            
        with conn.cursor() as stasiun:
            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,
                                            d.desa,
                                            a.id
                                        ),
                                        '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)
                                WHERE d.desa = '%s'"""% nama_desa)
            stasiun_res = stasiun.fetchall()
            
            lat_po_st = []
            long_po_st = []
            info_po_st = []
            rema_po_st = []
            
            for i in stasiun_res:
                lat_po_st.append(i[0]['geometry']['coordinates'][1])
                long_po_st.append(i[0]['geometry']['coordinates'][0])
                info_po_st.append(i[0]['administrasi']['f15'])
                rema_po_st.append(i[0]['administrasi']['f2'])
        
        stasiun_result = []
        for i in stasiun_res:
            stasiun_result.append(i[0])
            
        with conn.cursor() as terminal:
            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,
                                            a.luas,
                                            a.id
                                        ),
                                        '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)
                                WHERE d.desa = '%s'"""% nama_desa)
            terminal_res = terminal.fetchall()
            
            lat_po_tr = []
            long_po_tr = []
            info_po_tr = []
            rema_po_tr = []
            
            for i in terminal_res:
                lat_po_tr.append(i[0]['geometry']['coordinates'][1])
                long_po_tr.append(i[0]['geometry']['coordinates'][0])
                info_po_tr.append(i[0]['administrasi']['f16'])
                rema_po_tr.append(i[0]['administrasi']['f2'])
        
        terminal_result = []
        for i in terminal_res:
            terminal_result.append(i[0])
            
        with conn.cursor() as cagar_budaya:
            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,
                                                a.luas,
                                                a.id
                                            ),
                                            '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)
                                    WHERE d.desa = '%s'"""% nama_desa)
            cagar_budaya_res = cagar_budaya.fetchall()
        
            lat_po_cb = []
            long_po_cb = []
            info_po_cb = []
            rema_po_cb = []
            
            for i in cagar_budaya_res:
                lat_po_cb.append(i[0]['geometry']['coordinates'][1])
                long_po_cb.append(i[0]['geometry']['coordinates'][0])
                info_po_cb.append(i[0]['administrasi']['f11'])
                rema_po_cb.append(i[0]['administrasi']['f2'])
        
        cagar_budaya_result = []
        for i in cagar_budaya_res:
            cagar_budaya_result.append(i[0])
            
        with conn.cursor() as gardu_listrik:
            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,
                                                d.desa,
                                                a.id
                                            ),
                                            '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)
                                    WHERE d.desa = '%s'"""% nama_desa)
            gardu_listrik_res = gardu_listrik.fetchall()
            
            lat_po_gl = []
            long_po_gl = []
            info_po_gl = []
            rema_po_gl = []
            
            for i in gardu_listrik_res:
                lat_po_gl.append(i[0]['geometry']['coordinates'][1])
                long_po_gl.append(i[0]['geometry']['coordinates'][0])
                info_po_gl.append(i[0]['administrasi']['f15'])
                rema_po_gl.append(i[0]['administrasi']['f2'])
        
        gardu_listrik_result = []
        for i in gardu_listrik_res:
            gardu_listrik_result.append(i[0])
Muhamad's avatar
Muhamad committed
1516 1517 1518 1519
        
        lat = json.loads(lat_lng[0][0])['coordinates'][0]
        lng = json.loads(lat_lng[0][0])['coordinates'][1]
                        
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
        respon={'polygon_o':polygon_o, 
                'tinggi_pol':tinggi_pol, 
                'info_polygon': info_polygon, 
                'lat':lat, 
                'lng':lng, 
                'line_o':line_o, 
                'tampak_atas_pol':tampak_atas_pol, 
                'tampak_depan_pol':tampak_depan_pol,
                'kantor_administrasi':kantor_administrasi_result,
                'latadm':lat_po,
                'longadm':long_po,
                'infoadm':info_po,
                'remaadm':rema_po,
                'layanan_kesehatan':layanan_kesehatan_result,
                'latlk':lat_po_lk,
                'longlk':long_po_lk,
                'infolk':info_po_lk,
                'remalk':rema_po_lk,
                'puskesmas':puskesmas_result,
                'latpks':lat_po_pks,
                'longpks':long_po_pks,
                'infopks':info_po_pks,
                'remapks':rema_po_pks,
                'rumah_sakit':rumah_sakit_result,
                'latrs':lat_po_rs,
                'longrs':long_po_rs,
                'infors':info_po_rs,
                'remars':rema_po_rs,
                'sarana_ibadah':sarana_ibadah_result,
                'latsi':lat_po_si,
                'longsi':long_po_si,
                'infosi':info_po_si,
                'remasi':rema_po_si,
                'sarana_pendidikan':sarana_pendidikan_result,
                'latsp':lat_po_sp,
                'longsp':long_po_sp,
                'infosp':info_po_sp,
                'remasp':rema_po_sp,
                'spbu':spbu_result,
                'latspbu':lat_po_spbu,
                'longspbu':long_po_spbu,
                'infospbu':info_po_spbu,
                'remaspbu':rema_po_spbu,
                'spbu':spbu_result,
                'stasiun':stasiun_result,
                'latst':lat_po_st,
                'longst':long_po_st,
                'infost':info_po_st,
                'remast':rema_po_st,
                'terminal':terminal_result,
                'lattr':lat_po_tr,
                'longtr':long_po_tr,
                'infotr':info_po_tr,
                'rematr':rema_po_tr,
                'cagar_budaya':cagar_budaya_result,
                'latcb':lat_po_cb,
                'longcb':long_po_cb,
                'infocb':info_po_cb,
                'remacb':rema_po_cb,
                'gardu_listrik':gardu_listrik_result,
                'latgl':lat_po_gl,
                'longgl':long_po_gl,
                'infogl':info_po_gl,
                'remagl':rema_po_gl,
                }
Muhamad's avatar
Muhamad committed
1585
        return Response(respon)
Muhamad's avatar
Muhamad committed
1586
    
Muhamad's avatar
Muhamad committed
1587 1588
import random
        
Muhamad's avatar
Muhamad committed
1589
@api_view(('POST',))
Muhamad's avatar
Muhamad committed
1590
@method_decorator(login_required(login_url='/login/'))
Muhamad's avatar
Muhamad committed
1591 1592
def editkependudukan(request):
    if request.method == 'POST':
Muhamad's avatar
Muhamad committed
1593 1594 1595 1596
        file = request.FILES.get('file')
        coordinate = request.POST.get('coordinate')
        coord_new = json.loads(coordinate)
        
Muhamad's avatar
Muhamad committed
1597 1598 1599 1600 1601 1602 1603 1604 1605
        ketket = ['kelurahan', 'kecamatan', 'kabupaten', 'provinsi', 'luasdesa', 'jumlahpenduduk', 'jumlahkk', 'jumlahlakilaki', 'jumlahperempuan', 'belumkawin', 'kawin', 'ceraihidup', 'ceraimati', 'wajibktp', 'islam', 'kristen', 'katolik', 'hindu', 'budha', 'konghucu', 'kepercayaanlain', 'u0', 'u5', 'u10', 'u15', 'u20', 'u25', 'u30', 'u35', 'u40', 'u45', 'u50', 'u55', 'u60', 'u65', 'u70', 'u75', 'tidaksekolah', 'belumtamat', 'tamatsd', 'tamatsltp', 'tamatslta', 'diplomai', 'diplomaiii', 'diplomaiv', 'strataii', 'strataiii', 'tidakbekerja', 'aparaturpemerintah', 'tenagapendidik', 'wiraswasta', 'pertanian', 'tenagakesehatan', 'pensiunan', 'pegawai', 'tentara', 'kepolisian', 'dosen', 'guru', 'pilot', 'pedagang', 'petani', 'peternak', 'nelayan', 'karyawan', 'buruh', 'pembantu', 'tukang', 'pendeta', 'pastor', 'ustadz', 'pengacara', 'notaris', 'arsitek', 'akuntan', 'dokter', 'bidan', 'perawat', 'psikiater', 'sopir', 'lainnya', 'kodekelurahan']
        
        list_ketek = []
        for i in ketket:
            list_ketek.append(request.POST.get(i))
            # locals()
            
        kelurahan, kecamatan, kabupaten, provinsi, luasdesa, jumlahpenduduk, jumlahkk, jumlahlakilaki, jumlahperempuan, belumkawin, kawin, ceraihidup, ceraimati, wajibktp, islam, kristen, katolik, hindu, budha, konghucu, kepercayaanlain, u0, u5, u10, u15, u20, u25, u30, u35, u40, u45, u50, u55, u60, u65, u70, u75, tidaksekolah, belumtamat, tamatsd, tamatsltp, tamatslta, diplomai, diplomaiii, diplomaiv, strataii, strataiii, tidakbekerja, aparaturpemerintah, tenagapendidik, wiraswasta, pertanian, tenagakesehatan, pensiunan, pegawai, tentara, kepolisian, dosen, guru, pilot, pedagang, petani, peternak, nelayan, karyawan, buruh, pembantu, tukang, pendeta, pastor, ustadz, pengacara, notaris, arsitek, akuntan, dokter, bidan, perawat, psikiater, sopir, lainnya, kodekelurahan = list_ketek
        
1606
        fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/image/kelurahan/')
Muhamad's avatar
Muhamad committed
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
        try:
            format = (file.name).split(".")
            num = random.randrange(1, 10000000000000)
            namafile = f"{kelurahan}_{kodekelurahan}_{num}.{format[-1:][0]}"
            filename = fss.save(namafile, file)
            url = fss.url(filename)
        except:
            num = random.randrange(1, 10000000000000)
            namafile = f"{kelurahan}_{kodekelurahan}_{num}.jpg"
            filename = fss.save(namafile, file)
            url = fss.url(filename)
        
        
            
        try:
            with conn.cursor() as buldings:
                buldings.execute("""UPDATE geo_data_kelurahan set geom = st_setsrid(st_geomfromgeojson('{"type":"MultiPolygon", "coordinates":[%s]}'),4326),desa = '%s', kecamatan = '%s', kab_kota = '%s', provinsi = '%s', luas_desa = %s, jumlah_pen = %s, jumlah_kk = %s, pria=%s, wanita = %s, belum_kawin = %s, kawin = %s, cerai_hidup = %s, cerai_mati = %s, wajib_ktp = %s, islam = %s, kristen = %s, khatolik = %s, hindu = %s, budha = %s, konghucu = %s, kepercayaan_lain = %s, u0 = %s, u5 = %s, u10 = %s,u15 = %s, u20 = %s, u25 = %s, u30 = %s, u35 = %s, u40 = %s, u45 = %s, u50 = %s, u55 = %s, u60 = %s, u65 = %s, u70 = %s, u75 = %s, tidak_sekolah = %s, belum_tamat = %s, tamat_sd = %s, sltp = %s, slta = %s, diploma_i = %s, diploma_ii = %s, diploma_iv = %s, strata_ii = %s, strata_iii = %s, tidak_bekerja = %s, aparatur_pemerintah = %s, tenaga_pendidik = %s, wiraswasta = %s, pertanian = %s, tenaga_kesehatan = %s, pensiunan = %s, pegawai = %s, tentara = %s, kepolisian = %s, dosen = %s, guru = %s, pilot = %s, pedagang = %s, petani = %s, peternak = %s, nelayan = %s, karyawan = %s, buruh = %s, pembantu = %s, tukang = %s, pendeta = %s, pastor = %s, ustadz = %s, pengacara = %s, notaris = %s, arsitek = %s, akuntan = %s, dokter = %s, bidan = %s, perawat = %s, psikiater = %s, sopir = %s, lainnya = %s, gambar = '%s' WHERE kode_desa = '%s'"""% (coord_new['geometry']['coordinates'],kelurahan, kecamatan, kabupaten, provinsi, luasdesa,jumlahpenduduk,jumlahkk,jumlahlakilaki,jumlahperempuan,belumkawin,kawin,ceraihidup,ceraimati, wajibktp,islam,kristen,katolik,hindu,budha,konghucu,kepercayaanlain,u0,u5,u10,u15,u20,u25,u30,u35,u40,u45,u50,u55,u60,u65,u70,u75, tidaksekolah, belumtamat, tamatsd, tamatsltp, tamatslta, diplomai, diplomaiii, diplomaiv, strataii, strataiii, tidakbekerja, aparaturpemerintah, tenagapendidik, wiraswasta, pertanian, tenagakesehatan, pensiunan, pegawai, tentara, kepolisian, dosen, guru, pilot, pedagang, petani, peternak, nelayan, karyawan, buruh, pembantu, tukang, pendeta, pastor, ustadz, pengacara, notaris, arsitek, akuntan, dokter, bidan, perawat, psikiater, sopir, lainnya, namafile, kodekelurahan))
            conn.commit()
            
            with conn.cursor() as kependuduk:
                kependuduk.execute("""SELECT kode_desa, desa, kecamatan, kab_kota, provinsi, luas_desa, jumlah_pen, jumlah_kk, 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, 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, dosen, guru, pilot, pedagang, petani, peternak, nelayan, karyawan, buruh, pembantu, tukang, pendeta, pastor, ustadz, pengacara, notaris, arsitek, akuntan, dokter, bidan, perawat, psikiater, sopir, lainnya, gambar FROM geo_data_kelurahan WHERE kode_desa = '%s'"""% kodekelurahan)
                data_kependuduk = kependuduk.fetchall()      
                     
            data = 'SUCCESS'
            info = 'data telah diperbaharui'
            status = 0
        except:
            data = 'NOT SUCCESS'
            info = 'Hubungi Orang Server'
            status = 1
            
        respon={'data':data, 'info':info, 'code':status, 'penduduk':data_kependuduk[0]}
Muhamad's avatar
Muhamad committed
1639
        data = data_kependuduk[0]
Muhamad's avatar
Muhamad committed
1640
        return Response(respon)
Muhamad's avatar
Muhamad committed
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
@api_view(('POST',))
def valideditkependudukan(request):
    if request.method == 'POST':
        file = request.FILES.get('file')
        coordinate = request.POST.get('coordinate')
        coord_new = json.loads(coordinate)
        
        ketket = ['kelurahan', 'kecamatan', 'kabupaten', 'provinsi', 'luasdesa', 'jumlahpenduduk', 'jumlahkk', 'jumlahlakilaki', 'jumlahperempuan', 'belumkawin', 'kawin', 'ceraihidup', 'ceraimati', 'wajibktp', 'islam', 'kristen', 'katolik', 'hindu', 'budha', 'konghucu', 'kepercayaanlain', 'u0', 'u5', 'u10', 'u15', 'u20', 'u25', 'u30', 'u35', 'u40', 'u45', 'u50', 'u55', 'u60', 'u65', 'u70', 'u75', 'tidaksekolah', 'belumtamat', 'tamatsd', 'tamatsltp', 'tamatslta', 'diplomai', 'diplomaiii', 'diplomaiv', 'strataii', 'strataiii', 'tidakbekerja', 'aparaturpemerintah', 'tenagapendidik', 'wiraswasta', 'pertanian', 'tenagakesehatan', 'pensiunan', 'pegawai', 'tentara', 'kepolisian', 'dosen', 'guru', 'pilot', 'pedagang', 'petani', 'peternak', 'nelayan', 'karyawan', 'buruh', 'pembantu', 'tukang', 'pendeta', 'pastor', 'ustadz', 'pengacara', 'notaris', 'arsitek', 'akuntan', 'dokter', 'bidan', 'perawat', 'psikiater', 'sopir', 'lainnya', 'kodekelurahan']
        
        list_ketek = []
        for i in ketket:
            list_ketek.append(request.POST.get(i))
            # locals()
            
        kelurahan, kecamatan, kabupaten, provinsi, luasdesa, jumlahpenduduk, jumlahkk, jumlahlakilaki, jumlahperempuan, belumkawin, kawin, ceraihidup, ceraimati, wajibktp, islam, kristen, katolik, hindu, budha, konghucu, kepercayaanlain, u0, u5, u10, u15, u20, u25, u30, u35, u40, u45, u50, u55, u60, u65, u70, u75, tidaksekolah, belumtamat, tamatsd, tamatsltp, tamatslta, diplomai, diplomaiii, diplomaiv, strataii, strataiii, tidakbekerja, aparaturpemerintah, tenagapendidik, wiraswasta, pertanian, tenagakesehatan, pensiunan, pegawai, tentara, kepolisian, dosen, guru, pilot, pedagang, petani, peternak, nelayan, karyawan, buruh, pembantu, tukang, pendeta, pastor, ustadz, pengacara, notaris, arsitek, akuntan, dokter, bidan, perawat, psikiater, sopir, lainnya, kodekelurahan = list_ketek
        
        fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/image/kelurahan/')
        try:
            format = (file.name).split(".")
            num = random.randrange(1, 10000000000000)
            namafile = f"{kelurahan}_{kodekelurahan}_{num}.{format[-1:][0]}"
            filename = fss.save(namafile, file)
            url = fss.url(filename)
        except:
            namafile = 'None'
        
        
            
        try:
            with conn.cursor() as buldings:
                buldings.execute("""UPDATE geo_data_kelurahan set geom = st_setsrid(st_geomfromgeojson('{"type":"MultiPolygon", "coordinates":[%s]}'),4326),desa = '%s', kecamatan = '%s', kab_kota = '%s', provinsi = '%s', luas_desa = %s, jumlah_pen = %s, jumlah_kk = %s, pria=%s, wanita = %s, belum_kawin = %s, kawin = %s, cerai_hidup = %s, cerai_mati = %s, wajib_ktp = %s, islam = %s, kristen = %s, khatolik = %s, hindu = %s, budha = %s, konghucu = %s, kepercayaan_lain = %s, u0 = %s, u5 = %s, u10 = %s,u15 = %s, u20 = %s, u25 = %s, u30 = %s, u35 = %s, u40 = %s, u45 = %s, u50 = %s, u55 = %s, u60 = %s, u65 = %s, u70 = %s, u75 = %s, tidak_sekolah = %s, belum_tamat = %s, tamat_sd = %s, sltp = %s, slta = %s, diploma_i = %s, diploma_ii = %s, diploma_iv = %s, strata_ii = %s, strata_iii = %s, tidak_bekerja = %s, aparatur_pemerintah = %s, tenaga_pendidik = %s, wiraswasta = %s, pertanian = %s, tenaga_kesehatan = %s, pensiunan = %s, pegawai = %s, tentara = %s, kepolisian = %s, dosen = %s, guru = %s, pilot = %s, pedagang = %s, petani = %s, peternak = %s, nelayan = %s, karyawan = %s, buruh = %s, pembantu = %s, tukang = %s, pendeta = %s, pastor = %s, ustadz = %s, pengacara = %s, notaris = %s, arsitek = %s, akuntan = %s, dokter = %s, bidan = %s, perawat = %s, psikiater = %s, sopir = %s, lainnya = %s, gambar = '%s' WHERE kode_desa = '%s'"""% (coord_new['geometry']['coordinates'],kelurahan, kecamatan, kabupaten, provinsi, luasdesa,jumlahpenduduk,jumlahkk,jumlahlakilaki,jumlahperempuan,belumkawin,kawin,ceraihidup,ceraimati, wajibktp,islam,kristen,katolik,hindu,budha,konghucu,kepercayaanlain,u0,u5,u10,u15,u20,u25,u30,u35,u40,u45,u50,u55,u60,u65,u70,u75, tidaksekolah, belumtamat, tamatsd, tamatsltp, tamatslta, diplomai, diplomaiii, diplomaiv, strataii, strataiii, tidakbekerja, aparaturpemerintah, tenagapendidik, wiraswasta, pertanian, tenagakesehatan, pensiunan, pegawai, tentara, kepolisian, dosen, guru, pilot, pedagang, petani, peternak, nelayan, karyawan, buruh, pembantu, tukang, pendeta, pastor, ustadz, pengacara, notaris, arsitek, akuntan, dokter, bidan, perawat, psikiater, sopir, lainnya, namafile, kodekelurahan))
            conn.commit()
            
            with conn.cursor() as kependuduk:
                kependuduk.execute("""SELECT kode_desa, desa, kecamatan, kab_kota, provinsi, luas_desa, jumlah_pen, jumlah_kk, 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, 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, dosen, guru, pilot, pedagang, petani, peternak, nelayan, karyawan, buruh, pembantu, tukang, pendeta, pastor, ustadz, pengacara, notaris, arsitek, akuntan, dokter, bidan, perawat, psikiater, sopir, lainnya, gambar FROM geo_data_kelurahan WHERE kode_desa = '%s'"""% kodekelurahan)
                data_kependuduk = kependuduk.fetchall()      
                     
            data = 'SUCCESS'
            info = 'data telah diperbaharui'
            status = 0
        except:
            data = 'NOT SUCCESS'
            info = 'Hubungi Orang Server'
            status = 1
            
        respon={'data':data, 'info':info, 'code':status, 'penduduk':data_kependuduk[0]}
        data = data_kependuduk[0]
        return Response(respon)

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
@api_view(('POST',))
def editsearchpoint(request):
    idsave = request.POST.get('idspo')
    dbsave = request.POST.get('dbspo')
    namobj = request.POST.get('namobj')
    luas = request.POST.get('luas')
    remark = request.POST.get('remark')
    gambar = request.POST.get('gambar')
    alamat = request.POST.get('alamat')
    
    if (dbsave == "geo_cagar_budaya" or dbsave == "geo_terminal_bus"):
        l = "luas"
    else:
        l = "luas_tanah"
    
    if alamat is None:
        alamat = 'alamat'
    elif alamat == '':
        alamat = 'alamat'
    else:
        alamat = request.POST.get('alamat')
    perangkat = request.POST.get('perangkat')
    if perangkat is None:
        perangkat = 'bpk'
    elif perangkat == '':
        perangkat = 'bpk'
    else:
        perangkat = request.POST.get('perangkat')
        
    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']
    
    file = request.FILES.get('file')
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{namobj}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        # print(file.name)
        url = fss.url(filename)
    except:
        namafile = gambar
    print(dbsave, co_edit, namobj, alamat, remark, l, luas, perangkat, namafile, idsave)
    with conn.cursor() as editspo:
        editspo.execute("""update %s set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s', alamat = '%s', remark = '%s', %s = '%s', perangkat = '%s', gambar = '%s' where id = %s"""% (dbsave, co_edit, namobj, alamat, remark, l, luas, perangkat, namafile, idsave))
    conn.commit()    
    
    with conn.cursor() as hasiledit:
        hasiledit.execute("""select namobj, alamat, remark, %s, gambar, perangkat from %s where id = %s"""% (l, dbsave, idsave))
        hasileditspo = hasiledit.fetchall()
    
    data = hasileditspo[0]
    data = {'status':'Success'}
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
1752

Muhamad's avatar
Muhamad committed
1753 1754
@api_view(('POST',))
def editrumahsakit(request):
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1755 1756
    idrs = request.POST.get('idrs')
    namars = request.POST.get('namobj')
Muhamad's avatar
Muhamad committed
1757 1758
    remark = request.POST.get('remark')
    luastanah = request.POST.get('luastanah')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1759 1760
    gambar = request.POST.get('gambar')
    alamat = request.POST.get('alamat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1761
    if alamat is None:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1762 1763 1764
        alamat = 'alamat'
    elif alamat == '':
        alamat = 'alamat'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1765 1766
    else:
        alamat = request.POST.get('alamat')
Muhamad's avatar
Muhamad committed
1767
    perangkat = request.POST.get('perangkat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1768
    if perangkat is None:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1769 1770 1771
        perangkat = 'bpk'
    elif perangkat == '':
        perangkat = 'bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1772 1773 1774 1775 1776 1777 1778
    else:
        perangkat = request.POST.get('perangkat')
    
    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']
Muhamad's avatar
Muhamad committed
1779
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1780
    file = request.FILES.get('file')
1781
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1782 1783 1784 1785 1786 1787 1788 1789
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{namars}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        # print(file.name)
        url = fss.url(filename)
    except:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1790
        namafile = gambar
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1791
    
Muhamad's avatar
Muhamad committed
1792
    with conn.cursor() as editrs:
1793
        editrs.execute("""update geo_rumah_sakit set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s', alamat = '%s', remark = '%s', luas_tanah = '%s', perangkat = '%s', gambar = '%s' where id = %s"""%(co_edit, namars, alamat, remark, luastanah, perangkat, namafile, idrs))
Muhamad's avatar
Muhamad committed
1794 1795 1796
    conn.commit()    
    
    with conn.cursor() as hasledit:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1797
        hasledit.execute("SELECT id, namobj, alamat, remark, luas_bangunan, luas_tanah, tinggi_bangunan, perangkat, gambar, json_build_object('type', 'Feature', 'geometry', ST_AsGeoJSON(geom :: geometry) :: json) from geo_rumah_sakit where id = %s"% idrs)
Muhamad's avatar
Muhamad committed
1798 1799
        hasil_editrs = hasledit.fetchall()
    
1800 1801 1802 1803 1804 1805
    data = {'status':'Success'}
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
Muhamad's avatar
Muhamad committed
1806 1807 1808

@api_view(('POST',))
def editcagarbudaya(request):
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1809
    idcagar = request.POST.get('idcb')
Muhamad's avatar
Muhamad committed
1810 1811 1812 1813 1814
    namobj = request.POST.get('namobj')
    luas = request.POST.get('luas')
    remark = request.POST.get('remark')
    gambar = request.POST.get('gambar')
    alamat = request.POST.get('alamat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1815 1816 1817 1818 1819 1820
    if alamat is None:
        alamat = 'alamat'
    elif alamat == '':
        alamat = 'alamat'
    else:
        alamat = request.POST.get('alamat')
Muhamad's avatar
Muhamad committed
1821
    perangkat = request.POST.get('perangkat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
    if perangkat is None:
        perangkat = 'bpk'
    elif perangkat == '':
        perangkat = 'bpk'
    else:
        perangkat = request.POST.get('perangkat')
        
    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']
    
    file = request.FILES.get('file')
1835
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1836 1837 1838 1839 1840 1841 1842 1843
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{namobj}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        # print(file.name)
        url = fss.url(filename)
    except:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1844
        namafile = gambar
Muhamad's avatar
Muhamad committed
1845 1846
    
    with conn.cursor() as editcagar:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1847
        editcagar.execute("""update geo_cagar_budaya set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s', alamat = '%s', remark = '%s', luas = '%s', perangkat = '%s', gambar = '%s' where id = %s"""% (co_edit, namobj, alamat, remark, luas, perangkat, namafile, idcagar))
Muhamad's avatar
Muhamad committed
1848 1849 1850 1851 1852 1853 1854
    conn.commit()    
    
    with conn.cursor() as hasiledit:
        hasiledit.execute("""select namobj, alamat, remark, luas, gambar, perangkat from geo_cagar_budaya where id = %s"""% idcagar)
        hasileditcagar = hasiledit.fetchall()
    
    data = hasileditcagar[0]
1855 1856 1857 1858 1859 1860
    data = {'status':'Success'}
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
Muhamad's avatar
Muhamad committed
1861 1862 1863 1864 1865 1866 1867

@api_view(('POST',))
def editkantoradmin(request):
    idkantor = request.POST.get('id')
    namobj = request.POST.get('namobj')
    remark = request.POST.get('remark')
    luat_tanah = request.POST.get('luastanah')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1868
    gambar = request.POST.get('gambar')
Muhamad's avatar
Muhamad committed
1869
    alamat = request.POST.get('alamat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1870
    if alamat is None:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1871 1872 1873
        alamat = 'alamat'
    elif alamat == '':
        alamat = 'alamat'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1874 1875
    else:
        alamat = request.POST.get('alamat')
Muhamad's avatar
Muhamad committed
1876
    perangkat = request.POST.get('perangkat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1877
    if perangkat is None:
1878
        perangkat = 'Bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1879
    elif perangkat == '':
1880
        perangkat = 'Bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1881 1882 1883 1884 1885 1886 1887
    else:
        perangkat = request.POST.get('perangkat')
    
    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']
Muhamad's avatar
Muhamad committed
1888

1889 1890
    file1 = request.FILES.get('file')
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1891
    try:
1892
        format = (file1.name).split(".")
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1893 1894
        num = random.randrange(1, 10000000000000)
        namafile = f"{namobj}_{num}.{format[-1:][0]}"
1895
        filename = fss.save(namafile, file1)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1896 1897 1898
        # print(file.name)
        url = fss.url(filename)
    except:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1899
        namafile = gambar
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1900

Manggar Mahardhika's avatar
Manggar Mahardhika committed
1901
    with conn.cursor() as editka:
1902
        editka.execute("""update geo_kantor_administrasi set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s',  remark = '%s', luas_tanah = %s, perangkat = '%s', alamat = '%s', gambar = '%s' where id = %s"""%(co_edit, namobj, remark, luat_tanah, perangkat, alamat, namafile, idkantor))
1903
    conn.commit()
Muhamad's avatar
Muhamad committed
1904
    
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)

@api_view(('POST',))
def deletekantoradmin(request):
    idkantor = request.POST.get('id')
    try:
        with conn.cursor() as editka:
            editka.execute("""update geo_kantor_administrasi set is_deleted = 1 where id = %s"""%(idkantor))
        conn.commit()    
        
        
        data = {'status':'Success'}
        data = 'SUCCESS'
        info = 'data telah diperbaharui'
        status = 0
    except:
        data = 'Sorry'
        info = 'Dalam Perbaikan'
        status = 1
Muhamad's avatar
Muhamad committed
1928
    
1929 1930
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
Muhamad's avatar
Muhamad committed
1931

Manggar Mahardhika's avatar
Manggar Mahardhika committed
1932 1933 1934 1935 1936 1937
@api_view(('POST',))
def editlayanankesehatan(request):
    idlakes = request.POST.get('id')
    namobj = request.POST.get('namobj')
    remark = request.POST.get('remark')
    luat_tanah = request.POST.get('luastanah')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1938
    gambar = request.POST.get('gambar')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1939 1940
    alamat = request.POST.get('alamat')
    if alamat is None:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1941 1942 1943
        alamat = 'alamat'
    elif alamat == '':
        alamat = 'alamat'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1944 1945 1946 1947
    else:
        alamat = request.POST.get('alamat')
    perangkat = request.POST.get('perangkat')
    if perangkat is None:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1948 1949 1950
        perangkat = 'bpk'
    elif perangkat == '':
        perangkat = 'bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1951 1952 1953 1954 1955 1956 1957 1958
    else:
        perangkat = request.POST.get('perangkat')
    
    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']

Manggar Mahardhika's avatar
Manggar Mahardhika committed
1959
    file = request.FILES.get('file')
1960
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1961 1962 1963 1964 1965 1966 1967 1968
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{namobj}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        # print(file.name)
        url = fss.url(filename)
    except:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1969
        namafile = gambar
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1970
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1971
    with conn.cursor() as editka:
1972
        editka.execute("""update geo_layanan_kesehatan set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s',  remark = '%s', luas_tanah = %s, perangkat = '%s', alamat = '%s', gambar = '%s' where id = %s"""%(co_edit, namobj, remark, luat_tanah, perangkat, alamat, namafile, idlakes))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1973 1974 1975 1976 1977 1978
    conn.commit()    
    
    with conn.cursor() as hasiledit:
        hasiledit.execute("""select namobj,  remark, luas_bangunan, luas_tanah, tinggi_bangunan, perangkat, gambar, alamat from geo_layanan_kesehatan where id = %s"""% idlakes)
        hasileditkantor = hasiledit.fetchall()
    
1979 1980 1981 1982 1983 1984
    data = {'status':'Success'}
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1985

Muhamad's avatar
Muhamad committed
1986 1987
@api_view(('POST',))
def editpuskesmas(request):
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1988
    idpus = request.POST.get('idpus')
Muhamad's avatar
Muhamad committed
1989 1990 1991
    namobj = request.POST.get('namobj')
    remark = request.POST.get('remark')
    luat_tanah = request.POST.get('luastanah')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1992
    gambar = request.POST.get('gambar')
Muhamad's avatar
Muhamad committed
1993
    alamat = request.POST.get('alamat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1994
    if alamat is None:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1995 1996 1997
        alamat = 'alamat'
    elif alamat == '':
        alamat = 'alamat'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1998 1999
    else:
        alamat = request.POST.get('alamat')
Muhamad's avatar
Muhamad committed
2000
    perangkat = request.POST.get('perangkat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2001
    if perangkat is None:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2002 2003 2004
        perangkat = 'bpk'
    elif perangkat == '':
        perangkat = 'bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2005 2006 2007 2008 2009 2010 2011
    else:
        perangkat = request.POST.get('perangkat')
    
    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']
Muhamad's avatar
Muhamad committed
2012

Manggar Mahardhika's avatar
Manggar Mahardhika committed
2013
    file = request.FILES.get('file')
2014
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2015 2016 2017 2018 2019 2020 2021 2022
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{namobj}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        # print(file.name)
        url = fss.url(filename)
    except:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2023
        namafile = gambar
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2024

Muhamad's avatar
Muhamad committed
2025
    with conn.cursor() as editrs:
2026
        editrs.execute("""update geo_puskesmas set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s', remark = '%s', luas_tanah = '%s', perangkat = '%s', alamat = '%s', gambar = '%s' where id = %s"""%(co_edit, namobj, remark, luat_tanah, perangkat, alamat, namafile, idpus))
Muhamad's avatar
Muhamad committed
2027 2028 2029 2030 2031 2032
    conn.commit()    
    
    with conn.cursor() as hasiledit:
        hasiledit.execute("""select namobj,  remark, luas_bangunan, luas_tanah, tinggi_bangunan, perangkat, gambar, alamat from geo_puskesmas where id = %s"""% idpus)
        hasileditkantor = hasiledit.fetchall()
    
2033 2034 2035 2036 2037 2038
    data = {'status':'Success'}
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
Muhamad's avatar
Muhamad committed
2039 2040 2041

@api_view(('POST',))
def editibadah(request):
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2042
    idsarib = request.POST.get('idsi')
Muhamad's avatar
Muhamad committed
2043 2044 2045
    namobj = request.POST.get('namobj')
    remark = request.POST.get('remark')
    luat_tanah = request.POST.get('luastanah')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2046
    gambar = request.POST.get('gambar')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2047 2048
    alamat = request.POST.get('alamat')
    perangkat = request.POST.get('perangkat')
Muhamad's avatar
Muhamad committed
2049
    alamat = request.POST.get('alamat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2050
    if alamat is None:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2051 2052 2053
        alamat = 'alamat'
    elif alamat == '':
        alamat = 'alamat'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2054 2055
    else:
        alamat = request.POST.get('alamat')
Muhamad's avatar
Muhamad committed
2056
    perangkat = request.POST.get('perangkat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2057
    if perangkat is None:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2058 2059 2060
        perangkat = 'bpk'
    elif perangkat == '':
        perangkat = 'bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2061 2062 2063 2064 2065 2066 2067
    else:
        perangkat = request.POST.get('perangkat')
    
    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2068 2069
    
    file = request.FILES.get('file')
2070
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2071 2072 2073 2074 2075 2076 2077 2078
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{namobj}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        # print(file.name)
        url = fss.url(filename)
    except:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2079
        namafile = gambar
Muhamad's avatar
Muhamad committed
2080 2081

    with conn.cursor() as editrs:
2082
        editrs.execute("""update geo_sarana_ibadah set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s',  remark = '%s', luas_tanah = '%s', perangkat = '%s', alamat = '%s', gambar = '%s' where id = %s"""%(co_edit, namobj, remark, luat_tanah, perangkat, alamat, namafile, idsarib))
Muhamad's avatar
Muhamad committed
2083 2084 2085 2086 2087 2088
    conn.commit()    
    
    with conn.cursor() as hasiledit:
        hasiledit.execute("""select namobj,  remark, luas_bangunan, luas_tanah, tinggi_bangunan, perangkat, gambar, alamat from geo_sarana_ibadah where id = %s"""% idsarib)
        hasileditkantor = hasiledit.fetchall()
    
2089 2090 2091 2092 2093 2094
    data = {'status':'Success'}
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
Muhamad's avatar
Muhamad committed
2095 2096 2097

@api_view(('POST',))
def editsekolah(request):
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2098
    idsekolah = request.POST.get('idsp')
Muhamad's avatar
Muhamad committed
2099 2100 2101
    namobj = request.POST.get('namobj')
    remark = request.POST.get('remark')
    luat_tanah = request.POST.get('luastanah')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2102
    gambar = request.POST.get('gambar')
Muhamad's avatar
Muhamad committed
2103
    alamat = request.POST.get('alamat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2104
    if alamat is None:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2105 2106 2107
        alamat = 'alamat'
    elif alamat == '':
        alamat = 'alamat'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2108 2109
    else:
        alamat = request.POST.get('alamat')
Muhamad's avatar
Muhamad committed
2110
    perangkat = request.POST.get('perangkat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2111
    if perangkat is None:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2112 2113 2114
        perangkat = 'bpk'
    elif perangkat == '':
        perangkat = 'bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2115 2116 2117 2118 2119 2120 2121
    else:
        perangkat = request.POST.get('perangkat')
    
    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2122 2123
    
    file = request.FILES.get('file')
2124
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2125 2126 2127 2128 2129 2130 2131 2132
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{namobj}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        # print(file.name)
        url = fss.url(filename)
    except:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2133
        namafile = gambar
Muhamad's avatar
Muhamad committed
2134 2135

    with conn.cursor() as editrs:
2136
        editrs.execute("""update geo_sarana_pendidikan set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s',  remark = '%s', luas_tanah = '%s', perangkat = '%s', alamat = '%s', gambar = '%s' where id = %s"""%(co_edit, namobj, remark, luat_tanah, perangkat, alamat, namafile, idsekolah))
Muhamad's avatar
Muhamad committed
2137 2138 2139 2140 2141 2142 2143 2144
    conn.commit()    
    
    with conn.cursor() as hasiledit:
        hasiledit.execute("""select namobj,  remark, luas_bangunan, luas_tanah, tinggi_bangunan, perangkat, gambar, alamat from geo_sarana_ibadah where id = %s"""% idsekolah)
        hasileditkantor = hasiledit.fetchall()
    
    data = hasileditkantor[0]

2145 2146 2147 2148 2149 2150
    data = {'status':'Success'}
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
Muhamad's avatar
Muhamad committed
2151 2152 2153

@api_view(('POST',))
def editspbu(request):
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2154
    idspbu = request.POST.get('idspbu')
Muhamad's avatar
Muhamad committed
2155 2156
    namobj = request.POST.get('namobj')
    remark = request.POST.get('remark')
2157
    # luas_bangunan = request.POST.get('luasbangunan')
Muhamad's avatar
Muhamad committed
2158
    luat_tanah = request.POST.get('luastanah')
2159
    # tinggi_bangunan = request.POST.get('tinggibangunan')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2160
    gambar = request.POST.get('gambar')
Muhamad's avatar
Muhamad committed
2161
    alamat = request.POST.get('alamat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2162 2163
    if alamat is None:
        alamat = 'alamat'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2164 2165
    elif alamat == '':
        alamat = 'alamat'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2166 2167
    else:
        alamat = request.POST.get('alamat')
Muhamad's avatar
Muhamad committed
2168
    perangkat = request.POST.get('perangkat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2169 2170
    if perangkat is None:
        perangkat = 'bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2171 2172
    elif perangkat == '':
        perangkat = 'bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2173 2174 2175 2176 2177 2178 2179
    else:
        perangkat = request.POST.get('perangkat')

    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2180 2181
    
    file = request.FILES.get('file')
2182
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2183 2184 2185 2186 2187 2188 2189 2190
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{namobj}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        # print(file.name)
        url = fss.url(filename)
    except:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2191
        namafile = gambar
Muhamad's avatar
Muhamad committed
2192 2193

    with conn.cursor() as editrs:
2194
        editrs.execute("""update geo_spbu set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s',  remark = '%s', luas_tanah = '%s', perangkat = '%s', alamat = '%s', gambar = '%s' where id = %s"""%(co_edit, namobj, remark, luat_tanah, perangkat, alamat, namafile, idspbu))
Muhamad's avatar
Muhamad committed
2195 2196 2197
    conn.commit()    
    
    with conn.cursor() as hasiledit:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2198
        hasiledit.execute("""select namobj,  remark, luas_bangunan, luas_tanah, tinggi_bangunan, perangkat, gambar, alamat from geo_spbu where id = %s"""% idspbu)
Muhamad's avatar
Muhamad committed
2199 2200 2201
        hasileditkantor = hasiledit.fetchall()
    
    data = hasileditkantor[0]
2202 2203 2204 2205 2206 2207 2208
    
    data = {'status':'Success'}
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
Muhamad's avatar
Muhamad committed
2209 2210 2211

@api_view(('POST',))
def editstasiun(request):
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2212
    idstasiun = request.POST.get('idst')
Muhamad's avatar
Muhamad committed
2213 2214 2215
    namobj = request.POST.get('namobj')
    remark = request.POST.get('remark')
    luat_tanah = request.POST.get('luastanah')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2216
    gambar = request.POST.get('gambar')
Muhamad's avatar
Muhamad committed
2217
    alamat = request.POST.get('alamat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2218 2219
    if alamat is None:
        alamat = 'alamat'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2220 2221
    elif alamat == '':
        alamat = 'alamat'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2222 2223
    else:
        alamat = request.POST.get('alamat')
Muhamad's avatar
Muhamad committed
2224
    perangkat = request.POST.get('perangkat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2225 2226
    if perangkat is None:
        perangkat = 'bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2227 2228
    elif perangkat == '':
        perangkat = 'bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2229 2230 2231 2232 2233 2234 2235
    else:
        perangkat = request.POST.get('perangkat')

    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2236 2237
    
    file = request.FILES.get('file')
2238
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2239 2240 2241 2242 2243 2244 2245 2246
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{namobj}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        # print(file.name)
        url = fss.url(filename)
    except:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2247
        namafile = gambar
Muhamad's avatar
Muhamad committed
2248 2249

    with conn.cursor() as editrs:
2250
        editrs.execute("""update geo_stasiun set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s',  remark = '%s', luas_tanah = '%s', perangkat = '%s', alamat = '%s', gambar = '%s' where id = %s"""%(co_edit, namobj, remark, luat_tanah, perangkat, alamat, namafile, idstasiun))
Muhamad's avatar
Muhamad committed
2251 2252 2253
    conn.commit()    
    
    with conn.cursor() as hasiledit:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2254
        hasiledit.execute("""select namobj,  remark, luas_bangunan, luas_tanah, tinggi_bangunan, perangkat, gambar, alamat from geo_stasiun where id = %s"""% idstasiun)
Muhamad's avatar
Muhamad committed
2255 2256 2257 2258
        hasileditkantor = hasiledit.fetchall()
    
    data = hasileditkantor[0]

2259 2260 2261 2262 2263 2264
    data = {'status':'Success'}
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
Muhamad's avatar
Muhamad committed
2265 2266 2267

@api_view(('POST',))
def editterminal(request):
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2268 2269 2270 2271
    idterm = request.POST.get('idtr')
    namobj = request.POST.get('namobj')
    remark = request.POST.get('remark')
    luas = request.POST.get('luas')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2272
    gambar = request.POST.get('gambar')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2273 2274 2275
    alamat = request.POST.get('alamat')
    if alamat is None:
        alamat = 'alamat'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2276 2277
    elif alamat == '':
        alamat = 'alamat'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2278 2279
    else:
        alamat = request.POST.get('alamat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2280
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2281 2282 2283
    perangkat = request.POST.get('perangkat')
    if perangkat is None:
        perangkat = 'bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2284 2285
    elif perangkat == '':
        perangkat = 'bpk'
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2286 2287 2288 2289 2290 2291 2292
    else:
        perangkat = request.POST.get('perangkat')

    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2293 2294
    
    file = request.FILES.get('file')
2295
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2296 2297 2298 2299 2300 2301 2302 2303
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{namobj}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        # print(file.name)
        url = fss.url(filename)
    except:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2304
        namafile = gambar
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2305 2306

    with conn.cursor() as editrs:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2307
        editrs.execute("""update geo_terminal_bus set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s',  remark = '%s', luas = '%s', perangkat = '%s', alamat = '%s', gambar = '%s' where id = %s"""%(co_edit, namobj, remark, luas, perangkat, alamat, namafile, idterm))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2308 2309 2310 2311 2312 2313 2314 2315
    conn.commit()    
    
    with conn.cursor() as hasiledit:
        hasiledit.execute("""select namobj, remark, luas, perangkat, gambar, alamat from geo_terminal_bus where id = %s"""% idterm)
        hasileditkantor = hasiledit.fetchall()
    
    data = hasileditkantor[0]

2316 2317 2318 2319 2320 2321
    data = {'status':'Success'}
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2322 2323 2324 2325

@api_view(('POST',))
def editgardu(request):
    idgardu = request.POST.get('idgl')
Muhamad's avatar
Muhamad committed
2326 2327 2328
    namobj = request.POST.get('namobj')
    remark = request.POST.get('remark')
    luat_tanah = request.POST.get('luastanah')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2329
    gambar = request.POST.get('gambar')
Muhamad's avatar
Muhamad committed
2330
    alamat = request.POST.get('alamat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2331 2332 2333 2334
    if alamat is None:
        alamat = 'alamat'
    else:
        alamat = request.POST.get('alamat')
Muhamad's avatar
Muhamad committed
2335
    perangkat = request.POST.get('perangkat')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2336 2337 2338 2339 2340 2341 2342 2343 2344
    if perangkat is None:
        perangkat = 'bpk'
    else:
        perangkat = request.POST.get('perangkat')

    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2345 2346
    
    file = request.FILES.get('file')
2347
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2348 2349 2350 2351 2352 2353 2354 2355
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{namobj}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        # print(file.name)
        url = fss.url(filename)
    except:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2356
        namafile = gambar
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2357

Muhamad's avatar
Muhamad committed
2358 2359

    with conn.cursor() as editrs:
2360
        editrs.execute("""update geo_gardu_listrik set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s',  remark = '%s', luas_tanah = '%s', perangkat = '%s', alamat = '%s', gambar = '%s' where id = %s"""%(co_edit, namobj, remark, luat_tanah, perangkat, alamat, namafile, idgardu))
Muhamad's avatar
Muhamad committed
2361 2362 2363
    conn.commit()    
    
    with conn.cursor() as hasiledit:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2364
        hasiledit.execute("""select namobj, remark, luas_bangunan, luas_tanah, tinggi_bangunan, perangkat, gambar, alamat from geo_gardu_listrik where id = %s"""% idgardu)
Muhamad's avatar
Muhamad committed
2365 2366 2367
        hasileditkantor = hasiledit.fetchall()
    
    data = hasileditkantor[0]
2368 2369 2370 2371 2372 2373
    data = {'status':'Success'}
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
Muhamad's avatar
Muhamad committed
2374

Muhamad's avatar
Muhamad committed
2375 2376
@api_view(('GET',))
@group_required(('Dinas Pekerjaan Umum dan Penataan Ruang', 'Dinas Perumahan dan Kawasan Permukiman', 'Dinas Lingkungan Hidup'), login_url='/')
Muhamad's avatar
Muhamad committed
2377
def edittuplah(request):
Muhamad's avatar
Muhamad committed
2378 2379 2380 2381
    idtuplah = request.GET.get('idtuplah')

    try:
        with conn.cursor() as desa:
2382
            desa.execute(f"SELECT json_build_object('type', 'Feature', 'legend', (a.id, a.legenda, st_area(a.geom::geography), a.gambar) ,'geometry', ST_AsGeoJSON( st_intersection(a.geom, b.geom):: geometry) :: json) from geo_tuplah a, geo_prov_kab b where a.id=%s and b.kabupaten = 'OGAN KOMERING ULU'"% idtuplah)
Muhamad's avatar
Muhamad committed
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
            all = desa.fetchall()    
            
        data = 'SUCCESS'
        info = 'data telah diperbaharui'
        status = 0
    except:
        data = 'Sorry'
        info = 'Anda Dilarang Akses Link ini'
        status = 1
        
    respon = {
        'data':data, 
        'info':info, 
        'code':status,
        'tuplah':all[0][0]
    }
        
    return Response(respon)
Muhamad's avatar
Muhamad committed
2401 2402

@api_view(('POST',))
Muhamad's avatar
Muhamad committed
2403 2404
@group_required(('Dinas Pekerjaan Umum dan Penataan Ruang', 'Dinas Perumahan dan Kawasan Permukiman', 'Dinas Lingkungan Hidup'),login_url='/')
def edittuplahvalid(request):
Muhamad's avatar
Muhamad committed
2405
    file = request.FILES.get('file')
Muhamad's avatar
Muhamad committed
2406 2407 2408 2409
    idtuplah = request.POST.get('idtuplah')
    cordi = request.POST.get('cord')
    cord = json.loads(cordi)
    
2410
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/image/tuplah/')
Muhamad's avatar
Muhamad committed
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444
    try:
        format = (file.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{idtuplah}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file)
        url = fss.url(filename)
    except:
        namafile = "NONE"
        
    try:
        with conn.cursor() as desa:
            desa.execute("""UPDATE geo_tuplah set geom = st_setsrid(st_geomfromgeojson('{"type":"MultiPolygon", "coordinates":[[%s]]}'),4326), gambar = '%s' where id=%s"""%(cord['geometry']['coordinates'][0], namafile, int(idtuplah)))
        conn.commit()
            
        data = 'SUCCESS'
        info = 'data telah diperbaharui'
        status = 0
    except:
        data = 'Sorry'
        info = 'Hubungi Developer'
        status = 1
        
    
    
    respon={'data':data, 'info':info, 'code':status}
    return Response(respon)

@api_view(('GET',))
def maps3d(request):
    lattt = request.GET.get('latt')
    longg = request.GET.get('loog')
    
    print(lattt, type(longg))
    with conn.cursor() as gp3:
2445
        gp3.execute("""select json_build_object('type', 'Feature', 'properties', (id, name, information), 'administrasi',(address_1, address_2, address_3, provinsi, alamat, luas_bangunan, luas_tanah, tinggi_bangunan, gambar, pemilik, tampak_atas, tampak_depan), 'geometry', ST_AsGeoJSON(geom :: geometry) :: json) from geo_bangunan WHERE ST_DistanceSphere(geom, ST_MakePoint(%s,%s)) <= 400 LIMIT 1000"""%(float(longg), float(lattt)))
Muhamad's avatar
Muhamad committed
2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474
        polygon_buildings = gp3.fetchall()
        
        polygon_o = []
        info_polygon = []
        tinggi_pol = []
        for i in polygon_buildings:
            o_pol1 = ''
            o_poly = []
            for x in i[0]['geometry']['coordinates'][0][0]:
                o_pol1 += (f'{x[0]},{x[1]}""')
            
            o_pol2 = o_pol1.replace('""1',',1')

            o_poly.append(o_pol2.replace('""',''))
            
            polygon_o.append(o_poly)
            tinggi_pol.append(i[0]['administrasi']['f8'])
            info_polygon.append(i[0]['properties']['f1']) 
                    
    respon={'polygon_o':polygon_o,'tinggi_pol':tinggi_pol, 'info_polygon': info_polygon}
    return Response(respon)

@api_view(('GET',))
def createbang(request):
    poly_create = request.GET.get('polygon')
    p_create = poly_create.replace('"','')
    po_create = p_create.replace(', ','],[')
    
    name = request.GET.get('name')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2475
    tipedb = request.GET.get('tipedb')
2476 2477 2478 2479 2480
    poin = request.GET.get('poin_id')
    print(poin)
    nama_poin = request.GET.get('nama_po')
    print(nama_poin)
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2481
    remark = request.GET.get('remark')
Muhamad's avatar
Muhamad committed
2482
    info = request.GET.get('info')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2483
    atalam = request.GET.get('alamat')
Muhamad's avatar
Muhamad committed
2484 2485
    lb = request.GET.get('lb')
    lt = request.GET.get('lt')
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2486 2487 2488
    tibang = request.GET.get('tb')
    ltbang = request.GET.get('ltb')
    kilimep = request.GET.get('pemilik')
Muhamad's avatar
Muhamad committed
2489 2490 2491
    namafile = "None"
    
    latlng = po_create.split("],[")[0]
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2492 2493
    print(p_create)
    print(latlng)
Muhamad's avatar
Muhamad committed
2494
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2495 2496 2497 2498
    if name == "":
        name = "None"
    else:
        name = name
Muhamad's avatar
Muhamad committed
2499
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
    if info == "":
        info = "None"
    else:
        info = info
        
    if lb == "":
        lb = 0
    else:
        lb = lb
        
    if lt == "":
        lt = 0
    else:
        lt = lt
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2515 2516 2517 2518
    if tipedb == "":
        tdb = "bangunan"
    else:
        tdb = tipedb
2519 2520 2521 2522 2523
        
    if remark == "":
        rem = "bangunan"
    else:
        rem = remark
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2524
    
2525
    if str(poin) == "":
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2526 2527 2528
        pointopeto = 0
    else:
        pointopeto = poin
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2529
    
2530 2531
    if str(nama_poin) == "":
        napoi = "-"
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2532
    else:
2533
        napoi = nama_poin
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2534 2535 2536 2537 2538
    
    if atalam == "":
        alamat = "None"
    else:
        alamat = atalam
2539
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2540 2541
    try:
        if tibang == "" and ltbang != "":
2542
            tb = int(int(ltbang)*3)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2543 2544 2545 2546 2547
        elif tibang == "" and ltbang == "":
            tb = 0
            ltb = 0
        else:
            tb = tibang
Muhamad's avatar
Muhamad committed
2548
    except:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2549 2550
        tb = 0
    print(tb)
2551
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
    try:
        if ltbang == "" and tibang != "":
            ltb = int(int(tibang)/3)
        elif tibang == "" and ltbang == "":
            tb = 0
            ltb = 0
        else:
            ltb = ltbang
    except:
        ltb = 0
2562 2563
    print(ltb)
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
    if kilimep == "":
        pemilik = "unnamed"
    else:
        pemilik = kilimep
    print(pemilik)
    
    
    # try:
    with conn.cursor() as kel:
        kel.execute("SELECT desa, kecamatan, kab_kota, provinsi FROM geo_data_kelurahan WHERE st_intersects(geom, 'SRID=4326;POINT(%s)'::geometry)"% latlng)
        adm = kel.fetchall()
        
    administrasi = adm[0]
            
    with conn.cursor() as buldings:
2579
        buldings.execute("""INSERT INTO geo_bangunan(geom, name, information, sumber, alamat, address_1, address_2, address_3, provinsi, no, luas_bangunan, luas_tanah, tinggi_bangunan, lantai, gambar, pemilik, point_id, point_name, type_id, remark, db_tipe) VALUES (ST_GeomFromEWKT('SRID=0;MULTIPOLYGON(((%s)))'), '%s', '%s', 'new', '%s', '%s', '%s', '%s', '%s', '0', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '1', '%s', '%s')"""% (p_create, name, info, alamat, administrasi[0], administrasi[1], administrasi[2], administrasi[3], lb, lt, tb, ltb, namafile, pemilik, pointopeto, napoi, rem, tdb))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2580 2581 2582 2583 2584 2585 2586 2587 2588 2589
    conn.commit()
    data = {'status':'Success'}
    
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    # except:
    #     data = 'Sorry'
    #     info = 'Dalam Perbaikan'
    #     status = 1
Muhamad's avatar
Muhamad committed
2590 2591 2592 2593
    
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)

2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
@api_view(('POST',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def upload_tampak(request):
    if request.method == 'POST':
        name_b = request.POST.get("name_b")
        
        id_poly = request.POST.get("id_poly")
        gmb_a = request.POST.get("gambar_atas")
        gmb_d = request.POST.get("gambar_depan")

        fileatas = request.FILES.get('fileatas')
        filedepan = request.FILES.get('filedepan')
        fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/image/bangunan/3d/')

        try:
            try:
                format = (fileatas.name).split(".")
                num = random.randrange(1, 10000000000000)
                namafileatas = f"{name_b}_{num}.{format[-1:][0]}"
                filenamea = fss.save(namafileatas, fileatas)
                urla = fss.url(filenamea)
            except:
                namafileatas = gmb_a
            
            try:
                format = (filedepan.name).split(".")
                num = random.randrange(1, 10000000000000)
                namafiledepan = f"{name_b}_{num}.{format[-1:][0]}"
                filenamed = fss.save(namafiledepan, filedepan)
                urld = fss.url(filenamed)
            except:
                namafiledepan = gmb_d
            
            with conn.cursor() as buldings:
                buldings.execute("""UPDATE geo_bangunan set tampak_atas='%s', tampak_depan='%s' WHERE id = %s"""% (namafileatas, namafiledepan, int(id_poly)))
                conn.commit()
                
                data = 'SUCCESS'
                info = 'data telah diperbaharui'
                status = 0
            
        except:
            data = 'SORRY'
            info = 'hubungi orang terdekat'
            status = 1
            
        respon = {
            'data':data, 
            'info':info, 
            'code':status,
        }
        print(respon)
            
        return Response(respon)

2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
@api_view(('GET',))
def createlahan(request):
    poly_create = request.GET.get('polygon')
    p_create = poly_create.replace('"','')
    po_create = p_create.replace(', ','],[')
    
    name = request.GET.get('name')
    tipelh = request.GET.get('tipelh')
    tahun = request.GET.get('tahun')
    luas = request.GET.get('luas')
    obid = 666
    provinsi = "Sumatera Selatan"
    
    gambar = "NONE"
    
    latlng = po_create.split("],[")[0]
    print(p_create)
    print(latlng)
    
    if name == "":
        name = "Sumatera Selatan"
    else:
        name = name
              
    with conn.cursor() as lahan:
        lahan.execute("""INSERT INTO geo_tuplah(geom, objectid_1, name, provinsi, pl_19_r, legenda, gambar) VALUES (ST_GeomFromEWKT('SRID=4326;MULTIPOLYGON(((%s)))'), '%s', '%s', '%s', '%s', '%s', '%s')"""% (p_create, obid, name, provinsi, tahun, tipelh, gambar))
    conn.commit()
    data = {'status':'Success'}
    
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    # except:
    #     data = 'Sorry'
    #     info = 'Dalam Perbaikan'
    #     status = 1
    
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)
2688

Manggar Mahardhika's avatar
Manggar Mahardhika committed
2689
import re
2690 2691 2692 2693 2694 2695 2696 2697 2698
@api_view(('POST',))
def createjalan(request):
    polyline = request.POST.get('polyline')
    name_jalan = request.POST.get('name_jalan')
    panjang_jalan = request.POST.get('panjang_jalan')
    lebar_jalan = request.POST.get('lebar_jalan')
    tipe_jalan = request.POST.get('tipe_jalan')
    img_jalan = request.FILES.get('gambar')
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2699 2700 2701 2702
    panjang_jalan = re.sub('[^0-9.]+', '', str(panjang_jalan))
    if lebar_jalan == '':
        lebar_jalan = 0
    print(panjang_jalan)
2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729
    coord = json.loads(str(polyline))
    print(coord)
    
    try:
        if coord['features'][0]['geometry']['type'] == 'LineString':
            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})'
    except:
        if coord['geometry']['type'] == 'LineString':
            
            ll = str(coord["geometry"]['coordinates'])
                
            ll = ll.replace(", "," ")
            ll = ll.replace("]][[","),(")
            ll = ll.replace("] [",", ")
            ll = ll.replace("]]",")")
            ll = ll.replace("[[","(")

            coordinat = f'MULTILINESTRING({ll})'
2730
    fss_p = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/image/jalan/')
2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
    try:
        format = (img_jalan.name).split(".")
        num = random.randrange(1, 10000000000000)
        ngambar = f"{name_jalan}_{num}.{format[-1:][0]}"
        ngambar = ngambar.replace(" ","-")
        filename_p = fss_p.save(ngambar, img_jalan)
        url_p = fss_p.url(filename_p)
    except:
        ngambar = 'None'
    
    try:
                
        with conn.cursor() as jalan:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2744
            jalan.execute("""INSERT INTO geo_jalan(geom, namrjl, panjang_jalan, lebar_jalan, remark, gambar) VALUES (ST_GeomFromEWKT('SRID=4326;%s'), '%s', %s, %s,'%s','%s')"""% (coordinat, name_jalan, panjang_jalan, lebar_jalan, tipe_jalan, ngambar))
2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757
        conn.commit()
        
        data = 'SUCCESS'
        info = 'data telah diperbaharui'
        status = 0
    except:
        data = 'Opss..!!'
        info = 'Hubungi Developer'
        status = 1
    
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)

Manggar Mahardhika's avatar
Manggar Mahardhika committed
2758 2759 2760 2761
@api_view(('GET',))
def changetipe(request):
    tipe_point = request.GET.get('tipe')
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2762 2763 2764 2765
    if tipe_point == 'bangunan':
            tipepoint = [['Rumah'],['Ruko'],['Pabrik'],['Pasar/Swalayan']]
    
    if tipe_point == 'kantor_administrasi':
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2766 2767 2768 2769
        with conn.cursor() as point:
            point.execute("""SELECT DISTINCT remark FROM geo_kantor_administrasi""")
            tipepoint = point.fetchall()
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2770
    if tipe_point == 'layanan_kesehatan':
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2771 2772 2773 2774 2775 2776 2777 2778 2779
        with conn.cursor() as point:
            point.execute("""SELECT DISTINCT remark FROM geo_layanan_kesehatan""")
            tipepoint = point.fetchall()

    if tipe_point == 'puskesmas':
        with conn.cursor() as point:
            point.execute("""SELECT DISTINCT remark FROM geo_puskesmas""")
            tipepoint = point.fetchall()
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2780
    if tipe_point == 'rumah_sakit':
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2781 2782 2783 2784
        with conn.cursor() as point:
            point.execute("""SELECT DISTINCT remark FROM geo_rumah_sakit""")
            tipepoint = point.fetchall()
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2785
    if tipe_point == 'sarana_ibadah':
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2786 2787 2788 2789
        with conn.cursor() as point:
            point.execute("""SELECT DISTINCT remark FROM geo_sarana_ibadah""")
            tipepoint = point.fetchall()
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2790
    if tipe_point == 'sarana_pendidikan':
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2791 2792 2793 2794
        with conn.cursor() as point:
            point.execute("""SELECT DISTINCT remark FROM geo_sarana_pendidikan""")
            tipepoint = point.fetchall()
            
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2795
    if tipe_point == 'cagar_budaya':
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
        with conn.cursor() as point:
            point.execute("""SELECT DISTINCT remark FROM geo_cagar_budaya""")
            tipepoint = point.fetchall()

    if tipe_point == 'spbu':
        with conn.cursor() as point:
            point.execute("""SELECT DISTINCT remark FROM geo_spbu""")
            tipepoint = point.fetchall()
            
    if tipe_point == 'stasiun':
        with conn.cursor() as point:
            point.execute("""SELECT DISTINCT remark FROM geo_stasiun""")
            tipepoint = point.fetchall()
            
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2810
    if tipe_point == 'terminal_bus':
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2811 2812 2813 2814
        with conn.cursor() as point:
            point.execute("""SELECT DISTINCT remark FROM geo_terminal_bus""")
            tipepoint = point.fetchall()
            
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2815
    if tipe_point == 'gardu_listrik':
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2816 2817 2818 2819 2820 2821
        with conn.cursor() as point:
            point.execute("""SELECT DISTINCT remark FROM geo_gardu_listrik""")
            tipepoint = point.fetchall()
    print(tipepoint)
    respon = {'result':tipepoint}
    return Response(respon)
2822

Manggar Mahardhika's avatar
Manggar Mahardhika committed
2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835
@api_view(('GET',))
def changejenis(request):
    tipe_point = request.GET.get('tipe')
    jenis_point = request.GET.get('jenis')
    if tipe_point == 'bangunan':
        tipepoint1 = [['0','-']]
    else:
        with conn.cursor() as point:
            point.execute("""SELECT id, namobj FROM geo_%s where remark = '%s'"""% (tipe_point, jenis_point))
            tipepoint1 = point.fetchall()
    respon = {'result':tipepoint1}
    return Response(respon)

2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
@api_view(('POST',))
def createpoint(request):
    gambar = request.FILES.get('gambar')
    co_point = request.POST.get('point')
    name_create_point = request.POST.get('name_create_point')
    tipe_point = request.POST.get('tipe_point')
    info_create_point = request.POST.get('info_create_point')
    alamat_create_point = request.POST.get('alamat_create_point')
    luas_tanah_point = request.POST.get('luas_tanah_point')
    pemilik_point = request.POST.get('pemilik_point')
    
2847 2848
    print(tipe_point)
    print(info_create_point)
2849 2850 2851 2852 2853
    
    coord = json.loads(str(co_point))
    
    coordinat = coord['geometry']['coordinates']
    
2854
    fss_p = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
    try:
        format = (gambar.name).split(".")
        num = random.randrange(1, 10000000000000)
        ngambar = f"{name_create_point}_{num}.{format[-1:][0]}"
        ngambar = ngambar.replace(" ","-")
        filename_p = fss_p.save(ngambar, gambar)
        url_p = fss_p.url(filename_p)
    except:
        ngambar = 'None'
    
    # update geo_rumah_sakit set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s', alamat = '%s', remark = '%s', luas_bangunan = '%s', luas_tanah = '%s', tinggi_bangunan = '%s', perangkat = '%s', gambar = '%s' where id = %s
    try:
2867
        if tipe_point == 'kantor_administrasi':
2868
            with conn.cursor() as point:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2869
                point.execute("""INSERT INTO geo_kantor_administrasi(geom, namobj, remark, luas_tanah, alamat, perangkat, type_id, gambar) VALUES (st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), '%s','%s', %s, '%s', '%s', 5,'%s')"""% (coordinat, name_create_point, info_create_point, luas_tanah_point, alamat_create_point, pemilik_point, ngambar))
2870 2871
            conn.commit()
            
2872
        if tipe_point == 'layanan_kesehatan':
2873
            with conn.cursor() as point:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2874
                point.execute("""INSERT INTO geo_layanan_kesehatan(geom, namobj, remark, luas_tanah, alamat, perangkat, type_id, gambar) VALUES (st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), '%s','%s', %s, '%s', '%s', 6,'%s')"""% (coordinat, name_create_point, info_create_point, luas_tanah_point, alamat_create_point, pemilik_point, ngambar))
2875 2876 2877 2878
            conn.commit()

        if tipe_point == 'puskesmas':
            with conn.cursor() as point:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2879
                point.execute("""INSERT INTO geo_puskesmas(geom, namobj, remark, luas_tanah, alamat, perangkat, type_id, gambar) VALUES (st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), '%s','%s', %s, '%s', '%s', 7,'%s')"""% (coordinat, name_create_point, info_create_point, luas_tanah_point, alamat_create_point, pemilik_point, ngambar))
2880 2881
            conn.commit()
            
2882
        if tipe_point == 'rumah_sakit':
2883
            with conn.cursor() as point:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2884
                point.execute("""INSERT INTO geo_rumah_sakit(geom, namobj, remark, luas_tanah, alamat, perangkat, type_id, gambar) VALUES (st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), '%s','%s', %s, '%s', '%s', 8,'%s')"""% (coordinat, name_create_point, info_create_point, luas_tanah_point, alamat_create_point, pemilik_point, ngambar))
2885 2886
            conn.commit()
            
2887
        if tipe_point == 'sarana_ibadah':
2888
            with conn.cursor() as point:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2889
                point.execute("""INSERT INTO geo_sarana_ibadah(geom, namobj, remark, luas_tanah, alamat, perangkat, type_id, gambar) VALUES (st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), '%s','%s', %s, '%s', '%s', 9,'%s')"""% (coordinat, name_create_point, info_create_point, luas_tanah_point, alamat_create_point, pemilik_point, ngambar))
2890 2891
            conn.commit()
            
2892
        if tipe_point == 'sarana_pendidikan':
2893
            with conn.cursor() as point:
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2894
                point.execute("""INSERT INTO geo_sarana_pendidikan(geom, namobj, remark, luas_tanah, alamat, perangkat, type_id, gambar) VALUES (st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), '%s','%s', %s, '%s', '%s', 10,'%s')"""% (coordinat, name_create_point, info_create_point, luas_tanah_point, alamat_create_point, pemilik_point, ngambar))
2895 2896
            conn.commit()

2897
        if tipe_point == 'cagar_budaya':
2898
            with conn.cursor() as point:
2899
                point.execute("""INSERT INTO geo_cagar_budaya(geom, namobj, remark, luas, alamat, perangkat, type_id, gambar) VALUES (st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), '%s','%s', %s, '%s', '%s', 3,'%s')"""% (coordinat, name_create_point, info_create_point, luas_tanah_point, alamat_create_point, pemilik_point, ngambar))
2900 2901
            conn.commit()
        
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2902 2903 2904 2905 2906 2907 2908 2909 2910 2911
        if tipe_point == 'spbu':
            with conn.cursor() as point:
                point.execute("""INSERT INTO geo_spbu(geom, namobj, remark, luas_tanah, alamat, perangkat, type_id, gambar) VALUES (st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), '%s','%s', %s, '%s', '%s', 3,'%s')"""% (coordinat, name_create_point, info_create_point, luas_tanah_point, alamat_create_point, pemilik_point, ngambar))
            conn.commit()
            
        if tipe_point == 'stasiun':
            with conn.cursor() as point:
                point.execute("""INSERT INTO geo_stasiun(geom, namobj, remark, luas_tanah, alamat, perangkat, type_id, gambar) VALUES (st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), '%s','%s', %s, '%s', '%s', 3,'%s')"""% (coordinat, name_create_point, info_create_point, luas_tanah_point, alamat_create_point, pemilik_point, ngambar))
            conn.commit()
            
2912
        if tipe_point == 'terminal_bus':
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2913 2914 2915 2916
            with conn.cursor() as point:
                point.execute("""INSERT INTO geo_terminal_bus(geom, namobj, remark, luas_tanah, alamat, perangkat, type_id, gambar) VALUES (st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), '%s','%s', %s, '%s', '%s', 3,'%s')"""% (coordinat, name_create_point, info_create_point, luas_tanah_point, alamat_create_point, pemilik_point, ngambar))
            conn.commit()
            
2917
        if tipe_point == 'gardu_listrik':
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2918
            with conn.cursor() as point:
2919
                point.execute("""INSERT INTO geo_gardu_listrik(geom, namobj, remark, luas_tanah, alamat, perangkat, gambar) VALUES (st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), '%s','%s', %s, '%s', '%s', '%s')"""% (coordinat, name_create_point, info_create_point, luas_tanah_point, alamat_create_point, pemilik_point, ngambar))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2920
            conn.commit()
2921 2922 2923 2924 2925 2926
        
        data = 'SUCCESS'
        info = 'data telah diperbaharui'
        status = 0
    except:
        data = 'Opss..!!'
2927
        info = 'Selain Foto dan Alamat, Semua Data Harus Terisi'
2928 2929 2930
        status = 1
    
    respon={'data':data, 'info': info, 'status':status}
Muhamad's avatar
Muhamad committed
2931 2932 2933 2934 2935 2936
    return Response(respon)

@api_view(('GET',))
def filt_perbaikan_kecamatan(request):
    id_kec = request.GET.get('id_kec')
    
Muhamad's avatar
Muhamad committed
2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978
    if id_kec == 'all_kecamatan':
        res_kel = 'all_kecamatan'
        
        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 
                                JOIN geo_data_kecamatan gdk on st_intersects(st_setsrid(gdk.geom, 4326), st_setsrid(tp.geom, 4326))
                            WHERE
                                rn = 1
                            """)
            res_perbaikan = data_per.fetchall()
    else:
        with conn.cursor() as kel:
            kel.execute("""SELECT ID,
                                desa 
                            FROM
                                geo_data_kelurahan gd 
                            WHERE
                                kecamatan = ( SELECT kecamatan FROM geo_data_kecamatan WHERE ID = %s )
                            """% int(id_kec))
            res_kel = kel.fetchall()
        
        
        with conn.cursor() as data_per:
            data_per.execute("""
                            SELECT
                                gl.NAME,
                                tp.id_object,
                                tp.NAME,
                                tp.TYPE,
                                tp.tgl_perbaikan,
                                tp.anggaran,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2979 2980 2981
                                tp.tgl_selesai,
                                gdl.desa,
                                gdc.kecamatan
Muhamad's avatar
Muhamad committed
2982 2983
                            FROM
                                ( SELECT tbl_perbaikan.*, ROW_NUMBER ( ) OVER ( PARTITION BY id_object ORDER BY tgl_selesai DESC ) AS rn FROM tbl_perbaikan ) tp
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2984 2985 2986
                                JOIN geo_label gl ON gl.ID = tp.type_id
                                JOIN geo_data_kelurahan gdl on st_intersects(st_setsrid(gdl.geom, 4326), st_setsrid(tp.geom, 4326))
                                JOIN geo_data_kecamatan gdc ON st_intersects ( st_setsrid ( gdc.geom, 4326 ), st_setsrid ( tp.geom, 4326 ) ) 
Muhamad's avatar
Muhamad committed
2987
                            WHERE
Manggar Mahardhika's avatar
Manggar Mahardhika committed
2988 2989
                                rn = 1 and gdc.ID = %s
                                """% int(id_kec))
Muhamad's avatar
Muhamad committed
2990
            res_perbaikan = data_per.fetchall()
Muhamad's avatar
Muhamad committed
2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001
    
    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],
Manggar Mahardhika's avatar
Manggar Mahardhika committed
3002 3003
            'desa':j[7],
            'kecamatan':j[8],
Muhamad's avatar
Muhamad committed
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024
        }
        res_all.append(data_all)
    
    
    
    respon = {'res_kel':res_kel,'res_perbaikan':res_all}
    return Response(respon)

@api_view(('GET',))
def filt_perbaikan_desa(request):
    id_desa = request.GET.get('id_desa')
    
    with conn.cursor() as data_per:
        data_per.execute("""
                        SELECT
                            gl.NAME,
                            tp.id_object,
                            tp.NAME,
                            tp.TYPE,
                            tp.tgl_perbaikan,
                            tp.anggaran,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
3025 3026 3027
                            tp.tgl_selesai,
                            gdl.desa,
                            gdc.kecamatan
Muhamad's avatar
Muhamad committed
3028 3029
                        FROM
                            ( SELECT tbl_perbaikan.*, ROW_NUMBER ( ) OVER ( PARTITION BY id_object ORDER BY tgl_selesai DESC ) AS rn FROM tbl_perbaikan ) tp
Manggar Mahardhika's avatar
Manggar Mahardhika committed
3030 3031 3032
                            JOIN geo_label gl ON gl.ID = tp.type_id
                            JOIN geo_data_kelurahan gdl on st_intersects(st_setsrid(gdl.geom, 4326), st_setsrid(tp.geom, 4326))
                            JOIN geo_data_kecamatan gdc ON st_intersects ( st_setsrid ( gdc.geom, 4326 ), st_setsrid ( tp.geom, 4326 ) ) 
Muhamad's avatar
Muhamad committed
3033
                        WHERE
Manggar Mahardhika's avatar
Manggar Mahardhika committed
3034
                            rn = 1 and gdl.ID = %s
Muhamad's avatar
Muhamad committed
3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047
                        """% int(id_desa))
        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],
Manggar Mahardhika's avatar
Manggar Mahardhika committed
3048 3049
            'desa':j[7],
            'kecamatan':j[8],
Muhamad's avatar
Muhamad committed
3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069
        }
        res_all.append(data_all)


    respon = {'res_perbaikan':res_all}
    return Response(respon)

@api_view(('GET',))
def user_data(request):
    
    with conn.cursor() as userlist_:
        if request.user.is_superuser:
            # print("is superuser")
            sql = """
                    SELECT
                    au.id,
                    au.username,
                    au.first_name,
                    au.last_name,
                    au.is_active,
Muhamad's avatar
Muhamad committed
3070
                    DATE(au.last_login),
Muhamad's avatar
Muhamad committed
3071 3072 3073 3074
                    au.email,
                    ag.name,
                    au.is_staff,
                    au.is_superuser,
Muhamad's avatar
Muhamad committed
3075 3076
                    au.file_path,
                    au.phone
Muhamad's avatar
Muhamad committed
3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094
                FROM
                    auth_user au,
                    auth_user_groups aug,
                    auth_group ag
                WHERE
                    au.id = aug.user_id AND
                    ag.id = aug.group_id
                ORDER BY 
                    au.username ASC"""
        else:
            if request.user.is_staff:
                    sql = """
                    SELECT
                    au.id,
                    au.username,
                    au.first_name,
                    au.last_name,
                    au.is_active,
Muhamad's avatar
Muhamad committed
3095
                    DATE(au.last_login),
Muhamad's avatar
Muhamad committed
3096 3097 3098 3099
                    au.email,
                    ag.name,
                    au.is_staff,
                    au.is_superuser,
Muhamad's avatar
Muhamad committed
3100 3101
                    au.file_path,
                    au.phone
Muhamad's avatar
Muhamad committed
3102 3103 3104 3105 3106 3107 3108
                FROM
                    auth_user au,
                    auth_user_groups aug,
                    auth_group ag
                WHERE
                    au.id = aug.user_id AND
                    ag.id = aug.group_id AND  
3109 3110
                ORDER BY 
                    au.username ASC"""
Muhamad's avatar
Muhamad committed
3111 3112 3113 3114 3115 3116 3117
            else:
                print('not staff')
        userlist_.execute(sql)
        allUser = userlist_.fetchall()
    
    user_res = []
    for rows in allUser:
Muhamad's avatar
Muhamad committed
3118 3119 3120 3121 3122 3123 3124
        if rows[8] == True and rows[9] == True:
            jabatan = 'Super user'
        elif rows[8] == True and rows[9] == False:
            jabatan = 'Staff'
        elif rows[8] == False and rows[9] == False:
            jabatan = 'User'
        
Muhamad's avatar
Muhamad committed
3125 3126 3127 3128 3129 3130 3131 3132
        datas = {
            "id": rows[0],
            "username": rows[1],
            "name": rows[2] +" "+ rows[3],
            "email": rows[6],
            "isactive": rows[4],
            "last": rows[5],
            "group": rows[7],
Muhamad's avatar
Muhamad committed
3133 3134 3135
            "filepath": rows[10],
            "phone": rows[11],
            "jabatan": jabatan
Muhamad's avatar
Muhamad committed
3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164
        }
        user_res.append(datas)

    
    current_user = request.user
    thisUser     = current_user.id
    
    # print()
    sql = "select * from auth_user where id = "+str(thisUser)
    with conn.cursor() as userprop:
        userprop.execute(sql)
        us = userprop.fetchall()
    
    user_data = []
    for row in us:
        datas = {
            "id": row[0],
            "username": row[4],
            "firstname": row[5],
            "lastname": row[6],
            "email"   : row[7],
            "filepath": row[11],
            # "password": row[1]
        }

        user_data.append(datas)
        
        
    with conn.cursor() as po:
3165
        po.execute("""
Muhamad's avatar
Muhamad committed
3166
                                SELECT id, namobj, alamat, luas_bangunan, perangkat ,'geo_kantor_administrasi'
3167 3168 3169
                            FROM
                                geo_kantor_administrasi
                            UNION
Muhamad's avatar
Muhamad committed
3170
                            SELECT id, namobj, alamat, luas_bangunan, perangkat, 'geo_layanan_kesehatan'
3171 3172 3173
                            FROM
                                geo_layanan_kesehatan
                            UNION
Muhamad's avatar
Muhamad committed
3174
                            SELECT id, namobj, alamat, luas_bangunan, perangkat, 'geo_puskesmas'
3175 3176 3177
                            FROM
                                geo_puskesmas 
                            UNION
Muhamad's avatar
Muhamad committed
3178
                            SELECT id, namobj, alamat, luas_bangunan, perangkat, 'geo_rumah_sakit'
3179 3180 3181
                            FROM
                                geo_rumah_sakit 
                            UNION
Muhamad's avatar
Muhamad committed
3182
                            SELECT id, namobj, alamat, luas_bangunan, perangkat, 'geo_sarana_ibadah'
3183 3184 3185
                            FROM
                                geo_sarana_ibadah 
                            UNION
Muhamad's avatar
Muhamad committed
3186
                            SELECT id, namobj, alamat, luas_bangunan, perangkat, 'geo_sarana_pendidikan'
3187 3188 3189
                            FROM
                                geo_sarana_pendidikan
                            UNION
Muhamad's avatar
Muhamad committed
3190
                            SELECT id, namobj, alamat, luas_bangunan, perangkat, 'geo_spbu'
3191 3192 3193
                            FROM
                                geo_spbu 
                            UNION
Muhamad's avatar
Muhamad committed
3194
                            SELECT id, namobj, alamat, luas_bangunan, perangkat, 'geo_stasiun'
3195 3196 3197
                            FROM
                                geo_stasiun 
                            UNION
Muhamad's avatar
Muhamad committed
3198
                            SELECT id, namobj, alamat, luas_bangunan, perangkat, 'geo_terminal_bus'
3199 3200 3201
                            FROM
                                geo_terminal_bus
                            UNION
Muhamad's avatar
Muhamad committed
3202
                            SELECT id, namobj, alamat, luas_bangunan, perangkat, 'geo_cagar_budaya'
3203 3204 3205
                            FROM
                                geo_cagar_budaya
                            UNION
Muhamad's avatar
Muhamad committed
3206
                            SELECT id, namobj, alamat, luas_bangunan, perangkat, 'geo_gardu_listrik'
3207 3208 3209
                            FROM
                                geo_gardu_listrik
                            """)
Muhamad's avatar
Muhamad committed
3210 3211 3212 3213 3214 3215 3216 3217 3218 3219
        poi = po.fetchall()
    
    point = []
    for p in poi:
        
        datap = {
            "id":p[0],
            "nama_tempat":p[1],
            "alamat":p[2],
            "luas":p[3],
Muhamad's avatar
Muhamad committed
3220 3221
            "pj":p[4],
            "table":p[5]
Muhamad's avatar
Muhamad committed
3222 3223 3224 3225 3226 3227 3228 3229
        }
        point.append(datap)
    
    respon = {
        'data'  : user_res,
        'thisUser' : user_data,
        'point':point
    }
3230 3231
    return Response(respon)

3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382
@api_view(('GET',))
def search_spes(request):
    tipe = request.GET.get('type')
    inp = request.GET.get('inp')
    inp = str(inp)
    
    kill = None

    if tipe == ' Point':
        with conn.cursor() as search_point:
            search_point.execute("""
                                 SELECT a.ID
                                    ,
                                    a.namobj,
                                    a.remark,
                                    a.geom,
                                    'geo_kantor_administrasi' 
                                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)
                                    join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                WHERE 
                                    a.namobj ILIKE '%{}%'
                                UNION
                                SELECT a.ID
                                    ,
                                    a.namobj,
                                    a.remark,
                                    a.geom,
                                    'geo_layanan_kesehatan' 
                                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)
                                WHERE 
                                    a.namobj ILIKE '%{}%' 
                                UNION
                                SELECT a.ID
                                    ,
                                    a.namobj,
                                    a.remark,
                                    a.geom,
                                    'geo_puskesmas' 
                                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)
                                WHERE 
                                    a.namobj ILIKE '%{}%' 
                                UNION
                                SELECT a.ID
                                    ,
                                    a.namobj,
                                    a.remark,
                                    a.geom,
                                    'geo_rumah_sakit' 
                                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)
                                WHERE 
                                    a.namobj ILIKE '%{}%' 
                                UNION
                                SELECT a.ID
                                    ,
                                    a.namobj,
                                    a.remark,
                                    a.geom,
                                    'geo_sarana_ibadah' 
                                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)
                                WHERE 
                                    a.namobj ILIKE '%{}%' 
                                UNION
                                SELECT a.ID
                                    ,
                                    a.namobj,
                                    a.remark,
                                    a.geom,
                                    'geo_sarana_pendidikan' 
                                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)
                                WHERE 
                                    a.namobj ILIKE '%{}%' 
                                UNION
                                SELECT a.ID
                                    ,
                                    a.namobj,
                                    a.remark,
                                    a.geom,
                                    'geo_stasiun' 
                                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)
                                WHERE 
                                    a.namobj ILIKE '%{}%' 
                                UNION
                                SELECT a.ID
                                    ,
                                    a.namobj,
                                    a.remark,
                                    a.geom,
                                    'geo_terminal_bus' 
                                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)
                                WHERE 
                                    a.namobj ILIKE '%{}%' 
                                UNION
                                SELECT a.ID
                                    ,
                                    a.namobj,
                                    a.remark,
                                    a.geom,
                                    'geo_cagar_budaya' 
                                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)
                                WHERE 
                                    a.namobj ILIKE '%{}%' 
                                UNION
                                SELECT a.ID
                                    ,
                                    a.namobj,
                                    a.remark,
                                    a.geom,
                                    'geo_gardu_listrik' 
                                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)
                                WHERE 
                                    a.namobj ILIKE '%{}%'
                                LIMIT 10
3383
                                 """.format(inp, inp, inp, inp, inp, inp, inp, inp, inp, inp))
3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539
            s_point = search_point.fetchall()
        kill = s_point
    elif tipe == ' Bangunan':
        with conn.cursor() as search_bangunan:
            search_bangunan.execute("""
                                 select id, name, geom FROM geo_bangunan where name ilike '%{}%' LIMIT 10
                                 """.format(inp))
            s_bangunan = search_bangunan.fetchall()
        kill = s_bangunan
    elif tipe == ' Jalan':
        with conn.cursor() as search_jalan:
            search_jalan.execute("""select id, namrjl, geom FROM geo_jalan where namrjl ilike '%{}%' LIMIT 10
                                 """.format(inp))
            s_jalan = search_jalan.fetchall()
        kill = s_jalan
    
    return Response({'result':kill})

@api_view(('GET',))
def search_lanjut(request):
    tipe = request.GET.get('type')
    idm = request.GET.get('idx')
    
    kills = None

    if tipe == ' Point':
        db = request.GET.get('db')
        suk = ''
        if (db == 'geo_cagar_budaya' or db == 'geo_terminal_bus'):
            suk == ', a.luas'
        else:
            suk == ''
            
        with conn.cursor() as search_point:
            search_point.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,
                                            a.id
                                            {}
                                        ),
                                        'geometry',
                                        ST_AsGeoJSON ( A.geom :: geometry ) :: json 
                                    ) 
                                FROM
                                    {} 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)
                                WHERE a.id = {}
                                 """.format(suk, db, idm))
            s_point = search_point.fetchall()
        kills = s_point
    elif tipe == ' Bangunan':
        with conn.cursor() as search_bangunan:
            search_bangunan.execute("""
                                 SELECT
                                json_build_object (
                                    'type',
                                    'Feature',
                                    'properties',
                                    ( gb.ID, gb.NAME, gb.information,
                                        (
                                            SELECT ARRAY_AGG
                                                ( '' || ID || ',' || tgl_perbaikan || ',' || jenis_perbaikan || '' ORDER BY tgl_perbaikan DESC ) 
                                            FROM
                                                tbl_perbaikan 
                                            WHERE
                                                id_object = gb.ID 
                                                AND type_id = 1 
                                        )
                                    ),
                                    'administrasi',
                                    (
                                        gb.address_1,
                                        gb.address_2,
                                        gb.address_3,
                                        gb.provinsi,
                                        gb.alamat,
                                        gb.luas_bangunan,
                                        gb.luas_tanah,
                                        gb.tinggi_bangunan,
                                        gb.gambar,
                                        gb.pemilik,
                                        gb.db_tipe,
                                        gb.remark,
                                        gb.point_id,
                                        gb.point_name 
                                    ),
                                    'geometry',
                                    ST_AsGeoJSON ( gb.geom :: geometry ) :: json 
                                ) 
                            FROM
                                geo_bangunan gb
                            WHERE 
                                gb.id = {}
                                 """.format(idm))
            s_bangunan = search_bangunan.fetchall()
            polbang = []        
            for i in s_bangunan:
                i[0]['properties']['f2'] = i[0]['properties']['f2'].replace("'"," ")
                polbang.append(i[0])
            data_bang = {'gen':s_bangunan}
        kills = data_bang
    elif tipe == ' Jalan':
        with conn.cursor() as search_jalan:
            search_jalan.execute("""SELECT
                                    json_build_object (
                                        'type',
                                        'Feature',
                                        'properties',
                                        (
                                            gj.ID,
                                            namrjl,
                                            st_length ( gj.geom :: geography ),
                                            gj.lebar_jalan,
                                            gj.remark,
                                            gj.gambar,
                                            (
                                            SELECT ARRAY_AGG
                                                ( '' || ID || ',' || tgl_perbaikan || '' ORDER BY tgl_perbaikan DESC ) 
                                            FROM
                                                tbl_perbaikan 
                                            WHERE
                                                id_object = gj.ID 
                                                AND type_id = 2 
                                            ) 
                                        ),
                                        'geometry',
                                        ST_AsGeoJSON ( gj.geom :: geometry ) :: json 
                                ) 
                                FROM
                                    geo_jalan gj 
                                WHERE
                                    gj.id = {}""".format(idm))
            s_jalan = search_jalan.fetchall()
            data_jal = {'gen':s_jalan}
        kills = data_jal
    
Muhamad's avatar
Muhamad committed
3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552
    return Response(kills)


@api_view(('GET',))
def load_point_id(request):
    id = request.GET.get('idx')
    dtb = request.GET.get('db')
    with conn.cursor() as poi:
        poi.execute("""
                    SELECT json_build_object (
                        'type',
                        'Feature',
                        'properties',
Muhamad's avatar
Muhamad committed
3553
                        (id, namobj, alamat, luas_bangunan, perangkat, gambar),
Muhamad's avatar
Muhamad committed
3554 3555 3556 3557 3558 3559 3560 3561 3562
                        'geometry',
                        ST_AsGeoJSON ( geom :: geometry ) :: json 
                        )
                    FROM
                        %s
                    WHERE
                        id = %s
                    """%(dtb, id))
        poin = poi.fetchall()
Muhamad's avatar
Muhamad committed
3563 3564 3565
        
    print(poin)
    
Muhamad's avatar
Muhamad committed
3566
    context = {'result':poin}
Muhamad's avatar
Muhamad committed
3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678
    return Response(context)

@api_view(('POST',))
def edit_detail_poin(request):
    idkantor = request.POST.get('id')
    namobj = request.POST.get('namobj')
    luat_tanah = request.POST.get('luastanah')
    gambar = request.POST.get('gambar')
    alamat = request.POST.get('alamat')
    dbp = request.POST.get('db')
    
    
    if alamat is None:
        alamat = 'alamat'
    elif alamat == '':
        alamat = 'alamat'
    else:
        alamat = request.POST.get('alamat')
    perangkat = request.POST.get('perangkat')
    if perangkat is None:
        perangkat = 'Bpk'
    elif perangkat == '':
        perangkat = 'Bpk'
    else:
        perangkat = request.POST.get('perangkat')
    
    point = request.POST.get("point")
    cood_edit = json.loads(point)
        
    co_edit = cood_edit['geometry']['coordinates']

    file1 = request.FILES.get('file')
    fss = FileSystemStorage(location='/var/www/django/oku.khansia.co.id/static/img/image/')
    try:
        format = (file1.name).split(".")
        num = random.randrange(1, 10000000000000)
        namafile = f"{namobj}_{num}.{format[-1:][0]}"
        filename = fss.save(namafile, file1)
        # print(file.name)
        url = fss.url(filename)
    except:
        namafile = gambar

    with conn.cursor() as editka:
        editka.execute("""update %s set geom = st_geomfromgeojson('{"type":"Point", "coordinates":%s, "crs":{"type":"name","properties":{"name":"EPSG:4326"}}}'), namobj = '%s', luas_tanah = %s, perangkat = '%s', alamat = '%s', gambar = '%s' where id = %s"""%(dbp, co_edit, namobj, luat_tanah, perangkat, alamat, namafile, idkantor))
    conn.commit()
    
    data = 'SUCCESS'
    info = 'data telah diperbaharui'
    status = 0
    respon={'data':data, 'info': info, 'status':status}
    return Response(respon)


@api_view(('GET',))

def download_perbaikan_data(request):
    with conn.cursor() as tbl_p:
        tbl_p.execute("""
                       SELECT
                            gl.NAME,
                            tp.name,
                            gj.remark,
                            st_length ( gj.geom :: geography ),
                            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.pjg_perbaikan,
                            tp.lbr_perbaikan,
                            tp.id_object,
                            tp.id,
                            tp.proposal,
                            tp.bukti_selesai
                        FROM
                            tbl_perbaikan tp
                            JOIN geo_label gl ON gl.ID = tp.type_id
                            JOIN geo_jalan gj ON gj.ID = tp.id_object
                        ORDER BY
                            tp.id ASC
                      """)
        tbl_per = tbl_p.fetchall()
        
    per_res = []
    for rows in tbl_per:
        print(rows)
        datas = {
            "id": rows[15],
            "jenis_perbaikan": rows[0],
            "nama": rows[1],
            "tipe": rows[2],
            "panjang": rows[3],
            "tgl_pembuatan": rows[4],
            "tgl_perbaikan": rows[5],
            "tgl_selesai": rows[6],
            
            "anggaran": rows[7],
            "pj": rows[8],
            "pj_lapangan": rows[9],
            "kontraktor": rows[10],
            "panjang_per": rows[12],
            "lebar_per": rows[13],
        }
        per_res.append(datas)
        
    respon = {
        'data'  : per_res,
    }
Muhamad's avatar
Muhamad committed
3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701
    return Response(respon)

@api_view(('POST',))
def dpoint(request):
    id = request.POST.get('id')
    db = request.POST.get('db')
    
    try:
        with conn.cursor() as editka:
            editka.execute("""update %s set is_deleted = 1 where id = %s"""%(db, id))
        conn.commit()    
        
        
        data = {'status':'Success'}
        data = 'SUCCESS'
        info = 'data telah diperbaharui, akan terupdate setelah reload'
        status = 0
    except:
        data = 'Sorry'
        info = 'Gagal diperbaharui, hubungi developer'
        status = 1
    
    respon={'data':data, 'info': info, 'status':status}
Muhamad's avatar
Muhamad committed
3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724
    return Response(respon)

@api_view(('GET',))
def search_list_perbaikan(request):
    v = request.GET.get('v_list')
    with conn.cursor() as list_s:
        list_s.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 
                        AND ( gl.NAME ILIKE'{}%' OR tp.NAME ILIKE'{}%' OR tp.TYPE ILIKE'{}%' OR tp.anggaran ILIKE'{}%' )""".format(v,v,v,v))
        search_list = list_s.fetchall()
    return Response(search_list)
Manggar Mahardhika's avatar
Manggar Mahardhika committed
3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780

@api_view(('POST',))
def reload_point(request):
    with conn.cursor() as kantor_administrasi:
        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,
                                                d.desa,
                                                a.id,
                                                (
                                                    SELECT ARRAY_AGG
                                                        ( '' || ID || ',' || tgl_perbaikan || ',' || jenis_perbaikan || '' ORDER BY tgl_perbaikan DESC ) 
                                                    FROM
                                                        tbl_perbaikan 
                                                    WHERE
                                                        id_object = A.ID 
                                                        AND type_id = 1 
                                                )
                                            ),
                                            '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)
                                        join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
                                    WHERE a.is_deleted = 0""")
        kantor_administrasi_res = kantor_administrasi.fetchall()
            
        kantor_administrasi_result = []
        for i in kantor_administrasi_res:
            if i[0]['administrasi']['f16'] == None:
                i[0]['administrasi']['f16'] = "None"
            kantor_administrasi_result.append(i[0])
            
        context = {
        'kantor_administrasi':kantor_administrasi_result,
        }
        return (context)
Muhamad's avatar
Muhamad committed
3781