-
-
Save anonymous/edcfc24a167b66c32f46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding=utf-8 | |
| from pysal import W, Moran | |
| import numpy as np | |
| import qgis | |
| from qgis.core import * | |
| from qgis.gui import QgsMessageBar | |
| from PyQt4.QtGui import QProgressBar | |
| from PyQt4.QtCore import * | |
| import matplotlib.pyplot as plt | |
| # 전역변수 설정 | |
| TEST_DIST = 100000 | |
| NAME_FIELD = "SGG" | |
| VALUE_FIELD = u"노인비율" | |
| ########################## | |
| # 레이어에서 정보 추출 | |
| # 레이어 선택 | |
| oLayer = iface.activeLayer() | |
| if not oLayer: | |
| raise UserWarning(u"레이어를 먼저 선택해야 합니다.") # 종료 | |
| layerName = oLayer.name() | |
| layerType = oLayer.geometryType() | |
| crs = oLayer.crs() | |
| # ID 리스트 확보 | |
| oIDs = oLayer.allFeatureIds() | |
| # Progress 생성 | |
| progressMessageBar = iface.messageBar().createMessage(u"레이어 정보 수집중...") | |
| progress = QProgressBar() | |
| progress.setMaximum(len(oIDs)) | |
| progress.setAlignment(Qt.AlignLeft | Qt.AlignVCenter) | |
| progressMessageBar.layout().addWidget(progress) | |
| iface.messageBar().pushWidget(progressMessageBar, iface.messageBar().INFO) | |
| # centroid,value(y),name 모으기 | |
| centroidList = [] | |
| dataList = [] | |
| nameList = [] | |
| for i, oID in enumerate(oIDs): | |
| progress.setValue(i) | |
| iFeature = oLayer.getFeatures(QgsFeatureRequest(oID)).next() | |
| iGeom = iFeature.geometry().centroid() | |
| centroidList.append(iGeom) | |
| data = iFeature[VALUE_FIELD] | |
| dataList.append(data) | |
| name = iFeature[NAME_FIELD] | |
| nameList.append(name) | |
| # 통계 대상 값 수집 | |
| y = np.array(dataList) | |
| # Progress 제거 | |
| iface.messageBar().clearWidgets() | |
| # Weight Matrix 계산 위한 정보 수집 | |
| neighbors = {} | |
| weights = {} | |
| for iID, iCent in zip(oIDs, centroidList): | |
| iRowNeighbors = [] | |
| iRowWeights = [] | |
| for jID, jCent in zip(oIDs, centroidList): | |
| # 동일 지역인 경우 제외 | |
| if iID == jID: | |
| continue | |
| # 기준거리 이내인 경우 인접한 것으로 기록 | |
| dist = iCent.distance(jCent) | |
| if dist <= TEST_DIST: | |
| iRowNeighbors.append(jID) | |
| iRowWeights.append(1) | |
| # iID 지역에 대한 인접 지역 및 가중치 기록 | |
| neighbors[iID] = iRowNeighbors | |
| weights[iID] = iRowWeights | |
| # 인접지역과 가중치를 기준으로 현재 testDist의 Weight Matrix 계산 | |
| w = W(neighbors, weights) | |
| # 현재 testDist의 Moran's I 계산 | |
| mi = Moran(y, w, two_tailed=False) | |
| # 결과 표시 | |
| resString = "Moran’s I: %.3f, EI: %f" % (mi.I, mi.EI) | |
| print (resString) | |
| iface.messageBar().pushMessage("[Result] ", resString) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment