"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var JordanResearchService_1; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.JordanResearchService = void 0; const common_1 = require("@nestjs/common"); const typeorm_1 = require("@nestjs/typeorm"); const typeorm_2 = require("typeorm"); const place_jordan_entity_1 = require("./entities/place-jordan.entity"); const axios_1 = __importDefault(require("axios")); const fs = __importStar(require("fs")); const path = __importStar(require("path")); let JordanResearchService = JordanResearchService_1 = class JordanResearchService { placeRepo; logger = new common_1.Logger(JordanResearchService_1.name); constructor(placeRepo) { this.placeRepo = placeRepo; } async fetchWithRetry(url, data, method = 'get', retries = 2) { for (let i = 0; i <= retries; i++) { try { const config = { timeout: 20000, headers: { 'User-Agent': 'Mozilla/5.0 (compatible; IntaleqMapBot/1.0; +https://intaleq.xyz)', 'Accept': 'application/json, application/sparql-results+json' } }; const response = method === 'post' ? await axios_1.default.post(url, data, config) : await axios_1.default.get(url, { ...config, params: data ? { query: data, format: 'json' } : {} }); return response.data; } catch (error) { if (i === retries) { this.logger.error(`Failed to fetch from ${url} after ${retries} retries: ${error.message}`); throw error; } await new Promise(res => setTimeout(res, 2000)); } } } async fetchOsmZarqa() { this.logger.log('Fetching Zarqa Geometries from OSM...'); const query = `[out:json];(relation["boundary"="administrative"]["admin_level"~"6|8|10"](31.86,35.94,32.22,36.25);node["place"~"neighbourhood|suburb|town"](31.86,35.94,32.22,36.25););out geom;`; try { const data = await this.fetchWithRetry('https://overpass-api.de/api/interpreter', `data=${encodeURIComponent(query)}`, 'post'); return data.elements.map(e => ({ id: e.id, name_ar: e.tags['name:ar'] || e.tags['name'], type: e.tags['admin_level'] ? `admin_level_${e.tags['admin_level']}` : `place_${e.tags['place']}`, geometry: e.geometry ? e.geometry : (e.lat && e.lon ? { type: 'Point', coordinates: [e.lon, e.lat] } : null) })); } catch (e) { return { error: `OSM Fetch failed: ${e.message}` }; } } async fetchOvertureZarqa() { try { const tableCheck = await this.placeRepo.query("SELECT count(*) FROM information_schema.tables WHERE table_name = 'overture_segment'"); if (parseInt(tableCheck[0].count) === 0) return { error: 'table overture_segment does not exist in this database' }; const columns = await this.placeRepo.query("SELECT column_name FROM information_schema.columns WHERE table_name = 'overture_segment'"); const colList = columns.map(c => c.column_name); const roadClassCol = colList.includes('road_class') ? 'road_class' : (colList.includes('class') ? 'class' : 'NULL'); return await this.placeRepo.query(` SELECT DISTINCT COALESCE(names->>'primary', names->>'common') as name_ar, ${roadClassCol} as road_class, ST_AsGeoJSON(ST_Centroid(location)) as centroid FROM overture_segment WHERE (names->>'primary' IS NOT NULL OR names->>'common' IS NOT NULL) AND ST_Within(location, ST_MakeEnvelope(35.94, 31.86, 36.25, 32.22, 4326)) LIMIT 10 `); } catch (e) { return { error: `Overture query failed: ${e.message}` }; } } async fetchWikidataZarqa() { const sparql = `SELECT ?item ?itemLabel WHERE { ?item wdt:P131 wd:Q231710. SERVICE wikibase:label { bd:serviceParam wikibase:language "ar,en". } } LIMIT 20`; try { const data = await this.fetchWithRetry('https://query.wikidata.org/sparql', sparql, 'get'); return data.results.bindings.map(b => ({ id: b.item.value.split('/').pop(), name_ar: b.itemLabel.value })); } catch (e) { return { error: `Wikidata Fetch failed: ${e.message}` }; } } async checkStaticFiles() { const paths = ['/data/infrastructure/osm-data/', './data/', './infrastructure/osm-data/']; const files = ['jor_adm2_geoboundaries.geojson', 'gadm41_JOR_2.json']; const results = {}; for (const file of files) { let found = false; for (const p of paths) { if (fs.existsSync(path.join(p, file))) { results[file] = `Found at ${p}`; found = true; break; } } if (!found) results[file] = 'Not Found locally - Check if overture_ingest.sh was run for division_area'; } return results; } async generateZarqaReport() { const [osm, overture, wikidata, staticFiles] = await Promise.all([ this.fetchOsmZarqa(), this.fetchOvertureZarqa(), this.fetchWikidataZarqa(), this.checkStaticFiles() ]); return { area: 'Zarqa Governorate, Jordan', report_timestamp: new Date().toISOString(), sources: { osm, overture, wikidata, static_files: staticFiles, hdx: "Requires GeoBoundaries GeoJSON for Level 2 (Districts)", gadm: "Requires GADM v4.1 for Level 1-2" }, comparison_summary: "Multi-source research enabled with geometries. OSM provides precise neighborhood centroids and boundaries where available. Use the provided centroids to visualize Zarqa subunits." }; } }; exports.JordanResearchService = JordanResearchService; exports.JordanResearchService = JordanResearchService = JordanResearchService_1 = __decorate([ (0, common_1.Injectable)(), __param(0, (0, typeorm_1.InjectRepository)(place_jordan_entity_1.PlaceJordan)), __metadata("design:paramtypes", [typeof (_a = typeof typeorm_2.Repository !== "undefined" && typeorm_2.Repository) === "function" ? _a : Object]) ], JordanResearchService); //# sourceMappingURL=jordan-research.service.js.map