@bravemobile/korea-administrative-area-geo-json-util
Version:
```shell npm install @bravemobile/korea-administrative-area-geo-json-util ```
189 lines (188 loc) • 10.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KoreaAdministrativeAreaGeoJsonUtil = void 0;
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var helpers_1 = require("@turf/helpers");
var boolean_intersects_1 = __importDefault(require("@turf/boolean-intersects"));
var center_of_mass_1 = __importDefault(require("@turf/center-of-mass"));
var memoizedUnionAdministrativeAreaFeature_1 = require("./memoizedUnionAdministrativeAreaFeature");
var GeoUtils_1 = require("./GeoUtils");
var UnionAdministrativeAreaFeatureCache_1 = require("./UnionAdministrativeAreaFeatureCache");
/**
* 행정구역 GeoJSON 레포지토리
*/
var KoreaAdministrativeAreaGeoJsonUtil = /** @class */ (function () {
function KoreaAdministrativeAreaGeoJsonUtil(
// eslint-disable-next-line no-unused-vars
admSdGeoJson,
// eslint-disable-next-line no-unused-vars
admSggGeoJson,
// eslint-disable-next-line no-unused-vars
admAllGeoJson,
// 옵션
option) {
this.admSdGeoJson = admSdGeoJson;
this.admSggGeoJson = admSggGeoJson;
this.admAllGeoJson = admAllGeoJson;
this.unionAdministrativeAreaFeatureCache = new UnionAdministrativeAreaFeatureCache_1.UnionAdministrativeAreaFeatureCache(option === null || option === void 0 ? void 0 : option.cacheSize);
}
/**
* 대한민국 전체 경계 GeoJSON 조회
*/
KoreaAdministrativeAreaGeoJsonUtil.prototype.getKorea = function () {
return rxjs_1.of(this.admAllGeoJson).toPromise();
};
/**
* 행정 구역 code 목록 => 행정구역 GeoJSON 목록 매핑
* @param administrativeAreaCodes
* @private
*/
KoreaAdministrativeAreaGeoJsonUtil.prototype.getFeaturesByAreaCodes = function (administrativeAreaCodes) {
var _this = this;
return rxjs_1.of(administrativeAreaCodes).pipe(operators_1.map(function (areaCodes) {
// 시도 행정구역 GeoJSON 목록 조회
var sdFeatures = _this.admSdGeoJson.features.filter(function (feature) {
return areaCodes.includes(feature.properties.adm_cd);
});
// 시군구 행정구역 GeoJSON 목록 조회
var sggFeatures = _this.admSggGeoJson.features.filter(function (feature) {
return areaCodes.includes(feature.properties.adm_cd);
});
// 시도 & 시군구 행정구역 GeoJSON 목록 병합
return __spreadArrays(sdFeatures, sggFeatures);
}));
};
/**
* Feature 배열 병합
* @param features
* @private
*/
KoreaAdministrativeAreaGeoJsonUtil.prototype.mergeFeatures = function (features) {
return memoizedUnionAdministrativeAreaFeature_1.memoizedUnionAdministrativeAreaFeature(GeoUtils_1.cloneMultiPolygons(features), this.unionAdministrativeAreaFeatureCache);
};
/**
* 이어진 지역을 그룹화한 결과를 반환합니다.
* @param administrativeAreaCodes
*/
KoreaAdministrativeAreaGeoJsonUtil.prototype.groupConnectedAreas = function (administrativeAreaCodes) {
return __awaiter(this, void 0, void 0, function () {
var features, groupedFeatures;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getFeaturesByAreaCodes(administrativeAreaCodes).toPromise()];
case 1:
features = _a.sent();
groupedFeatures = features.reduce(function (result, feature) {
// 인접한 지역, 인접하지 않은 지역 구분
var _a = result.reduce(function (result1, group) {
// 인접 지역 여부
var isConnected = group.find(function (value) { return boolean_intersects_1.default(feature, value); }) !==
undefined;
return {
unconnected: __spreadArrays(result1.unconnected, (isConnected ? [] : [group])),
connected: __spreadArrays(result1.connected, (isConnected ? group : [])),
};
}, {
unconnected: [],
connected: [],
}), unconnected = _a.unconnected, connected = _a.connected;
// 인접한 지역을 묶습니다.
return __spreadArrays(unconnected, [__spreadArrays(connected, [feature])]);
}, []);
return [2 /*return*/, rxjs_1.from(groupedFeatures)
.pipe(operators_1.concatMap(function (features1) {
return _this.mergeFeatures(features1).pipe(operators_1.map(function (merged) { return ({
mergedFeature: merged,
areaCodes: features1.map(function (value) { return value.properties.adm_cd; }),
}); }));
}), operators_1.map(function (_a) {
var areaCodes = _a.areaCodes, mergedFeature = _a.mergedFeature;
var centerFeature = center_of_mass_1.default(mergedFeature);
return {
areaCodes: areaCodes,
centerFeature: centerFeature,
mergedFeature: mergedFeature,
};
}), operators_1.toArray())
.toPromise()];
}
});
});
};
/**
* 주어진 행정구역 목록을 병합한 GeoJSON 조회
*/
KoreaAdministrativeAreaGeoJsonUtil.prototype.mergeAreas = function (administrativeAreaCodes) {
var _this = this;
// 행정구역 목록이 비어있을경우 null 반환
if (administrativeAreaCodes.length === 0)
return rxjs_1.of(null).toPromise();
return rxjs_1.of(administrativeAreaCodes)
.pipe(operators_1.concatMap(function (areaCodes) { return _this.getFeaturesByAreaCodes(areaCodes); }),
// 행정구역 GeoJSON 목록 => 병합된 Polygon GeoJSON 변환
operators_1.concatMap(function (features) { return _this.mergeFeatures(features); }),
// 병합된 Polygon GeoJSON => FeatureCollection GeoJSON 매핑
operators_1.map(function (unionedFeature) {
return unionedFeature === null
? unionedFeature
: helpers_1.featureCollection([unionedFeature]);
}))
.toPromise();
};
/**
* 캐시 초기화
*/
KoreaAdministrativeAreaGeoJsonUtil.prototype.clearCache = function () {
this.unionAdministrativeAreaFeatureCache.clear();
};
return KoreaAdministrativeAreaGeoJsonUtil;
}());
exports.KoreaAdministrativeAreaGeoJsonUtil = KoreaAdministrativeAreaGeoJsonUtil;