population.html 11.3 KB
<!DOCTYPE html>
<html lang="kr">
    <head>
        <title>RF-Project</title>
        <!-- link에 들어가 있는건 지도 데이터를 위한 css 파일 -->
        <!-- script에 들어가 있는건 지도 데이터를 위한 js 파일 -->
        <!-- 직접 다운받아 넣을 수도 있는데 우선은 그냥 웹에서 불러올 수 있도록 해놨다. -->
        <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" />
        <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script>
        <!--css 내용 여기에 direct로 넣었다-->
        <style>
            #mapbase {position:relative; width:40%; height:0; padding-bottom:40%; }
            h1 {
                font-size: 20px;
            }
            .info {
                padding: 6px 8px;
                font: 14px/16px Arial, Helvetica, sans-serif;
                background: white;
                background: rgba(255,255,255,0.8);
                box-shadow: 0 0 15px rgba(0,0,0,0.2);
                border-radius: 5px;
            }
            .info h4 {
                margin: 0 0 5px;
                color: #777;
            }
            .legend {
                line-height: 18px;
                color: #555;
            }
            .legend i {
                width: 18px;
                height: 18px;
                float: left;
                margin-right: 8px;
                opacity: 0.7;
            }

        </style>

        <!--BOOTSTRAP 사용 부분-->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>

    </head>

    <body>
        <div id="bodyWrap">
            <div id="header">
              <h1><a href="/market_analysis/maplist">Statistics on Return Farm Project</a></h1>
              <br>
              <h3>농산물 시장 유통에 대한 통계분석 입니다.</h3>
              <br>
            </div>
            <div id="menubar">
              <ul class="nav justify-content-center">
                <li class="nav-item">
                  <a class="nav-link active" href="/market_analysis/market">Market</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="/market_analysis/population">Population</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="/market_analysis/returnfarm">Return Farm</a>
                </li>
              </ul>
            </div>
        </div>
        <!--population 단독 부분-->
        <!--return farm 단독 부분-->
        <div id="contentWrap">
            <!--검색 바 만든 부분-->
            <div id="searchmenu">
                <form action="{% url 'population' %}" method="GET">
                  <div class="form-group">
                    <select name="maptype">
                      <option value="9">시도</option>
                      <option value="10">시군구</option>
                    </select>
                    <select name="densitytype">
                      <option value="edu">귀농가구원 연령별</option>
                    </select>
                    <input type="submit">
                  </div>
                </form>
              </div>
            <!--지도 생성 부분-->
            <div id="mapbase">
                <script type="text/javascript">
                    // map 객체 생성 map안에 넣어주는건 div id
                    // 위도, 경도, zoom 수준
                    var map = L.map('mapbase', {zoomDelta: 0.25, zoomSnap: 0}).setView([36.5205243,128.0540569], 7);
                    // tilelayer를 씌우는 작업 OSM을 사용했음
                    L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
                        attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
                        maxZoom: 10,
                        id: 'mapbox/light-v9',
                        tileSize: 512,
                        zoomOffset:-1,
                        accessToken: 'pk.eyJ1IjoidGpzZGszMTA1IiwiYSI6ImNrYWM5bXljZzB1N28ycnA5bTVsNHZsbnkifQ.W3-irWof5WMR8BcfBR4Ftw'
                    }).addTo(map);

                    // mapdata 가져오는 부분
                    // replace 쓰는 방법 : (바꾸기 전 문자, 바꾸고 난 후 문자)
                    var statesdata = "{{maps.mapdata}}".replace(/&quot;/g,"\"");
                    statesdata = JSON.parse(statesdata);
                    let key = Object.keys(statesdata.features[1].properties);
                </script>
            </div>
            <!--지도에 밀도 데이터 받는 부분-->
            <div id="density_mapping">
                <script>
                    var densitydatas = "{{exportdata}}".replace(/&quot;/g,"\"");
                    densitydatas = JSON.parse(densitydatas);
                    for(var i=0; i < densitydatas.length; i++)
                    {
                        for(var j=0; j<statesdata.features.length; j++)
                        {
                            if (statesdata.features[j]['properties'][key[2]]==densitydatas[i]['C1_NM'] && densitydatas[i]['ITM_NM']=='귀농가구원수')
                            {
                                statesdata.features[j]['properties']['density']=densitydatas[i]['DT'];
                                break;
                            }
                        }
                    }
                </script>
            </div>
            <!--지도의 큰 모양 바꾸는 부분-->
            <div id="map_design">
                <script>
                    var geojson;
                    function getColor(d) {
                        return d > parseInt(density_max*0.9) ? '#FF0015' :
                            d > parseInt(density_max*0.8)  ? '#F20D20' :
                            d > parseInt(density_max*0.7)  ? '#E6192A' :
                            d > parseInt(density_max*0.6)  ? '#D92635' :
                            d > parseInt(density_max*0.5)  ? '#CC3340' :
                            d > parseInt(density_max*0.4)  ? '#BF404A' :
                            d > parseInt(density_max*0.3)  ? '#B34D55' :
                            d > parseInt(density_max*0.2)  ? '#A65960' :
                            d > parseInt(density_max*0.1)  ? '#99666A' :
                                        '#8C7375';
                    }
                    function style(feature) {
                        return {
                            fillColor: getColor(feature.properties.density),
                            weight: 2,
                            opacity: 1,
                            color: 'white',
                            dashArray: '3',
                            fillOpacity: 0.7
                        };
                    }
                    L.geoJson(statesdata, {style: style}).addTo(map);
                    // mouseover event listener
                    function highlightFeature(e) {
                        var layer = e.target;
                        layer.setStyle({
                            weight: 5,
                            color: '#666',
                            dashArray: '',
                            fillOpacity: 0.7
                        });

                        if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
                            layer.bringToFront();
                        }
                        info.update(layer.feature.properties);
                    }
                    // mouseout define
                    function resetHighlight(e) {
                        geojson.resetStyle(e.target);
                        info.update();
                    }
                    function zoomToFeature(e) {
                        map.fitBounds(e.target.getBounds());
                    }
                    function onEachFeature(feature, layer) {
                        layer.on({
                            mouseover: highlightFeature,
                            mouseout: resetHighlight,
                            click: zoomToFeature
                        });
                    }

                    geojson = L.geoJson(statesdata, {
                        style: style,
                        onEachFeature: onEachFeature
                    }).addTo(map);
                </script>
            </div>
            <!--information, legend, zoom 넣는 부분-->
            <div class="map_info">
                <script>
                    var info = L.control();

                    info.onAdd = function (map) {
                        this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
                        this.update();
                        return this._div;
                    };  

                    // method that we will use to update the control based on feature properties passed
                    info.update = function (props) {
                        this._div.innerHTML = '<h4>Korea Return Farm Density</h4>' +  (props ?
                            '<b>' + props[key[2]] + '</b><br />' + props.density + ' 개의 교육 예정'
                            : 'Hover over a state');
                    };

                    info.addTo(map);

                    var legend = L.control({position: 'bottomright'});

                    legend.onAdd = function (map) {

                        var div = L.DomUtil.create('div', 'info legend'),
                            grades = [0, parseInt(density_max*0.1), parseInt(density_max*0.2), parseInt(density_max*0.3), parseInt(density_max*0.4),parseInt(density_max*0.5), parseInt(density_max*0.6), parseInt(density_max*0.7), parseInt(density_max*0.8),parseInt(density_max*0.9)],
                            labels = [],
                            from, to;

                        // loop through our density intervals and generate a label with a colored square for each interval
                        for (var i = 0; i < grades.length; i++) {
                            from = grades[i];
                            to = grades[i+1];
                            labels.push(
                                '<i style="background: '+ getColor(from + 1) + '"></i> ' + from + (to ? '&ndash;' + to :'+'));
                        }
                        div.innerHTML = labels.join('<br>');
                        return div;
                    };
                    legend.addTo(map);
                </script>
            </div>
        </div>
    </body>
</html>